

 Amazon Redshift tidak akan lagi mendukung pembuatan Python UDFs baru mulai Patch 198. Python yang ada UDFs akan terus berfungsi hingga 30 Juni 2026. Untuk informasi lebih lanjut, lihat [posting blog](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Contoh kueri katalog
<a name="c_join_PG_examples"></a>

Kueri berikut menunjukkan beberapa cara di mana Anda dapat menanyakan tabel katalog untuk mendapatkan informasi berguna tentang database Amazon Redshift.

## Lihat ID tabel, database, skema, dan nama tabel
<a name="c_join_PG_examples-view-tableid-db-schema-tablename"></a>

Definisi tampilan berikut menggabungkan tabel sistem STV\$1TBL\$1PERM dengan tabel katalog sistem PG\$1CLASS, PG\$1NAMESPACE, dan PG\$1DATABASE untuk mengembalikan ID tabel, nama database, nama skema, dan nama tabel.

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

Contoh berikut mengembalikan informasi untuk ID tabel 117855.

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

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

## Buat daftar jumlah kolom per tabel Amazon Redshift
<a name="c_join_PG_examples-list-the-number-of-columns-per-amazon-redshift-table"></a>

Kueri berikut bergabung dengan beberapa tabel katalog untuk mengetahui berapa banyak kolom yang berisi setiap tabel Amazon Redshift. Nama tabel Amazon Redshift disimpan di PG\$1TABLES dan STV\$1TBL\$1PERM; jika memungkinkan, gunakan PG\$1TABLES untuk mengembalikan nama tabel Amazon Redshift.

Kueri ini tidak melibatkan tabel Amazon Redshift apa pun.

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



## Daftar skema dan tabel dalam database
<a name="c_join_PG_examples-list-the-schemas-and-tables-in-a-database"></a>

Kueri berikut bergabung dengan STV\$1TBL\$1PERM ke beberapa tabel PG untuk mengembalikan daftar tabel dalam database TICKIT dan nama skema mereka (kolom NSPNAME). Query juga mengembalikan jumlah total baris di setiap tabel. (Kueri ini berguna ketika beberapa skema di sistem Anda memiliki nama tabel yang sama.)

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

## Daftar tabel IDs, tipe data, nama kolom, dan nama tabel
<a name="c_join_PG_examples-list-table-ids-data-types-column-names-and-table-names"></a>

Kueri berikut mencantumkan beberapa informasi tentang setiap tabel pengguna dan kolomnya: ID tabel, nama tabel, nama kolomnya, dan tipe data setiap kolom:

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

## Hitung jumlah blok data untuk setiap kolom dalam tabel
<a name="c_join_PG_examples-count-the-number-of-data-blocks-for-each-column-in-a-table"></a>

Kueri berikut menggabungkan tabel STV\$1BLOCKLIST ke PG\$1CLASS untuk mengembalikan informasi penyimpanan untuk kolom dalam tabel PENJUALAN.

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