Skip to content

Conversation

@iamcc30
Copy link

@iamcc30 iamcc30 commented Aug 19, 2025

No description provided.

@hailaz hailaz requested a review from Copilot August 21, 2025 07:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a field duplication issue in PostgreSQL multi-schema environments where tables with the same name exist across different schemas. The fix ensures that table field queries are properly scoped to the current schema.

  • Adds schema namespace filtering to the PostgreSQL table fields query
  • Prevents field aggregation from tables with identical names in different schemas

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@lingcoder
Copy link
Contributor

我感觉应该将
SELECT a.attname AS field, t.typname AS type,a.attnotnull as null, (case when d.contype = 'p' then 'pri' when d.contype = 'u' then 'uni' else '' end) as key ,ic.column_default as default_value,b.description as comment ,coalesce(character_maximum_length, numeric_precision, -1) as length ,numeric_scale as scale FROM pg_attribute a left join pg_class c on a.attrelid = c.oid left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1] left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid left join pg_type t ON a.atttypid = t.oid left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname WHERE c.relname = 'xxxxxxx' and a.attisdropped is false and a.attnum > 0 ORDER BY a.attnum;
改成
SELECT a.attname AS field, t.typname AS type,a.attnotnull as null, (case when d.contype = 'p' then 'pri' when d.contype = 'u' then 'uni' else '' end) as key ,ic.column_default as default_value,b.description as comment ,coalesce(character_maximum_length, numeric_precision, -1) as length ,numeric_scale as scale FROM pg_attribute a left join pg_class c on a.attrelid = c.oid left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1] left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid left join pg_type t ON a.atttypid = t.oid left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname WHERE c.oid = 'xxxxxxx'::regclass and a.attisdropped is false and a.attnum > 0 ORDER BY a.attnum;

正常逻辑表现,应该是按照优先级,返回第一张表的元数据,这和PG的数据查询表现是一致的。
现在问题是 元数据返回了多张表,数据查询返回的是第一张表。select 多表字段 from 第一张表 导致报错。

@lingcoder
Copy link
Contributor

PR的实现有点问题,search_path多schema设置下,只会返回第一个schema。 如果表出现在第2个schema里,就会查不到。

@lingcoder
Copy link
Contributor

如果PR主能看到的话,可以尝试下我的实现,加上单测^_^

@hailaz
Copy link
Contributor

hailaz commented Sep 30, 2025

@iamcc30 see see

我感觉应该将 SELECT a.attname AS field, t.typname AS type,a.attnotnull as null, (case when d.contype = 'p' then 'pri' when d.contype = 'u' then 'uni' else '' end) as key ,ic.column_default as default_value,b.description as comment ,coalesce(character_maximum_length, numeric_precision, -1) as length ,numeric_scale as scale FROM pg_attribute a left join pg_class c on a.attrelid = c.oid left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1] left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid left join pg_type t ON a.atttypid = t.oid left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname WHERE c.relname = 'xxxxxxx' and a.attisdropped is false and a.attnum > 0 ORDER BY a.attnum; 改成 SELECT a.attname AS field, t.typname AS type,a.attnotnull as null, (case when d.contype = 'p' then 'pri' when d.contype = 'u' then 'uni' else '' end) as key ,ic.column_default as default_value,b.description as comment ,coalesce(character_maximum_length, numeric_precision, -1) as length ,numeric_scale as scale FROM pg_attribute a left join pg_class c on a.attrelid = c.oid left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1] left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid left join pg_type t ON a.atttypid = t.oid left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname WHERE c.oid = 'xxxxxxx'::regclass and a.attisdropped is false and a.attnum > 0 ORDER BY a.attnum;

正常逻辑表现,应该是按照优先级,返回第一张表的元数据,这和PG的数据查询表现是一致的。 现在问题是 元数据返回了多张表,数据查询返回的是第一张表。select 多表字段 from 第一张表 导致报错。

@iamcc30
Copy link
Author

iamcc30 commented Oct 9, 2025

我理解的是只会操作当前的scheme,不会有多个scheme一起查询的情况,不知道是否理解有误。

@lingcoder
Copy link
Contributor

我理解的是只会操作当前的scheme,不会有多个scheme一起查询的情况,不知道是否理解有误。

不是的,现实中,search_path 很可能配置多个schema。 按照兄台之前的改动,永远只会命中当前schema。而实际情况是根据schema 优先级,命中第一个

@lingcoder
Copy link
Contributor

如果你配置多个search_path=user,public (默认),表如果在public下,你的实现就会查不到

@iamcc30
Copy link
Author

iamcc30 commented Oct 9, 2025

我明白你的意思了

@hailaz hailaz changed the title fix:修复pgsql多schema模式下相同表名中字段叠加的问题 fix: Fixed the problem of overlapping fields in the same table name in pgsql multiple schema mode Oct 11, 2025
@hailaz hailaz changed the title fix: Fixed the problem of overlapping fields in the same table name in pgsql multiple schema mode fix(contrib/drivers/pgsql): Fixed the problem of overlapping fields in the same table name in pgsql multiple schema mode Oct 11, 2025
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.

3 participants