Remember to use primary keys vs unique constraints in Postgres
Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication. Primary keys combine uniqueness with NOT NULL, create an index, act as the usual foreign-key target, and help the query planner. If you rely on UNIQUE, you need to add NOT NULL and run ALTER TABLE ... REPLICA IDENTITY USING INDEX yourself. The simpler habit is id SERIAL PRIMARY KEY, with UNIQUE kept for naturally unique columns like emails or SKUs.
Related posts
- Choosing a Postgres Primary Key
Supabase mediumAdeptPostgres primary key options progress from integer and serial sequences to standards-compliant IDENTITY columns, then to random IDs and UUIDs, each fixing a weakness of the last. Sequences can leave gaps and expose guessable, enumerable IDs. Python's random module is unsafe for IDs, so the secrets module is needed. UUIDv1 and v4 are hard to guess but are twice the size of bigint, and they sort and index poorly. Newer k-sortable formats (UUIDv6/v7/v8, ULID, KSUID, Snowflake and others) fix ordering, and the uuid-ossp and Rust-based pg_idkit extensions are benchmarked on generation speed and table/index size.
- Dynamic Table Partitioning in Postgres
Supabase mediumAdeptPostgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock. The walkthrough builds a simulated chat app with pl/pgSQL functions that create partitions dynamically. It also covers why large tables hurt (btree depth, vacuum, archiving), the requirement that the partition key be part of composite primary and foreign keys, and ORM support gaps such as Django's lack of composite keys.
- Postgres to ClickHouse: Data Modeling Tips
ClickHouse mediumAdept
Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (_peerdb_version, _peerdb_is_deleted), so duplicates persist until background merges run. Deduplicate at query time with FINAL, argMax, ROW_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.
- The silent syntax difference in foreign keys between Postgres and MySQL
Neon mediumAdept
MySQL parses but silently ignores inline column-level `REFERENCES` clauses, so a foreign key written Postgres-style (`customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE`) creates no constraint, no index and no cascade behavior in MySQL. Orphaned rows insert without error. Only a separate table-level `FOREIGN KEY (...) REFERENCES ...` clause is enforced. Use the explicit `FOREIGN KEY` form in both databases, review schemas when porting between them, and test that constraints actually reject invalid data.
- PostgreSQL 15: Three features you can try with Neon
Neon mediumAdept
Neon now supports PostgreSQL 15, and three of its features can be tried from Neon's SQL Editor with runnable examples. MERGE performs insert, update or delete in a single statement, though unlike INSERT ON CONFLICT it can fail with a unique violation under concurrent inserts. CREATE privilege on the public schema is no longer granted to all users, so non-owners must be granted it explicitly. UNIQUE NULLS NOT DISTINCT lets a unique constraint permit only one NULL value.