Most active commenters

    ←back to thread

    570 points davidgu | 17 comments | | HN request time: 1.683s | source | bottom
    1. polote ◴[] No.44525307[source]
    Rls and triggers dont scale either
    replies(2): >>44525677 #>>44525805 #
    2. shivasaxena ◴[] No.44525677[source]
    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.

    replies(5): >>44525741 #>>44526565 #>>44526644 #>>44526757 #>>44526964 #
    3. GuinansEyebrows ◴[] No.44525741[source]
    that, and keeping your business logic in the database makes everything more opaque!
    replies(1): >>44526749 #
    4. Spivak ◴[] No.44525805[source]
    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.
    replies(1): >>44526573 #
    5. brikym ◴[] No.44526565[source]
    Have you tried deferring them?
    6. mulmen ◴[] No.44526573[source]
    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.

    7. candiddevmike ◴[] No.44526644[source]
    How do you handle trigger logic that compares old/new without having a round trip back to the application?
    replies(1): >>44527025 #
    8. lelanthran ◴[] No.44526749{3}[source]
    > 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?

    replies(2): >>44526911 #>>44532956 #
    9. lelanthran ◴[] No.44526757[source]
    > 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?

    10. thisoneisreal ◴[] No.44526911{4}[source]
    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.
    11. nine_k ◴[] No.44526964[source]
    Hmm, imho, triggers do scale, they are just slow. But as you add more connections, partitionss, and CPUs, the slowness per operation remains constant.
    replies(1): >>44530474 #
    12. SoftTalker ◴[] No.44527025{3}[source]
    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.
    replies(1): >>44527147 #
    13. candiddevmike ◴[] No.44527147{4}[source]
    I don't follow how you would do that in a stored procedure outside of a trigger.
    replies(1): >>44527554 #
    14. const_cast ◴[] No.44527554{5}[source]
    I think instead of performing an INSERT you call a stored proc that does the insert and some extra stuff.
    replies(1): >>44535087 #
    15. ants_a ◴[] No.44530474{3}[source]
    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.
    16. GuinansEyebrows ◴[] No.44532956{4}[source]
    you make good points; i'm overcorrecting from past trigger abuses :)
    17. shivasaxena ◴[] No.44535087{6}[source]
    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.