Amazon Redshift RSQL 元命令 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon Redshift RSQL 元命令

Amazon Redshift RSQL 元命令返回有关数据库或特定数据库对象的信息性记录。结果可以包括各种列和元数据。其他命令会执行特定操作。这些命令的前面都有反斜杠。

\d[S+]

列出本地用户创建的表、常规视图、后期绑定视图和实体化视图。\dS 还列出了表和视图,如 \d,但系统对象包含在返回的记录中。+ 为所有列出的对象生成额外的元数据列 description。下面显示的是作为命令返回的示例记录。

List of relations schema | name | type | owner --------+-----------+-------+--------- public | category | table | awsuser public | date | table | awsuser public | event | table | awsuser public | listing | table | awsuser public | sales | table | awsuser public | users | table | awsuser public | venue | table | awsuser (7 rows)

\d[S+] NAME

描述表、视图或索引。包括列名称和类型。它还提供了 diststyle、备份配置、创建日期(2018 年 10 月之后创建的表)和约束。例如,\dS+ sample 返回对象属性。附加 S+ 会导致返回的记录中包含其它列。

Table "public.sample" Column | Type | Collation | Nullable | Default Value | Encoding | DistKey | SortKey --------+-----------------------------+----------------+----------+---------------+-----------+---------+--------- col1 | smallint | | NO | | none | t | 1 col2 | character(100) | case_sensitive | YES | | none | f | 2 col3 | character varying(100) | case_sensitive | YES | | text32k | f | 3 col4 | timestamp without time zone | | YES | | runlength | f | 0 col5 | super | | YES | | zstd | f | 0 col6 | bigint | | YES | | az64 | f | 0 Diststyle: KEY Backup: YES Created: 2021-07-20 19:47:27.997045 Unique Constraints: "sample_pkey" PRIMARY KEY (col1) "sample_col2_key" UNIQUE (col2) Foreign-key constraints: "sample_col2_fkey" FOREIGN KEY (col2) REFERENCES lineitem(l_orderkey)

表的分布样式或 Diststyle 可以是 KEY、AUTO、EVEN 或 ALL。

备份指示在拍摄快照时是否备份了表。有效值为 YESNO

已创建是创建表的时间的时间戳。创建日期不适用于 2018 年 11 月之前创建的 Amazon Redshift 表。在此日期之前创建的表显示 n/a(不可用)。

独特约束列出了表中的唯一的主键约束。

外键约束列出了表中的外键越塾。

\dC[+] [PATTERN]

列出转换。包括源类型、目标类型以及转换是否为隐式。

以下显示来自 \dC+ 的结果子集:

List of casts source type | target type | function | implicit? | description -----------------------------+-----------------------------+---------------------+---------------+------------- "char" | character | bpchar | in assignment | "char" | character varying | text | in assignment | "char" | integer | int4 | no | "char" | text | text | yes | "path" | point | point | no | "path" | polygon | polygon | in assignment | abstime | date | date | in assignment | abstime | integer | (binary coercible) | no | abstime | time without time zone | time | in assignment | abstime | timestamp with time zone | timestamptz | yes | abstime | timestamp without time zone | timestamp | yes | bigint | bit | bit | no | bigint | boolean | bool | yes | bigint | character | bpchar | in assignment | bigint | character varying | text | in assignment | bigint | double precision | float8 | yes | bigint | integer | int4 | in assignment | bigint | numeric | numeric | yes | bigint | oid | oid | yes | bigint | real | float4 | yes | bigint | regclass | oid | yes | bigint | regoper | oid | yes | bigint | regoperator | oid | yes | bigint | regproc | oid | yes | bigint | regprocedure | oid | yes | bigint | regtype | oid | yes | bigint | smallint | int2 | in assignment | bigint | super | int8_partiql | in assignment |

\dd[S] [PATTERN]

显示在别处未显示的对象描述。

\de

