PostgreSQL 11 新特性解读: psql 新增 \\gdesc 显示查询结果的列名和类型

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: PostgreSQL 11 的 psql 新增 gdesc 选项,此选项可以返回查询结果的列名和类型,而不实际执行SQL。Release 说明psqlAdd psql command gdesc to display the column names and types of the quer...

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就能返回查询结果的列和数据类型,在某些特定场景比较有用。

参考

新书推荐

最后推荐和张文升共同编写的《PostgreSQL实战》,本书基于PostgreSQL 10 编写,共18章,重点介绍SQL高级特性、并行查询、分区表、物理复制、逻辑复制、备份恢复、高可用、性能优化、PostGIS等,涵盖大量实战用例!

链接:https://item.jd.com/12405774.html

_5_PostgreSQL_

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
28天前
|
关系型数据库 分布式数据库 数据库
PolarDB常见问题之加了索引但是查询没有使用如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
4月前
|
存储 关系型数据库 数据库
postgresql|数据库|提升查询性能的物化视图解析
postgresql|数据库|提升查询性能的物化视图解析
153 0
|
5月前
|
关系型数据库 Go PostgreSQL
golang pgx自定义PostgreSQL类型
golang的pgx驱动提供了大约70种PostgreSQL类型支持,但还是有一些类型没有涵盖,本文介绍如何自己编写代码支持特殊的类型。
75 3
|
5月前
|
消息中间件 存储 关系型数据库
PostgreSQL技术大讲堂 - 第33讲:并行查询管理
PostgreSQL从小白到专家,技术大讲堂 - 第33讲:并行查询管理
287 1
|
4月前
|
关系型数据库 MySQL 分布式数据库
PolarDB MySQL版并行查询技术探索与实践
PolarDB MySQL版并行查询技术探索与实践 PolarDB MySQL版在企业级查询加速特性上进行了深度技术探索,其中并行查询作为其重要组成部分,已经在线稳定运行多年,持续演进。本文将详细介绍并行查询的背景、挑战、方案、特性以及实践。
107 2
|
5月前
|
SQL 关系型数据库 分布式数据库
数据库内核那些事|细说PolarDB优化器查询变换:IN-List变换
数据库内核那些事|细说PolarDB优化器查询变换:IN-List变换
104 0
|
1月前
|
关系型数据库 Serverless 分布式数据库
PolarDB的Serverless能力与同类型产品的对比
【2月更文挑战第21天】PolarDB的Serverless能力与同类型产品的对比
18 2
|
6月前
|
存储 NoSQL 关系型数据库
深入探索地理空间查询:如何优雅地在MySQL、PostgreSQL及Redis中实现精准的地理数据存储与检索技巧
深入探索地理空间查询:如何优雅地在MySQL、PostgreSQL及Redis中实现精准的地理数据存储与检索技巧
599 0
|
2月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
42 7
|
2月前
|
存储 关系型数据库 分布式数据库
PolarDB for PostgreSQL查询问题之条件查询失败如何解决
PolarDB for PostgreSQL是基于PostgreSQL开发的一款云原生关系型数据库服务,它提供了高性能、高可用性和弹性扩展的特性;本合集将围绕PolarDB(pg)的部署、管理和优化提供指导,以及常见问题的排查和解决办法。