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

11 Upvotes

17 comments sorted by

3

u/Low_Rush_8535 2d 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 2h ago

Good point right there!

2

u/amberglad3 2d ago

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

3

u/Weekly-Werewolf4007 2d 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 2h 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 2d 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 2h 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.

8

u/Free-Good9203 2d 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/cthart 2d ago

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

3

u/tankerkiller125real 2d ago

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/manu144x 1d 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.

2

u/runtime-ghost 2d 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/Aide_Sea 2d 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/Electronic_Special48 2d 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 1d 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 1d 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 22h 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".