The case · 04

Why PostgreSQL

This is the least controversial choice in the stack and the most consequential. PostgreSQL keeps making other infrastructure unnecessary — and every piece of infrastructure you do not run is one you never have to operate, secure, back up or explain.

⏱ 12 min readOpinion, with evidence

1 · Correctness is the default

Databases differ most in what they do when you make a mistake. PostgreSQL's answer is consistently "refuse".

SituationPostgreSQLMySQL (historically)
String too long for the columnErrorTruncated silently in non-strict mode
Invalid date such as 0000-00-00RejectedAccepted
DDL inside a transactionTransactional — a failed migration rolls backImplicit commit
Check constraintsEnforced since foreverIgnored before 8.0.16
Standards complianceVery highImproving
Transactional DDL is quietly enormous

A migration that adds three columns and fails on the third leaves the database exactly as it was. On engines without it you get a half-applied schema at 3 a.m. and a manual repair.

2 · One engine instead of five services

RequirementThe usual extra servicePostgreSQL feature
Document / schemaless dataMongoDBJSONB with GIN indexes
Full-text searchElasticsearchtsvector + GIN, pg_trgm for fuzzy
Job queueRabbitMQ, SQSSELECT … FOR UPDATE SKIP LOCKED
GeospatialA dedicated GIS storePostGIS — the industry standard
Time seriesInfluxDBPartitioning, or TimescaleDB
Vector / embeddingsPinecone, Weaviatepgvector
CachingRedisUNLOGGED tables (for many cases)
"Just use Postgres" is advice, not a dogma

At sufficient scale a specialised system wins — Elasticsearch really is better at search relevance, Kafka really is a better log. The point is that PostgreSQL takes you much further than people expect, and the second system you add doubles your operational surface: another deployment, another backup policy, another failure mode, another consistency boundary.

a correct, contention-free job queue in one statement
-- each worker claims a distinct batch; no broker required
UPDATE outbox_event
SET    published_at = now()
WHERE  event_id IN (
         SELECT event_id
         FROM   outbox_event
         WHERE  published_at IS NULL
         ORDER  BY event_id
         FOR UPDATE SKIP LOCKED
         LIMIT  100
       )
RETURNING event_id, aggregate_id, type, payload;

3 · JSONB is a real data type, not a text column

indexed, queryable, constrained
ALTER TABLE customer_order ADD COLUMN metadata jsonb NOT NULL DEFAULT '{}';

CREATE INDEX idx_order_metadata ON customer_order USING gin (metadata);

-- containment, index-accelerated
SELECT * FROM customer_order
WHERE metadata @> '{"channel": "mobile", "giftWrap": true}';

-- extract a typed value
SELECT reference, (metadata ->> 'campaign') AS campaign
FROM customer_order
WHERE metadata ? 'campaign';

-- you can still enforce rules on it
ALTER TABLE customer_order ADD CONSTRAINT metadata_channel_valid
  CHECK (metadata ->> 'channel' IN ('web', 'mobile', 'phone', 'partner'));

This is the pragmatic middle ground: a strict relational core for the parts of the domain you understand, and a flexible column for the parts that vary per customer — without running a second database.

4 · Analytics without an export

window functions, CTEs, filtered aggregates
WITH monthly AS (
  SELECT date_trunc('month', placed_at) AS month,
         customer_id,
         sum(total_amount)              AS revenue,
         count(*) FILTER (WHERE status = 'CANCELLED') AS cancellations
  FROM   customer_order
  GROUP  BY 1, 2
)
SELECT month,
       customer_id,
       revenue,
       lag(revenue)  OVER w                       AS previous_month,
       revenue - lag(revenue) OVER w              AS delta,
       rank()        OVER (PARTITION BY month ORDER BY revenue DESC) AS rank_in_month
FROM   monthly
WINDOW w AS (PARTITION BY customer_id ORDER BY month)
ORDER  BY month DESC, rank_in_month;

Most "we need a data warehouse" requirements at small and medium scale are a handful of queries like this one plus a materialised view refreshed nightly.

5 · Extensions

the ones worth knowing
CREATE EXTENSION pg_stat_statements;  -- which queries actually cost you time
CREATE EXTENSION pg_trgm;             -- fuzzy / typo-tolerant search
CREATE EXTENSION citext;              -- case-insensitive text, e.g. email
CREATE EXTENSION uuid-ossp;           -- UUID generation
CREATE EXTENSION postgis;             -- geospatial
CREATE EXTENSION vector;              -- embeddings for AI features
CREATE EXTENSION pgcrypto;            -- hashing and encryption in the database

pg_stat_statements alone is worth the paragraph: it ranks every query by total time, calls and rows, which turns "the app feels slow" into a sorted list.

6 · The licence and the ecosystem

  • PostgreSQL Licence — permissive, BSD-like. No open-core, no relicensing risk, no vendor able to change the terms.
  • Managed everywhere — RDS and Aurora, Cloud SQL and AlloyDB, Azure Database, Neon, Supabase, Crunchy. Migration between them is realistic because they are all Postgres.
  • Predictable releases — one major version a year, five years of support each.
  • No single owner — a global development group, not one company's roadmap.

What it costs

CostReality
Connections are processes (~10 MB each)Real. Use HikariCP, and PgBouncer above a few hundred connections.
Vacuum needs attentionAutovacuum handles most cases; long transactions cause bloat.
Some DDL takes locksModern versions avoid most rewrites; use CREATE INDEX CONCURRENTLY.
No built-in multi-masterCorrect — that is usually a feature. Patroni or a managed service for HA.
Defaults are conservativeshared_buffers and work_mem need tuning; managed services do it for you.

A team wants to add MongoDB for a flexible per-customer attributes field. What is usually the better first move?

JSONB gives document flexibility with indexing, containment queries and the option of constraints — inside the transaction that already protects the rest of the order. A second database means a second backup policy, a second failure mode and no cross-store transactions.