r/n8n • • 3d ago

Servers, Hosting, & Tech Stuff Storing generated PDFs in Postgres vs object storage: where do you draw the line?

👋 Hey n8n Community,

Follow-up on the approval system I posted about last week. One decision I made this week that I am still not sure about.

The workflow generates a final PDF once Finance approves a request. People download it from a portal afterwards, sometimes repeatedly, sometimes months later. Regenerating on every download meant paying for the conversion again and making them wait several seconds each time.

So I cache it: the PDF is generated at the moment of approval and stored as base64 in a column on the fact sheets table, then served from there.

What I think I got right

Generating at approval rather than on demand means the document always carries the real approval timestamps, and I never pay twice for the same conversion. Caching any earlier would have meant storing drafts that never got approved.

What I am less sure about

Base64 in Postgres is simple. Everything lives in one place, no extra service, no signed URLs, no bucket permissions. But it is blobs in a relational database, and I have not yet hit the point where row sizes start to hurt. The alternative is object storage with a reference in the row, which is more correct and also more moving parts for something a handful of people download a few times a month.

Where would you draw that line? Curious whether people go to object storage from day one or wait until something actually breaks.

Best,
Felix

12 Upvotes

23 comments sorted by

9

u/Free-Good9203 3d ago

Everyone already covered bytea vs base64 and backup/restore times, so one small schema tweak if you decide to stay in Postgres for now: put the PDF blob in its own dedicated child table (approval_id FK, pdf_bytes bytea, sha256 text) instead of a column on fact_sheets.

It prevents any future SELECT * from accidentally dragging binaries into memory, keeps vacuum and row bloat off your hot operational table, gives you a hash to verify the approved document was never altered, and makes migrating to S3/R2 later a clean one-table swap without touching the core schema.

1

u/easybits_ai 8h ago

That's actually something I've already thought about. I've moved the PDF blobs into a separate table to make sure they don't affect performance down the line. But good point about it also making it easier to swap out the underlying storage technology later on.

2

u/cthart 2d ago

Putting that table in a separate tablespace with cheaper/slower storage remains possible then too.

4

u/tankerkiller125real 3d ago • Bravo

As an IT guy it comes down to this.

Is it a file of any kind? Yes? It goes in a proper blob storage system/file store. Full stop, no exceptions, no "Oh it's only a few of them" no BS.

I've been bitten in the ass way too many times by developers and engineers that insisted that they would at max be storing only a few MB at most of blob objects in SQL, only for it to turn into 100s of GBs down the line when they decided "Hey, we did it this way last time, might as well do it again for this entirely different thing".

1

u/easybits_ai 8h ago

Thanks so much for the honest answer! I really appreciate it. I don't come from a strong technical background myself, but I've been building quite a few client automations with n8n, so feedback like this is really valuable to me.

2

u/slvrsmth 10h ago

I am that developer. 

I used to have fast and hard rules like you. Now I happily stash files in the database, and worry about it when it starts biting me. I will also use DB as cache service, and work queue. 

There are much better options for each. I know them, I will occasionally use them. MUCH better options, hands down. 

Also, most systems never reach the scale to benefit from them. But what they do benefit from, from day one, is less moving parts. Less dependent systems, less state synchronization, less network trips. 

Fight the complexity spirit demon only when you have to.

1

u/tankerkiller125real 10h ago

It not just about performance, it's also related to compliance, security, and other things like for example anti-malware/anti-virus scanning performed by the blob cloud provider if we're using one (or whatever is installed on the host system).

1

u/manu144x 2d ago

I would never ever ever store files in the database.

It defeats the whole purpose of it. It can do it but it’s like using a sports car to carry a 20 ton trailer. It can do it, it has the same 450hp a semi has, but it’s just not what it’s built for.

3

u/Low_Rush_8535 3d ago

i'd draw the line at restore time. seed staging with a year's worth of PDFs at your actual file sizes, then time a full restore and check that an old approved PDF still matches byte for byte. if that fits your recovery window, i'd keep them where they are.

1

u/easybits_ai 14h ago

Good point right there!

2

u/amberglad3 3d ago

yeah backup and restore times are usually what bites people way before query performance does

3

u/Weekly-Werewolf4007 3d ago

For a small workflow, I'd keep the simple setup and store the PDF as bytea instead of base64, assuming the n8n step can pass binary data. I'd watch how much the files add to backup size and restore time. Those are the numbers I'd use to decide when a bucket is worth it.

