Skip to content

Allow non-preparable queries in postgres_query - #565

Merged
staticlibs merged 1 commit into
duckdb:mainfrom
staticlibs:query_prepare_flag
Sep 2, 2026
Merged

Allow non-preparable queries in postgres_query#565
staticlibs merged 1 commit into
duckdb:mainfrom
staticlibs:query_prepare_flag

Conversation

@staticlibs

Copy link
Copy Markdown
Member

After #552 the postgres_query() function is also used to run DDL and DML queries. Though the queries are still being prepared on a remote server before execution (this is necessary to get the resulting columns during binding) - the same "general" code path is used for both SELECTs and DML queries.

For majority of the queries this does not cause problems, as Postgres (comparing to other DBs) has minimal limitations to queries that cannot be prepared. Though two notable groups that cannot be prepared contain utility queries (like VACUUM) and concatenated multi-query strings.

This PR adds support for a prepare=FALSE named parameter to postgres_query() that makes such calls to use the "fast-path", when the input string is run in Postgres at once without preparing it.

This non-prepared mode does not allow to specify query parameters and does not return a result set - intended to be used only with utility or DML queries.

Testing: existing postgres_query test is updated with multi-query strings coverage.

After duckdb#552 the `postgres_query()` function is also used to run DDL and
DML queries. Though the queries are still being prepared on a remote
server before execution (this is necessary to get the resulting columns
during binding) - the same "general" code path is used for both
`SELECT`s and DML queries.

For majority of the queries this does not cause problems, as Postgres
(comparing to other DBs) has minimal limitations to queries that cannot
be prepared. Though two notable groups that cannot be prepared contain
utility queries (like `VACUUM`) and concatenated multi-query strings.

This PR adds support for a `prepare=FALSE` named parameter to
`postgres_query()` that makes such calls to use the "fast-path", when
the input string is run in Postgres at once without preparing it.

This non-prepared mode does not allow to specify query parameters and
does not return a result set - intended to be used only with utility or
DML queries.

Testing: existing `postgres_query` test is updated with multi-query
strings coverage.
staticlibs added a commit to staticlibs/ducklake that referenced this pull request Sep 2, 2026
This PR effectively reverts [the change](duckdb@7f2f82c)
to `PostgresMetadataManager` that was added in duckdb#1407.

It is understood, that usage of `postgres_execute()` in
`PostgresMetadataManager` does not expect (and does not need) the
queries to be prepared in Postgres server before execution. To
facilitate this usage, the `prepare=FALSE` named parameter is being
added to `postgres_execute()` in duckdb/duckdb-postgres#565.

Note 1: the PR is filed as a draft, intended to be undrafted after the
`prepare=FALSE` PR is merged and after `duckdb-postgres` version is
bumped in the `duckdb/duckdb` repo.

Note 2: it is expected, that even after the full support of the
[RemoteExecute optimizer](duckdb/duckdb#22914)
is added (still pending for Postgres), the `prepare=FALSE` still can be
useful. As `RemoteExecute` will result into a query like this:

```sql
CALL postgres_execute('s', '<DML1>');CALL postgres_execute('s', '<DML2>');...
```

when the following (original logic before duckdb#1407) may be prefferred
instead:

```sql
CALL postgres_execute('s', '<DML1>;<DML2>;...')
```
@staticlibs
staticlibs merged commit fffcb35 into duckdb:main Sep 2, 2026
7 checks passed
@staticlibs
staticlibs deleted the query_prepare_flag branch September 2, 2026 22:32
Dtenwolde pushed a commit to Dtenwolde/duckdb that referenced this pull request Sep 3, 2026
This PR updates the Postgres extension to bring in the
duckdb/duckdb-postgres#565 change to unblock the DuckLake PR
duckdb/ducklake#1430.
Dtenwolde pushed a commit to Dtenwolde/duckdb that referenced this pull request Sep 3, 2026
This PR updates the Postgres extension to bring in the
duckdb/duckdb-postgres#565 change to unblock the DuckLake PR
duckdb/ducklake#1430.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant