Show HN: Embed an SQLite database in your PostgreSQL table

frectonz | 180 points

> Most relational database management systems do not support nested records, so tables are in first normal form by default. In particular, SQL does not have any facilities for creating or exploiting nested tables. [0]

“Not with that attitude.”

– frectonz

[0]: https://en.wikipedia.org/wiki/First_normal_form

sgarland | 2 months ago

Ok, hear me out: what if we make something that takes a postgres database dir, tars it together and encodes it as a binary blob in SQLite?

We could have SQLite within postgres within sqlite within postgres! Is it practical or even slightly useful? Of course not - but it's SQL databases all the way down. Not that this is a good thing in itself.

aerzen | 2 months ago

What are the use cases for this? I can't imagine designing a database schemas to use this in a typical product. Is it intended for hybrid applications to back up local user data directly with their account info?

robertclaus | 2 months ago

I think SQLite columns for SQLite would be superior to SQLite’s JSON columns whose operators are a whole ‘nother query language you need to learn and seem comparatively limited.

ecuaflo | 2 months ago

They /tmp file mechanism sounds like a bit of a hack, is that definitely necessary?

It may be possible to create a SQLite in-memory database instead and then load the binary blob data into it using the backup API or some kind of trick with VACUUM INTO.

simonw | 2 months ago

Yo, dawg, I heard you like databases...

This is nuts. I can't think of a use for it, but I'm sure it's "a solution that will eventually find a problem".

bitwize | 2 months ago

If you’re using Postgres, multi tenancy has been solved with row level security. It’s super easy to add a tenant id column to every table and a policy that only allows connections to see data from one tenant

klysm | 2 months ago

Noob question: isn't it kind of defeating the main purpose of SQLite? Meaning having db running in the same process as the program?

id02009 | 2 months ago

1NF crime against humanity?

bni | 2 months ago

"multitenancy has been solved" - I'm confused, can you not use multiple SQLite files??

abrookewood | 2 months ago

You may want to use the $$ way to declare strings for your examples. Something like:

-- Create a todo for "frectonz"

UPDATE people

SET database = execute_sqlite(

    database,

    $sqlite$INSERT INTO todos VALUES ('solve multitenancy')$sqlite$
)

WHERE name = 'frectonz';

arkh | 2 months ago

I’m trying to think through when I’d reach for this over jsonb… I guess the fact that there’s an enforced schema? And that you could do aggregations on your SQLite db? Or maybe if you wanted to send the whole delete db to a client??

zekenie | 2 months ago

This is likely great for serving SQLite data to frontends using the SQLite http-vfs.

Would be great combined with functions/triggers/views to mirror specific data/queries from Postgres as SQLite.

rustman123 | 2 months ago

What, no operators? I want indexes on these columns, and some weird and wonderful operator syntax for doing cross-database joins between multiple DATABASE columns! =)

Pinus | 2 months ago
[deleted]
| 2 months ago

This is not as crazy as it sounds but I'd rather have PG-in-PG than the hetrogeneous arrangement here. PG in S3 would be useful too.

dboreham | 2 months ago

Are there still reasons to use PostgreSQL?

I like the simplicity of SQLite's "a file is all you need" approach so much, that I started to converge all my projects to SQLite. So far, I have not come across any roadblocks.

Can anyone think of a use case where PostgreSQL is better suited than SQLite?

TekMol | 2 months ago

A query on very ugly tables will count 26 thousands occurrences of the apostrophe, I love it

foul | 2 months ago

Please let us embed that sqlite-hosting postgres in a TXT DNS record. We really need that.

tacone | 2 months ago

That’s fun, but I think I'll just use an SQLite foreign data wrapper instead.

Apreche | 2 months ago

Someone, somewhere will eventually find a legitimate use case for it.

BiteCode_dev | 2 months ago

If you do the same for DuckDB you could call it pgducken. ;-)

snthpy | 2 months ago

Speed, anyone?

How long does it take to update a table of, say, 1k rows? 1m rows? Same when subqueries and joins are involved to calculate what's to be updated?

kunley | 2 months ago

Yo dawg, I heard you liked databases. So I put a database inside your database!

Exactly what we need from a Show HN.

nojvek | 2 months ago

Posts like this make me realize I never even scratched the surface of any technology ever.

counterpartyrsk | 2 months ago

The real win of this seems like schema divergence among one column?

klysm | 2 months ago

Yet again SQlite is over represented here in the server context.

amazingamazing | 2 months ago

Why?

lakomen | 2 months ago

How would embedding a database inside another help someone?

DeathArrow | 2 months ago

Yo dawg I heard you liked databases so we put a database in your database.

StayTrue | 2 months ago

[flagged]

fredtalty5 | 2 months ago