From 600873a89e76124f294b096a824d6fd4e24c4a28 Mon Sep 17 00:00:00 2001
From: Julien Rouhaud <julien.rouhaud@free.fr>
Date: Wed, 26 Jan 2022 15:03:27 +0800
Subject: [PATCH v20220126 3/3] Various fixes.

---
 doc/src/sgml/advanced.sgml                    | 35 +++++----
 doc/src/sgml/catalogs.sgml                    |  4 +-
 doc/src/sgml/config.sgml                      |  6 +-
 doc/src/sgml/glossary.sgml                    | 12 +--
 doc/src/sgml/plpgsql.sgml                     | 11 ++-
 .../sgml/ref/alter_default_privileges.sgml    | 16 ++--
 doc/src/sgml/ref/alter_variable.sgml          | 44 +++++------
 doc/src/sgml/ref/create_variable.sgml         | 75 ++++++++++---------
 doc/src/sgml/ref/discard.sgml                 |  4 +-
 doc/src/sgml/ref/drop_variable.sgml           | 18 ++---
 doc/src/sgml/ref/let.sgml                     |  8 +-
 doc/src/sgml/ref/pg_restore.sgml              |  4 +-
 src/backend/catalog/namespace.c               |  7 +-
 src/backend/catalog/pg_variable.c             | 16 ++--
 src/backend/commands/sessionvariable.c        |  2 +-
 src/backend/executor/svariableReceiver.c      |  2 +-
 src/include/catalog/pg_variable.h             |  2 +-
 src/include/commands/session_variable.h       |  2 +-
 src/include/executor/svariableReceiver.h      |  2 +-
 19 files changed, 140 insertions(+), 130 deletions(-)

diff --git a/doc/src/sgml/advanced.sgml b/doc/src/sgml/advanced.sgml
index 97470521a8..c50c2ff880 100644
--- a/doc/src/sgml/advanced.sgml
+++ b/doc/src/sgml/advanced.sgml
@@ -714,16 +714,18 @@ SELECT name, elevation
 
    <para>
     Session variables are database objects that can hold a value.
-    Session variables, like relations, exist within a schema and their access is
-    controlled via <command>GRANT</command> and <command>REVOKE</command> commands.
-    A session variable can be created by the <command>CREATE VARIABLE</command> command.
+    Session variables, like relations, exist within a schema and their access
+    is controlled via <command>GRANT</command> and <command>REVOKE</command>
+    commands.  A session variable can be created by the <command>CREATE
+    VARIABLE</command> command.
    </para>
 
    <para>
-    The value of a session variable is set with the <command>LET</command> SQL command.
-    While session variables share properties with tables, their value cannot be updated
-    with an <command>UPDATE</command> command. The value of a session variable may be
-    retrieved by the <command>SELECT</command> SQL command.
+    The value of a session variable is set with the <command>LET</command> SQL
+    command.  While session variables share properties with tables, their value
+    cannot be updated with an <command>UPDATE</command> command. The value of a
+    session variable may be retrieved by the <command>SELECT</command> SQL
+    command.
 <programlisting>
 CREATE VARIABLE var1 AS date;
 LET var1 = current_date;
@@ -742,18 +744,19 @@ SELECT current_user_id;
 
    <para>
     The value of a session variable is local to the current session. Retrieving
-    a variable's value returns either a <literal>NULL</literal> or a default value,
-    unless its value is set to something else in the current session using the
-    <command>LET</command> command. The content of a variable is not transactional.
-    This is the same as regular variables in PL languages.
+    a variable's value returns either a <literal>NULL</literal> or a default
+    value, unless its value has been set to something else in the current
+    session using the <command>LET</command> command. The content of a variable
+    is not transactional.  This is the same as regular variables in PL
+    languages.
    </para>
 
    <para>
-    The session variables can be shadowed by column references in a query. When a
-    query contains identifiers or qualified identifiers that could be used as both
-    a session variable identifiers and as column identifier, then the column
-    identifier is preferred every time. Warnings can be emitted when this situation
-    happens by enabling configuration parameter <xref
+    The session variables can be shadowed by column references in a query. When
+    a query contains identifiers or qualified identifiers that could be used as
+    both a session variable identifiers and as column identifier, then the
+    column identifier is preferred every time. Warnings can be emitted when
+    this situation happens by enabling configuration parameter <xref
     linkend="guc-session-variables-ambiguity-warning"/>.
    </para>
   </sect1>
diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index db805fe079..a85571c1a7 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -14001,8 +14001,8 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
   </indexterm>
 
   <para>
-   The table <structname>pg_variable</structname> provides information
-   about session variables.
+   The table <structname>pg_variable</structname> provides information about
+   session variables.
   </para>
 
   <table>
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 8990337f81..7a39da0e74 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -10031,9 +10031,9 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
       </term>
       <listitem>
        <para>
-        When on, a warning is raised when any identifier in a query could be used as both
-        a column identifier, routine variable or a session variable identifier.
-        The default is <literal>off</literal>.
+        When on, a warning is raised when any identifier in a query could be
+        used as both a column identifier, routine variable or a session
+        variable identifier.  The default is <literal>off</literal>.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/glossary.sgml b/doc/src/sgml/glossary.sgml
index 641dcc67b3..2529805ab4 100644
--- a/doc/src/sgml/glossary.sgml
+++ b/doc/src/sgml/glossary.sgml
@@ -1467,14 +1467,14 @@
    <glossterm>Session variable</glossterm>
    <glossdef>
     <para>
-     A persistent database object that holds a value in session memory.
-     This memory is not shared across sessions, and after session end, this
-     memory (the value) is released. The access (read or write) to session variables
-     is controlled by access rights similarly to other database object access rights.
+     A persistent database object that holds a value in session memory.  This
+     memory is not shared across sessions, and after session end, this memory
+     (the value) is released. The access (read or write) to session variables
+     is controlled by access rights similarly to other database object access
+     rights.
     </para>
     <para>
-     For more information, see
-     <xref linkend="tutorial-session-variables"/>.
+     For more information, see <xref linkend="tutorial-session-variables"/>.
     </para>
    </glossdef>
   </glossentry>
diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml
index 423677b9af..9569f2c0e4 100644
--- a/doc/src/sgml/plpgsql.sgml
+++ b/doc/src/sgml/plpgsql.sgml
@@ -5957,12 +5957,11 @@ $$ LANGUAGE plpgsql STRICT IMMUTABLE;
     <title><command>Session variables and constants</command></title>
 
     <para>
-     The <application>PL/pgSQL</application> language has no packages,
-     and therefore no package variables or package constants.
-     <productname>PostgreSQL</productname> has session variables and
-     immutable session variables. Session variables can be created
-     by <command>CREATE VARIABLE</command> described in <xref
-     linkend="sql-createvariable"/>.
+     The <application>PL/pgSQL</application> language has no packages, and
+     therefore no package variables or package constants.
+     <productname>PostgreSQL</productname> has session variables and immutable
+     session variables. Session variables can be created by <command>CREATE
+     VARIABLE</command>, as described in <xref linkend="sql-createvariable"/>.
     </para>
    </sect3>
   </sect2>
diff --git a/doc/src/sgml/ref/alter_default_privileges.sgml b/doc/src/sgml/ref/alter_default_privileges.sgml
index 7eb1e8cf8b..ec85fa39cf 100644
--- a/doc/src/sgml/ref/alter_default_privileges.sgml
+++ b/doc/src/sgml/ref/alter_default_privileges.sgml
@@ -102,14 +102,14 @@ REVOKE [ GRANT OPTION FOR ]
    that will be applied to objects created in the future.  (It does not
    affect privileges assigned to already-existing objects.)  Currently,
    only the privileges for schemas, tables (including views and foreign
-   tables), sequences, functions, types (including domains) and session variables
-   can be altered.  For this command, functions include aggregates and procedures.
-   The words <literal>FUNCTIONS</literal> and <literal>ROUTINES</literal> are
-   equivalent in this command.  (<literal>ROUTINES</literal> is preferred
-   going forward as the standard term for functions and procedures taken
-   together.  In earlier PostgreSQL releases, only the
-   word <literal>FUNCTIONS</literal> was allowed.  It is not possible to set
-   default privileges for functions and procedures separately.)
+   tables), sequences, functions, types (including domains) and session
+   variables can be altered.  For this command, functions include aggregates
+   and procedures.  The words <literal>FUNCTIONS</literal> and
+   <literal>ROUTINES</literal> are equivalent in this command.
+   (<literal>ROUTINES</literal> is preferred going forward as the standard term
+   for functions and procedures taken together.  In earlier PostgreSQL
+   releases, only the word <literal>FUNCTIONS</literal> was allowed.  It is not
+   possible to set default privileges for functions and procedures separately.)
   </para>
 
   <para>
