PostgreSQL11: psql 新增 \gdesc 显示查询结果的列名和类型

栏目: 数据库 · PostgreSQL · 发布时间: 6年前

内容简介:PostgreSQL 11 的\gdescShows the description (that is, the column names and data types) of the result of the current query buffer. The query is not actually executed; however, if it contains some type of syntax error, that error will be reported in the norm

PostgreSQL 11 的 psql 新增 \gdesc 选项,此选项可以返回查询结果的列名和类型,而不实际执行SQL。

Release 说明

psql Add psql command \gdesc to display the column names and types of the query output (Pavel Stehule)

\gdesc 选项说明

\gdesc

Shows the description (that is, the column names and data types) of the result of the current query buffer. The query is not actually executed; however, if it contains some type of syntax error, that error will be reported in the normal way.

If the current query buffer is empty, the most recently sent query is described instea

\gdesc 只是显示查询结果的列名和类型,并不实际执行SQL,下面演示下。

\gdesc 选项演示

数据库中存在一张大表big,结构如下:

[pg11@pghost2 ~]$ psql francs francs
psql (11beta3)
Type "help" for help.

francs=> \d big
                                  Table "francs.big"
  Column   |              Type              | Collation | Nullable |      Default
-----------+--------------------------------+-----------+----------+-------------------
 user_id   | integer                        |           |          |
 user_name | text                           |           |          |
 ctime     | timestamp(6) without time zone |           |          | clock_timestamp()
Indexes:
    "idx_big_ctime" btree (ctime)
    "idx_big_username" btree (user_name)

执行以下查询,如下:

francs=> \timing
Timing is on.

francs=> SELECT count(*),sum(hashtext(user_name)) FROM big;
  count   |      sum
----------+----------------
 30000000 | 11924569894736
(1 row)

Time: 1347.527 ms (00:01.348)

执行时间为 1347 ms 左右。

使用 \gdesc 选项查询,如下:

francs=> SELECT count(*),sum(hashtext(user_name)) FROM big \gdesc
 Column |  Type
--------+--------
 count  | bigint
 sum    | bigint
(2 rows)

Time: 0.634 ms

以上返回了查询结果的列和数据类型,执行很快,只需要 0.634 ms,可见没有实际执行SQL。

另一个示例,查询 pg_class 系统表,如下:

francs=> SELECT * FROM pg_class \gdesc
       Column        |     Type
---------------------+--------------
 relname             | name
 relnamespace        | oid
 reltype             | oid
 reloftype           | oid
 relowner            | oid
 relam               | oid
 relfilenode         | oid
 reltablespace       | oid
 relpages            | integer
 reltuples           | real
 relallvisible       | integer
 reltoastrelid       | oid
 relhasindex         | boolean
 relisshared         | boolean
 relpersistence      | "char"
 relkind             | "char"
 relnatts            | smallint
 relchecks           | smallint
 relhasoids          | boolean
 relhasrules         | boolean
 relhastriggers      | boolean
 relhassubclass      | boolean
 relrowsecurity      | boolean
 relforcerowsecurity | boolean
 relispopulated      | boolean
 relreplident        | "char"
 relispartition      | boolean
 relrewrite          | oid
 relfrozenxid        | xid
 relminmxid          | xid
 relacl              | aclitem[]
 reloptions          | text[]
 relpartbound        | pg_node_tree
(33 rows)

这个特性不需要实际执行 SQL 就能返回查询结果的列和数据类型,在某些特定场景比较有用。

参考


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新内容创业:我这样打造爆款IP

新内容创业:我这样打造爆款IP

南立新、曲琳 / 机械工业出版社 / 2016-5-10 / 39.00

这是个内容创业爆棚的时代,在采访几十家内容创业公司,与一线最优秀的创业者独家对话之后,作者写作了这本书,其中包括对这个行业的真诚感触,以及希望沉淀下来的体系化思考。 本书共分三个部分讲述了爆红大号的内容创业模式和方法。其中第一部分,讲述了新的生产方式,即内容形态发展的现状--正在被塑造;第二部分,讲述了新的盈利探索,即从贩卖产品到贩卖内容的转变,该部分以多个案例进行佐证,内容翔实;第三部分,......一起来看看 《新内容创业:我这样打造爆款IP》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换