I worked at a company which relied on significant use of Postgres triggers and it was not simplified in my mind due to: - Engineers being more comfortable expressing the required business logic in the other languages they were working in then PL/pgSQL - Challenging to write tests for the triggers - Harder to deploy variations for testing if needed
How Postgres Triggers Can Simplify Your Back End Development
11–20 of 112 posts
Re: How Postgres Triggers Can Simplify Your Back End Development
#12Luv Postgres
Simple as
A quick note for anyone thinking about triggers: if there's any case whatsoever that you're going to have more than one row insert into a table per transaction, please use statement level triggers -- especially if you're doing network calls inside the trigger. Triggers execute within the same transaction, they're synchronous, and will soak up resources.
Hell, if you're using network calls in your triggers... please don't. Use `LISTEN/NOTIFY` or queues to offload that to some other process (whether postgres itself or another process), so PG isn't left hanging around waiting for a network return.
Re: How Postgres Triggers Can Simplify Your Back End Development
#13Summarizes why in my opinion using triggers is rather risky and confusing. You introduce side effects to operations that one might suspect are CRUDlike. Your code is made non-atomic, in that you need knowledge of what happens elsewhere to guess why it's behaving a certain way. On small projects it's rather tempting, but small projects become large projects that then get given to someone else to maintain, and 4y later someone will spend a week trying to understand why the amount column gets updated to another value that they're pushing.
The only use that I find safe is for database metadata, say if you're using triggers to keep track of write origins, or schema metadata. For everything that's business logic, I'd stay away from them
Re: How Postgres Triggers Can Simplify Your Back End Development
#14I love these types of techniques. Need a basic no nonsense queue? Postgres. Need a basic reporting infrastructure? Postgres. Need a document store? Postgres. But every single time this comes up, people on the engineering teams Ive been on all throw their hands up and accuse folks of overengineering or underengineering. You need rabbit or kafka. We should move to mongo. Etc. Thats the part thats hard.
How do these techiques work with replication and sharding? Or do you just use cloud managed pg like AWS RDS to not think about it?
Now you'll never* don't need to shard your Postgres.
* Unless you work at a very rare company.
Re: How Postgres Triggers Can Simplify Your Back End Development
#15Re: How Postgres Triggers Can Simplify Your Back End Development
#16My only hesitation with methods like this is it ends up splitting the business rules into two places, where one is sort of obscured. It's obvious to look at `add_new_payment` for the code that runs when adding a new payment, but then the code isn't there, so you have to know/ask or search in either migrations, a fresh structure dump or poke at the actual db (!). I think they're great for other, well, effects when nee…
I’d highly recommend people avoid them, unless you feel that you really need them _and_ you have very robust development processes. As soon as you deploy your first table trigger, from that moment you have to check every DML statement for unintended side effects.
Re: How Postgres Triggers Can Simplify Your Back End Development
#17Re: How Postgres Triggers Can Simplify Your Back End Development
#18Re: How Postgres Triggers Can Simplify Your Back End Development
#19Re: How Postgres Triggers Can Simplify Your Back End Development
#20My only hesitation with methods like this is it ends up splitting the business rules into two places, where one is sort of obscured. It's obvious to look at `add_new_payment` for the code that runs when adding a new payment, but then the code isn't there, so you have to know/ask or search in either migrations, a fresh structure dump or poke at the actual db (!). I think they're great for other, well, effects when nee…
1. Just discovered this. 2. Implemented a bunch of them. 3. Hasn’t been maintaining this solution for more than a couple of months.
This is a trade-off that only manifests itself after maybe a couple years when you’ve built a system and you have many hands maintaining it. Either there’s some performance problem that makes it worth it or you’ve just obfuscated half your code for no reason. In the latter case those many hands are going to wreak havoc on your system before you figure out how to make it maintainable.