Skip to content

Conversation

dpxcc
Copy link
Contributor

@dpxcc dpxcc commented Aug 20, 2025

This PR adds support for subextensions in pg_duckdb. Subextensions can now register their own TAMs to trigger DuckDB execution, similar to how DuckDB temp tables are handled:

  1. Subextensions can call RegisterDuckdbTableAm() to register their DuckDB-specific TAMs
  2. During query planning, pg_duckdb calls IsDuckdbTable() in src/pgduckdb_hooks.cpp to check if it's a DuckDB-specific TAM. A query needs DuckDB execution when there are any DuckDB-specific TAMs
  3. When translating Postgres queries to DuckDB queries, a Postgres table <schema>.<table> with DuckDB-specific TAM <tam> will be translated to a DuckDB table <tam>.<schema>.<table> (special handling for DuckDB temp table is preserved)
  4. Subextensions can create a DuckDB storage extension that attaches to the <tam> database, enabling custom scans against their DuckDB-specific TAMs

Even better, the DuckDB extension can detect at runtime whether it is running inside Postgres with pg_duckdb or in standalone DuckDB, and adjust its logic accordingly. This allows the same extension to be used seamlessly in both environments

@dpxcc
Copy link
Contributor Author

dpxcc commented Aug 20, 2025


extern "C" __attribute__((visibility("default"))) void
RegisterDuckdbTableAm(const char *name, const TableAmRoutine *am) {
duckdb_table_ams[am] = name;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check first if it's already in there, so extensions cannot accidentally override already existing ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, fixed!

@JelteF JelteF merged commit a91cf6d into duckdb:main Aug 25, 2025
7 checks passed
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.

2 participants