Skip to content

Allow running postgres_query on a specific conn - #614

Merged
staticlibs merged 1 commit into
duckdb:mainfrom
staticlibs:connection_pinning
Oct 13, 2026
Merged

staticlibs merged 1 commit into
duckdb:mainfrom
staticlibs:connection_pinning

Conversation

@staticlibs

@staticlibs staticlibs commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

This PR adds 2 new scalar functions:

  • postgres_pin_connection(<attached_database_name>: VARCHAR) -> UBIGINT
  • postgres_close_pinned_connection(<attached_database_name>: VARCHAR, <connection_id>: UBIGINT) -> BOOL

postgres_pin_connection acquires a connection from the connection pool of the specified attached database and "pins" that connection returning its ID as a handle. This handle can be specified to the postgres_query() function to make it run on that specific connection. This allows to make multiple subsequent postgres_query() and postgres_execute() calls on the same connection, thus keeping consistent server session state.

Pinned connection remains "active" and takes a slot in the connection pool until it is closed by calling postgres_close_pinned_connection.

Usage example:

ATTACH '...' AS s (TYPE postgres);

SET VARIABLE conn = postgres_pin_connection('s');

CALL postgres_execute('s', 'CREATE TEMPORARY TABLE temp1(col1 INT)', connection=getvariable('conn'));

CALL postgres_execute('s', 'INSERT INTO temp1 values(42)', connection=getvariable('conn'));

FROM postgres_query('s', 'SELECT * FROM temp1', connection=getvariable('conn'));
----
00:42:00

SELECT postgres_close_pinned_connection('s', getvariable('conn'));

Testing: query_pin_connection.test added.

This PR adds 2 new scalar functions:

 - `postgres_pin_connection`(`<attached_database_name>`: `VARCHAR`) -> `UBIGINT`
 - `postgres_close_pinned_connection`(`<attached_database_name>`: `VARCHAR`, `<connection_id>`: `UBIGINT`) -> BOOL

`postgres_pin_connection` acquires a connection from the connection pool
of the specified attached database and "pins" that connection returning
its ID as a handle. This handle can be specified to the
`postgres_query()` function to make it run on that specific connection.
This allows to make multiple subsequent `postgres_query()` and
`postgres_execute()` calls on the same connection, thus keeping
consistent server session state.

Pinned connection remains "active" and takes a slot in the connection
pool until it is closed by calling `postgres_close_pinned_connection`.

Usage example:

```sql
ATTACH '...' AS s (TYPE postgres);

SET VARIABLE conn = postgres_pin_connection('s');

CALL postgres_execute('s', 'CREATE TEMPORARY TABLE temp1(col1 INT)', connection=getvariable('conn'));

CALL postgres_execute('s', 'INSERT INTO temp1 values(42)', connection=getvariable('conn'));

FROM postgres_query('s', 'SELECT * FROM temp1', connection=getvariable('conn'));
----
00:42:00

SELECT postgres_close_pinned_connection('s', getvariable('conn'));
```

Testing: `query_pin_connection.test` added.
@staticlibs
staticlibs merged commit 47730ea into duckdb:main Oct 13, 2026
7 checks passed
@staticlibs
staticlibs deleted the connection_pinning branch October 13, 2026 22:57
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