1

u/easybits_ai 14h ago

Yes, those are exactly the metrics I'm monitoring right now as well. I've been using base64 so far, but I think I'll switch to bytea this week and see how that affects performance.

3

u/GFAutoHQ 3d ago

If you stay in Postgres, switch the column from base64 text to bytea. Base64 makes each file about a third bigger, so bytea saves that space for free. Also keep the PDF column out of any query that lists rows, and only pull it in the download step, so normal lookups stay fast. The thing that usually pushes people to object storage is backups, since every PDF rides along in every database dump.

1

u/easybits_ai 14h ago

Thanks so much for sharing this! I've heard about bytea before, but I'll definitely take another look at it for this setup.

2

u/Aide_Sea 3d ago

One thing nobody mentioned: serving it from Postgres means every download holds a DB connection while the file streams out. Fine for a few people, but I think that's maybe the first thing that'd hurt, more than row size :)

1

u/easybits_ai 8h ago

I've actually tested it a few times now with larger groups, and it's held up pretty well. In the latest test, we had 30 users working simultaneously, and everything ran just fine. Since that's roughly the size of the team that will eventually be using the automation, I don't think it'll be an issue. If I were using Google Sheets, though, the story would probably be quite different.

2

u/runtime-ghost 3d ago

Storing base64 in Postgres works at tiny scale because it avoids managing bucket IAM and signed URLs, whereas it quietly adds about 33 percent storage overhead, bloats TOAST tables, and slows down WAL replication and pg_dump restores as volume grows. As a developer, I draw the line using query shape and total retention size rather than an arbitrary file count: if total PDFs stay under a few gigabytes and you only select the blob column by primary key on download (never in list queries), switching from base64 to binary bytea keeps everything atomic in one database, whereas once backups stretch your recovery window or n8n workers hit memory limits passing blobs around, write the file to S3 or Cloudflare R2 and store only the object key and SHA-256 hash in Postgres.

1

u/Electronic_Special48 3d ago

A tip: if the PDFs you are storing are not special (no digital signatures, no high-res diagrams, ...), you can optimize/compress the PDFs before storing them.

Using ghostscript, something like this works wonders:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf in.pdf

1

u/RabHanz 2d ago

Generating at approval is the right call. On where to store it, at a handful of downloads a month Postgres is fine, but I'd change two things now and set one trigger for moving.

  1. Store it as bytea, not base64 in a text column. Base64 makes every file about a third bigger and you pay that on every read and every backup. If the workflow hands you base64, decode it in the insert (decode($1, 'base64')) so the conversion happens once.
  2. Keep it in its own table (id, fact_sheet_id, sha256, size, created_at, the file) instead of a column on the fact sheets table. Then a SELECT * on fact sheets never drags the PDFs along, and moving to object storage later is one table to migrate, not a schema change.

The trigger I use for moving: when the database backup starts taking noticeably longer because of the files, or once the files are most of the database's size. Backups and restores are where blobs in Postgres actually hurt, long before query speed does.

When I've moved files out (to S3-compatible storage, MinIO when self-hosted), the row keeps the key and the hash, and downloads go through a short-lived signed URL. That's maybe an hour of work, so I wouldn't do it on day one for your volume.

1

u/lakeland_nz 2d ago

What benefit are you getting from storing them in the DB?

I get that it's possible, but why is it desirable?

1

u/FlowsmithLabs 1d ago

Generating at approval is the right call, for exactly the reason you gave.

On storage, a few things that might move the line for you:

  • Use bytea**, not base64 text.** Base64 adds about a third to the size. bytea stores the raw bytes, and Postgres moves large values out of the main table (TOAST) on its own.
  • The real pain is usually backups, not queries. Every dump carries every PDF. When backups get slow or large, that's the signal to move.
  • Keep it boring until then. A handful of people downloading a few times a month is well within what Postgres handles. I'd move to object storage when total size reaches a few GB, or when single files are routinely over about 10 MB.
  • Make the move easy later: store a sha256 and the size next to the blob now. Migrating to S3 or R2 then becomes "upload, write the key, check the hash, drop the column".

1

u/addy3006 11h ago

For a handful of downloads a month I'd keep it in Postgres for now, it's fine at that scale. Two small things: store it as bytea instead of base64 (about a third smaller), and keep the PDF in its own table so it doesn't bloat the main one. If volume grows or files get big, moving to object storage later is an easy swap.