Restrict the use of temporary namespace in two-phase transactions
authorMichael Paquier
Fri, 18 Jan 2019 00:21:58 +0000 (09:21 +0900)
committerMichael Paquier
Fri, 18 Jan 2019 00:21:58 +0000 (09:21 +0900)
Attempting to use a temporary table within a two-phase transaction is
forbidden for ages.  However, there have been uncovered grounds for
a couple of other object types and commands which work on temporary
objects with two-phase commit.  In short, trying to create, lock or drop
an object on a temporary schema should not be authorized within a
two-phase transaction, as it would cause its state to create
dependencies with other sessions, causing all sorts of side effects with
the existing session or other sessions spawned later on trying to use
the same temporary schema name.

Regression tests are added to cover all the grounds found, the original
report mentioned function creation, but monitoring closer there are many
other patterns with LOCK, DROP or CREATE EXTENSION which are involved.
One of the symptoms resulting in combining both is that the session
which used the temporary schema is not able to shut down completely,
waiting for being able to drop the temporary schema, something that it
cannot complete because of the two-phase transaction involved with
temporary objects.  In this case the client is able to disconnect but
the session remains alive on the backend-side, potentially blocking
connection backend slots from being used.  Other problems reported could
also involve server crashes.

This is back-patched down to v10, which is where 9b013dc has introduced
MyXactFlags, something that this patch relies on.

Reported-by: Alexey Bashtanov
Author: Michael Paquier
Reviewed-by: Masahiko Sawada
Discussion: https://postgr.es/m/5d910e2e-0db8-ec06-dd5f-baec420513c3@imap.cc
Backpatch-through: 10

doc/src/sgml/ref/prepare_transaction.sgml
src/backend/access/transam/xact.c
src/backend/catalog/namespace.c
src/backend/commands/dropcmds.c
src/backend/commands/extension.c
src/backend/commands/lockcmds.c
src/include/access/xact.h
src/test/modules/test_extensions/expected/test_extensions.out
src/test/modules/test_extensions/sql/test_extensions.sql
src/test/regress/expected/temp.out
src/test/regress/sql/temp.sql

index 50a93b45dd3d4759e8beaff0ab7e2e386d9c9ce4..0794bc531a90e129a108004b5febf6a727e5425b 100644 (file)
@@ -98,8 +98,8 @@ PREPARE TRANSACTION transaction_id
 
   
    It is not currently allowed to PREPARE a transaction that
-   has executed any operations involving temporary tables,
-   created any cursors WITH HOLD, or executed
+   has executed any operations involving temporary tables or the session's
+   temporary namespace, created any cursors WITH HOLD, or executed
    LISTEN, UNLISTEN, or
    NOTIFY.
    Those features are too tightly
index 74d95057469c6c8a041b2b38697f3814a34baf02..f02df7cf01189751d14f97b2bf8ea544b6e03cd0 100644 (file)
@@ -2296,6 +2296,18 @@ PrepareTransaction(void)
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
 
+   /*
+    * Similarly, PREPARE TRANSACTION is not allowed if the temporary
+    * namespace has been involved in this transaction as we cannot allow it
+    * to create, lock, or even drop objects within the temporary namespace
+    * as this can mess up with this session or even a follow-up session
+    * trying to use the same temporary namespace.
+    */
+   if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPNAMESPACE))
+       ereport(ERROR,
+               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                errmsg("cannot PREPARE a transaction that has operated on temporary namespace")));
+
    /*
     * Likewise, don't allow PREPARE after pg_export_snapshot.  This could be
     * supported if we added cleanup logic to twophase.c, but for now it
index 63859b5e76aaf5263e6d2cc8417abb9591c64431..5a737c8ab3e6dd8e594dbf9eb0d9157086768982 100644 (file)
@@ -193,6 +193,7 @@ char       *namespace_search_path = NULL;
 
 /* Local functions */
 static void recomputeNamespacePath(void);
+static void AccessTempTableNamespace(bool force);
 static void InitTempTableNamespace(void);
 static void RemoveTempRelations(Oid tempNamespaceId);
 static void RemoveTempRelationsCallback(int code, Datum arg);
@@ -440,9 +441,8 @@ RangeVarGetCreationNamespace(const RangeVar *newRelation)
        /* check for pg_temp alias */
        if (strcmp(newRelation->schemaname, "pg_temp") == 0)
        {
-           /* Initialize temp namespace if first time through */
-           if (!OidIsValid(myTempNamespace))
-               InitTempTableNamespace();
+           /* Initialize temp namespace */
+           AccessTempTableNamespace(false);
            return myTempNamespace;
        }
        /* use exact schema given */
