

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

# 系统目录表
<a name="c_intro_catalog_views"></a>

**Topics**
+ [PG\$1ATTRIBUTE\$1INFO](r_PG_ATTRIBUTE_INFO.md)
+ [PG\$1CLASS\$1INFO](r_PG_CLASS_INFO.md)
+ [PG\$1DATABASE\$1INFO](r_PG_DATABASE_INFO.md)
+ [PG\$1DEFAULT\$1ACL](r_PG_DEFAULT_ACL.md)
+ [PG\$1EXTERNAL\$1SCHEMA](r_PG_EXTERNAL_SCHEMA.md)
+ [PG\$1LIBRARY](r_PG_LIBRARY.md)
+ [PG\$1PROC\$1INFO](r_PG_PROC_INFO.md)
+ [PG\$1STATISTIC\$1INDICATOR](r_PG_STATISTIC_INDICATOR.md)
+ [PG\$1TABLE\$1DEF](r_PG_TABLE_DEF.md)
+ [PG\$1USER\$1INFO](pg_user_info.md)
+ [查询目录表](c_join_PG.md)

系统目录存储 schema 元数据，例如，有关表和列的信息。系统目录表具有 PG 前缀。

Amazon Redshift 用户可访问标准 PostgreSQL 目录表。有关 PostgreSQL 系统目录的更多信息，请参阅 [PostgreSQL 系统表](https://www.postgresql.org/docs/8.0/static/catalogs.html#CATALOGS-OVERVIEW)。

# PG\$1ATTRIBUTE\$1INFO
<a name="r_PG_ATTRIBUTE_INFO"></a>

PG\$1ATTRIBUTE\$1INFO 是基于 PostgreSQL 目录表 PG\$1ATTRIBUTE 和内部目录表 PG\$1ATTRIBUTE\$1ACL 构建的 Amazon Redshift 系统视图。PG\$1ATTRIBUTE\$1INFO 包括有关表或视图的列的详细信息，包括列访问控制列表（如果有）。

## 表列
<a name="r_PG_ATTRIBUTE_INFO-table-columns"></a>

除了 PG\$1ATTRIBUTE 中的列之外，PG\$1ATTRIBUTE\$1INFO 还显示以下列。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_ATTRIBUTE_INFO.html)

# PG\$1CLASS\$1INFO
<a name="r_PG_CLASS_INFO"></a>

PG\$1CLASS\$1INFO 是基于 PostgreSQL 目录表 PG\$1CLASS 和 PG\$1CLASS\$1EXTENDED 而构建的 Amazon Redshift 系统视图。PG\$1CLASS\$1INFO 包括有关表创建时间和当前分配方式的详细信息。有关更多信息，请参阅 [用于优化查询的数据分配](t_Distributing_data.md)。

PG\$1CLASS\$1INFO 对所有用户可见。超级用户可以查看所有行；普通用户只能查看其自己的数据。有关更多信息，请参阅 [系统表和视图中的数据可见性](cm_chap_system-tables.md#c_visibility-of-data)。

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

除了 PG\$1CLASS 中的列以外，PG\$1CLASS\$1INFO 还显示以下列。PG\$1CLASS 中的 `oid` 列在 PG\$1CLASS\$1INFO 表中称为 `reloid`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_CLASS_INFO.html)

PG\$1CLASS\$1INFO 中的 RELEFFECTIVEDISTSTYLE 列指示表的当前分配方式。如果表使用自动分配，则 RELEFFECTIVEDISTSTYLE 为 10、11 或 12，这指示有效分配方式是 AUTO（ALL）、AUTO（EVEN）或 AUTO（KEY）。如果表使用自动分配，则分配方式最初可能显示 AUTO（ALL）。然后，当表增长时更改为 AUTO（EVEN），或者，如果发现某个列可用作分配键，则更改为 AUTO（KEY）。

下表的 RELEFFECTIVEDISTSTYLE 列中提供了每个值的分配方式：


