r/PostgreSQL • u/easybits_ai • 20h ago
Projects 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/
12
Upvotes
15
u/PrestigiousStrike779 18h ago
For me, I’d probably never do that because I don’t think blobs belong in the database. If I was going to do that I’d at least put it in a separate table so I don’t incur any performance penalties when reading the main row unless I actually wanted that data
9
u/linuxhiker Guru 16h ago
Postgresql is not very efficient for binary objects. If your PDF is 100Mb, the connection will use at least that to deliver the PDF over the wire. Now multiply that by however many people want a pdf.
Use object storage and store the metadata in Postgresql
4
u/yvrelna 3h ago edited 3h ago •
If you want to do blobs in the database, at least do them properly and use the proper blob/bytea type instead of base64-encoded varchar.
Unless you needed the blob to be part of a transaction, it's a bad idea to be storing a blob in the most expensive and hard to scale part of the system which is the relational database.
An object storage are much cheaper and easier to scale horizontally. A CDN can cache frequently downloaded blobs.
Even if you run your own file server, the OS and the web server can serve downloaded file from disk extremely efficiently with zero copy syscall like sendfile or copy_file_range.
By storing and serving a blob in the database, it means you're tying a database server connection, an application server worker, and the RAM needed to cache the file in-memory while the file is being downloaded and base64-decoded. All of which are unnecessarily expensive components when simpler, cheaper, and much more easily scalable solutions exists for not too much extra effort.
2
u/manni66 19h ago
Why base64?
2
u/RevolutionaryRush717 14h ago
Maybe you got the cache part right.
The pdf seems to be ephemeral in nature, in that it always can be generated based on existing data.
Why that would take several seconds only you know.
So the question is why store it permanently at all?
Iff most users need it within, say, 24 hours after the workflow has completed, you can put the pdf in redis with a ttl of 24 hours.
If users return after weeks or months, couldn't they wait a couple of seconds for the pdf to be generated on demand?
2
u/gevorgter 12h ago
it's rarely good idea, main reasons.
1
u/AutoModerator 20h ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Youtube Channel
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/eipi-10 13h ago
How big are the PDFs? I'd store them as bytea until it starts causing problems. Once (if) it does, start writing them to S3 instead and writing the keys - the one-time job to move the existing ones to S3 will be trivial
As a bit of editorial advice: There's never a "right" way to do things, only tradeoffs of different approaches.
1
u/Discordbothost 9h ago
The approved PDF is a record of what Finance signed off on. If you move it to object storage, save the object version in Postgres after upload succeeds and keep that version for as long as a backup could point to it.
1
u/cosmic_kpz 6h ago
please read: https://wiki.postgresql.org/wiki/BinaryFilesInDB
TLDR: Postgres store blobs as text or bytea (with it's “non-printable” octets), in a nut shell you are saving a gigantic wall of "text" in a table field.
Not a single relational database are able to handle this type of operation well, you should look for a TUSD service and store the file_id + hash, if existe multiple versions of the file of time you can thing in append/read only file ledger table with the timestamps.
No need for base64. It will only increase the database size, just don't! for MYSQL exists BLOB type for postgres BYTEA, anyway use TUSD
1
u/krum 6h ago
pretty sure object storage is cheaper than db storage. i would go object storage from day one.
1
u/AintNoGodsUpHere 3h ago
Bruh, blobs don't belong in the database.
1
u/persicsb 12h ago
There is a thing specialized to store a large number of files, it is called a file system. Relational databases are specialized and optimized to store and query relational data.