Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/storage/postgres_table_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ string PostgresTableSet::GetInitializeQuery(const string &schema, const string &
SELECT pg_namespace.oid AS namespace_id, relname, relpages, attname,
pg_type.typname type_name, atttypmod type_modifier, pg_attribute.attndims ndim,
attnum, pg_attribute.attnotnull AS notnull, NULL constraint_id,
NULL constraint_type, NULL constraint_key, type_ns.nspname AS type_schema
NULL constraint_type, NULL constraint_key, type_ns.nspname AS type_schema,
col_desc.description AS column_comment,
tbl_desc.description AS table_comment
FROM pg_class
JOIN pg_namespace ON relnamespace = pg_namespace.oid
JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid
JOIN pg_type ON atttypid=pg_type.oid
JOIN pg_namespace type_ns ON pg_type.typnamespace = type_ns.oid
LEFT JOIN pg_description col_desc ON col_desc.objoid=pg_class.oid AND col_desc.objsubid=pg_attribute.attnum
LEFT JOIN pg_description tbl_desc ON tbl_desc.objoid=pg_class.oid AND tbl_desc.objsubid=0
WHERE attnum > 0 AND relkind IN ('r', 'v', 'm', 'f', 'p') ${CONDITION}
UNION ALL
SELECT pg_namespace.oid AS namespace_id, relname, NULL relpages, NULL attname, NULL type_name,
NULL type_modifier, NULL ndim, NULL attnum, NULL AS notnull,
pg_constraint.oid AS constraint_id, contype AS constraint_type,
conkey AS constraint_key, NULL AS type_schema
conkey AS constraint_key, NULL AS type_schema,
NULL AS column_comment, NULL AS table_comment
FROM pg_class
JOIN pg_namespace ON relnamespace = pg_namespace.oid
JOIN pg_constraint ON (pg_class.oid=pg_constraint.conrelid)
Expand All @@ -62,6 +67,7 @@ void PostgresTableSet::AddColumn(optional_ptr<PostgresTransaction> transaction,
type_info.type_modifier = result.GetInt64(row, column_index + 2);
type_info.array_dimensions = result.GetInt64(row, column_index + 3);
bool is_not_null = result.GetBool(row, column_index + 5);
string column_comment = result.IsNull(row, 13) ? "" : result.GetString(row, 13);
idx_t type_schema_index = column_index + 9;
if (!result.IsNull(row, type_schema_index)) {
type_info.type_schema = result.GetString(row, type_schema_index);
Expand All @@ -73,6 +79,9 @@ void PostgresTableSet::AddColumn(optional_ptr<PostgresTransaction> transaction,
table_info.postgres_types.push_back(std::move(postgres_type));
table_info.postgres_names.push_back(column_name);
ColumnDefinition column(std::move(column_name), std::move(column_type));
if (!column_comment.empty()) {
column.SetComment(Value(column_comment));
}
if (!default_value.empty()) {
auto expressions = Parser::ParseExpressionList(default_value);
if (expressions.empty()) {
Expand Down Expand Up @@ -135,6 +144,10 @@ void PostgresTableSet::CreateEntries(PostgresTransaction &transaction, PostgresR
}
info = make_uniq<PostgresTableInfo>(schema, table_name);
info->approx_num_pages = result.IsNull(row, 2) ? 0 : result.GetInt64(row, 2);
// Read table-level comment from column 14
if (!result.IsNull(row, 14)) {
info->create_info->comment = Value(result.GetString(row, 14));
}
}
AddColumnOrConstraint(&transaction, &schema, result, row, *info);
}
Expand Down Expand Up @@ -174,6 +187,10 @@ unique_ptr<PostgresTableInfo> PostgresTableSet::GetTableInfo(PostgresTransaction
AddColumnOrConstraint(&transaction, &schema, *result, row, *table_info);
}
table_info->approx_num_pages = result->IsNull(0, 2) ? 0 : result->GetInt64(0, 2);
// Read table-level comment from 14
if (!result->IsNull(0, 14)) {
table_info->create_info->comment = Value(result->GetString(0, 14));
}
return table_info;
}

Expand All @@ -190,6 +207,10 @@ unique_ptr<PostgresTableInfo> PostgresTableSet::GetTableInfo(ClientContext &cont
AddColumnOrConstraint(nullptr, nullptr, *result, row, *table_info);
}
table_info->approx_num_pages = result->IsNull(0, 2) ? 0 : result->GetInt64(0, 2);
// Read table-level comment
if (!result->IsNull(0, 14)) {
table_info->create_info->comment = Value(result->GetString(0, 14));
}
return table_info;
}

Expand Down Expand Up @@ -427,5 +448,4 @@ void PostgresTableSet::AlterTable(ClientContext &context, PostgresTransaction &t
}
ClearEntries();
}

} // namespace duckdb
49 changes: 49 additions & 0 deletions test/sql/storage/attach_comments.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# name: test/sql/storage/attach_comments.test
# description: Test that Postgres table and column comments propagate through DuckDB metadata
# group: [storage]

# group [storage]

require postgres_scanner

require-env POSTGRES_TEST_DATABASE_AVAILABLE

# Attach the Postgres database
statement ok
ATTACH 'dbname=postgresscanner' AS s (TYPE POSTGRES);

# Clean up from any previous runs
statement ok
CALL postgres_execute('s', 'DROP TABLE IF EXISTS comment_test');

# Create the test table in Postgres
statement ok
CALL postgres_execute('s', 'CREATE TABLE comment_test (name TEXT, value INTEGER)');

# Set table comment via Postgres
statement ok
CALL postgres_execute('s', $$COMMENT ON TABLE comment_test IS 'this is a table comment'$$);

# Set column comment via Postgres
statement ok
CALL postgres_execute('s', $$COMMENT ON COLUMN comment_test.name IS 'this is a column comment'$$);

# Clear DuckDB's metadata cache so fresh comments are loaded
statement ok
CALL pg_clear_cache();

# Verify the table comment appears in duckdb_tables()
query I
SELECT comment FROM duckdb_tables() WHERE table_name = 'comment_test';
----
this is a table comment

# Verify the column comment appears in duckdb_columns()
query I
SELECT comment FROM duckdb_columns() WHERE table_name = 'comment_test' AND column_name = 'name';
----
this is a column comment

# Cleanup
statement ok
CALL postgres_execute('s', 'DROP TABLE comment_test');
8 changes: 4 additions & 4 deletions test/sql/storage/attach_connection_pool_configure.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ ATTACH 'dbname=postgresscanner' AS s1 (TYPE POSTGRES);
statement ok
RESET GLOBAL pg_pool_max_connections

query IIIIIIIIIII
SELECT catalog_name, max_connections, cache_hits, cache_misses, try_failures, thread_local_cache_enabled, max_lifetime_millis, idle_timeout_millis, reaper_thread_running, reaper_thread_period_millis, health_check_query
query IIIIIIIIII
SELECT catalog_name, max_connections, /* cache_hits, */ cache_misses, try_failures, thread_local_cache_enabled, max_lifetime_millis, idle_timeout_millis, reaper_thread_running, reaper_thread_period_millis, health_check_query
FROM postgres_configure_pool()
ORDER BY catalog_name
----
s 8 1 1 0 FALSE 0 60000 1 30000 SELECT 1
s1 8 1 1 0 FALSE 0 60000 1 30000 SELECT 1
s 8 1 0 FALSE 0 60000 1 30000 SELECT 1
s1 8 1 0 FALSE 0 60000 1 30000 SELECT 1

query II
SELECT catalog_name, acquire_mode
Expand Down
Loading