@@ -451,9 +451,8 @@ RangeVarGetCreationNamespace(const RangeVar *newRelation)
    }
    else if (newRelation->relpersistence == RELPERSISTENCE_TEMP)
    {
-       /* Initialize temp namespace if first time through */
-       if (!OidIsValid(myTempNamespace))
-           InitTempTableNamespace();
+       /* Initialize temp namespace */
+       AccessTempTableNamespace(false);
        return myTempNamespace;
    }
    else
@@ -463,7 +462,7 @@ RangeVarGetCreationNamespace(const RangeVar *newRelation)
        if (activeTempCreationPending)
        {
            /* Need to initialize temp namespace */
-           InitTempTableNamespace();
+           AccessTempTableNamespace(true);
            return myTempNamespace;
        }
        namespaceId = activeCreationNamespace;
@@ -2901,9 +2900,8 @@ LookupCreationNamespace(const char *nspname)
    /* check for pg_temp alias */
    if (strcmp(nspname, "pg_temp") == 0)
    {
-       /* Initialize temp namespace if first time through */
-       if (!OidIsValid(myTempNamespace))
-           InitTempTableNamespace();
+       /* Initialize temp namespace */
+       AccessTempTableNamespace(false);
        return myTempNamespace;
    }
 
@@ -2966,9 +2964,8 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
        /* check for pg_temp alias */
        if (strcmp(schemaname, "pg_temp") == 0)
        {
-           /* Initialize temp namespace if first time through */
-           if (!OidIsValid(myTempNamespace))
-               InitTempTableNamespace();
+           /* Initialize temp namespace */
+           AccessTempTableNamespace(false);
            return myTempNamespace;
        }
        /* use exact schema given */
@@ -2982,7 +2979,7 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
        if (activeTempCreationPending)
        {
            /* Need to initialize temp namespace */
-           InitTempTableNamespace();
+           AccessTempTableNamespace(true);
            return myTempNamespace;
        }
        namespaceId = activeCreationNamespace;
@@ -3771,6 +3768,38 @@ recomputeNamespacePath(void)
    list_free(oidlist);
 }
 
+/*
+ * AccessTempTableNamespace
+ *     Provide access to a temporary namespace, potentially creating it
+ *     if not present yet.  This routine registers if the namespace gets
+ *     in use in this transaction.  'force' can be set to true to allow
+ *     the caller to enforce the creation of the temporary namespace for
+ *     use in this backend, which happens if its creation is pending.
+ */
+static void
+AccessTempTableNamespace(bool force)
+{
+   /*
+    * Make note that this temporary namespace has been accessed in this
+    * transaction.
+    */
+   MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
+
+   /*
+    * If the caller attempting to access a temporary schema expects the
+    * creation of the namespace to be pending and should be enforced, then go
+    * through the creation.
+    */
+   if (!force && OidIsValid(myTempNamespace))
+       return;
+
+   /*
+    * The temporary tablespace does not exist yet and is wanted, so
+    * initialize it.
+    */
+   InitTempTableNamespace();
+}
+
 /*
  * InitTempTableNamespace
  *     Initialize temp table namespace on first use in a particular backend
@@ -4199,7 +4228,7 @@ fetch_search_path(bool includeImplicit)
     */
    if (activeTempCreationPending)
    {
-       InitTempTableNamespace();
+       AccessTempTableNamespace(true);
        recomputeNamespacePath();
    }
 
index 2b30677d6f917bf5db9b3b2d0a6dc054ae327e9c..85f5e483532dec7fc72f7b79210ed7f31d76535f 100644 (file)
@@ -14,6 +14,7 @@
  */
 #include "postgres.h"
 
+#include "access/xact.h"
 #include "access/heapam.h"
 #include "access/htup_details.h"
 #include "catalog/dependency.h"
@@ -115,6 +116,13 @@ RemoveObjects(DropStmt *stmt)
            check_object_ownership(GetUserId(), stmt->removeType, address,
                                   object, relation);
 
+       /*
+        * Make note if a temporary namespace has been accessed in this
+        * transaction.
+        */
+       if (OidIsValid(namespaceId) && isTempNamespace(namespaceId))
+           MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
+
        /* Release any relcache reference count, but keep lock until commit. */
        if (relation)
            heap_close(relation, NoLock);
index e4340eed8c4b9b8180deb83741224dd15552a6e3..a52b6861b47bf841c5da402be0c0332db2e52f7d 100644 (file)
@@ -1474,6 +1474,13 @@ CreateExtensionInternal(char *extensionName,
        list_free(search_path);
    }
 
+   /*
+    * Make note if a temporary namespace has been accessed in this
+    * transaction.
+    */
+   if (isTempNamespace(schemaOid))
+       MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
+
    /*
     * We don't check creation rights on the target namespace here.  If the
     * extension script actually creates any objects there, it will fail if
index 9fe9e022b0f421ade71a62e5e242a4a091e5bed6..bb7d9e8239e350291519b0f07fb35cdd146463a7 100644 (file)
@@ -14,6 +14,7 @@
  */
 #include "postgres.h"
 
