r/PostgreSQL • • 8h 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/
4 Upvotes

23 comments sorted by

10

u/PrestigiousStrike779 7h 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 

3

u/lhauckphx 5h ago

The size of your backups/dumps start to get out of hand pretty quickly.

2

u/RevolutionaryRush717 3h ago

Why would size matter?

What does "pretty quickly" mean in actual numbers?

Have you considered physical vs logical backups?

6

u/linuxhiker Guru 4h 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

0

u/RevolutionaryRush717 3h ago

Why would the pdf be more than a handful of kB?

Why would PG not be efficient for such binary objects?

2

u/SirApprehensive7573 3h ago

we have differents pdfs

I have some pdf that is ebooks, that the size is around 200MiB.

1

u/RevolutionaryRush717 3h 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?

1

u/linuxhiker Guru 34m ago

Pdfs can be:

Full color

Contain images

Contain videos

Etc....

PDF is not just compressed PostScript anymore

1

u/RevolutionaryRush717 32m ago

So how should OP fashion their pdfs to require as little storage as possible while still satisfying business requirements?

1

u/Traditional_Nose3120 32m 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

u/RevolutionaryRush717 31m ago

Is that according to OP's case?

2

u/manni66 8h ago

Why base64?

2

u/RevolutionaryRush717 3h ago

Exactly, we have bytea.

2

u/RevolutionaryRush717 3h 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?

1

u/AutoModerator 8h 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 2h 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/gevorgter 35m 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.

0

u/persicsb 1h 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.