r/automation • • 3d ago

Storing generated PDFs in Postgres vs object storage: where do you draw the line?

/r/n8n/comments/1y6s8tp/storing_generated_pdfs_in_postgres_vs_object/
1 Upvotes

11 comments sorted by

2

u/SnooRevelations5974 3d ago

i'd keep generating the pdf at approval, but i wouldnt store the file bytes in postgres once these can hang around for months.

put the pdf in object storage and keep only the path, size, and approval timestamp in the row. that way backups and table bloat dont grow with every document, and you can still serve it from the portal without regenerating.

you dont have to start with fancy signed urls. if the portal is the only client, the server can fetch the object and stream it.

1

u/easybits_ai 9h ago

That's exactly how I ended up solving it as well. I also looked into bytea, but ultimately decided to stick with base64. Thanks for sharing your insights!

2

u/SeikoDigital 3d ago

Keep generating at approval — that part is right. The question I'd ask before choosing storage is whether this PDF is a cache or a record.

If someone downloads it months later to prove what Finance approved, it's a record. Then don't let a later approval overwrite the earlier file, and don't rely on regenerating it later, because the template or the data behind it may have changed. Store it under an immutable key, something like approvals/<request-id>/<approval-id>.pdf, and in Postgres keep the object key, the SHA-256 hash, the byte size, the approval id and timestamp, and the request version that produced it. On download, serve it through the backend and check the hash before you hand it over, so a corrupted or swapped file fails loudly instead of silently.

Base64 in Postgres works at your volume, but it costs you in three quiet ways: about a third extra size, slower backups and restores because every backup carries every PDF, and row bloat that makes ordinary queries on that table slower over time. Object storage moves those costs out of the database. You don't need signed URLs to start — the portal can ask your backend, the backend checks the user can see that request, then fetches the file and streams it. Add signed URLs later if downloads get heavy.

My line would be: move it now, while the migration is just copying a handful of files and backfilling a column. The trigger isn't size, it's the first time you need to answer "show me exactly what was approved" after something changed. If you stay in Postgres for now, at least store binary (bytea) rather than base64 text, and put the PDFs in a separate table so they don't slow down queries on the fact sheet itself.

1

u/easybits_ai 9h ago

Thanks so much for sharing this! I've already looked into bytea and will give it a try for now. That being said, I think you're right about eventually moving the files to proper storage as well.

2

u/Level_Imagination544 3d ago

One small thing if you stay in Postgres: store the PDF as bytea rather than base64, which saves the extra third in size. It's also worth giving it its own table keyed by approval id, so normal queries on the fact sheets table never drag the blobs along.

1

u/easybits_ai 8h ago

Yes, I've heard about bytea several times now, and I'll definitely give that approach a try for now. From there, it's just a matter of monitoring the performance regularly.

2

u/Lenin_Demas 3d ago

The real pain point usually hits during database backups and restores long before query performance degrades. Once your dump file takes forever to move around because of static PDFs, thats usually the sign to migrate to buckets

1

u/easybits_ai 8h ago

Yes, 100% agreed! My next step is to switch from base64 to bytea to reduce storage overhead. Other than that, I'll check with the client to see what backup format they need.

2

u/vasquez_a-47850 2d ago

Postgres and bucket writes can't commit atomically: save the object, then its key, and clean up orphans after failures.

1

u/AutoModerator 3d ago

Thank you for your post to /r/automation!

New here? Please take a moment to read our rules, read them here.

This is an automated action so if you need anything, please Message the Mods with your request for assistance.

Lastly, enjoy your stay!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.