| RELEFFECTIVEDISTSTYLE | 当前分配方式 | 
| --- | --- | 
| 0 | EVEN | 
| 1 | KEY | 
| 8 | ALL | 
| 10 | AUTO (ALL) | 
| 11 | AUTO (EVEN) | 
| 12 | AUTO (KEY) | 

## 示例
<a name="r_PG_CLASS_INFO-example"></a>

以下查询返回目录中表的当前分配模式。

```
select reloid as tableid,trim(nspname) as schemaname,trim(relname) as tablename,reldiststyle,releffectivediststyle, 
CASE WHEN "reldiststyle" = 0 THEN 'EVEN'::text 
     WHEN "reldiststyle" = 1 THEN 'KEY'::text 
     WHEN "reldiststyle" = 8 THEN 'ALL'::text 
     WHEN "releffectivediststyle" = 10 THEN 'AUTO(ALL)'::text 
     WHEN "releffectivediststyle" = 11 THEN 'AUTO(EVEN)'::text 
     WHEN "releffectivediststyle" = 12 THEN 'AUTO(KEY)'::text ELSE '<<UNKNOWN>>'::text END as diststyle,relcreationtime 
from pg_class_info a left join pg_namespace b on a.relnamespace=b.oid;
```

```
 tableid | schemaname | tablename | reldiststyle | releffectivediststyle | diststyle  |      relcreationtime       
---------+------------+-----------+--------------+-----------------------+------------+----------------------------
 3638033 | public     | customer  |            0 |                     0 | EVEN       | 2019-06-13 15:02:50.666718
 3638037 | public     | sales     |            1 |                     1 | KEY        | 2019-06-13 15:03:29.595007
 3638035 | public     | lineitem  |            8 |                     8 | ALL        | 2019-06-13 15:03:01.378538
 3638039 | public     | product   |            9 |                    10 | AUTO(ALL)  | 2019-06-13 15:03:42.691611
 3638041 | public     | shipping  |            9 |                    11 | AUTO(EVEN) | 2019-06-13 15:03:53.69192
 3638043 | public     | support   |            9 |                    12 | AUTO(KEY)  | 2019-06-13 15:03:59.120695
(6 rows)
```

# PG\$1DATABASE\$1INFO
<a name="r_PG_DATABASE_INFO"></a>

PG\$1DATABASE\$1INFO 是一个扩展 PostgreSQL 目录表 PG\$1DATABASE 的 Amazon Redshift 系统视图。

PG\$1DATABASE\$1INFO 对所有用户可见。

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

除了 PG\$1DATABASE 中的列之外，PG\$1DATABASE\$1INFO 还包含以下列。PG\$1DATABASE 中的 `oid` 列在 PG\$1DATABASE\$1INFO 表中称为 `datid`。有关更多信息，请参阅 [PostgreSQL 文档](https://www.postgresql.org/docs/8.0/catalog-pg-database.html)。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_DATABASE_INFO.html)

# PG\$1DEFAULT\$1ACL
<a name="r_PG_DEFAULT_ACL"></a>

存储有关默认访问权限的信息。有关默认访问权限的更多信息，请参阅 [ALTER DEFAULT PRIVILEGES](r_ALTER_DEFAULT_PRIVILEGES.md)。

PG\$1DEFAULT\$1ACL 对所有用户可见。超级用户可以查看所有行；普通用户只能查看其自己的数据。有关更多信息，请参阅 [系统表和视图中的数据可见性](cm_chap_system-tables.md#c_visibility-of-data)。

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

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_DEFAULT_ACL.html)

## 示例
<a name="r_PG_DEFAULT_ACL-example"></a>

以下查询返回为数据库定义的所有默认权限。

```
select pg_get_userbyid(d.defacluser) as user, 
n.nspname as schema, 
case d.defaclobjtype when 'r' then 'tables' when 'f' then 'functions' end 
as object_type, 
array_to_string(d.defaclacl, ' + ')  as default_privileges 
from pg_catalog.pg_default_acl d 
left join pg_catalog.pg_namespace n on n.oid = d.defaclnamespace;

 user  | schema | object_type |              default_privileges
-------+--------+-------------+-------------------------------------------------------
 admin | tickit | tables      | user1=r/admin + "group group1=a/admin" + user2=w/admin
```

上述示例中的结果表明，对于用户 `admin` 在 `tickit` schema 中创建的所有新表，`admin` 将 SELECT 权限授予 `user1`，将 INSERT 权限授予 `group1`，并将 UPDATE 权限授予 `user2`。

# PG\$1EXTERNAL\$1SCHEMA
<a name="r_PG_EXTERNAL_SCHEMA"></a>

存储有关外部 schema 的信息。

PG\$1EXTERNAL\$1SCHEMA 对所有用户可见。超级用户可以查看所有行；普通用户只能查看其具有访问权限的元数据。有关更多信息，请参阅 [CREATE EXTERNAL SCHEMA](r_CREATE_EXTERNAL_SCHEMA.md)。

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

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_EXTERNAL_SCHEMA.html)

