pg_attrdef 是一个 系统目录 表,用于存储列的默认值。
pg_attrdef 在 PostgreSQL 6.2 中被添加。
备注
"列 d.adsrc 不存在"
如果您遇到这样的错误
ERROR: column d.adsrc does not exist
这意味着您正在使用的客户端软件尚未更新以支持 PostgreSQL 12。
除了升级受影响的客户端软件外,没有其他方法可以解决此问题。
按 PostgreSQL 版本定义
pg_attrdef (PostgreSQL 19)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_oid_index" PRIMARY KEY, btree (oid)
"pg_attrdef_adrelid_adnum_index" UNIQUE CONSTRAINT, btree (adrelid, adnum)
文档: pg_attrdef
pg_attrdef (PostgreSQL 18)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_oid_index" PRIMARY KEY, btree (oid)
"pg_attrdef_adrelid_adnum_index" UNIQUE CONSTRAINT, btree (adrelid, adnum)
文档: pg_attrdef
pg_attrdef (PostgreSQL 17)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_oid_index" PRIMARY KEY, btree (oid)
"pg_attrdef_adrelid_adnum_index" UNIQUE CONSTRAINT, btree (adrelid, adnum)
文档: pg_attrdef
pg_attrdef (PostgreSQL 16)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_oid_index" PRIMARY KEY, btree (oid)
"pg_attrdef_adrelid_adnum_index" UNIQUE CONSTRAINT, btree (adrelid, adnum)
文档: pg_attrdef
pg_attrdef (PostgreSQL 15)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_oid_index" PRIMARY KEY, btree (oid)
"pg_attrdef_adrelid_adnum_index" UNIQUE CONSTRAINT, btree (adrelid, adnum)
文档: pg_attrdef
pg_attrdef (PostgreSQL 14)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_oid_index" PRIMARY KEY, btree (oid)
"pg_attrdef_adrelid_adnum_index" UNIQUE CONSTRAINT, btree (adrelid, adnum)
文档: pg_attrdef
pg_attrdef (PostgreSQL 13)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 12)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
oid | oid | | not null |
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | C | not null |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 11)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | | |
adsrc | text | | |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 10)
Table "pg_catalog.pg_attrdef"
Column | Type | Collation | Nullable | Default
---------+--------------+-----------+----------+---------
adrelid | oid | | not null |
adnum | smallint | | not null |
adbin | pg_node_tree | | |
adsrc | text | | |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.6)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+--------------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | pg_node_tree |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.5)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+--------------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | pg_node_tree |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.4)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+--------------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | pg_node_tree |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.3)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+--------------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | pg_node_tree |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.2)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+--------------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | pg_node_tree |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.1)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+--------------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | pg_node_tree |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 9.0)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+----------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | text |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 8.4)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+----------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | text |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 8.3)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+----------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | text |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
pg_attrdef (PostgreSQL 8.2)
Table "pg_catalog.pg_attrdef"
Column | Type | Modifiers
---------+----------+-----------
adrelid | oid | not null
adnum | smallint | not null
adbin | text |
adsrc | text |
Indexes:
"pg_attrdef_adrelid_adnum_index" UNIQUE, btree (adrelid, adnum)
"pg_attrdef_oid_index" UNIQUE, btree (oid)
文档: pg_attrdef
变更历史
- PostgreSQL 12
- 列
adsrc已移除(提交 fe503823)
- 列
- PostgreSQL 6.2
- 已添加(提交 25aa0f8d)
示例
postgres=# CREATE TABLE foo (val TEXT DEFAULT 'bar');
CREATE TABLE
postgres=# SELECT a.*
postgres-# FROM pg_attrdef a
postgres-# JOIN pg_class c ON a.adrelid=c.oid
postgres-# WHERE c.relname='foo'\x\g\x
Expanded display is on.
-[ RECORD 1 ]--------------------------------------------------------------
oid | 16456
adrelid | 16453
adnum | 1
adbin | {CONST :consttype 25
:consttypmod -1
:constcollid 100
:constlen -1
:constbyval false
:constisnull false
:location 35
:constvalue 7 [ 28 0 0 0 98 97 114 ]}
使用 pg_get_expr() 函数将 adbin 的值转换为人类可读的格式,例如:
postgres=# SELECT pg_catalog.pg_get_expr(a.adbin, a.adrelid, true) postgres-# FROM pg_attrdef a postgres-# JOIN pg_class c ON a.adrelid=c.oid postgres-# WHERE c.relname='foo'; pg_get_expr ------------- 'bar'::text (1 row)
参考资料
- PostgreSQL 文档: pg_attrdef
