Repository navigation
Description
Hi 👋 I've been reading through the roadmap of the ducklake project where the reduction of the number of round trips is showing up.
Originally, I've contributed #647 before, with the intention to suggest to hide encryption keys in query parameters to the ducklake project, but it seems that those cause four round trips instead of one, which would oppose a central goal of the project.
This brought me to the existing pipeline mode in libpq, its idea to simply send multiple statements with or without query parameters. However, in order to use it most efficiently, I think we'd need to open a statement with prepare=FALSE to have query parameters.
I'm sure you've had multiple sessions already about those round trips, thus I want to highlight that my thoughts here might be premature and at risk of hitting glass ceilings quickly, that are hidden to me, but obvious to you. I mean, I've already noticed that the prepare=FALSE path was equipped with a throw, thus there was some decision in the past against this combination.
Suggestion
Introduce a new function postgres_execute_pipeline which allows to send batches of statements with parameters that are sent without preparing them beforehand instead of joining query literals into a single string as done in ducklake:
FROM postgres_execute_pipeline('pg', [
{'query': 'SET LOCAL extra_float_digits = 3', 'params': []},
{'query': 'INSERT INTO "lake".ducklake_snapshot VALUES ($1, clock_timestamp(), $2, $3, $4)', 'params': ['2', '1', '2', '1']},
... -- cut off for simplicity
]);instead of a single string inside a CALL postgres_execute('__ducklake_metadata_lake', '<the string below>', prepare=FALSE);
SET LOCAL extra_float_digits = 3;
INSERT INTO "lake".ducklake_snapshot VALUES (2, clock_timestamp(), 1, 2, 1);
...I see that this is not improving on the round trip problem per se, but I feel like it enables a way forward in some other dimensions:
- efficient: keep the minimal network interaction
- elegant: enable query parameters
- observable: results/errors/metrics per statement
I guess my punchline being that we could gain something here without losing out on your existing ambitions of using stored procedures in postgres in general and might widen the use-cases the postgres extension can serve.
I'd be happy to learn about your thoughts about any kind of optimization in that regard or in general. The example here is tied to the ducklake project, but I'd argue it would apply in general. Of course, I'd feel more than happy to get my hands dirty or trying to compile some more evidence 🙃
Activity
wergstatt commentedon Nov 8, 2026
Oh damn, my remark aged like milk... I've just spotted duckdb/ducklake#1930 and must admit that I had missed it before. I guess this answers my question about your current plans. At the same time, I feel that query pipelines might still be a viable way forward for ducklake on a postgres catalog, even now that the round trips to it get short.
To me, pipelining could complement the REST API for deployments that live fully inside a data center (or are self-hosted). The REST API brings what pipelining can't, such as clients without direct database access and no tie to postgres. Pipelining attempts to shrink the collision window of
snapshot_idvalues by removing round trips between the client and the postgres catalog. For writes with a Parquet file upload, I see that the upload likely dominates. With inlined writes, however, there is no upload, so the catalog round trips become a major part of the write's cost.My claims above are based on a benchmark I've asked the genie to do. It compares the current ducklake main, #1930 as it is today (unmerged at the time of writing), and a branch that removes and batches statements in libpq pipelines. It illustrates that even in the "one zone" case, the branch can reduce the duration of, say, one inlined
MERGE:On the duckdb-postgres side, this needs a way to send several statements as one pipeline; my branch adds it as
postgres_execute_pipeline. If this direction is of interest, I'm happy to share those branches, but would rather split them into reasonable smaller PRs. Alternatively, I'm also perfectly fine with just proposing the pipeline batch idea here, in case it doesn't fit your plans, as it's also quite postgres-centric.🪄 Genie territory below
Naturally, I've asked the genie to check whether this holds up for a
MERGE, which covers insert, update and delete in one statement. It traced every statement ducklake sends to the postgres catalog for two builds, and counted each round trip twice: inside duckdb-postgres, where libpq talks to the server, and with a TCP proxy between DuckDB and postgres. Both counts agree.postgres_execute_pipeline, grouped into libpq pipelines, with$nparameters. The writes are main's statements. The reads are plainSELECTs too, but changed: catalog reads return rows only if the catalog changed, the file list is read once, and onepg_tableslisting replaces main's probe for one table. No functions,DOblocks or locks.SELECT 1BEGIN, the latest snapshot and 18 guarded catalog reads (rows only if the catalog changed)BEGINSET LOCAL, the 7 writes,COMMITpg_tables)SET LOCALand the 7 writes (inlined rows and deletions), in one stringCOMMITDISCARD ALLEach read on main costs four round trips: prepare, describe, close,
COPY. The 3 round trips are not from pipelining alone. They also include changes to which statements are emitted: no pool health checks, one file list instead of two, and thepg_tableslookup and the list of inlined-data tables read along with the catalog. The pipeline then removes the waits between the statements that remain. The workflow was the same on both builds: same SQL, same setup, same catalog state.snapshot_idis taken at commit, so a concurrent commit between reading the snapshot andCOMMITforces a retry or a failure. That window is 23 round trips on main and 2 with the pipeline.MERGEs. Insert-only, update-only and delete-onlyMERGEs, inlined or with data files, take 27–31 round trips on main and 3 with the pipeline.Time (the table at the top). The same
MERGEin a loop, median of 9 runs, with Toxiproxy adding latency between DuckDB and postgres: 0 ms is a 0.29 ms round trip, 1 ms a 1.49 ms one. For #1930, the latency sits between DuckDB and the REST server, which runs next to postgres. In a loop, main takes 35 round trips perMERGE, because it re-reads the stats the previous commit changed; REST takes 5 from DuckDB, and its server 67 to postgres; the pipeline stays at 3. The rows are inlined, so object storage plays no part. All runs were on one machine (macOS, postgres 17 in a container, Release builds), with #1930 at its head a0d2ead5.The pipeline costs postgres 0.6–1.6 ms more server time per
MERGE, mostly planning, which is less than one round trip at 1 ms.pipeline: 3 round trips
main: 31 round trips