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

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

百科 / psql 命令 / 查询执行

\gexec

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

当前查看 PostgreSQL 18。

中文定义与说明

将当前查询缓冲区发送到服务器,然后将查询输出的每行每列视为要执行的 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。)查询日志、单步模式、计时及其他查询执行功能也适用于每个生成的查询。

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

说明

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

客户端
psql 18.7
区分大小写的写法
\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.7
构建
https://ftp.postgresql.org/pub/source/v18.7/postgresql-18.7.tar.bz2
来源指纹
1b2d62b4845aa46374f2cb680f95d3392322cc8128646992ae4e18d0e7084915

版本比较

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;最早采样版本不一定是实际引入版本。