+#include "access/xact.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_inherits_fn.h"
 #include "commands/lockcmds.h"
@@ -77,6 +78,7 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
 {
    LOCKMODE    lockmode = *(LOCKMODE *) arg;
    char        relkind;
+   char        relpersistence;
    AclResult   aclresult;
 
    if (!OidIsValid(relid))
@@ -93,6 +95,14 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
                 errmsg("\"%s\" is not a table",
                        rv->relname)));
 
+   /*
+    * Make note if a temporary relation has been accessed in this
+    * transaction.
+    */
+   relpersistence = get_rel_persistence(relid);
+   if (relpersistence == RELPERSISTENCE_TEMP)
+       MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+
    /* Check permissions. */
    aclresult = LockTableAclCheck(relid, lockmode);
    if (aclresult != ACLCHECK_OK)
index 905f42c2b1d634049d96e65c2776340e579f2992..9144a69cb122de7bc0cd4164622a04597e5cfc9a 100644 (file)
@@ -91,6 +91,11 @@ extern int   MyXactFlags;
  */
 #define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1)
 
+/*
+ * XACT_FLAGS_ACCESSEDTEMPNAMESPACE - set when a temporary namespace is
+ * accessed.  We don't allow PREPARE TRANSACTION in that case.
+ */
+#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE       (1U << 2)
 
 /*
  * start- and end-of-transaction callbacks for dynamically loaded modules
index 28d86c4b87f6c9b9eae2e6520ccf98589809ef52..1eec5a37d33c8f4eedbd2681e2f01fd707880988 100644 (file)
@@ -121,3 +121,36 @@ Objects in extension "test_ext8"
 
 -- dropping it should still work
 drop extension test_ext8;
+-- Test creation of extension in temporary schema with two-phase commit,
+-- which should not work.  This function wrapper is useful for portability.
+-- Avoid noise caused by CONTEXT and NOTICE messages including the temporary
+-- schema name.
+\set SHOW_CONTEXT never
+SET client_min_messages TO 'warning';
+-- First enforce presence of temporary schema.
+CREATE TEMP TABLE test_ext4_tab ();
+CREATE OR REPLACE FUNCTION create_extension_with_temp_schema()
+  RETURNS VOID AS $$
+  DECLARE
+    tmpschema text;
+    query text;
+  BEGIN
+    SELECT INTO tmpschema pg_my_temp_schema()::regnamespace;
+    query := 'CREATE EXTENSION test_ext4 SCHEMA ' || tmpschema || ' CASCADE;';
+    RAISE NOTICE 'query %', query;
+    EXECUTE query;
+  END; $$ LANGUAGE plpgsql;
+BEGIN;
+SELECT create_extension_with_temp_schema();
+ create_extension_with_temp_schema 
+-----------------------------------
+(1 row)
+
+PREPARE TRANSACTION 'twophase_extension';
+ERROR:  cannot PREPARE a transaction that has operated on temporary namespace
+-- Clean up
+DROP TABLE test_ext4_tab;
+DROP FUNCTION create_extension_with_temp_schema();
+RESET client_min_messages;
+\unset SHOW_CONTEXT
index 9e64503eb5035f7f0f7f11c4218887354b3de273..f505466ab4ebbf00034c8b6836f85eee8c90a4b2 100644 (file)
@@ -64,3 +64,32 @@ end';
 
 -- dropping it should still work
 drop extension test_ext8;
+
+-- Test creation of extension in temporary schema with two-phase commit,
+-- which should not work.  This function wrapper is useful for portability.
+
+-- Avoid noise caused by CONTEXT and NOTICE messages including the temporary
+-- schema name.
+\set SHOW_CONTEXT never
+SET client_min_messages TO 'warning';
+-- First enforce presence of temporary schema.
+CREATE TEMP TABLE test_ext4_tab ();
+CREATE OR REPLACE FUNCTION create_extension_with_temp_schema()
+  RETURNS VOID AS $$
+  DECLARE
+    tmpschema text;
+    query text;
+  BEGIN
+    SELECT INTO tmpschema pg_my_temp_schema()::regnamespace;
+    query := 'CREATE EXTENSION test_ext4 SCHEMA ' || tmpschema || ' CASCADE;';
+    RAISE NOTICE 'query %', query;
+    EXECUTE query;
+  END; $$ LANGUAGE plpgsql;
+BEGIN;
+SELECT create_extension_with_temp_schema();
+PREPARE TRANSACTION 'twophase_extension';
+-- Clean up
+DROP TABLE test_ext4_tab;
+DROP FUNCTION create_extension_with_temp_schema();
+RESET client_min_messages;
+\unset SHOW_CONTEXT
index f018f17ca0a8e702a4f3f4e22e3c8405fa62187c..e677d6b86fcd48b5c2fc67aee1b5a7da5bc6972e 100644 (file)
@@ -301,3 +301,74 @@ select relname from pg_class where relname like 'temp_inh_oncommit_test%';
 (1 row)
 
 drop table temp_inh_oncommit_test;
+-- Tests with two-phase commit
+-- Transactions creating objects in a temporary namespace cannot be used
+-- with two-phase commit.
+-- These cases generate errors about temporary namespace.
+-- Function creation
+begin;
+create function pg_temp.twophase_func() returns text as
+  $$ select '2pc_func'::text $$ language sql;
+prepare transaction 'twophase_func';
+ERROR:  cannot PREPARE a transaction that has operated on temporary namespace
+-- Function drop
+create function pg_temp.twophase_func() returns text as
+  $$ select '2pc_func'::text $$ language sql;
+begin;
+drop function pg_temp.twophase_func();
+prepare transaction 'twophase_func';
+ERROR:  cannot PREPARE a transaction that has operated on temporary namespace
+-- Operator creation
+begin;
+create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
+prepare transaction 'twophase_operator';
+ERROR:  cannot PREPARE a transaction that has operated on temporary namespace
+-- These generate errors about temporary tables.
+begin;
+create type pg_temp.twophase_type as (a int);
+prepare transaction 'twophase_type';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+begin;
+create view pg_temp.twophase_view as select 1;
+prepare transaction 'twophase_view';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+begin;
+create sequence pg_temp.twophase_seq;
+prepare transaction 'twophase_sequence';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+-- Temporary tables cannot be used with two-phase commit.
+create temp table twophase_tab (a int);
+begin;
+select a from twophase_tab;
+ a 
+---
+(0 rows)
+
+prepare transaction 'twophase_tab';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+begin;
+insert into twophase_tab values (1);
+prepare transaction 'twophase_tab';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+begin;
+lock twophase_tab in access exclusive mode;
+prepare transaction 'twophase_tab';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+begin;
+drop table twophase_tab;
+prepare transaction 'twophase_tab';
+ERROR:  cannot PREPARE a transaction that has operated on temporary tables
+-- Corner case: current_schema may create a temporary schema if namespace
+-- creation is pending, so check after that.  First reset the connection
+-- to remove the temporary namespace.
+\c -
+SET search_path TO 'pg_temp';
+BEGIN;
+SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
+ is_temp_schema 
+----------------
+ t
+(1 row)
+
+PREPARE TRANSACTION 'twophase_search';
+ERROR:  cannot PREPARE a transaction that has operated on temporary namespace
index 1beccc6cebb0f6828a6b75898e99a40ed9bdc681..df7edd9410392af8ef472d1b3df9eef046da00cd 100644 (file)
@@ -224,3 +224,59 @@ select * from temp_inh_oncommit_test;
 -- one relation remains
 select relname from pg_class where relname like 'temp_inh_oncommit_test%';
 drop table temp_inh_oncommit_test;
+
+-- Tests with two-phase commit
+-- Transactions creating objects in a temporary namespace cannot be used
+-- with two-phase commit.
+
+-- These cases generate errors about temporary namespace.
+-- Function creation
+begin;
+create function pg_temp.twophase_func() returns text as
+  $$ select '2pc_func'::text $$ language sql;
+prepare transaction 'twophase_func';
+-- Function drop
+create function pg_temp.twophase_func() returns text as
+  $$ select '2pc_func'::text $$ language sql;
+begin;
+drop function pg_temp.twophase_func();
+prepare transaction 'twophase_func';
+-- Operator creation
+begin;
+create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
+prepare transaction 'twophase_operator';
+
+-- These generate errors about temporary tables.
+begin;
+create type pg_temp.twophase_type as (a int);
+prepare transaction 'twophase_type';
+begin;
+create view pg_temp.twophase_view as select 1;
+prepare transaction 'twophase_view';
+begin;
+create sequence pg_temp.twophase_seq;
+prepare transaction 'twophase_sequence';
+
+-- Temporary tables cannot be used with two-phase commit.
+create temp table twophase_tab (a int);
+begin;
+select a from twophase_tab;
+prepare transaction 'twophase_tab';
+begin;
+insert into twophase_tab values (1);
+prepare transaction 'twophase_tab';
+begin;
+lock twophase_tab in access exclusive mode;
+prepare transaction 'twophase_tab';
+begin;
+drop table twophase_tab;
+prepare transaction 'twophase_tab';
+
+-- Corner case: current_schema may create a temporary schema if namespace
+-- creation is pending, so check after that.  First reset the connection
+-- to remove the temporary namespace.
+\c -
+SET search_path TO 'pg_temp';
+BEGIN;
+SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
+PREPARE TRANSACTION 'twophase_search';