## 示例
<a name="r_PG_EXTERNAL_SCHEMA-example"></a>

以下示例显示有关外部 schema 的详细信息。

```
select esoid, nspname as schemaname, nspowner, esdbname as external_db, esoptions 
from pg_namespace a,pg_external_schema b where a.oid=b.esoid;

esoid  | schemaname      | nspowner | external_db | esoptions                                                   
-------+-----------------+----------+-------------+-------------------------------------------------------------
100134 | spectrum_schema |      100 | spectrum_db | {"IAM_ROLE":"arn:aws:iam::123456789012:role/mySpectrumRole"}
100135 | spectrum        |      100 | spectrumdb  | {"IAM_ROLE":"arn:aws:iam::123456789012:role/mySpectrumRole"}
100149 | external        |      100 | external_db | {"IAM_ROLE":"arn:aws:iam::123456789012:role/mySpectrumRole"}
```

# PG\$1LIBRARY
<a name="r_PG_LIBRARY"></a>

存储有关用户定义的库的信息。

PG\$1LIBRARY 对所有用户可见。超级用户可以查看所有行；普通用户只能查看其自己的数据。有关更多信息，请参阅 [系统表和视图中的数据可见性](cm_chap_system-tables.md#c_visibility-of-data)。

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

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_LIBRARY.html)

## 示例
<a name="r_PG_LIBRARY-example"></a>

以下示例返回用户安装的库的信息。

```
select * from pg_library;

name       | language_oid | file_store_id | owner
-----------+--------------+---------------+------
f_urlparse |       108254 |          2000 |   100
```

# PG\$1PROC\$1INFO
<a name="r_PG_PROC_INFO"></a>

PG\$1PROC\$1INFO 是 Amazon Redshift 系统视图，构建在 PostgreSQL 目录表 PG\$1PROC 和内部目录表 PG\$1PROC\$1EXTENDED 之上。PG\$1PROC\$1INFO 包括有关存储过程和函数的详细信息，这包括与输出参数（如果有）相关的信息。

## 表列
<a name="r_PG_PROC_INFO-table-columns"></a>

除了 PG\$1PROC 中的列以外，PG\$1PROC\$1INFO 还显示以下列。PG\$1PROC 中的 `oid` 列在 PG\$1PROC\$1INFO 表中称为 `prooid`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_PROC_INFO.html)

PG\$1PROC\$1INFO 中的 proargnames 字段包含所有类型参数（包含 OUT 和 INOUT，如有）的名称。

# PG\$1STATISTIC\$1INDICATOR
<a name="r_PG_STATISTIC_INDICATOR"></a>

存储有关自上次执行 ANALYZE 以来插入或删除的行数的信息。由于 PG\$1STATISTIC\$1INDICATOR 表在 DML 操作后频繁更新，因此统计数据是近似值。

