pg_duckdb: Splicing Duck and Elephant DNA
motherduck.com
motherduck.com
I don't and never have enjoyed SQL and I much prefer the ergonomics of time_bucket to date_bin.
For example, I would do this in duckdb:
SELECT
count(*) as y
, time_bucket(interval '2 weeks', at::timestamp) as x
FROM analytics
WHERE some_bool AND some_haystack = 'needle'
GROUP BY x
ORDER BY x
In postgres it looks more like: with counts as (
SELECT
date_bin('1 hour'::interval, at, (now() - interval '2 weeks')::timestamp)
, count(*) c
FROM analytics a
WHERE some_bool AND some_haystack = 'needle'
GROUP BY date_bin
)
select series as x, coalesce(counts.c, 0) as y
from generate_series(
(now() - interval '2 weeks')::timestamp,
now()::timestamp,
interval '1 hour'
) series
LEFT JOIN counts
ON counts.date_bin = series;We recently submitted our (Crunchy Bridge for Analytics-at most broad level based on same idea) benchmark for clickbench by clickhouse (https://benchmark.clickhouse.com/) which puts us at #6 overall amongst managed service providers and gives a real viable option for Postgres as an analytics database (at least per clickbench). Also of note there are a number of other Postgres variations such as ParadeDB that are definitely not 1000x slower than Clickhouse or DuckDB.
Really excited about the idea of being able to have everything under the postgres umbrella even with sacrifices.
From the engineering side I have nothing but good things to say about duckdb.
I opened up the database to the frontend (it's an internal reporting tool not unlike grafana and I filtered queries through an allowlist) and it was pure delight to have the metrics queries right next to the graph. Very rapid iterations.
I'm hopeful the pg_duckdb project will mature enough to be a stable foundation for ParadeDB and others, but that appears to be a matter of MotherDuck and how much they're willing to push this forward.
https://github.com/duckdb/pg_duckdb
Sounds like it would be useful for Postgres users to interact with Parquet and CSV data within a single SQL query and in a performant way (due to DuckDB's vectorization).
Here are the scenarios and how to address them
1. Query Parquet and Iceberg from Postgres. When Parquet files are stored in S3 Postgres should be able to run analytical queries on them.
2. Postgres should allow creation of columnstore tables inside Postgres storage subsystem. Analytical queries on top of these table should be FAST. Top 10 on Clickbench fast. This allows to run analytics without S3 and have super low latencies for analytics.
3. Postgres should allow creation of secondary columnstore indexes to speed up analytical queries in mixed workloads. This is super useful for Oracle migrations since Oracle had this feature for a while.
So How do we get there? 10 years ago it would be a MASSIVE project, but today we have @duckdb - super fast analytical engine with an open license. The work is still not trivial, but it is much much simpler.
First you need to integrate an analytical query processor into Postgres and today @duckdblabs announced github.com/duckdb/pg_duck…. Yay and congrats!
This plugin runs duckdb alongside with Postgres and integrated Postgres syntax with the @duckdb query processor (QP)
With that it now can trivially query external files from S3. This addresses scenario 1.
With that it now can trivially query external files from S3. This addresses scenario 1.
Building columnar table requires either implementing columnar storage from scratch or integrating duckdb storage into the Postgres subsystem. You can of course let duckdb create duckdb files on local disk, but then all the Postgres machinery: replication, backup, recovery won't work
Duckdb tables have to mapped into 8kb Postgres pages pushed through the Postgres WAL for replication, recovery and transactionality. This will give us scenario 2
Scenario 3 is even more work. You need secondary index maintenance and it will require hybrid query execution. We will need to modify Postgres executor so that it can mix and match regular Postgres query operators and "vectorized" query operators from duckdb. Or built vectorized operators into Postgres
Scenarios 2 and 3 will take some time, but I'm excited for this roadmap: this will unlock a huge world for millions of Postgres users and simplify the lives of many developers dealing with moving data between transactional and analytical systems.
a much updated fork of citus' columnar using Postgres tableam.
re: duckdb handling storage - https://github.com/hydradatabase/pg_quack/tree/branch-0.0.1
an earlier implementation
I'm not sure that saying it's abandoned is quite accurate. looking at it from a (now outside) lens, I see it as more of a hard engineering project.
I still use columnar: it's very mature at this point, but any further (large) improvements would require a lot larger engineering efforts.
https://duckdb.org/docs/data/partitioning/hive_partitioning....
Being a Postgres fan, Good luck and best wishes with the effort here!
It would be great if one could have a diversity of postgres's in a data mesh but you can execute the same exact sql on them