Live data from Hacker News

How Postgres Triggers Can Simplify Your Back End Development

themythicalengineer.com

11–20 of 112 posts

Re: How Postgres Triggers Can Simplify Your Back End Development

#11

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

You can write tests using pgTAP https://pgtap.org/

Re: How Postgres Triggers Can Simplify Your Back End Development

#12
'Ate Mongo

Luv 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

#13
> Triggers should be used with caution since they can obscure critical logic and create an illusion of automatic processes

Summarizes 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

#14
post #6

I 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?

Just don't keep all your old data in Postgres forever. Set up a nightly cron job to archive old crap out.

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

#16

My 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…

Table triggers are the ultimate foot gun in this respect. They are highly obscured! Stored procedures split your business logic too, but when you want to go and look at your database logic, it’s at least where you’re expecting to find it, and not built into a table.

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

#18
The article mentions it at the very bottom, but I almost never reach for triggers because they are obscure places to put application logic. On more than one occasion I've been burned by not realizing that the code in the backend did not represent the whole picture of business logic. It's more complexity, requiring more documentation, adding another point of failure that probably isn't necessary.

Re: How Postgres Triggers Can Simplify Your Back End Development

#19
post #4

There’s so much value in database triggers when they’re done right. Anything where live stats are needed gets a whole lot easier with triggers rather than counts.

Then you discover Materialised Views... :) Magic.

Just wait until we get Incremental Materialized Views!

Re: How Postgres Triggers Can Simplify Your Back End Development

#20

My 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…

Yeah I was expecting the post author to discuss the trade-off being made here because it’s really important to do so. The biggest complaint I have with these pithy articles is that they try to sell you on a particular trade-off without explaining what the deal is. It makes me think the author:

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.

Post reply on HN