Data Type Best Practices

pgfence catches common type choices that will cause problems down the line.

#PatternRiskSuggestion
20ADD COLUMN ... jsonLOWUse jsonb: json has no equality operator and is generally slower
21ADD COLUMN ... serialLOWUse GENERATED BY DEFAULT AS IDENTITY: SERIAL creates implicit sequences with ownership quirks and permission issues
22integer / int columnsLOWUse bigint: changing to bigint later requires an ACCESS EXCLUSIVE rewrite
23varchar(N) columnsLOWUse text: increasing varchar length requires ACCESS EXCLUSIVE on older Postgres versions
24timestamp without time zoneLOWUse timestamptz to avoid timezone-related bugs
25char(N) / character(N) columnsLOWUse text: char(n) pads values with spaces, wastes storage, and changing the length later requires ACCESS EXCLUSIVE + table rewrite
26serial / bigserial / smallserial columnsLOWUse GENERATED BY DEFAULT AS IDENTITY: IDENTITY columns have cleaner ownership semantics, avoid implicit sequence permission issues, and are the SQL standard

Rule reference

Every finding links here by rule id. There are 7 rules on this page.

RuleTitleDescription
add-column-json Prefer jsonb over json The json type has no equality operator and is generally slower than jsonb.
add-column-serial Prefer identity over serial serial creates an implicit sequence with ownership and permission quirks. GENERATED BY DEFAULT AS IDENTITY is the standard replacement.
ban-char-field Avoid char(n) char(n) pads values with spaces and wastes storage. Use text.
prefer-bigint-over-int Prefer bigint over integer Changing an integer column to bigint later rewrites the table under an ACCESS EXCLUSIVE lock.
prefer-identity Prefer identity over serial GENERATED BY DEFAULT AS IDENTITY is the standard replacement for serial columns.
prefer-text-field Prefer text over varchar(n) text has no length limit to outgrow, and changing a varchar length can need a table rewrite on older Postgres versions.
prefer-timestamptz Prefer timestamptz over timestamp timestamp without time zone discards the offset and invites timezone bugs.