openGauss gsql 常用元命令 二

openGauss gsql 常用元命令 二

\df 查看当前数据库函数信息

openGauss=# \df
********* QUERY **********
SELECT n.nspname as "Schema",
  p.proname as "Name",
  pg_catalog.pg_get_function_result(p.oid) as "Result data type",
  pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",
 CASE
  WHEN p.proisagg THEN 'agg'
  WHEN p.proiswindow THEN 'window'
  WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
  ELSE 'normal'
END as "Type" ,
 fencedmode as "fencedmode"
 ,
 propackage as "propackage"
 ,
 prokind as "prokind"

FROM pg_catalog.pg_proc p
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE pg_catalog.pg_function_is_visible(p.oid)
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'db4ai'
      AND n.nspname <> 'information_schema'
ORDER BY 1, 2, 4;
**************************

                                                           List of functions
 Schema |   Name    | Result data type |               Argument data types                |  Type  | fencedmode | propackage | prokind
--------+-----------+------------------+--------------------------------------------------+--------+------------+------------+---------
 public | fuc_worth | numeric          | price numeric, amount integer, OUT worth numeric | normal | f          | f          | f
(1 row)

\dx 查看已安装的扩展程序信息

openGauss=# \dx
********* QUERY **********
SELECT e.extname AS "Name", e.extversion AS "Version", n.nspname AS "Schema", c.description AS "Description"
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
ORDER BY 1;
**************************

                               List of installed extensions
      Name       | Version |   Schema   |                   Description
-----------------+---------+------------+--------------------------------------------------
 dist_fdw        | 1.0     | pg_catalog | foreign-data wrapper for distfs access
 file_fdw        | 1.0     | pg_catalog | foreign-data wrapper for flat file access
 hdfs_fdw        | 1.0     | pg_catalog | foreign-data wrapper for flat file access
 hstore          | 1.1     | pg_catalog | data type for storing sets of (key, value) pairs
 log_fdw         | 1.0     | pg_catalog | Foreign Data Wrapper for accessing logging data
 mot_fdw         | 1.0     | pg_catalog | foreign-data wrapper for MOT access
 plpgsql         | 1.0     | pg_catalog | PL/pgSQL procedural language
 security_plugin | 1.0     | pg_catalog | provides security functionality
(8 rows)

\x 语法 \x[on|off|auto] 设置语句的输出模式,模式为行的方式输出,执行 \x on 切换为以列的方式来显示

openGauss=# \x
Expanded display is on.
openGauss=# \dx
********* QUERY **********
SELECT e.extname AS "Name", e.extversion AS "Version", n.nspname AS "Schema", c.description AS "Description"
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
ORDER BY 1;
**************************

List of installed extensions
-[ RECORD 1 ]-------------------------------------------------
Name        | dist_fdw
Version     | 1.0
Schema      | pg_catalog
Description | foreign-data wrapper for distfs access
-[ RECORD 2 ]-------------------------------------------------
Name        | file_fdw
Version     | 1.0
Schema      | pg_catalog
Description | foreign-data wrapper for flat file access
-[ RECORD 3 ]-------------------------------------------------

\timing 语法 \timing [on|off] 控制显示 SQL 的执行时间,默认为 off, on 为显示 SQL 语句的执行时间

openGauss=# select * from test;
 id
----
  1
(1 row)

openGauss=# \timing on
Timing is on.
openGauss=# select * from test;
 id
----
  1
(1 row)

Time: 0.352 ms

\h 用于获取 SQL 语句的帮助,例如 \h merge

openGauss=# \h merge
Command:     MERGE
Description: insert, update, or delete rows of a table based upon source data
Syntax:
MERGE [/*+ plan_hint */] INTO table_name [ [ AS ] alias ]
USING { { table_name | view_name } | subquery } [ [ AS ] alias ]
ON ( condition )
[
  WHEN MATCHED THEN
  UPDATE SET { column_name = { expression | DEFAULT } |
          ( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
  [ WHERE condition ]
]
[
  WHEN NOT MATCHED THEN
  INSERT { DEFAULT VALUES |
  [ ( column_name [, ...] ) ] VALUES ( { expression | DEFAULT } [, ...] ) [, ...] [ WHERE condition ] }
];

? 获取 gsql 的元命令的帮助

openGauss=# \?
General
  \copyright             show openGauss usage and distribution terms
  \g [FILE] or ;         execute query (and send results to file or |pipe)
  \h(\help) [NAME]              help on syntax of SQL commands, * for all commands
  \parallel [on [num]|off] toggle status of execute (currently off)
  \q                     quit gsql

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
  \p                     show the contents of the query buffer
  \r                     reset (clear) the query buffer
  \w FILE                write query buffer to file

Input/Output
  \copy ...              perform SQL COPY with data stream to the client host
  \echo [STRING]         write string to standard output
  \i FILE                execute commands from file
  \i+ FILE KEY           execute commands from encrypted file
  \ir FILE               as \i, but relative to location of current script
  \ir+ FILE KEY          as \i+, but relative to location of current script
  \o [FILE]              send all query results to file or |pipe
  \qecho [STRING]        write string to query output stream (see \o)

! os_command 用于执行操作系统命令,同 oracle 的 !,mysql 的 system

openGauss-# \! pwd
/home/omm
openGauss-# \! ls
1.sh  create_db_tables.sql  test.sql

\o filename 用于重定向输出到文件,注意这个不是简单的将屏幕的内容输出到文本,而是将 SQL 语句正确执行的结果输出到文本

openGauss-# \o test.out
openGauss-# select * from test;
WARNING:  Session unused timeout.
FATAL:  terminating connection due to administrator command
could not send data to server: Broken pipe
The connection to the server was lost. Attempting reset: Succeeded.
openGauss=# select * from test;
openGauss=# \! cat test.out
 id
----
  1
(1 row)

openGauss=# select * from pg_tables;
openGauss=#  \! cat test.out
 id
----
  1
(1 row)

     schemaname     |           tablename           | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator |            created            |         last_ddl_time
--------------------+-------------------------------+------------+------------+------------+----------+-------------+--------------+-------------------------------+-------------------------------
 pg_catalog         | pg_statistic                  | omm        |            | t          | f        | f           |              |                               |

\i file.sql

\conninfo 显示 gsql 中显示会话的连接信息

\c[onnect] [DBNAME] 切换数据库

openGauss=# \conninfo
You are connected to database "postgres" as user "omm" via socket in "/opt/huawei/tmp" at port "15400".
openGauss=# \c mydb
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "mydb" as user "omm".
mydb=# \conninfo
You are connected to database "mydb" as user "omm" via socket in "/opt/huawei/tmp" at port "15400".

\echo [string] 打印字符串

mydb=# \echo Hello World!
Hello World!

\q 退出 gsql

相关推荐
一休哥助手6 分钟前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
翔云12345614 分钟前
MVCC(多版本并发控制)
数据库·mysql
代码敲上天.30 分钟前
数据库语句优化
android·数据库·adb
盒马盒马1 小时前
Redis:zset类型
数据库·redis
静听山水1 小时前
mysql语句执行过程
数据库·mysql
虽千万人 吾往矣1 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
mariokkm2 小时前
Django一分钟:在Django中怎么存储树形结构的数据,DRF校验递归嵌套模型的替代方案
数据库·django·sqlite
Wang's Blog3 小时前
Redis: 集群环境搭建,集群状态检查,分析主从日志,查看集群信息
数据库·redis
容器( ु⁎ᴗ_ᴗ⁎)ु.。oO3 小时前
MySQL事务
数据库·mysql
cyt涛5 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok