Live data from Hacker News

Querying Postgres Tables Directly from DuckDB

duckdb.org

21–30 of 41 posts

Re: Querying Postgres Tables Directly from DuckDB

#21

Earlier quoted context omitted.

Yes, ClickHouse can query Postgres for maybe two years already. https://clickhouse.com/docs/en/integrations/postgresql/postg... You can connect to Postgres using a table function: SELECT ... FROM postgresql(...) You can create a table with ENGINE = PostgreSQL and use it like a normal table. You can create a database with ENGINE = PostgreSQL and it will represent all the tables from the PostgreSQL database. And finall…

Similar features available for MySQL, MongoDB and SQLite.

What is surprising: if you query MySQL from ClickHouse, the queries work faster despite the fact that the data is simply read from MySQL.

And two more use-cases: - you can create a table in ClickHouse pointing to multiple MySQL servers for sharding and failover, so ClickHouse can be used to query sharded MySQL; - you can create a key-value dictionary on top of MySQL tables and use it in JOINs.

Re: Querying Postgres Tables Directly from DuckDB

#22
post #2

This might end up being the best way to etl postgres tables to parquet. From everything else that I tried, doing a copy to CSV and then converting to parquet was the fastest but can be a pain when dealing with type conversions.

It is very easy with ClickHouse. All you need is:

SELECT ... FROM postgresql(...) FORMAT Parquet

And you can run this query without installing ClickHouse, using the clickhouse-local command-line tool.

It can be downloaded simply as:

curl https://clickhouse.com/ | sh

Re: Querying Postgres Tables Directly from DuckDB

#24
The way they do this read is interesting, and especially why they don't just read the files on disk directly.

> The Postgres Scanner uses the standard libpq library, which it statically links in. Ironically, this makes the Postgres Scanner easier to install than the other Postgres clients. However, Postgres’ normal client-server protocol is quite slow, so we spent quite some time optimizing this. As a note, DuckDB’s SQLite Scanner does not face this issue, as SQLite is also an in-process database.

> We actually implemented a prototype direct reader for Postgres’ database files, but while performance was great, there is the issue that committed but not yet checkpointed data would not be stored in the heap files yet. In addition, if a checkpoint was currently running, our reader would frequently overtake the checkpointer, causing additional inconsistencies. We abandoned that approach since we want to be able to query an actively used Postgres database and believe that consistency is important. Another architectural option would have been to implement a DuckDB Foreign Data Wrapper (FDW) for Postgres similar to duckdb_fdw but while this could improve the protocol situation, deployment of a postgres extension is quite risky on production servers so we expect few people will be able to do so.

> Instead, we use the rarely-used binary transfer mode of the Postgres client-server protocol. This format is quite similar to the on-disk representation of Postgres data files and avoids some of the otherwise expensive to-string and from-string conversions. For example, to read a normal int32 from the protocol message, all we need to do is to swap byte order (ntohl).

Re: Querying Postgres Tables Directly from DuckDB

#25
post #15

SQLite and DuckDB are excellent demonstrations that the computers we have on our desks (and even in our pockets) are ridiculously fast and capable for almost everything but the largest workloads. They're a stark contrast to framework bloat that has stolen our CPU cycles and given us perceptible lag when typing and scrolling.

It makes one wonder what the software world would like if the whole notion of 'premature optimization is the root of all evil' never existed. Because that advice, well intentioned and perfectly reasonable when applied thoughtfully, gradually morphed into 'optimization is the root of all evil' contributing to this ongoing race between computers speeding up and software slowing down.

And that's a scenario which a cynic would observe was almost certainly anything but undesired by the top players on either side. It's much easier to require what would be considered a supercomputer not long ago, to run a word processor, than it is to create scenarios where such power is meaningfully applied in a mass market product.

Re: Querying Postgres Tables Directly from DuckDB

#26
post #15

SQLite and DuckDB are excellent demonstrations that the computers we have on our desks (and even in our pockets) are ridiculously fast and capable for almost everything but the largest workloads. They're a stark contrast to framework bloat that has stolen our CPU cycles and given us perceptible lag when typing and scrolling.

It makes one wonder what the software world would like if the whole notion of 'premature optimization is the root of all evil' never existed. Because that advice, well intentioned and perfectly reasonable when applied thoughtfully, gradually morphed into 'optimization is the root of all evil' contributing to this ongoing race between computers speeding up and software slowing down. And that's a scenario which a cynic…

I don't think the idea morphed into "any optimization" is evil. But it is the unfortunate consequence of leaving optimization until after functional requirements are met. Same with any kind of tech debt. A mentality of "Let's just get this out the door now any fix it later" results in later meaning never.

Re: Querying Postgres Tables Directly from DuckDB

#27

I wasn't aware of DuckDB but now that I read about I wonder should I replace my SQLite drivers with it, since it seems more versatile?

one thing to be aware of is that duckdb doesn't currently allow concurrent readers and writers (unlike sqlite). depending on your use case, that might be a showstopper.

Re: Querying Postgres Tables Directly from DuckDB

#28
Can I expect pretty significant boosts in query speed when using DuckDB on my existing PG database? I'm very intrigued now that it's possible to directly query PG. We don't yet have an analytics DB and instead run all analytics stuff on a read replica. Some of the queries are pretty slow. If I can just slap DuckDB on top and speed things up, I'd be really thrilled.

Re: Querying Postgres Tables Directly from DuckDB

#30
post #2

This might end up being the best way to etl postgres tables to parquet. From everything else that I tried, doing a copy to CSV and then converting to parquet was the fastest but can be a pain when dealing with type conversions.

This won't let you ETL from PG to parquet, but I used this in anger the other day https://github.com/manojkarthick/pqrs Worked quite well for my purposes!
Post reply on HN