PG_TABLE_DEF - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

PG_TABLE_DEF

存储有关表列的信息。

PG_TABLE_DEF 仅返回有关对用户可见的表的信息。如果 PG_TABLE_DEF 未返回预期结果,则验证 search_path 参数是否正确设置为包含相关 schemas。

可使用 SVV_TABLE_INFO 查看有关表的更多信息,包括数据分配偏斜、密钥分配偏斜、表大小和统计数据。

表列

列名称 数据类型 描述
schemaname 名称 schema 名称。
tablename 名称 表名称。
column 名称 列名称。
type text 列的数据类型。
encoding character(32) 列的编码。
distkey 布尔值 如果此列为表的分配键,则为 true。
sortkey integer 排序键中的列的顺序。如果表使用一个复合排序键,则排序键中的所有列将具有一个正值,该值指示列在排序键中的位置。如果表使用交错排序键,则排序键中的每个列将具有一个正值或负值,其中,绝对值指示列在排序键中的位置。如果为 0,则列不是排序键的一部分。
notnull 布尔值 如果列具有 NOT NULL 约束,则为 true。

示例

以下示例显示 LINEORDER_COMPOUND 表的复合排序键列。

select "column", type, encoding, distkey, sortkey, "notnull" from pg_table_def where tablename = 'lineorder_compound' and sortkey <> 0; column | type | encoding | distkey | sortkey | notnull -------------+---------+----------+---------+---------+-------- lo_orderkey | integer | delta32k | false | 1 | true lo_custkey | integer | none | false | 2 | true lo_partkey | integer | none | true | 3 | true lo_suppkey | integer | delta32k | false | 4 | true lo_orderdate | integer | delta | false | 5 | true (5 rows)

以下示例显示 LINEORDER_INTERLEAVED 表的交错排序键列。

select "column", type, encoding, distkey, sortkey, "notnull" from pg_table_def where tablename = 'lineorder_interleaved' and sortkey <> 0; column | type | encoding | distkey | sortkey | notnull -------------+---------+----------+---------+---------+-------- lo_orderkey | integer | delta32k | false | -1 | true lo_custkey | integer | none | false | 2 | true lo_partkey | integer | none | true | -3 | true lo_suppkey | integer | delta32k | false | 4 | true lo_orderdate | integer | delta | false | -5 | true (5 rows)

PG_TABLE_DEF 将仅返回搜索路径中包含的 schema 中的表的信息。有关更多信息,请参阅search_path

例如,假定您创建一个新 schema 和一个新表,然后查询 PG_TABLE_DEF。

create schema demo; create table demo.demotable (one int); select * from pg_table_def where tablename = 'demotable'; schemaname|tablename|column| type | encoding | distkey | sortkey | notnull ----------+---------+------+------+----------+---------+---------+--------

此查询未返回新表的行。检查 search_path 的设置。

show search_path; search_path --------------- $user, public (1 row)

demo 架构添加到搜索路径并重新运行查询。

set search_path to '$user', 'public', 'demo'; select * from pg_table_def where tablename = 'demotable'; schemaname| tablename |column| type | encoding |distkey|sortkey| notnull ----------+-----------+------+---------+----------+-------+-------+-------- demo | demotable | one | integer | none | f | 0 | f (1 row)