Data Type Best Practices
pgfence catches common type choices that will cause problems down the line.
| # | Pattern | Risk | Suggestion |
|---|---|---|---|
| 20 | ADD COLUMN ... json | LOW | Use jsonb: json has no equality operator and is generally slower |
| 21 | ADD COLUMN ... serial | LOW | Use GENERATED BY DEFAULT AS IDENTITY: SERIAL creates implicit sequences with ownership quirks and permission issues |
| 22 | integer / int columns | LOW | Use bigint: changing to bigint later requires an ACCESS EXCLUSIVE rewrite |
| 23 | varchar(N) columns | LOW | Use text: increasing varchar length requires ACCESS EXCLUSIVE on older Postgres versions |
| 24 | timestamp without time zone | LOW | Use timestamptz to avoid timezone-related bugs |
| 25 | char(N) / character(N) columns | LOW | Use text: char(n) pads values with spaces, wastes storage, and changing the length later requires ACCESS EXCLUSIVE + table rewrite |
| 26 | serial / bigserial / smallserial columns | LOW | Use 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.
| Rule | Title | Description |
|---|---|---|
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. |