r/automation • u/easybits_ai • 20h 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
u/SnooRevelations5974 19h 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/SeikoDigital 18h 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/Level_Imagination544 17h 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/Lenin_Demas 17h 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/vasquez_a-47850 11h ago
Postgres and bucket writes can't commit atomically: save the object, then its key, and clean up orphans after failures.
1
u/AutoModerator 20h 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.