diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index e3188660a3d..f945c9f394b 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -3835,6 +3835,11 @@ SELECT 1 \bind \sendpipeline
pg_dumpall, and
pg_restore, but it may be useful elsewhere.
+
+ Unlike most other meta-commands, the entire remainder of the line is
+ always taken to be the argument of \unrestrict, and
+ neither variable interpolation nor backquote expansion are performed.
+
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 4e97775adab..09a1e79a6a7 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2788,6 +2788,12 @@ exec_command_restrict(PsqlScanState scan_state, bool active_branch,
Assert(!restricted);
+ /*
+ * Unlike \unrestrict, this argument may safely undergo backquote and
+ * variable expansion: HandleSlashCmds() rejects \restrict in
+ * restricted mode before its argument is scanned, so we only get here
+ * when the input could execute such things anyway.
+ */
opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
if (opt == NULL || opt[0] == '\0')
{
@@ -3196,7 +3202,7 @@ exec_command_unrestrict(PsqlScanState scan_state, bool active_branch,
{
char *opt;
- opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+ opt = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, true);
if (opt == NULL || opt[0] == '\0')
{
pg_log_error("\\%s: missing required argument", cmd);
@@ -3220,7 +3226,7 @@ exec_command_unrestrict(PsqlScanState scan_state, bool active_branch,
}
}
else
- ignore_slash_options(scan_state);
+ ignore_slash_whole_line(scan_state);
return PSQL_CMD_SKIP_LINE;
}
diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl
index 6f2341fcaa1..a335acca68c 100644
--- a/src/bin/psql/t/001_basic.pl
+++ b/src/bin/psql/t/001_basic.pl
@@ -539,4 +539,11 @@ psql_fails_like(
qr/backslash commands are restricted; only \\unrestrict is allowed/,
'meta-command in restrict mode fails');
+psql_fails_like(
+ $node,
+ qq{\\restrict test
+\\unrestrict `echo test`},
+ qr/wrong key/,
+ '\unrestrict does not do backquote expansion');
+
done_testing();