News for hackers. Stuff that matters.
Re: by polote on Thursday July 10, 2025 @04:38PM (#44525307)
Rls and triggers dont scale either
Re: by shivasaxena on Thursday July 10, 2025 @05:12PM (#44525677)
Yeah, I'm going to remove triggers in next deploy of a POS system since they are adding 10-50ms to each insert.

Becomes a problem if you are inserting 40 items to order_items table.

Re: by lelanthran on Thursday July 10, 2025 @07:13PM (#44526757)
> Yeah, I'm going to remove triggers in next deploy of a POS system since they are adding 10-50ms to each insert.

Do you expect it to be faster to do the trigger logic in the application? Wouldn't be slower to execute two statements from the application (even if they are in a transaction) than to rely on triggers?

Re: by nine_k on Thursday July 10, 2025 @07:48PM (#44526964)
Hmm, imho, triggers do scale, they are just slow. But as you add more connections, partitionss, and CPUs, the slowness per operation remains constant.
Re: by ants_a on Friday July 11, 2025 @06:20AM (#44530474)
Triggers are not even particularly slow. They just hide the extra work that is being done and thus sometimes come back to bite programmers by adding a ton of work to statements that look like they should be quick.
Re: by candiddevmike on Thursday July 10, 2025 @06:58PM (#44526644)
How do you handle trigger logic that compares old/new without having a round trip back to the application?
Re: by SoftTalker on Thursday July 10, 2025 @07:58PM (#44527025)
Do it in a stored procedure not a trigger. Triggers have their place but a stored procedure is almost always better. Triggers can surprise you.
Re: by candiddevmike on Thursday July 10, 2025 @08:17PM (#44527147)
I don't follow how you would do that in a stored procedure outside of a trigger.
Re: by const_cast on Thursday July 10, 2025 @09:27PM (#44527554)
I think instead of performing an INSERT you call a stored proc that does the insert and some extra stuff.
Re: by shivasaxena on Friday July 11, 2025 @01:48PM (#44535087)
Yes, we already have all of our business logic in postgres functions(create_order, create_partial_payment etc).

Doing the extra work in stored procedures is noticeably faster than relying on triggers.

Re: by brikym on Thursday July 10, 2025 @06:48PM (#44526565)
Have you tried deferring them?
Re: by GuinansEyebrows on Thursday July 10, 2025 @05:20PM (#44525741)
that, and keeping your business logic in the database makes everything more opaque!
Re: by lelanthran on Thursday July 10, 2025 @07:12PM (#44526749)
> that, and keeping your business logic in the database makes everything more opaque!

Opaque to who? If there's a piece of business logic that says "After this table's record is updated, you MUST update this other table", what advantages are there to putting that logic in the application?

When (not if) some other application updates that record you are going to have a broken database.

Some things are business constraints, and as such they should be moved into the database if at all possible. The application should never enforce constraints such as "either this column or that column is NULL, but at least one must be NULL and both must never be NULL at the same time".

Your database enforces constraints; what advantages are there to code the enforcement into every application that touches the database over simply coding the constraints into the database?

Re: by thisoneisreal on Thursday July 10, 2025 @07:38PM (#44526911)
I think the dream is that business requirements are contained to one artifact and everything else responds to that driver. In an ideal world, it would be great to have databases care only about persistence and be able to swap them out based on persistence needs only. But you're right, in the real world the database is much better at enforcing constraints than applications.
Re: by GuinansEyebrows on Friday July 11, 2025 @11:04AM (#44532956)
you make good points; i'm overcorrecting from past trigger abuses :)
Re: by Spivak on Thursday July 10, 2025 @05:26PM (#44525805)
Neither do foreign keys the moment you need to shard. Turns out that there's no free lunch when you ask your database to do "secret extra work" that's supposed to be transparent-ish to the user.
Re: by mulmen on Thursday July 10, 2025 @06:49PM (#44526573)
Does that only apply when you need to shard within tenants?

If each tenant gets an instance I would call that a “shard” but in that pattern there’s no need for cross-shard references.

Maybe in the analytics stack but that can be async and eventually consistent.