

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# PG\_TABLE\_DEF
<a name="r_PG_TABLE_DEF"></a>

存储有关表列的信息。

PG\_TABLE\_DEF 仅返回有关对用户可见的表的信息。如果 PG\_TABLE\_DEF 未返回预期结果，则验证 [search\_path](r_search_path.md) 参数是否正确设置为包含相关 schemas。

可使用 [SVV\_TABLE\_INFO](r_SVV_TABLE_INFO.md) 查看有关表的更多信息，包括数据分配偏斜、密钥分配偏斜、表大小和统计数据。

## 表列
<a name="r_PG_TABLE_DEF-table-columns2"></a>


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

## 示例
<a name="r_PG_TABLE_DEF-example2"></a>

以下示例显示 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](r_search_path.md)。

例如，假定您创建一个新 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)
```