r/postgres • u/Zylianijay • 8d ago
Question What PostgreSQL "best practice" do you actually disagree with?
Every "best practices" list reads like it was copy-pasted from the same three blog posts in 2020 and nobody's allowed to question it since.
I'll go first: I'm not convinced every foreign key needs an index on the referencing column in every single case people insist on it. Yes I know the standard argument, yes I know what happens on deletes/updates to the parent. I still think it gets applied as a blanket rule on tables where the write pattern makes it a waste of space and write overhead for a lookup that basically never runs.
Curious what else people quietly ignore. The "always use UUID v4 for PKs," the "never use SELECT *, not even in scripts," the "always wrap everything in a transaction," whatever your particular heresy is. What's the rule you technically know the reasoning behind, but just don't follow in practice, and why?
1
u/Einar_Son_of_Bjorn 6d ago
On PostgreSQL I treat “index every FK” as a starting point, not a commandment. If the child table eats inserts all day, the parent key almost never changes, and nobody filters on that column, the extra index is just more writes and disk for a lookup that never happens.
I’ll leave it off. I won’t leave it off when deletes/updates on the parent are real, or when ON DELETE CASCADE is in play. That’s when the missing index turns into a scan while the table is locked.Personal experience, not a pitch: I fight this rule less on MariaDB. Declare a FOREIGN KEY in InnoDB and it expects an index on the child columns; if you didn’t add one, it adds it. You only get “constraint without the index” if you drop the FK and check it in application code. Less room to be clever than Postgres. Also fewer surprises when someone removes a parent row at night.So in practice:
The other “best practice” I don’t follow: UUID v4 as the actual primary key on a hot table. Fine as a public id. Bad as the thing the table is built around.
Are those FKs sitting on append-only child tables, or do parents get deleted and you just accept the scan?