↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / psql 命令 / 查询执行

\gexec

将当前查询缓冲区发送到服务器,然后将查询输出的每行每列视为要执行的SQL语句。例如,要在 my_table 的每一列上创建索引:

当前查看 PostgreSQL 18.6。

说明

将当前查询缓冲区发送到服务器,然后将查询输出的每行每列视为要执行的SQL语句。例如,要在 my_table 的每一列上创建索引:

客户端
psql 18.6
区分大小写的写法
\gexec

用法

\gexec

本版手册中的写法

命令手册中的后缀修饰符
\gexec此语法签名未列出

手册定义

\gexec

将当前查询缓冲区发送到服务器,然后将查询输出的每行每列视为要执行的SQL语句。例如,要在my_table的每一列上创建索引:

=> SELECT format('create index on my_table(%I)', attname)
-> FROM pg_attribute
-> WHERE attrelid = 'my_table'::regclass AND attnum > 0
-> ORDER BY attnum
-> \gexec
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX

生成的查询按返回行的顺序执行;同一行有多列时从左到右执行,忽略 NULL 字段。查询原样发送给服务器,因此不能是 psql 元命令,也不能包含 psql 变量引用。除非设置 ON_ERROR_STOP,某条查询失败后仍继续执行其余查询。每条查询均遵循 ECHO 设置(使用 \gexec 时,通常建议将 ECHO 设为 all 或 queries),查询日志、单步模式、计时等执行功能也分别应用于每条生成的查询。

若当前查询缓冲区为空,则重新执行最近发送的查询。

相关条目

文档与源码

来源构建
版本
18.6
构建
https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8

版本比较

PostgreSQL 18 → 19: 属性变化。

以下差异保留原始字段名与英文源描述。

--- PostgreSQL 18
+++ PostgreSQL 19
@@ -1,5 +1,5 @@
 {
-  "definition": "Sends the current query buffer to the server, then treats each column of each row of the query's output (if any) as an SQL statement to be executed. For example, to create an index on each column of my_table : => SELECT format('create index on my_table(%I)', attname) -> FROM pg_attribute -> WHERE attrelid = 'my_table'::regclass AND attnum > 0 -> ORDER BY attnum -> \\gexec CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX The generated queries are executed in the order in which the rows are returned, and left-to-right within each row if there is more than one column. NULL fields are ignored. The generated queries are sent literally to the server for processing, so they cannot be psql meta-commands nor contain psql variable references. If any individual query fails, execution of the remaining queries continues unless ON_ERROR_STOP is set. Execution of each query is subject to ECHO processing. (Setting ECHO to all or queries is often advisable when using \\gexec .) Query logging, single-step mode, timing, and other query execution features apply to each generated query as well. If the current query buffer is empty, the most recently sent query is re-executed instead.",
+  "definition": "Sends the current query buffer to the server, then treats each column of each row of the query's output (if any) as an SQL statement to be executed. For example, to create an index on each column of my_table : => SELECT format('CREATE INDEX ON my_table (%I)', attname) -> FROM pg_attribute -> WHERE attrelid = 'my_table'::regclass AND attnum > 0 -> ORDER BY attnum -> \\gexec CREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEX The generated queries are executed in the order in which the rows are returned, and left-to-right within each row if there is more than one column. NULL fields are ignored. The generated queries are sent literally to the server for processing, so they cannot be psql meta-commands nor contain psql variable references. If any individual query fails, execution of the remaining queries continues unless ON_ERROR_STOP is set. Execution of each query is subject to ECHO processing. (Setting ECHO to all or queries is often advisable when using \\gexec .) Query logging, single-step mode, timing, and other query execution features apply to each generated query as well. If the current query buffer is empty, the most recently sent query is re-executed instead.",
   "modifiers": [],
   "signature": "\\gexec",
   "spellings": [

比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。

相关条目

导出 JSON · 返回psql 命令 · 收录范围为 PostgreSQL 10 至 20;最早采样版本不一定是实际引入版本。