You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe what's wrong
Clickhouse cannot query postgres tables with autogenerated columns that have been altered
Looks like its coming from #57568
Does it reproduce on the most recent release?
Yes, reproducible on 24.3 and head-alpine docker image.
How to reproduce
The setup:
Clickhouse instance connected to postgres instance, using pg_conn named collection for creds
Postgres table:
db=# create table test (id integer primary key);
CREATE TABLE
db=# alter table test add column val integer generated always as (id + 1) stored;
ALTER TABLE
db=# alter table test drop column val;
ALTER TABLE
db=# alter table test add column val integer generated always as (id + 2) stored;
ALTER TABLE
db=# select attname,attnum from pg_attribute
where attrelid = 'test'::regclass and attnum > 0
order by attnum;
attname | attnum
------------------------------+--------
id | 1
........pg.dropped.2........ | 2
val | 3
(3 rows)
Clickhouse query:
ba443a2d339a :) select * from postgresql(pg_conn, table='test');
SELECT *
FROM postgresql(pg_conn, `table` = 'test')
Query id: f70d8b00-55e6-4255-8ce8-822a89e93c18
Received exception from server (version 24.3.2):
Code: 49. DB::Exception: Received from localhost:9000. DB::Exception: Received adnum 3, but currently fetched columns list has 2 columns. (LOGICAL_ERROR)
I'm able to repro this. It seems any alter operation on the columns of the table will add an entry to the pg_attribute table. Whenever we're trying to fetch data from pg_attribute related to the table, we get data of all the columns that were deleted too. And that doesn't match with the actual number of columns present in the table, and an exception is being raised[1].
One solution would be to ignore all such additional rows that were added during deletes("........pg.dropped.2........") instead of throwing an exception. @Algunenano thoughts on how this should be addressed? I can help fix this.
[1]
if (check_generated)
{
std::string attrdef_query = fmt::format(
"SELECT adnum, pg_get_expr(adbin, adrelid) as generated_expression "
"FROM pg_attrdef "
"WHERE adrelid = (SELECT oid FROM pg_class WHERE {});", where);
pqxx::result result{tx.exec(attrdef_query)};
for (const auto row : result)
{
size_t adnum = row[0].as<int>();
if (!adnum || adnum > table.physical_columns->names.size())
{
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Received adnum {}, but currently fetched columns list has {} columns",
adnum, table.physical_columns->attributes.size());
}
const auto column_name = table.physical_columns->names[adnum - 1];
table.physical_columns->attributes.at(column_name).attr_def = row[1].as<std::string>();
}
}
It makes sense to ignore columns that are dropped. I guess that we need to do it via SQL pg_attribute.attisdropped but I haven't tested it to confirm how it works.
Describe what's wrong
Clickhouse cannot query postgres tables with autogenerated columns that have been altered
Looks like its coming from #57568
Does it reproduce on the most recent release?
Yes, reproducible on 24.3 and head-alpine docker image.
How to reproduce
The setup:
Clickhouse instance connected to postgres instance, using pg_conn named collection for creds
Postgres table:
Clickhouse query:
Expected behavior
Table 'test' can be queried from clickhouse
Error message and/or stacktrace
Stacktrace
The text was updated successfully, but these errors were encountered: