Wiki / psql Commands / Query execution
\gexec
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 :
Reading PostgreSQL 18.6.
Description
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 :
- Client
- psql 18.6
- Case-sensitive spelling
- \gexec
Usage
\gexecSpellings in this manual definition
| Command | Documented suffix modifiers |
|---|---|
| \gexec | None listed in this signature |
Manual definition
\gexec-
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->\gexecCREATE INDEX CREATE INDEX CREATE INDEX CREATE INDEXThe 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_STOPis set. Execution of each query is subject toECHOprocessing. (SettingECHOtoallorqueriesis 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.
Related entries
Documentation and source
Source build
- Version
- 18.6
- Build
- https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
- Source fingerprint
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8
Compare versions
PostgreSQL 18 → 19: changed.
--- 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": [
Compares recorded interfaces and attributes. Source fingerprints and build metadata are excluded; an absent sample is not proof of the introduction or removal release.
Related entries
Export JSON · Back to psql Commands · Recorded in PostgreSQL 10 through 20; the first sample is not necessarily its introduction.