diff --git a/doc/src/sgml/ref/alter_variable.sgml b/doc/src/sgml/ref/alter_variable.sgml
index 50b1f6a2d7..06e7f8cd96 100644
--- a/doc/src/sgml/ref/alter_variable.sgml
+++ b/doc/src/sgml/ref/alter_variable.sgml
@@ -22,7 +22,7 @@ PostgreSQL documentation
  <refnamediv>
   <refname>ALTER VARIABLE</refname>
   <refpurpose>
-   change the definition of a variable
+   change the definition of a session variable
   </refpurpose>
  </refnamediv>
 
@@ -38,15 +38,15 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
   <title>Description</title>
 
   <para>
-   The <command>ALTER VARIABLE</command> command changes the definition of an existing
-   variable. There are several subforms:
+   The <command>ALTER VARIABLE</command> command changes the definition of an
+   existing session variable. There are several subforms:
 
   <variablelist>
    <varlistentry>
     <term><literal>OWNER</literal></term>
     <listitem>
      <para>
-      This form changes the owner of the variable.
+      This form changes the owner of the session variable.
      </para>
     </listitem>
    </varlistentry>
@@ -55,7 +55,7 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
     <term><literal>RENAME</literal></term>
     <listitem>
      <para>
-      This form changes the name of the variable.
+      This form changes the name of the session variable.
      </para>
     </listitem>
    </varlistentry>
@@ -64,7 +64,7 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
     <term><literal>SET SCHEMA</literal></term>
     <listitem>
      <para>
-      This form moves the variable into another schema.
+      This form moves the session variable into another schema.
      </para>
     </listitem>
    </varlistentry>
@@ -74,16 +74,16 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
 
   <para>
    Only the owner or a superuser is allowed to alter a session variable.
-   In order to move a session variable from one schema to another, the user must
-   also have the <literal>CREATE</literal> privilege on the new schema (or be
-   a superuser).
+   In order to move a session variable from one schema to another, the user
+   must also have the <literal>CREATE</literal> privilege on the new schema (or
+   be a superuser).
 
    In order to move the session variable ownership from one role to another,
    the user must also be a direct or indirect member of the new
    owning role, and that role must have the <literal>CREATE</literal> privilege
-   on the variable's schema (or be a superuser). These restrictions enforce that
-   altering the owner doesn't do anything you couldn't do by dropping and
-   recreating the variable.
+   on the session variable's schema (or be a superuser). These restrictions
+   enforce that altering the owner doesn't do anything you couldn't do by
+   dropping and recreating the session variable.
   </para>
  </refsect1>
 
@@ -96,8 +96,8 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
       <term><replaceable class="parameter">name</replaceable></term>
       <listitem>
        <para>
-        The name (possibly schema-qualified) of the existing variable to
-        alter.
+        The name (possibly schema-qualified) of the existing session variable
+        to alter.
        </para>
       </listitem>
      </varlistentry>
@@ -106,7 +106,7 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
       <term><replaceable class="parameter">new_name</replaceable></term>
       <listitem>
        <para>
-        The new name for the variable.
+        The new name for the session variable.
        </para>
       </listitem>
      </varlistentry>
@@ -115,7 +115,7 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
       <term><replaceable class="parameter">new_owner</replaceable></term>
       <listitem>
        <para>
-        The user name of the new owner of the variable.
+        The user name of the new owner of the session variable.
        </para>
       </listitem>
      </varlistentry>
@@ -124,7 +124,7 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
       <term><replaceable class="parameter">new_schema</replaceable></term>
       <listitem>
        <para>
-        The new schema for the variable.
+        The new schema for the session variable.
        </para>
       </listitem>
      </varlistentry>
@@ -136,23 +136,23 @@ ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
   <title>Examples</title>
 
   <para>
-   To rename a variable:
+   To rename a session variable:
 <programlisting>
 ALTER VARIABLE foo RENAME TO boo;
 </programlisting>
   </para>
 
   <para>
-   To change the owner of the variable <literal>boo</literal>
-   to <literal>joe</literal>:
+   To change the owner of the session variable <literal>boo</literal> to
+   <literal>joe</literal>:
 <programlisting>
 ALTER VARIABLE boo OWNER TO joe;
 </programlisting>
   </para>
 
   <para>
-   To change the schema of the variable <literal>boo</literal>
-   to <literal>private</literal>:
+   To change the schema of the session variable <literal>boo</literal> to
+   <literal>private</literal>:
 <programlisting>
 ALTER VARIABLE boo SET SCHEMA private;
 </programlisting>
diff --git a/doc/src/sgml/ref/create_variable.sgml b/doc/src/sgml/ref/create_variable.sgml
index 38b8a2fbc6..04fb30873a 100644
--- a/doc/src/sgml/ref/create_variable.sgml
+++ b/doc/src/sgml/ref/create_variable.sgml
@@ -36,22 +36,23 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
   <para>
    The <command>CREATE VARIABLE</command> command creates a session variable.
    Session variables, like relations, exist within a schema and their access is
-   controlled via <command>GRANT</command> and <command>REVOKE</command> commands.
-   Changing a session variable is non-transactional.
+   controlled via <command>GRANT</command> and <command>REVOKE</command>
+   commands.  Changing a session variable is non-transactional.
   </para>
 
   <para>
    The value of a session variable is local to the current session. Retrieving
-   a variable's value returns either a NULL or a default value, unless its value
-   is set to something else in the current session with a LET command. The content
-   of a variable is not transactional. This is the same as regular variables in PL languages.
+   a session variable's value returns either a NULL or a default value, unless
+   its value is set to something else in the current session with a LET
+   command. The content of a session variable is not transactional. This is the
+   same as regular variables in PL languages.
   </para>
 
   <para>
-   Session variables are retrieved by the <command>SELECT</command> SQL command.
-   Their value is set with the <command>LET</command> SQL command.
-   While session variables share properties with tables, their value cannot be updated
-   with an <command>UPDATE</command> command.
+   Session variables are retrieved by the <command>SELECT</command> SQL
+   command.  Their value is set with the <command>LET</command> SQL command.
+   While session variables share properties with tables, their value cannot be
+   changed with an <command>UPDATE</command> command.
   </para>
  </refsect1>
 
@@ -63,10 +64,10 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><literal>IMMUTABLE</literal></term>
     <listitem>
      <para>
-      The assigned value of the variable cannot be changed. The value can be assigned
-      by command <command>LET</command> only first time (when session variable has not
-      defined default expression), or never (when session variable has defined not null
-      default expression).
+      The assigned value of the session variable can never change.
+      If the session variable doesn't have a default value, then a single
+      initialization is allowed using the <command>LET</command> command.  Once
+      done, no other change will be allowed in the session lifetime.
      </para>
     </listitem>
    </varlistentry>
@@ -75,7 +76,8 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><literal>IF NOT EXISTS</literal></term>
     <listitem>
      <para>
-      Do not throw an error if the name already exists. A notice is issued in this case.
+      Do not throw an error if the name already exists. A notice is issued in
+      this case.
      </para>
     </listitem>
    </varlistentry>
@@ -84,7 +86,7 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><replaceable class="parameter">name</replaceable></term>
     <listitem>
      <para>
-      The name, optionally schema-qualified, of the variable.
+      The name, optionally schema-qualified, of the session variable.
      </para>
     </listitem>
    </varlistentry>
@@ -93,7 +95,8 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><replaceable class="parameter">data_type</replaceable></term>
     <listitem>
      <para>
-      The name, optionally schema-qualified, of the data type of the variable.
+      The name, optionally schema-qualified, of the data type of the session
+      variable.
      </para>
     </listitem>
    </varlistentry>
@@ -102,9 +105,9 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><literal>COLLATE <replaceable>collation</replaceable></literal></term>
     <listitem>
      <para>
-      The <literal>COLLATE</literal> clause assigns a collation to
-      the variable (which must be of a collatable data type).
-      If not specified, the data type's default collation is used.
+      The <literal>COLLATE</literal> clause assigns a collation to the session
+      variable (which must be of a collatable data type).  If not specified,
+      the data type's default collation is used.
      </para>
     </listitem>
    </varlistentry>
