Skip to content

Infer the columns of pg_lake_remote tables created without a column list - #933

Open
sfc-gh-agedemenli wants to merge 3 commits into
mainfrom
agedemenli/external-engine-columns
Open

sfc-gh-agedemenli wants to merge 3 commits into
mainfrom
agedemenli/external-engine-columns

Conversation

@sfc-gh-agedemenli

@sfc-gh-agedemenli sfc-gh-agedemenli commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Lets a pg_lake_remote foreign table be created without a column list, as pg_lake already allows for files: pg_lake asks the engine for the table's columns and declares them with the matching PostgreSQL types.

CREATE FOREIGN TABLE hits () SERVER analytics OPTIONS (schema_name 'main', table_name 'hits');

How

  • Columns: pg_lake sends DESCRIBE SELECT * FROM <table> to the engine, connecting with the user mapping of the user that creates the table, and maps each DuckDB type with the same function as for files. Columns keep the engine's names and order. A name longer than PostgreSQL's limit is an error rather than being truncated, since the names have to match the engine's.
  • Fixed-size arrays: a DuckDB fixed-size array such as INTEGER[3] is declared as an array (integer[]), also inside a STRUCT or MAP, since PostgreSQL arrays have no fixed size. Arrays of maps are now declared as arrays too, which also fixes inferring a list of maps from a file.
  • Checks first: USAGE on the server and the table options are checked before connecting, as CREATE FOREIGN TABLE would check them later. Otherwise a non-superuser could make the engine describe a table name that it reads as a file.
  • Skipped: partitions and tables created with INHERITS still take their columns from their parent, and IF NOT EXISTS does not contact the engine when the table exists in the schema it would be created in.
  • Docs: docs/external-query-engines.md shows the syntax, how fixed-size arrays are declared, and lists DESCRIBE among what an engine has to provide.

Tests

test_external_engine_table.py covers inferred names, order and types (including numeric(p,s), timestamptz, arrays, fixed-size arrays at the top level and inside structs and maps, arrays of structs and maps, and a mixed-case name) with pushdown on and off. It also covers IF NOT EXISTS on an existing table, including one of the same name in a later search_path schema, a table the engine lacks, a missing table_name, a column name that is too long, and a non-superuser getting PostgreSQL's errors without the engine being contacted. The existing column inference, permissions, complex types and inheritance tests also pass.

@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from 065be52 to 1c96aff Compare November 25, 2026 13:22
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-columns branch from 92ceab0 to b35d7bc Compare November 25, 2026 13:23
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from 1c96aff to da2b434 Compare November 26, 2026 03:24
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-columns branch from b35d7bc to fc4ae86 Compare November 26, 2026 03:24
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from da2b434 to c6b2cc2 Compare November 26, 2026 04:10
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-columns branch 2 times, most recently from 7fa1393 to 73127ab Compare November 27, 2026 08:40
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from e371e44 to 8aa2ca7 Compare November 27, 2026 08:44
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-columns branch from 73127ab to 6872b8c Compare November 27, 2026 08:44
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-table branch from 8aa2ca7 to c335c6e Compare November 27, 2026 11:32
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-columns branch from 6872b8c to 2a962b8 Compare November 27, 2026 11:32
Base automatically changed from agedemenli/external-engine-table to main November 28, 2026 08:14
sfc-gh-agedemenli and others added 2 commits November 28, 2026 11:17
CREATE FOREIGN TABLE t () SERVER <pg_lake_remote server> OPTIONS (...) now
asks the engine for the columns of its table with DESCRIBE, as the user that
creates the foreign table, and declares them with the PostgreSQL types of
their DuckDB types, as pg_lake does for files. Partitions and tables created
with INHERITS still take their columns from their parent.

USAGE on the server and the table options, which only superusers can set,
are checked before connecting, as CREATE FOREIGN TABLE would.

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/external-engine-columns branch from 2a962b8 to df6c4c5 Compare November 28, 2026 08:20

@sfc-gh-mslot sfc-gh-mslot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, this looks good. I checked that a non-superuser can't reach the DESCRIBE, and a wide range of DuckDB types (struct, map, hugeint, enum, timestamp_ns, ...) map and read back correctly with pushdown on and off. Two small things: the IF NOT EXISTS shortcut can produce a table with no columns, and fixed-size arrays are declared as scalars.

Comment thread pg_lake_table/src/ddl/create_table.c Outdated

/* let PostgreSQL skip a table that already exists */
if (createStmt->base.if_not_exists &&
OidIsValid(RangeVarGetRelid(createStmt->base.relation, NoLock, true)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

RangeVarGetRelid searches the whole search_path, so with search_path = a, b and an existing b.events, CREATE FOREIGN TABLE IF NOT EXISTS events () ... skips inference and PostgreSQL then creates a.events with no columns. Could this look up the name in RangeVarGetCreationNamespace(...) with get_relname_relid instead?

errdetail("Column names are limited to %d bytes.",
NAMEDATALEN - 1)));

Oid typeId = GetOrCreatePGTypeForDuckDBTypeName(columnType, &typeMod);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A DuckDB fixed-size array like INTEGER[3] comes back as plain integer here, since only a [] suffix counts as an array. The CREATE succeeds but every query then fails with Unimplemented type for cast (INTEGER[3] -> INTEGER). Maybe map it to integer[], or give the "not supported" error at CREATE time?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Mapped them to arrays: the type parser now takes [n] wherever it takes [], so INTEGER[3] becomes integer[], also inside a STRUCT or MAP, where it failed the CREATE before. Arrays of maps (MAP(...)[] and MAP(...)[2]) were declared as a single map, so they're arrays now too. The test covers these with pushdown on and off.

…s in their schema

A DuckDB fixed-size array such as INTEGER[3] was declared as a scalar, so
queries on the table failed, and one in a STRUCT or MAP failed the CREATE.
The type parser now takes [n] wherever it takes [], since PostgreSQL arrays
have no fixed size, and arrays of maps become arrays.

CREATE FOREIGN TABLE IF NOT EXISTS without a column list skipped inferring
the columns if a table of that name was anywhere on the search_path, and
PostgreSQL then created one without columns in the first schema. It now
looks in the schema that the table would be created in.

Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

This branch has not been deployed

No deployments
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