

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 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)

系統目錄儲存結構描述中繼資料，例如資料表和資料欄的相關資訊。系統目錄資料表有 PG 字首。

Amazon Redshift 使用者可存取標準 PostgreSQL 目錄資料表。如需 PostgreSQL 系統目錄的相關資訊，請參閱 [PostgreSQL system tables](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_tw/redshift/latest/dg/r_PG_ATTRIBUTE_INFO.html)

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

PG\$1CLASS\$1INFO 是一個 Amazon Redshift 系統檢視，以 PostgreSQL 類別資料表 PG\$1CLASS 和 PG\$1CLASS\$1EXTENDED 為建置基礎。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\$1CLAS\$1INFO 資料表中稱為 `reloid`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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` 所建立的所有新資料表，`admin` 將 SELECT 權限授予給 `user1`、INSERT 權限授予給 `group1` 而 UPDATE 權限授予給 `user2`。

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

儲存外部結構描述的相關資訊。

所有使用者都可看見 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_tw/redshift/latest/dg/r_PG_EXTERNAL_SCHEMA.html)

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

下列範例顯示外部結構描述的詳細資訊。

```
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_tw/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 是以 PostgreSQL 目錄資料表 PG\$1PROC 和內部目錄資料表 PG\$1PROC\$1EXTENDED 為基礎建立的 Amazon Redshift 系統檢視。PG\$1PROC\$1INFO 包含儲存程序和函數的詳細資訊，包括輸出引數 (若有) 的相關資訊。

## 資料表欄
<a name="r_PG_PROC_INFO-table-columns"></a>

除了 PG\$1PROG 中的欄，PG\$1PROC\$1INFO 還會顯示下列幾欄。PG\$1PROC 中的 `oid` 欄在 PG\$1PROC\$1INFO 資料表中稱為 `prooid`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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) 參數是否正確設定為包含相關的結構描述。

您可以使用 [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_tw/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 將僅傳回在搜尋路徑中包含之結構描述中資料表的相關資訊。如需詳細資訊，請參閱[search\$1path](r_search_path.md)。

例如，假設您建立新結構描述和新資料表，以及查詢 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_tw/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 資料表聯結目錄資料表時受到支援：
+ bool
+ "char"
+ float4
+ int2
+ int4
+ int8
+ name
+ oid
+ text
+ varchar

如果您寫入加入查詢，其明確或隱含地參考擁有不受支援之資料類型的欄位，則查詢會傳回錯誤。在某些目錄資料表中使用的 SQL 函數也不受支援，除非是 PG\$1SETTINGS 和 PG\$1LOCKS 資料表所用的那些函數。

例如，PG\$1STATS 資料表不得與 Amazon Redshift 資料表一同受到查詢，因為此功能不受支援。

下列目錄資料表和檢視提供的資訊很有用，您可以將其與 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、資料庫名稱、結構描述名稱和資料表名稱。

```
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 資料庫和其結構描述名稱 (NSPNAME 欄位) 中傳回資料表清單。查詢也會傳回每個資料表中的列總數。(當系統中多個結構描述的資料表名稱相同時，此查詢相當實用。)

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