列出外部表。这包括 Amazon Glue 数据目录中的表、Hive Metastore 和来自 Amazon RDS/Aurora MySQL、Amazon RDS/Aurora PostgreSQL 和 Amazon Redshift 数据共享表的联合表。

\de NAME

描述外部表。

以下示例显示 Amazon Glue 外部表。

# \de spectrum.lineitem Glue External table "spectrum.lineitem" Column | External Type | Redshift Type | Position | Partition Key | Nullable -----------------+---------------+---------------+----------+---------------+---------- l_orderkey | bigint | bigint | 1 | 0 | l_partkey | bigint | bigint | 2 | 0 | l_suppkey | int | int | 3 | 0 | l_linenumber | int | int | 4 | 0 | l_quantity | decimal(12,2) | decimal(12,2) | 5 | 0 | l_extendedprice | decimal(12,2) | decimal(12,2) | 6 | 0 | l_discount | decimal(12,2) | decimal(12,2) | 7 | 0 | l_tax | decimal(12,2) | decimal(12,2) | 8 | 0 | l_returnflag | char(1) | char(1) | 9 | 0 | l_linestatus | char(1) | char(1) | 10 | 0 | l_shipdate | date | date | 11 | 0 | l_commitdate | date | date | 12 | 0 | l_receiptdate | date | date | 13 | 0 | l_shipinstruct | char(25) | char(25) | 14 | 0 | l_shipmode | char(10) | char(10) | 15 | 0 | l_comment | varchar(44) | varchar(44) | 16 | 0 | Location: s3://redshiftbucket/kfhose2019/12/31 Input_format: org.apache.hadoop.mapred.TextInputFormat Output_format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat Serialization_lib: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe Serde_parameters: {"field.delim":"|","serialization.format":"|"} Parameters: {"EXTERNAL":"TRUE","numRows":"178196721475","transient_lastDdlTime":"1577771873"}

Hive Metastore 表。

# \de emr.lineitem Hive Metastore External Table "emr.lineitem" Column | External Type | Redshift Type | Position | Partition Key | Nullable -----------------+---------------+---------------+----------+---------------+---------- l_orderkey | bigint | bigint | 1 | 0 | l_partkey | bigint | bigint | 2 | 0 | l_suppkey | int | int | 3 | 0 | l_linenumber | int | int | 4 | 0 | l_quantity | decimal(12,2) | decimal(12,2) | 5 | 0 | l_extendedprice | decimal(12,2) | decimal(12,2) | 6 | 0 | l_discount | decimal(12,2) | decimal(12,2) | 7 | 0 | l_tax | decimal(12,2) | decimal(12,2) | 8 | 0 | l_returnflag | char(1) | char(1) | 9 | 0 | l_linestatus | char(1) | char(1) | 10 | 0 | l_commitdate | date | date | 11 | 0 | l_receiptdate | date | date | 12 | 0 | l_shipinstruct | char(25) | char(25) | 13 | 0 | l_shipmode | char(10) | char(10) | 14 | 0 | l_comment | varchar(44) | varchar(44) | 15 | 0 | l_shipdate | date | date | 16 | 1 | Location: s3://redshiftbucket/cetas Input_format: org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat Output_format: org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat Serialization_lib: org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe Serde_parameters: {"serialization.format":"1"} Parameters: {"EXTERNAL":"TRUE", "numRows":"4307207", "transient_lastDdlTime":"1626990007"}

PostgreSQL 外部表。

