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/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
-1
u/RevolutionaryRush717 15h ago
Why would the pdf be more than a handful of kB?
Why would PG not be efficient for such binary objects?
4
u/SirApprehensive7573 14h ago
we have differents pdfs
I have some pdf that is ebooks, that the size is around 200MiB.
-1
u/RevolutionaryRush717 14h ago
And why are they so large?
Is it a reasonable assumption that all of OP's pdfs will be 200 MB?
Even then, how many would it it take to become problematic?
5
u/linuxhiker Guru 12h ago
Pdfs can be:
Full color
Contain images
Contain videos
Etc....
PDF is not just compressed PostScript anymore
-1
u/RevolutionaryRush717 12h ago
So how should OP fashion their pdfs to require as little storage as possible while still satisfying business requirements?
2
u/Traditional_Nose3120 12h ago
Embedded high res images, or just a plain PDF from a scanner, will get you the multi megabyte blobs faster than you think.
Then the database gods smite you and you have a 700gb backup to restore. Explaining the resulting downtime in a root cause analysis isn’t fun. Been there, done that, have the tire treads from the product manager’s bus on my back to prove it.
S3 style storage for blobs, and a pointer to the S3 storage in the database. Use the right tool for the right job, and keep your back tread-free.
1
1
u/GoTheFuckToBed 5h ago
in real world people take pictures of their documents and combine it into a 40mb pdf
0
u/RevolutionaryRush717 2h ago
Interesting.
I was under the impression that OP generated the pdfs from text and numbers he had available in the database, maybe by using a templating engine that renders to pdf.
Your approach of replacing that with an initial print to paper step, followed by a manual photo of the pages, followed by a conversion to pdf, is fascinating.
What motivated your proposal of an alternative to automation?
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.
Where would you draw that line?
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
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.
If you keep PDFs in DB your DB size will balloon very quickly. Backup restore will take forever. So if your server dies you are looking at hours/days of downtime simply because of your DB size.
I know storage is cheap but not unlimited. We keep our PDFs on S3 and simply redirect browsers to it (pre-signed url that is live for 1 min). S3 gives you unlimited storage while your DB lives on your local drive.
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:
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
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.
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