postgres / queries

I follow these rules for a schema and its queries. Each rule comes from how Postgres works. Several came from Hazel Bachrach's What I Wish Someone Told Me About Postgres. Each of my repos states the same rules in its AGENTS.md, in the same order, so an agent reads one version of each. postgres / safe migrations covers DDL.

Column types

I give a column the type of its data:

CREATE TABLE notes (
  id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  company_id bigint NOT NULL REFERENCES companies (id),
  body text NOT NULL,
  pinned boolean NOT NULL DEFAULT false,
  happened_at timestamptz NOT NULL,
  extra jsonb NOT NULL DEFAULT '{}'
);

I use text with no length. varchar(n) adds a check and no speed. timestamptz stores an instant. timestamp stores a clock reading with no time zone. jsonb parses the value once, at the write, and an index can read it. json keeps the text and parses it on each read. The Postgres wiki page Don't Do This lists more.

One app stores a time as RFC 3339 text on purpose. The text sorts in time order, and Go scans every time into one type. An app that I ported from SQLite has TEXT times and INTEGER flags. I do not copy them into a new column. Each new TEXT time makes the later change to timestamptz larger.

One place for each fact

I store a fact in one table and refer to it by key. A copy of a fact is a cache. I name the job that rebuilds it from the source. A search table that a job fills again every 15 minutes is a cache of this kind.

jsonb

Postgres keeps statistics on each column. The planner reads them to guess how many rows a filter matches. It keeps no statistics on the keys in a jsonb value, so it uses a fixed guess. A field that a query filters, joins, or sorts on gets its own column. I keep jsonb for a payload that code reads whole, such as the arguments of a job.

A filter on a jsonb key compares text or jsonb, not a bare value:

SELECT id FROM jobs WHERE args ->> 'company_id' = '42';
SELECT id FROM jobs WHERE args @> '{"company_id": 42}';

NULL

NULL means unknown. A comparison with NULL is NULL, and WHERE keeps only the rows where its condition is true. So on a nullable column, <> drops the NULL rows:

-- Leaves out each user whose title is NULL.
SELECT id FROM users WHERE title <> 'manager';

-- Keeps them.
SELECT id FROM users WHERE title IS DISTINCT FROM 'manager';

NOT IN (...) and <> ALL (...) drop them too. coalesce is the other fix.

NOT IN over a subquery is worse. One NULL from the subquery makes the NOT IN match no rows:

-- Matches nothing when one data_reviews.company_id is NULL.
SELECT id FROM companies
WHERE id NOT IN (SELECT company_id FROM data_reviews);

SELECT id FROM companies
WHERE NOT EXISTS (
  SELECT 1 FROM data_reviews
  WHERE data_reviews.company_id = companies.id
);

I write NOT EXISTS. A static check in CI fails on a NOT IN (SELECT ...) in a query file.

LIKE

In LIKE, % matches any run of characters, and _ matches any one character. A name with an underscore in it becomes a pattern:

-- Also matches 'dailyreport'.
SELECT id FROM checks WHERE name LIKE 'daily_%';

SELECT id FROM checks WHERE starts_with(name, 'daily_');

I match a prefix with starts_with, which has no wildcards. In one repo, a check fails on a LIKE pattern that is a plain prefix or that holds an unescaped _.

Column positions

GROUP BY 1 and ORDER BY 2 name a column by its position in the SELECT list. An edit to that list changes what they mean, with no error. I use positions in psql, and I write the column names in a query file.

Indexes

Postgres reads table statistics to choose between an index and a table scan. On a small test table it scans the table, so I run EXPLAIN on a restored copy of production.

A btree index on (a, b) serves filters on a or (a, b), not on b alone. Because PRIMARY KEY and UNIQUE constraints also build btree indexes, an index on (a) beside a key on (a, b) is redundant. In one repo, a CI check rejects redundant prefix indexes.

Transactions

A transaction holds every lock until it ends, so I run only SQL between Begin and Commit. HTTP calls, git commands, and file writes stay outside the transaction, and large updates run in batched transactions.

One server sets idle_in_transaction_session_timeout to one minute, so a stalled goroutine releases its locks. Jobs that hold a long transaction override the limit. I never set idle_session_timeout on a session holding an advisory lock, because closing the session releases the lock silently.

postgres / safe migrations shows how one slow read and one fast ALTER stop every query on a table.

← All articles