@@ -113,11 +116,12 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><literal>NOT NULL</literal></term>
     <listitem>
      <para>
-      The <literal>NOT NULL</literal> clause forbids setting the variable to
-      a null value. A variable created as NOT NULL and without an explicitly
-      declared default value cannot be read until it is initialized by a LET
-      command. This requires the user to explicitly initialize the variable
-      content before reading it.
+      The <literal>NOT NULL</literal> clause forbids setting the session
+      variable to a null value. A session variable created as NOT NULL and
+      without an explicitly declared default value cannot be read until it is
+      initialized by a LET command. This requires the user to explicitly
+      initialize the session variable content before reading it, otherwise an
+      error will be thrown.
      </para>
     </listitem>
    </varlistentry>
@@ -126,8 +130,8 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><literal>DEFAULT <replaceable>default_expr</replaceable></literal></term>
     <listitem>
      <para>
-      The <literal>DEFAULT</literal> clause can be used to assign a default value to
-      a session variable.
+      The <literal>DEFAULT</literal> clause can be used to assign a default
+      value to a session variable.
      </para>
     </listitem>
    </varlistentry>
@@ -136,11 +140,11 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
     <term><literal>ON COMMIT DROP</literal>, <literal>ON TRANSACTION END RESET</literal></term>
     <listitem>
      <para>
-      The <literal>ON COMMIT DROP</literal> clause specifies the behaviour
-      of a temporary session variable at transaction commit. With this clause, the
-      variable is dropped at commit time. The clause is only allowed
-      for temporary variables. The <literal>ON TRANSACTION END RESET</literal>
-      clause causes the variable to be reset to its default value when
+      The <literal>ON COMMIT DROP</literal> clause specifies the behaviour of a
+      temporary session variable at transaction commit. With this clause, the
+      session variable is dropped at commit time. The clause is only allowed
+      for temporary variables. The <literal>ON TRANSACTION END RESET</literal>
+      clause causes the session variable to be reset to its default value when
       the transaction is committed or rolled back.
      </para>
     </listitem>
@@ -153,7 +157,8 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
   <title>Notes</title>
 
   <para>
-   Use the <command>DROP VARIABLE</command> command to remove a variable.
+   Use the <command>DROP VARIABLE</command> command to remove a session
+   variable.
   </para>
  </refsect1>
 
@@ -161,7 +166,7 @@ CREATE [ { TEMPORARY | TEMP } ] [ IMMUTABLE ] VARIABLE [ IF NOT EXISTS ] <replac
   <title>Examples</title>
 
   <para>
-   Create an integer variable <literal>var1</literal>:
+   Create an date session variable <literal>var1</literal>:
 <programlisting>
 CREATE VARIABLE var1 AS date;
 LET var1 = current_date;
@@ -175,8 +180,8 @@ SELECT var1;
   <title>Compatibility</title>
 
   <para>
-   The <command>CREATE VARIABLE</command> command is a PostgreSQL extension.
-   <!-- The choice of wording here seems to be left to personal preference... -->
+   The <command>CREATE VARIABLE</command> command is a
+   <productname>PostgreSQL</productname> extension.
   </para>
  </refsect1>
 
diff --git a/doc/src/sgml/ref/discard.sgml b/doc/src/sgml/ref/discard.sgml
index 48d96af187..6f90672afa 100644
--- a/doc/src/sgml/ref/discard.sgml
+++ b/doc/src/sgml/ref/discard.sgml
@@ -80,8 +80,8 @@ DISCARD { ALL | PLANS | SEQUENCES | TEMPORARY | TEMP | VARIABLES }
     <listitem>
      <para>
       Resets the value of all session variables. If a variable
-      is later reused, it is re-initialized to either
-      <literal>NULL</literal> or its default value.
+      is later reused, it is re-initialized to either
+      <literal>NULL</literal> or its default value.
      </para>
     </listitem>
    </varlistentry>
