Ways to shoot yourself in the foot with Postgres
philbooth.me
Ways to shoot yourself in the foot with Postgres
1–10 of 329 posts
Re: Ways to shoot yourself in the foot with Postgres
#2Re: Ways to shoot yourself in the foot with Postgres
#3Re: Ways to shoot yourself in the foot with Postgres
#4Does anybody know why this is the case? Usually, an index is not used if the semantics of the index do not match the semantics of the query, so "using" it cannot ever produce correct results. But the workaround presented seems to have identical semantics to IS DISTINCT FROM and still uses the index, so why isn't IS DISTINCT FROM using the index then?
Re: Ways to shoot yourself in the foot with Postgres
#5Any good guides like this for MySQL? Unfortunately having to use it instead of Postgres at my current job.
Re: Ways to shoot yourself in the foot with Postgres
#6Re: Ways to shoot yourself in the foot with Postgres
#7Re: Ways to shoot yourself in the foot with Postgres
#8I don't know of any database system that does this. In the case for SQL Server, the foreign keys usually get added by the ORM layer (Entity Framework migrations if you're using dotnet).
Re: Ways to shoot yourself in the foot with Postgres
#9- Triggers for validation are awesome. Avoid triggers for logic if you can help it -- harder to debug and update than a server sending SQL and easier than you might think to cause performance problems and cascading triggers.
- Use custom error codes in validation triggers and add as much context as possible to the message when raising the exception. Future you will thank you.
RAISE EXCEPTION USING
ERRCODE = 'SR010',
MESSAGE = 'cannot add a draft invoice ' || new.invoice_id || ' to route ' || new.route_id;
- Postgres exceptions abort transactions, so if using explicit transactions, make sure you have a defer Rollback() so you don't return an aborted transaction to the server connection pool.- For better trigger performance, prefer statement-level triggers [1] or conditional before-row-based triggers.
[1]: https://www.postgresql.org/docs/current/trigger-definition.h...