【PostgreSQL-9.6.3】如何得到psql中命令的实际执行SQL
2017-06-25 19:43
501 查看
当我们在psql界面执行以“\”开头的命令时,数据库会立刻返回执行结果,而不会返回命令的实际执行过程。通过两种方式可以实现执行过程的查看:
方法一:启动psql命令时加“-E”参数
postgres@lgr-pc:~$ psql -E test
psql (9.6.3)
Type "help" for help.
test=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | people | table | postgres
public | persons | table | postgres
public | students | table | postgres
public | t1 | table | postgres
public | t11 | table | postgres
public | t2 | table | postgres
public | tmp12 | table | postgres
public | tmp15 | table | postgres
这种方式只能在启动psql时使用,如果想关闭查询功能只能重新启动psql。
方法二:在psql中执行“\set ECHO_HIDDEN on|off”命令。on为开启,off为关闭,“ECHO_HIDDEN”必须为大写。
test=# \set ECHO_HIDDEN on
test=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | people | table | postgres
public | persons | table | postgres
public | students | table | postgres
public | t1 | table | postgres
public | t11 | table | postgres
public | t2 | table | postgres
public | tmp12 | table | postgres
public | tmp15 | table | postgres
(8 rows)
test=# \set ECHO_HIDDEN off
test=# \d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | people | table | postgres
public | persons | table | postgres
public | students | table | postgres
public | t1 | table | postgres
public | t11 | table | postgres
public | t2 | table | postgres
public | tmp12 | table | postgres
public | tmp15 | table | postgres
(8 rows)
方法一:启动psql命令时加“-E”参数
postgres@lgr-pc:~$ psql -E test
psql (9.6.3)
Type "help" for help.
test=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | people | table | postgres
public | persons | table | postgres
public | students | table | postgres
public | t1 | table | postgres
public | t11 | table | postgres
public | t2 | table | postgres
public | tmp12 | table | postgres
public | tmp15 | table | postgres
这种方式只能在启动psql时使用,如果想关闭查询功能只能重新启动psql。
方法二:在psql中执行“\set ECHO_HIDDEN on|off”命令。on为开启,off为关闭,“ECHO_HIDDEN”必须为大写。
test=# \set ECHO_HIDDEN on
test=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | people | table | postgres
public | persons | table | postgres
public | students | table | postgres
public | t1 | table | postgres
public | t11 | table | postgres
public | t2 | table | postgres
public | tmp12 | table | postgres
public | tmp15 | table | postgres
(8 rows)
test=# \set ECHO_HIDDEN off
test=# \d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | people | table | postgres
public | persons | table | postgres
public | students | table | postgres
public | t1 | table | postgres
public | t11 | table | postgres
public | t2 | table | postgres
public | tmp12 | table | postgres
public | tmp15 | table | postgres
(8 rows)
相关文章推荐
- PSQL中命令实际执行的SQL
- postgresql数据库--psql、pg_dump命令带密码执行sql语句
- Hibernate SQLQuery查询返回空List,在mysql命令下直接执行sql语句可以正常得到记录
- 如何得到精确的SQL执行时间
- PL/SQL语言中的execute immediate 命令只能用于DDL语句的执行,对于DML和DQL语句却不能实际执行
- 过sql_trace跟踪得到了该SQL的执行计划,如何看懂执行计划
- oracle中如何得到一条SQL语句的执行时间
- 如何查看Linq to SQL运行时,实际执行的Sql语句
- plsql如何执行sql命令及oracle数据库用户的授权
- 如何得到plsql中执行时报错的SQL的位置行号
- 如何得到SQL精确的执行时间
- PostgreSql中如何kill掉正在执行的sql语句
- 如何在DB2命令窗口执行sql脚本
- 如何使用psql命令去调试含参的sql文件
- postgresql中如何Kill掉正在执行的SQL语句
- ORACLE数据库SQL优化--->如何得到真实的执行计划
- 如何得到SQL精确的执行时间
- 如何得到执行最耗时的前N条T-SQL语句--【叶子】
- 如何得到服务器端sql执行脚本的方法
- 如何得到执行最耗时的前N条T-SQL语句