# \de pgrsql.alltypes Postgres Federated Table "pgrsql.alltypes" Column | External Type | Redshift Type | Position | Partition Key | Nullable --------+-----------------------------+-----------------------------+----------+---------------+---------- col1 | bigint | bigint | 1 | 0 | col2 | bigint | bigint | 2 | 0 | col5 | boolean | boolean | 3 | 0 | col6 | box | varchar(65535) | 4 | 0 | col7 | bytea | varchar(65535) | 5 | 0 | col8 | character(10) | character(10) | 6 | 0 | col9 | character varying(10) | character varying(10) | 7 | 0 | col10 | cidr | varchar(65535) | 8 | 0 | col11 | circle | varchar(65535) | 9 | 0 | col12 | date | date | 10 | 0 | col13 | double precision | double precision | 11 | 0 | col14 | inet | varchar(65535) | 12 | 0 | col15 | integer | integer | 13 | 0 | col16 | interval | varchar(65535) | 14 | 0 | col17 | json | varchar(65535) | 15 | 0 | col18 | jsonb | varchar(65535) | 16 | 0 | col19 | line | varchar(65535) | 17 | 0 | col20 | lseg | varchar(65535) | 18 | 0 | col21 | macaddr | varchar(65535) | 19 | 0 | col22 | macaddr8 | varchar(65535) | 20 | 0 | col23 | money | varchar(65535) | 21 | 0 | col24 | numeric | numeric(38,20) | 22 | 0 | col25 | path | varchar(65535) | 23 | 0 | col26 | pg_lsn | varchar(65535) | 24 | 0 | col28 | point | varchar(65535) | 25 | 0 | col29 | polygon | varchar(65535) | 26 | 0 | col30 | real | real | 27 | 0 | col31 | smallint | smallint | 28 | 0 | col32 | smallint | smallint | 29 | 0 | col33 | integer | integer | 30 | 0 | col34 | text | varchar(65535) | 31 | 0 | col35 | time without time zone | varchar(65535) | 32 | 0 | col36 | time with time zone | varchar(65535) | 33 | 0 | col37 | timestamp without time zone | timestamp without time zone | 34 | 0 | col38 | timestamp with time zone | timestamp with time zone | 35 | 0 | col39 | tsquery | varchar(65535) | 36 | 0 | col40 | tsvector | varchar(65535) | 37 | 0 | col41 | txid_snapshot | varchar(65535) | 38 | 0 | col42 | uuid | varchar(65535) | 39 | 0 | col43 | xml | varchar(65535) | 40 | 0 |

\df[anptw][S+] [PATTERN]

列出各种类型的函数。例如,命令 \df 返回函数列表。结果包括名称、返回的数据类型、访问权限和其它元数据等属性。函数类型可以包括触发器、存储过程、窗口函数和其它类型。当您将 S+ 附加到命令(例如 \dfantS+)时,会包含其它元数据列,例如 ownersecurityaccess privileges

\dL[S+] [PATTERN]

列出与数据库关联的过程语言的数据。信息包括名称(例如 plpgsql)和其它元数据,其中包括名称是否受信任、访问权限和描述。例如,示例调用是 \dLS+,它列出了语言及其属性。当您将 S+ 附加到命令时,会包含其它元数据列,例如 call handleraccess privileges

示例结果:

List of languages name | trusted | internal language | call handler | validator | access privileges | description -----------+---------+-------------------+-------------------------+------------------------------------------------------------+-------------------+-------------------------------- c | f | t | - | fmgr_c_validator(oid) | | Dynamically-loaded C functions exfunc | f | f | exfunc_call_handler() | - | rdsdb=U/rdsdb | internal | f | t | - | fmgr_internal_validator(oid) | | Built-in functions mlfunc | f | f | mlfunc_call_handler() | - | rdsdb=U/rdsdb | plpgsql | t | f | plpgsql_call_handler() | plpgsql_validator(oid) | | plpythonu | f | f | plpython_call_handler() | plpython_compiler(cstring,cstring,cstring,cstring,cstring) | rdsdb=U/rdsdb | sql | t | t | - | fmgr_sql_validator(oid) | =U/rdsdb | SQL-language functions

\dm[S+] [PATTERN]

列出实体化视图。例如,\dmS+ 列出了实体化视图及其属性。当您将 S+ 附加到命令时,会包含其它元数据列。

\dn[S+] [PATTERN]

列出架构。当您将 S+ 附加到命令(例如 \dnS+)时,会包含其它元数据列,例如 descriptionaccess privileges

\dp [PATTERN]

列出表、视图和序列访问权限。

\dt[S+] [PATTERN]

列出表 当您将 S+ 附加到命令(例如 \dtS+)时,会包含其它元数据列,例如本例中的 description

\du

列出数据库的用户。包括他们的姓名和角色,例如超级用户和属性。

\dv[S+] [PATTERN]

列出视图。包括架构、类型和拥有者数据。当您将 S+ 附加到命令(例如 \dvS+)时,会包含其它元数据列。

\H

打开 HTML 输出。这对于快速返回格式化的结果非常有用。例如,select * from sales; \H 以 HTML 格式返回销售表中的结果。要切换回表格结果,请使用 \q,或者无提示。

\i

从文件中运行命令。例如,假设您的工作目录中有 rsql_steps.sql,以下将运行文件中的命令:\i rsql_steps.sql

\l[+] [PATTERN]

列出数据库。包括所有者、编码和其它信息。

\q

退出,或者 \q 命令,注销数据库会话并退出 RSQL。

\sv[+] VIEWNAME

显示视图的定义。

\timing

例如,显示查询的运行时间。

\z [PATTERN]

输出与\ dp 相同。

\?

显示帮助信息。可选的参数指定要解释的项目。

\EXIT

注销所有数据库会话并退出 Amazon Redshift RSQL。此外,您可以指定可选的退出代码。例如,\EXIT 15 将退出 Amazon Redshift RSQL 终端并返回退出代码 15。

以下示例显示来自连接的输出并从 RSQL 退出。

% rsql -D testuser DSN Connected DBMS Name: Amazon Redshift Driver Name: Amazon Redshift ODBC Driver Driver Version: 1.4.34.1000 Rsql Version: 1.0.1 Redshift Version: 1.0.29306 Type "help" for help. (testcluster) user1@dev=# \exit 15 % echo $? 15

\EXPORT

指定导出文件的名称,RSQL 将该文件用于存储后续 SQL SELECT 语句返回的数据库信息。

export_01.sql

\export report file='E:\\accounts.out' \rset rformat off \rset width 1500 \rset heading "General Title" \rset titledashes on select * from td_dwh.accounts; \export reset

控制台输出

Rformat is off. Target width is 1500. Heading is set to: General Title Titledashes is on. (exported 40 rows)

\LOGON

连接到数据库。您可以使用位置语法指定连接参数或将其指定作为连接字符串。

命令语法如下所示:\logon {[DBNAME|- USERNAME|- HOST|- PORT|- [PASSWORD]] | conninfo}

DBNAME 是要连接的数据库的名称。USERNAME 是连接的用户名。默认 HOSTlocalhost。默认 PORT5439

当在 \LOGON 命令中指定主机名时,它将成为其它 \LOGON 命令的默认主机名。要更改默认主机名,请在附加 \LOGON 命令中指定新的 HOST

user1\LOGON 命令输出示例如下。

(testcluster) user1@redshiftdb=# \logon dev DBMS Name: Amazon Redshift Driver Name: Amazon Redshift ODBC Driver Driver Version: 1.4.27.1000 Rsql Version: 1.0.1 You are now connected to database "dev" as user "user1". (testcluster) user1@dev=#

user2 的示例输出。

(testcluster) user1@dev=# \logon dev user2 testcluster2.example.com Password for user user2: DBMS Name: Amazon Redshift Driver Name: Amazon Redshift ODBC Driver Driver Version: 1.4.27.1000 Rsql Version: 1.0.1 You are now connected to database "dev" as user "user2" on host "testcluster2.example.com" at port "5439". (testcluster2) user2@dev=#

\REMARK

\echo 命令的扩展。\REMARK 将指定的字符串打印到输出流。\REMARK 通过添加在单独的行上中断输出的能力来扩展 \echo

以下示例显示了命令的输出。

(testcluster) user1@dev=# \remark 'hello//world' hello world

\RSET

\rset 命令将设置命令参数和变量。\rset 还具有交互式模式和批处理模式。它不支持 bash 选项(例如 -x)或参数(例如 --<arg>)。