PG\$1STATISTIC\$1INDICATOR 只对超级用户可见。有关更多信息，请参阅 [系统表和视图中的数据可见性](cm_chap_system-tables.md#c_visibility-of-data)。

## 表列
<a name="r_PG_STATISTIC_INDICATOR-table-columns"></a>

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_STATISTIC_INDICATOR.html)

## 示例
<a name="r_PG_STATISTIC_INDICATOR-example"></a>

以下示例返回自上次执行 ANALYZE 以来发生更改的表的信息。

```
select * from pg_statistic_indicator;

stairelid | stairows | staiins | staidels
----------+----------+---------+---------
   108271 |       11 |       0 |        0
   108275 |      365 |       0 |        0
   108278 |     8798 |       0 |        0
   108280 |    91865 |       0 |   100632
   108267 |    89981 |   49990 |     9999
   108269 |      808 |     606 |      374
   108282 |   152220 |   76110 |   248566
```

# PG\$1TABLE\$1DEF
<a name="r_PG_TABLE_DEF"></a>

存储有关表列的信息。

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

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

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

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/r_PG_TABLE_DEF.html)

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

以下示例显示 LINEORDER\$1COMPOUND 表的复合排序键列。

```
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\$1INTERLEAVED 表的交错排序键列。

```
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\$1TABLE\$1DEF 将仅返回搜索路径中包含的 schema 中的表的信息。有关更多信息，请参阅 [search\$1path](r_search_path.md)。

例如，假定您创建一个新 schema 和一个新表，然后查询 PG\$1TABLE\$1DEF。

```
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)
```

# PG\$1USER\$1INFO
<a name="pg_user_info"></a>

PG\$1USER\$1INFO 是 Amazon Redshift 系统视图，此视图显示用户信息，例如用户 ID 和密码过期时间。

只有超级用户才能看到 PG\$1USER\$1INFO。

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