diff --git a/doc/src/sgml/ref/drop_variable.sgml b/doc/src/sgml/ref/drop_variable.sgml
index 5cced4ea90..67988b5fcd 100644
--- a/doc/src/sgml/ref/drop_variable.sgml
+++ b/doc/src/sgml/ref/drop_variable.sgml
@@ -35,7 +35,7 @@ DROP VARIABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> [,
 
   <para>
    <command>DROP VARIABLE</command> removes a session variable.
-   A variable can only be dropped by its owner or a superuser.
+   A session variable can only be removed by its owner or a superuser.
   </para>
  </refsect1>
 
@@ -47,8 +47,8 @@ DROP VARIABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> [,
     <term><literal>IF EXISTS</literal></term>
     <listitem>
      <para>
-      Do not throw an error if the variable does not exist. A notice is issued
-      in this case.
+      Do not throw an error if the session variable does not exist. A notice is
+      issued in this case.
      </para>
     </listitem>
    </varlistentry>
@@ -66,9 +66,8 @@ DROP VARIABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> [,
     <term><literal>CASCADE</literal></term>
     <listitem>
      <para>
-      Automatically drop objects that depend on the variable (such as
-      views),
-      and in turn all objects that depend on those objects
+      Automatically drop objects that depend on the session variable (such as
+      views), and in turn all objects that depend on those objects
       (see <xref linkend="ddl-depend"/>).
      </para>
     </listitem>
@@ -78,7 +77,7 @@ DROP VARIABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> [,
     <term><literal>RESTRICT</literal></term>
     <listitem>
      <para>
-      Refuse to drop the variable if any objects depend on it.  This is
+      Refuse to drop the session variable if any objects depend on it.  This is
       the default.
      </para>
     </listitem>
@@ -101,9 +100,8 @@ DROP VARIABLE var1;
   <title>Compatibility</title>
 
   <para>
-   The <command>DROP VARIABLE</command> command is a PostgreSQL extension.
-   <!-- create variable is a "PostgreSQL feature",
-        this is a "proprietary PostgreSQL command" ... -->
+   The <command>DROP VARIABLE</command> command is a
+   <productname>PostgreSQL</productname> extension.
   </para>
  </refsect1>
 
diff --git a/doc/src/sgml/ref/let.sgml b/doc/src/sgml/ref/let.sgml
index fcbfefb4f6..cf5515ac67 100644
--- a/doc/src/sgml/ref/let.sgml
+++ b/doc/src/sgml/ref/let.sgml
@@ -35,7 +35,8 @@ LET <replaceable class="parameter">session_variable</replaceable> = DEFAULT
   <title>Description</title>
 
   <para>
-   The <command>LET</command> command assigns a value to the specified session variable.
+   The <command>LET</command> command assigns a value to the specified session
+   variable.
   </para>
 
  </refsect1>
@@ -57,7 +58,8 @@ LET <replaceable class="parameter">session_variable</replaceable> = DEFAULT
     <term><literal>sql_expression</literal></term>
     <listitem>
      <para>
-      An SQL expression. The result is cast to the data type of the session variable.
+      An SQL expression. The result is cast to the data type of the session
+      variable.
      </para>
     </listitem>
    </varlistentry>
@@ -91,7 +93,7 @@ LET myvar = DEFAULT;
   <para>
    <literal>LET</literal> extends the syntax defined in the SQL
    standard. The <literal>SET</literal> command from the SQL standard
-   is used for different purposes in PostgreSQL.
+   is used for different purposes in <productname>PostgreSQL</productname>.
   </para>
  </refsect1>
 
diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml
index 3e8c26c695..85f29431c1 100644
--- a/doc/src/sgml/ref/pg_restore.sgml
+++ b/doc/src/sgml/ref/pg_restore.sgml
@@ -111,8 +111,8 @@ PostgreSQL documentation
       <term><option>--variable=<replaceable class="parameter">schema_variable</replaceable></option></term>
       <listitem>
        <para>
-        Restore a named session variable only.  Multiple session variables may be specified with
-        multiple <option>-A</option> switches.
+        Restore a named session variable only.  Multiple session variables may
+        be specified with multiple <option>-A</option> switches.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index fc0e08be46..bb95207977 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -2907,8 +2907,7 @@ TSConfigIsVisible(Oid cfgid)
 }
 
 /*
- * Returns oid of session variable specified by possibly qualified
- * identifier.
+ * Returns oid of session variable specified by possibly qualified identifier.
  *
  * If not found, returns InvalidOid if missing_ok, else throws error.
  */
@@ -2924,8 +2923,8 @@ LookupVariable(const char *nspname, const char *varname, bool missing_ok)
 		namespaceId = LookupExplicitNamespace(nspname, missing_ok);
 
 		/*
-		 * When nspname is not known namespace, then nspname.varname
-		 * cannot to identify any session variable.
+		 * If nspname is not a known namespace, then nspname.varname cannot be
+		 * any usable session variable.
 		 */
 		if (!OidIsValid(namespaceId))
 			return InvalidOid;
diff --git a/src/backend/catalog/pg_variable.c b/src/backend/catalog/pg_variable.c
index fa81b54f59..1f73848312 100644
--- a/src/backend/catalog/pg_variable.c
+++ b/src/backend/catalog/pg_variable.c
@@ -3,7 +3,7 @@
  * pg_variable.c
  *		session variables
  *
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
@@ -71,8 +71,8 @@ to_eoxaction(VariableEOXActionCodes code)
 }
 
 /*
- * Returns name of session variable. When the variable is not in the path,
- * the name is qualified.
+ * Returns the name of a given session variable. When the variable is not in
+ * the path, the name is qualified.
  */
 char *
 session_variable_get_name(Oid varid)
@@ -129,7 +129,7 @@ get_session_variable_name(Oid varid)
 }
 
 /*
- * Returns type, typmod and collid of session variable.
+ * Returns the type, typmod and collid of the given session variable.
  */
 void
 get_session_variable_type_typmod_collid(Oid varid, Oid *typid, int32 *typmod,
@@ -379,11 +379,15 @@ VariableCreate(const char *varName,
 	}
 
 	/*
-	 * When this new field in catalog is temporal, we need to create
-	 * new xact action to ensure delete from catalog.
+	 * For temporary variables, we need to create a new end of xact action to
+	 * ensure deletion from catalog.
 	 */
 	if (eoxaction == VARIABLE_EOX_DROP)
+	{
+		Assert(isTempNamespace(varNamespace));
+
 		RegisterOnCommitDropSessionVariable(myself.objectId);
+	}
 
 	heap_freetuple(tup);
 
diff --git a/src/backend/commands/sessionvariable.c b/src/backend/commands/sessionvariable.c
index 0e058b693e..1fc050ee80 100644
--- a/src/backend/commands/sessionvariable.c
+++ b/src/backend/commands/sessionvariable.c
@@ -3,7 +3,7 @@
  * sessionvariable.c
  *	  session variable creation/manipulation commands
  *
- * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
diff --git a/src/backend/executor/svariableReceiver.c b/src/backend/executor/svariableReceiver.c
index 66b8b6ec8c..d5ce377b0f 100644
--- a/src/backend/executor/svariableReceiver.c
+++ b/src/backend/executor/svariableReceiver.c
@@ -4,7 +4,7 @@
  *	  An implementation of DestReceiver that stores the result value in
  *	  a session variable.
  *
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
diff --git a/src/include/catalog/pg_variable.h b/src/include/catalog/pg_variable.h
index b6716ca8cc..72c9f8a3ce 100644
--- a/src/include/catalog/pg_variable.h
+++ b/src/include/catalog/pg_variable.h
@@ -4,7 +4,7 @@
  *	  definition of session variables system catalog (pg_variables)
  *
  *
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * src/include/catalog/pg_variable.h
diff --git a/src/include/commands/session_variable.h b/src/include/commands/session_variable.h
index c8c3ab2570..7606235afc 100644
--- a/src/include/commands/session_variable.h
+++ b/src/include/commands/session_variable.h
@@ -4,7 +4,7 @@
  *	  prototypes for sessionvariable.c.
  *
  *
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * src/include/commands/session_variable.h
diff --git a/src/include/executor/svariableReceiver.h b/src/include/executor/svariableReceiver.h
index 5305936547..1d823a42a8 100644
--- a/src/include/executor/svariableReceiver.h
+++ b/src/include/executor/svariableReceiver.h
@@ -4,7 +4,7 @@
  *	  prototypes for svariableReceiver.c
  *
  *
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * src/include/executor/svariableReceiver.h
-- 
2.33.1

