{"Entry":{"collection":"psql","key":"cmd-67-73-65-74","name":"\\gset","aliases":["\\gset"],"metadata":{"aliases":["\\gset"],"category":"Query execution","content_hash":"f9264beeff7ad2120ecf172a83c2477bab94566cf59572286b94035187055920","derived_content_hash":"49ae964b57629ff9feb7de9e677f3cb82b97522bba338a60e5eaea2ddeb2505d","documentation_refresh":{"batch":"20261113","original_source_rev":"","versions":{"14":"14.25","15":"15.20","16":"16.16","17":"17.12","18":"18.7","19":"19rc2"}},"imported_at":"2026-09-30T00:40:45.040682+08:00","name":"\\gset","name_zh":"","slug":"cmd-67-73-65-74","summary":"Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example:"}},"Definition":{"Collection":"psql","Key":"cmd-67-73-65-74","SourceDatabase":"center","Version":"18","SourceTable":"psql_command","SourceKey":"cmd-67-73-65-74","SourceRevision":"1b2d62b4845aa46374f2cb680f95d3392322cc8128646992ae4e18d0e7084915","Facts":{"command":"\\gset","comparison_data":{"definition":"Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example: =\u003e SELECT 'hello' AS var1, 10 AS var2 -\u003e \\gset =\u003e \\echo :var1 :var2 hello 10 If you specify a prefix , that string is prepended to the query's column names to create the variable names to use: =\u003e SELECT 'hello' AS var1, 10 AS var2 -\u003e \\gset result_ =\u003e \\echo :result_var1 :result_var2 hello 10 If a column result is NULL, the corresponding variable is unset rather than being set. If the query fails or does not return one row, no variables are changed. If the current query buffer is empty, the most recently sent query is re-executed instead.","modifiers":[],"signature":"\\gset [ prefix ]","spellings":["\\gset"]},"comparison_hash":"b304ede1f50c8841096e0cd06c74f32fbe00e8078cf099ddb56b48cfc62b1a34","description":["Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example:"],"description_zh":"将当前查询缓冲区发送给服务器，并将查询输出存入 psql 变量（参见下面的 变量 ）。要执行的查询必须恰好返回一行。该行的每一列分别存入一个变量，变量名与列名相同。例如：","description_zh_html":"\u003cdd\u003e\u003cp\u003e将当前查询缓冲区发送给服务器，并将查询输出存入 \u003cspan class=\"application\"\u003epsql\u003c/span\u003e 变量（参见下面的\u003ca class=\"xref\" href=\"/zh/docs/18/app-psql.html#APP-PSQL-VARIABLES\" title=\"变量\"\u003e变量\u003c/a\u003e）。要执行的查询必须恰好返回一行。该行的每一列分别存入一个变量，变量名与列名相同。例如：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003eSELECT 'hello' AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\gset\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\echo :var1 :var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e如果指定了 \u003cem class=\"replaceable\"\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e，则会将该字符串加到查询的列名前面，以构成要使用的变量名：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003eSELECT 'hello' AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\gset result_\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\echo :result_var1 :result_var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e如果某一列的结果为 NULL，则取消设置对应的变量，而不是设置它。\u003c/p\u003e\u003cp\u003e如果查询失败或没有恰好返回一行，则不会更改任何变量。\u003c/p\u003e\u003cp\u003e如果当前查询缓冲区为空，则改为重新执行最近发送的查询。\u003c/p\u003e\u003c/dd\u003e","documentation_revision":{"inputs":[{"file":"app-psql.html","kind":"same-version-reviewed-manual","locale":"en","sha256":"af50448485df02bdef2b72b8347192ba029c7082b8d0392ecb17db44ad533aaf","version":"18.7"},{"file":"app-psql.html","kind":"same-version-reviewed-manual","locale":"zh-Hans","sha256":"cb475c53a16d9e06358f5e6c9c5649645db92568cfd319f230c62c9f57ae090b","version":"18.7"}],"release":"18.7","runtime_remeasured":false,"source_revision":"pgdoc:b9f0e7232f60f380d642ac9b99226606c254b552"},"facts":[{"label":"Client","value":"psql 18.7"},{"label":"Case-sensitive spelling","value":"\\gset"}],"facts_zh":[{"label":"客户端","value":"psql 18.7"},{"label":"区分大小写的写法","value":"\\gset"}],"localization":{"documentation_release":"18.7","language":"en","source_language":"en","source_revision":"pgdoc:b9f0e7232f60f380d642ac9b99226606c254b552","status":"same-version-reviewed-manual"},"manual_html":"\u003cdiv\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"APP-PSQL-META-COMMAND-GSET\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\\gset [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n         Sends the current query buffer to the server and stores the\n         query's output into \u003cspan class=\"application\"\u003epsql\u003c/span\u003e variables\n         (see \u003ca class=\"xref\" href=\"/docs/18/app-psql.html#APP-PSQL-VARIABLES\" title=\"Variables\"\u003eVariables\u003c/a\u003e below).\n         The query to be executed must return exactly one row.  Each column of\n         the row is stored into a separate variable, named the same as the\n         column.  For example:\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003eSELECT 'hello' AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\gset\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\echo :var1 :var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n         If you specify a \u003cem class=\"replaceable\"\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e,\n         that string is prepended to the query's column names to create the\n         variable names to use:\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003eSELECT 'hello' AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\gset result_\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\echo :result_var1 :result_var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n         If a column result is NULL, the corresponding variable is unset\n         rather than being set.\n        \u003c/p\u003e\u003cp\u003e\n         If the query fails or does not return one row,\n         no variables are changed.\n        \u003c/p\u003e\u003cp\u003e\n         If the current query buffer is empty, the most recently sent query\n         is re-executed instead.\n        \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","manual_path":"app-psql.html#APP-PSQL-META-COMMAND-GSET","modifiers":[],"related":[{"label":"psql command-line options and complete manual","url":"/wiki/tool/psql/?v=18"}],"related_zh":[{"label":"psql 命令行选项及完整手册","url":"/wiki/tool/psql/?v=18"}],"release":{"channel":"stable","evidence_kind":"English manual and source declarations","label":"18.7","major":"18","manifest":{"major":"18","release":"18.7","source_sha256":"441dfae4280cae3e0425e5f876f80559fb0731c140f1aae22b3e5a54d0411bfd","source_url":"https://ftp.postgresql.org/pub/source/v18.7/postgresql-18.7.tar.bz2"},"ref":"https://ftp.postgresql.org/pub/source/v18.7/postgresql-18.7.tar.bz2","revision":"1b2d62b4845aa46374f2cb680f95d3392322cc8128646992ae4e18d0e7084915","source_sha256":"441dfae4280cae3e0425e5f876f80559fb0731c140f1aae22b3e5a54d0411bfd"},"sections":[],"sections_zh":[],"signature":"\\gset [ prefix ]","sources":[{"anchor":"APP-PSQL-META-COMMAND-GSET","file":"app-psql.html","label":"18.7 English manual · app-psql.html","sha256":"af50448485df02bdef2b72b8347192ba029c7082b8d0392ecb17db44ad533aaf","url":"/docs/18/app-psql.html#APP-PSQL-META-COMMAND-GSET"}],"spellings":["\\gset"],"tables":[{"columns":[{"key":"command","label":"Command"},{"key":"modifiers","label":"Documented suffix modifiers"}],"key":"spellings","rows":[{"command":{"text":"\\gset","url":"/wiki/psql/cmd-67-73-65-74/?v=18"},"modifiers":"None listed in this signature"}],"title":"Spellings in this manual definition"}],"tables_zh":[{"columns":[{"key":"command","label":"命令"},{"key":"modifiers","label":"手册中的后缀修饰符"}],"key":"spellings","rows":[{"command":{"text":"\\gset","url":"/wiki/psql/cmd-67-73-65-74/?v=18"},"modifiers":"此语法签名未列出"}],"title":"本版手册中的写法"}],"zh_from":"same-version-manual"},"ManualEvidence":{"manual_path":"app-psql.html#APP-PSQL-META-COMMAND-GSET","release":{"channel":"stable","evidence_kind":"English manual and source declarations","label":"18.7","major":"18","manifest":{"major":"18","release":"18.7","source_sha256":"441dfae4280cae3e0425e5f876f80559fb0731c140f1aae22b3e5a54d0411bfd","source_url":"https://ftp.postgresql.org/pub/source/v18.7/postgresql-18.7.tar.bz2"},"ref":"https://ftp.postgresql.org/pub/source/v18.7/postgresql-18.7.tar.bz2","revision":"1b2d62b4845aa46374f2cb680f95d3392322cc8128646992ae4e18d0e7084915","source_sha256":"441dfae4280cae3e0425e5f876f80559fb0731c140f1aae22b3e5a54d0411bfd"},"sources":[{"anchor":"APP-PSQL-META-COMMAND-GSET","file":"app-psql.html","label":"18.7 English manual · app-psql.html","sha256":"af50448485df02bdef2b72b8347192ba029c7082b8d0392ecb17db44ad533aaf","url":"/docs/18/app-psql.html#APP-PSQL-META-COMMAND-GSET"}]},"MeasuredEvidence":{}},"Text":{"Collection":"psql","Key":"cmd-67-73-65-74","SourceDatabase":"center","Version":"18","Locale":"en","Title":"\\gset","Summary":"Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example:","BodyHTML":"\u003cdiv\u003e\u003cdl\u003e\u003cdt id=\"APP-PSQL-META-COMMAND-GSET\"\u003e\u003cspan\u003e\u003ccode\u003e\\gset [ \u003cem\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n         Sends the current query buffer to the server and stores the\n         query\u0026#39;s output into \u003cspan\u003epsql\u003c/span\u003e variables\n         (see \u003ca href=\"/docs/18/app-psql.html#APP-PSQL-VARIABLES\" title=\"Variables\" rel=\"nofollow\"\u003eVariables\u003c/a\u003e below).\n         The query to be executed must return exactly one row.  Each column of\n         the row is stored into a separate variable, named the same as the\n         column.  For example:\n\u003c/p\u003e\u003cpre\u003e=\u0026gt; \u003cstrong\u003e\u003ccode\u003eSELECT \u0026#39;hello\u0026#39; AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong\u003e\u003ccode\u003e\\gset\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong\u003e\u003ccode\u003e\\echo :var1 :var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n         If you specify a \u003cem\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e,\n         that string is prepended to the query\u0026#39;s column names to create the\n         variable names to use:\n\u003c/p\u003e\u003cpre\u003e=\u0026gt; \u003cstrong\u003e\u003ccode\u003eSELECT \u0026#39;hello\u0026#39; AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong\u003e\u003ccode\u003e\\gset result_\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong\u003e\u003ccode\u003e\\echo :result_var1 :result_var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n         If a column result is NULL, the corresponding variable is unset\n         rather than being set.\n        \u003c/p\u003e\u003cp\u003e\n         If the query fails or does not return one row,\n         no variables are changed.\n        \u003c/p\u003e\u003cp\u003e\n         If the current query buffer is empty, the most recently sent query\n         is re-executed instead.\n        \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","SourceRevision":"1b2d62b4845aa46374f2cb680f95d3392322cc8128646992ae4e18d0e7084915","ContentHash":"0ff75021c9dd5e7eb9e0e6ab351fb0c3cc69b228f143507381e3fb002147acf7","Payload":{"description":["Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example:"],"manual_html":"\u003cdiv\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"APP-PSQL-META-COMMAND-GSET\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003e\\gset [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n         Sends the current query buffer to the server and stores the\n         query's output into \u003cspan class=\"application\"\u003epsql\u003c/span\u003e variables\n         (see \u003ca class=\"xref\" href=\"/docs/18/app-psql.html#APP-PSQL-VARIABLES\" title=\"Variables\"\u003eVariables\u003c/a\u003e below).\n         The query to be executed must return exactly one row.  Each column of\n         the row is stored into a separate variable, named the same as the\n         column.  For example:\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003eSELECT 'hello' AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\gset\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\echo :var1 :var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n         If you specify a \u003cem class=\"replaceable\"\u003e\u003ccode\u003eprefix\u003c/code\u003e\u003c/em\u003e,\n         that string is prepended to the query's column names to create the\n         variable names to use:\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003eSELECT 'hello' AS var1, 10 AS var2\u003c/code\u003e\u003c/strong\u003e\n-\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\gset result_\u003c/code\u003e\u003c/strong\u003e\n=\u0026gt; \u003cstrong class=\"userinput\"\u003e\u003ccode\u003e\\echo :result_var1 :result_var2\u003c/code\u003e\u003c/strong\u003e\nhello 10\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n         If a column result is NULL, the corresponding variable is unset\n         rather than being set.\n        \u003c/p\u003e\u003cp\u003e\n         If the query fails or does not return one row,\n         no variables are changed.\n        \u003c/p\u003e\u003cp\u003e\n         If the current query buffer is empty, the most recently sent query\n         is re-executed instead.\n        \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","related":[{"label":"psql command-line options and complete manual","url":"/wiki/tool/psql/?v=18"}],"sections":[],"tables":[{"columns":[{"key":"command","label":"Command"},{"key":"modifiers","label":"Documented suffix modifiers"}],"key":"spellings","rows":[{"command":{"text":"\\gset","url":"/wiki/psql/cmd-67-73-65-74/?v=18"},"modifiers":"None listed in this signature"}],"title":"Spellings in this manual definition"}]}},"RequestedLocale":"en","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":[{"Version":"18","Ordinal":0,"Command":"\\gset","Signature":"\\gset [ prefix ]","Modifiers":[]}],"SQLState":null,"Evidence":null}