PG\$1USER\$1INFO 包含以下列。有关更多信息，请参阅 [PostgreSQL 文档](https://www.postgresql.org/docs/8.0/view-pg-user.html)。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/redshift/latest/dg/pg_user_info.html)

# 查询目录表
<a name="c_join_PG"></a>

**Topics**
+ [目录查询示例](c_join_PG_examples.md)

通常，可将目录表和视图（其名称以 **PG\$1** 开头的关系）联接到 Amazon Redshift 表和视图。

目录表使用了 Amazon Redshift 不支持的大量数据类型。当查询将目录表联接到 Amazon Redshift 表时，支持以下数据类型：
+ 布尔
+ "char"
+ float4
+ int2
+ int4
+ int8
+ 名称
+ oid
+ text
+ varchar

如果编写一个显式或隐式引用具有不支持的数据类型的列的联接查询，则该查询将返回一个错误。某些目录表中使用的 SQL 函数也受支持，但 PG\$1SETTINGS 和 PG\$1LOCKS 表使用的 SQL 函数除外。

例如，无法在与 Amazon Redshift 表的联接中查询 PG\$1STATS 表，因为存在不受支持的函数。

以下目录表和视图提供了可联接到 Amazon Redshift 表中的信息的有用信息。由于数据类型和函数限制，其中的一些表仅允许部分访问。在查询可部分访问的表时，请仔细选择或引用其列。

以下表可完全访问且不包含任何不受支持的数据类型或函数：
+  [pg\$1attribute](https://www.postgresql.org/docs/8.0/static/catalog-pg-attribute.html) 
+  [pg\$1cast ](https://www.postgresql.org/docs/8.0/static/catalog-pg-cast.html) 
+  [pg\$1depend](https://www.postgresql.org/docs/8.0/static/catalog-pg-depend.html) 
+  [pg\$1description ](https://www.postgresql.org/docs/8.0/static/catalog-pg-description.html) 
+  [pg\$1locks ](https://www.postgresql.org/docs/8.0/static/view-pg-locks.html) 
+  [pg\$1opclass ](https://www.postgresql.org/docs/8.0/static/catalog-pg-opclass.html) 

以下表可部分访问且包含一些不受支持的类型、函数和已截断的文本列。文本列中的值将截断为 varchar(256) 值。
+  [pg\$1class](https://www.postgresql.org/docs/8.0/static/catalog-pg-class.html) 
+  [pg\$1constraint](https://www.postgresql.org/docs/8.0/static/catalog-pg-constraint.html) 
+  [pg\$1database](https://www.postgresql.org/docs/8.0/static/catalog-pg-database.html) 
+  [pg\$1group](https://www.postgresql.org/docs/8.0/static/catalog-pg-group.html) 
+  [pg\$1language ](https://www.postgresql.org/docs/8.0/static/catalog-pg-language.html) 
+  [pg\$1namespace](https://www.postgresql.org/docs/8.0/static/catalog-pg-namespace.html) 
+  [pg\$1operator](https://www.postgresql.org/docs/8.0/static/catalog-pg-operator.html) 
+  [pg\$1proc](https://www.postgresql.org/docs/8.0/static/catalog-pg-proc.html) 
+  [pg\$1settings](https://www.postgresql.org/docs/8.0/static/view-pg-settings.html) 
+  [pg\$1statistic](https://www.postgresql.org/docs/8.0/static/catalog-pg-statistic.html) 
+  [pg\$1tables](https://www.postgresql.org/docs/8.0/static/view-pg-tables.html) 
+  [pg\$1type ](https://www.postgresql.org/docs/8.0/static/catalog-pg-type.html) 
+  [pg\$1user](https://www.postgresql.org/docs/8.0/static/view-pg-user.html) 
+  [pg\$1views](https://www.postgresql.org/docs/8.0/static/view-pg-views.html) 

对于 Amazon Redshift 管理员，此处未列出的目录表是不可访问的或可能没有用。不过，当查询未涉及与 Amazon Redshift 表的联接时，可以查询任何目录表或视图。

可以将 Postgres 目录表中的 OID 列用作联接列。例如，联接条件 `pg_database.oid = stv_tbl_perm.db_id` 将每个 PG\$1DATABASE 行的内部数据库对象 ID 与 STV\$1TBL\$1PERM 表中的可见 DB\$1ID 列匹配。OID 列是内部主键，当您从表中选择时，这些列不可见。目录视图没有 OID 列。

某些 Amazon Redshift 函数必须只在计算节点上运行。如果查询引用用户创建的表，则 SQL 在计算节点上运行。

仅引用目录表（具有 PG 前缀的表（如 PG\$1TABLE\$1DEF））或不引用任何表的查询在领导节点上以独占方式运行。

如果使用计算节点函数的查询不引用用户定义的表，否则 Amazon Redshift 系统表会返回以下错误。

```
[Amazon](500310) Invalid operation: One or more of the used functions must be applied on at least one user created table.
```

# 目录查询示例
<a name="c_join_PG_examples"></a>

以下查询说明了用来查询目录表以获取有关 Amazon Redshift 数据库的有用信息的几种方法。

## 查看表 ID、数据库、架构和表名称
<a name="c_join_PG_examples-view-tableid-db-schema-tablename"></a>

以下视图定义将 STV\$1TBL\$1PERM 系统表与 PG\$1CLASS、PG\$1NAMESPACE 和 PG\$1DATABASE 系统目录表联接以返回表 ID、数据库名称、schema 名称和表名称。

```
create view tables_vw as
select distinct(stv_tbl_perm.id) table_id
,trim(pg_database.datname)   db_name
,trim(pg_namespace.nspname)   schema_name
,trim(pg_class.relname)   table_name
from stv_tbl_perm
join pg_class on pg_class.oid = stv_tbl_perm.id
join pg_namespace on pg_namespace.oid = pg_class.relnamespace
join pg_database on pg_database.oid = stv_tbl_perm.db_id;
```

以下示例返回 ID 为 117855 的表的信息。

```
select * from tables_vw where table_id = 117855;
```

```
table_id | db_name   | schema_name | table_name
---------+-----------+-------------+-----------
  117855 |       dev | public      | customer
```

## 列出每个 Amazon Redshift 表的列数
<a name="c_join_PG_examples-list-the-number-of-columns-per-amazon-redshift-table"></a>

以下查询联接了一些目录表，以了解每个 Amazon Redshift 表包含多少列。Amazon Redshift 表名称存储在 PG\$1TABLES 和 STV\$1TBL\$1PERM 中；如果可能，请使用 PG\$1TABLES 返回 Amazon Redshift 表名称。

此查询不涉及任何 Amazon Redshift 表。

```
select nspname, relname, max(attnum) as num_cols
from pg_attribute a, pg_namespace n, pg_class c
where n.oid = c.relnamespace and  a.attrelid = c.oid
and c.relname not like '%pkey'
and n.nspname not like 'pg%'
and n.nspname not like 'information%'
group by 1, 2
order by 1, 2;

nspname | relname  | num_cols
--------+----------+----------
public  | category |        4
public  | date     |        8
public  | event    |        6
public  | listing  |        8
public  | sales    |       10
public  | users    |       18
public  | venue    |        5
(7 rows)
```



## 列出数据库中的架构和表
<a name="c_join_PG_examples-list-the-schemas-and-tables-in-a-database"></a>

以下查询将 STV\$1TBL\$1PERM 联接到一些 PG 表以返回 TICKIT 数据库中的表及其 schema 名称（NSPNAME 列）的列表。该查询还返回每个表中的总行数。（在系统中的多个 schemas 具有相同的表名称时，此查询很有用。）

```
select datname, nspname, relname, sum(rows) as rows
from pg_class, pg_namespace, pg_database, stv_tbl_perm
where pg_namespace.oid = relnamespace
and pg_class.oid = stv_tbl_perm.id
and pg_database.oid = stv_tbl_perm.db_id
and datname ='tickit'
group by datname, nspname, relname
order by datname, nspname, relname;

datname | nspname | relname  |  rows
--------+---------+----------+--------
tickit  | public  | category |     11
tickit  | public  | date     |    365
tickit  | public  | event    |   8798
tickit  | public  | listing  | 192497
tickit  | public  | sales    | 172456
tickit  | public  | users    |  49990
tickit  | public  | venue    |    202
(7 rows)
```

## 列出表 ID、数据类型、列名称和表名称
<a name="c_join_PG_examples-list-table-ids-data-types-column-names-and-table-names"></a>

以下查询列出有关每个用户表及其列的一些信息：表 ID、表名称、表列名称和每个列的数据类型：

```
select distinct attrelid, rtrim(name), attname, typname
from pg_attribute a, pg_type t, stv_tbl_perm p
where t.oid=a.atttypid and a.attrelid=p.id
and a.attrelid between 100100 and 110000
and typname not in('oid','xid','tid','cid')
order by a.attrelid asc, typname, attname;

attrelid |  rtrim   |    attname     |  typname
---------+----------+----------------+-----------
  100133 | users    | likebroadway   | bool
  100133 | users    | likeclassical  | bool
  100133 | users    | likeconcerts   | bool
...
  100137 | venue    | venuestate     | bpchar
  100137 | venue    | venueid        | int2
  100137 | venue    | venueseats     | int4
  100137 | venue    | venuecity      | varchar
...
```

## 计算表中的每个列的数据块数
<a name="c_join_PG_examples-count-the-number-of-data-blocks-for-each-column-in-a-table"></a>

以下查询将 STV\$1BLOCKLIST 表联接到 PG\$1CLASS 以返回 SALES 表中列的存储信息。

```
select col, count(*)
from stv_blocklist s, pg_class p
where s.tbl=p.oid and relname='sales'
group by col
order by col;

col | count
----+-------
  0 |     4
  1 |     4
  2 |     4
  3 |     4
  4 |     4
  5 |     4
  6 |     4
  7 |     4
  8 |     4
  9 |     8
 10 |     4
 12 |     4
 13 |     8
(13 rows)
```