它设置如下所示的变量:

  • ERRORLEVEL

  • HEADING 和 RTITLE

  • RFORMAT

  • MAXERROR

  • TITLEDASHES

  • WIDTH

以下示例指定一个标题。

\rset heading "Winter Sales Report"

有关如何使用 \rset 的更多示例,您可以在 Amazon Redshift RSQL 变量 主题中查找示例。

\RUN

运行包含在指定文件中的 Amazon Redshift RSQL 脚本。\RUN 通过添加一个选项来跳过文件中的标题行以扩展 \i 命令。

如果文件名包含逗号、分号或空格,请用单引号将其括起来。此外,如果文本在文件名后面,请将其括在引号中。在 UNIX 中,文件名区分大小写。在 Windows 中,文件名不区分大小写。

以下示例显示了命令的输出。

(testcluster) user1@dev=# \! cat test.sql select count(*) as lineitem_cnt from lineitem; select count(*) as customer_cnt from customer; select count(*) as orders_cnt from orders; (testcluster) user1@dev=# \run file=test.sql lineitem_cnt -------------- 4307207 (1 row) customer_cnt -------------- 37796166 (1 row) orders_cnt ------------ 0 (1 row) (testcluster) user1@dev=# \run file=test.sql skip=2 2 records skipped in RUN file. orders_cnt ------------ 0 (1 row)

\OS

\! 命令的别名。\OS 运行作为参数传递的操作系统命令。命令运行后,控制将返回到 Amazon Redshift RSQL。例如,您可以运行以下命令以打印当前系统日期时间并返回到 RSQL 终端:\os date

(testcluster) user1@dev=# \os date Tue Sep 7 20:47:54 UTC 2021

\GOTO

适用于 Amazon Redshift RSQL 的新命令。\GOTO 跳过所有干预命令并在指定的 \LABEL 继续处理。\LABEL 必须是前向引用。您不能跳转到词汇上位于 \GOTO 之前的 \LABEL

下面显示了示例输出。

(testcluster) user1@dev=# \! cat test.sql select count(*) as cnt from lineitem \gset select :cnt as cnt; \if :cnt > 100 \goto LABELB \endif \label LABELA \remark 'this is label LABELA' \label LABELB \remark 'this is label LABELB' (testcluster) user1@dev=# \i test.sql cnt --------- 4307207 (1 row) \label LABELA ignored \label LABELB processed this is label LABELB

\LABEL

适用于 Amazon Redshift RSQL 的新命令。\LABEL 建立运行程序的入口点,作为 \GOTO 命令的目标。

以下显示了命令的输出。

(testcluster) user1@dev=# \! cat test.sql select count(*) from lineitem limit 5; \goto LABELB \remark "this step was skipped by goto label"; \label LABELA \remark 'this is label LABELA' \label LABELB \remark 'this is label LABELB' (testcluster) user1@dev=# \i testgoto.sql count 4307193 (1 row) \label LABELA ignored \label LABELB processed this is label LABELB

\IF (\ELSEIF, \ELSE, \ENDIF)

\IF 和相关命令有条件地运行部分输入脚本。PSQL \if\elif\else\endif)命令的扩展。\IF\ELSEIF 支持布尔表达式,包括 ANDORNOT 条件。

以下显示了命令的示例输出。

(testcluster) user1@dev=# \! cat test.sql SELECT query FROM stv_inflight LIMIT 1 \gset select :query as query; \if :query > 1000000 \remark 'Query id is greater than 1000000' \elseif :query = 1000000 \remark 'Query id is equal than 1000000' \else \remark 'Query id is less than 1000000' \endif (testcluster) user1@dev=# \i test.sql query -------- 994803 (1 row) Query id is less than 1000000

在您的分支逻辑中使用 ERRORCODE

\if :'ERRORCODE' = '00000' \remark 'The statement was executed without error' \else \remark :LAST_ERROR_MESSAGE \endif

\IF 块内使用 \GOTO 以控制代码的运行方式。