

 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_join_PG_examples"></a>

下列查詢顯示幾個方法，您可以用這些方法來查詢目錄資料表來取得 Amazon Redshift 資料庫的相關有用資訊。

## 檢視資料表 ID、資料庫、結構描述和資料表名稱
<a name="c_join_PG_examples-view-tableid-db-schema-tablename"></a>

下列檢視定義加入 STV\_TBL\_PERM 系統資料表和 PG\_CLASS、PG\_NAMESPACE 和 PG\_DATABASE 系統目錄資料表，以傳回資料表 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\_TABLES 和 STV\_TBL\_PERM 中；請盡可能使用 PG\_TABLES 傳回 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\_TBL\_PERM 加入至部分 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\_BLOCKLIST 資料表加入至 PG\_CLASS，以傳回 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)
```