r/postgres • u/easybits_ai • 14h ago
Discussion Storing generated PDFs in Postgres vs object storage: where do you draw the line?
👋 Hey postgres Community,
Follow-up on the approval system I posted about last week. One decision I made this week that I am still not sure about.
The workflow generates a final PDF once Finance approves a request. People download it from a portal afterwards, sometimes repeatedly, sometimes months later. Regenerating on every download meant paying for the conversion again and making them wait several seconds each time.
So I cache it: the PDF is generated at the moment of approval and stored as base64 in a column on the fact sheets table, then served from there.
What I think I got right
Generating at approval rather than on demand means the document always carries the real approval timestamps, and I never pay twice for the same conversion. Caching any earlier would have meant storing drafts that never got approved.
What I am less sure about
Base64 in Postgres is simple. Everything lives in one place, no extra service, no signed URLs, no bucket permissions. But it is blobs in a relational database, and I have not yet hit the point where row sizes start to hurt. The alternative is object storage with a reference in the row, which is more correct and also more moving parts for something a handful of people download a few times a month.
Where would you draw that line? Curious whether people go to object storage from day one or wait until something actually breaks.
Best,
Felix
3
u/716green 13h ago
It depends what is costing you more. If you have tens of thousands of PDFs maybe R2 object storage on Cloudflare will be cheaper. If you only expect to have a few hundred or the low thousands than you can probably pay $5 flat on Layerbase and store it as a BLOB directly in Postgres
You shouldn't get to a point where performance is a problem until you have nearly a terrabyte of documents so I'd make the decision off of convenience and pricing.
If you're using a provider that charges you for ingress and egress it might be costly which is why I mentioned Layerbase instead of Supabase for example
2
u/cthart 12h ago
How big is your database? How do you back it up?
1
u/scotterockaroo 12h ago
It comes down to your scale. I have a small app I built for my wife's business, for that, I keep 100% of everything in postgres (images and PDFs as bytea). In that case though, we're looking at around 3GB of total data over years with the max bytea size ~1.5 MB, and I wanted it all in one place. This way, pg_dump can move the entire thing as a single unit.
Now, if I were doing thousands of these, or if I'm doing huge volume (hundreds / second), you're going to bump into some common pitfalls (https://pgcraftsman.io/talks/the-postgres-mistakes-we-all-still-make/) very quickly.
-- Scott - pgCraftsman.io - Databricks Lakebase / Neon Engineering
1
u/redrabbitreader 11h ago
I had something similar a couple of years ago, but in our case our decision was driven by regulation. For a "final" PDF, like in your case, we had to produce the original only once. A hard copy of that original had to be stored as well.
Some time after I left the company I heard they were hit with a heavy fine because the customer disputed a later requested PDF copy and they failed to produce the original hard copy to proof accuracy of the new PDF copy (this was in court).
Check with your legal team what is actually required. This may drive the decision to recreate the PDF or store the original. The difference matter over time, especially as volumes grow.
If regulation is not an issue, re-creating a PDF from data on request, provided the volume is low, is probably fine. Caching it in the DB itself for some time should also be fine - again depending sizes and request volumes. Just keep an eye on your connection counts and system RAM. Delegating to a dedicated read-replica may also buy some time before introducing other technologies.
1
u/rbobby 9h ago
One issue you should consider is admin overhead of keeping files outside of the db. You lose ACID... which can be tricky to solve. You also lose a coordinated database restore point (everything comes back together). Separate files also introduces new failure modes and corrective actions (eg. folder permissions got messed up, folders moved from expect location, folders compressed "transparently").
The full scope of in the database vs in the file system can have some odd corners.
1
u/Aggressive_Ad_5454 4h ago edited 4h ago
Object storage or file system storage, with a reference in the dbms. Every time. No doubt. Pulling CLOBs or BLOBs out of a dbms turns into a serious bottleneck when you scale up.
If use UUIDv4 values as URL file names, they’re probably hard enough for cybercreeps to guess that you’re secure. (128 bits). But it’s not hard to use longer random strings if 128 bits isn’t enough.
1
u/kgotson 3h ago
The base64 solution looks the easiest right now but long term it will hurt you. Rather use object storage and store the reference instead.
Multiple Object storage solutions also include version management these days.
1
u/Euronodes 32m ago
Why would you assume that reference to external storage is "more correct"? Unless you really have a lot of data, and I mean A LOT, you dont win anything; instead you are adding second tier overhead and keys that are always error prone by nature
Not only you have a single backup job now, but also replication for free
Just drop base64 and use bytea, youre wasting 30% (kinda sorta) for nothing. TOAST handles the rest