]> granicus.if.org Git - postgresql/commitdiff
Fix failure to ignore leftover temp tables after a server crash.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Dec 2012 01:15:45 +0000 (20:15 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Dec 2012 01:15:45 +0000 (20:15 -0500)
During crash recovery, we remove disk files belonging to temporary tables,
but the system catalog entries for such tables are intentionally not
cleaned up right away.  Instead, the first backend that uses a temp schema
is expected to clean out any leftover objects therein.  This approach
requires that we be careful to ignore leftover temp tables (since any
actual access attempt would fail), *even if their BackendId matches our
session*, if we have not yet established use of the session's corresponding
temp schema.  That worked fine in the past, but was broken by commit
debcec7dc31a992703911a9953e299c8d730c778 which incorrectly removed the
rd_islocaltemp relcache flag.  Put it back, and undo various changes
that substituted tests like "rel->rd_backend == MyBackendId" for use
of a state-aware flag.  Per trouble report from Heikki Linnakangas.

Back-patch to 9.1 where the erroneous change was made.  In the back
branches, be careful to add rd_islocaltemp in a spot in the struct that
was alignment padding before, so as not to break existing add-on code.

src/backend/catalog/toasting.c
src/backend/commands/copy.c
src/backend/commands/sequence.c
src/backend/commands/tablecmds.c
src/backend/utils/adt/dbsize.c
src/backend/utils/cache/relcache.c
src/include/utils/rel.h

index f2b4b62837e8fa4be9f929c96ecdc4fc364b7e39..6ef33f94927a1d671184246336a1b6db8cefb958 100644 (file)
@@ -199,7 +199,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptio
         * Toast tables for regular relations go in pg_toast; those for temp
         * relations go into the per-backend temp-toast-table namespace.
         */
-       if (RelationUsesTempNamespace(rel))
+       if (isTempOrToastNamespace(rel->rd_rel->relnamespace))
                namespaceid = GetTempToastNamespace();
        else
                namespaceid = PG_TOAST_NAMESPACE;
index 11493bbc4d33cd1cdc1ed2e60b143e7b5e6128be..c6df89c09ea101cc27be7d109eac08c20a37ecdb 100644 (file)
@@ -790,7 +790,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
        if (is_from)
        {
                /* check read-only transaction */
-               if (XactReadOnly && rel->rd_backend != MyBackendId)
+               if (XactReadOnly && !rel->rd_islocaltemp)
                        PreventCommandIfReadOnly("COPY FROM");
 
                cstate = BeginCopyFrom(rel, stmt->filename,
index 2409e5d6a1ffb58071d36a71f9516b178a93fae7..47d76a3c86615c284773e9eb1ea7598f8f72dd5a 100644 (file)
@@ -538,7 +538,7 @@ nextval_internal(Oid relid)
                                                RelationGetRelationName(seqrel))));
 
        /* read-only transactions may only modify temp sequences */
-       if (seqrel->rd_backend != MyBackendId)
+       if (!seqrel->rd_islocaltemp)
                PreventCommandIfReadOnly("nextval()");
 
        if (elm->last != elm->cached)           /* some numbers were cached */
@@ -831,7 +831,7 @@ do_setval(Oid relid, int64 next, bool iscalled)
                                                RelationGetRelationName(seqrel))));
 
        /* read-only transactions may only modify temp sequences */
-       if (seqrel->rd_backend != MyBackendId)
+       if (!seqrel->rd_islocaltemp)
                PreventCommandIfReadOnly("setval()");
 
        /* lock page' buffer and read tuple */
index d7bb20fa2ec17aba06b61b24d31185fd06eecc0f..3a57cc701f82d3534a2641afc32de20e6335e39a 100644 (file)
@@ -1362,13 +1362,20 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
                                         errmsg("inherited relation \"%s\" is not a table",
                                                        parent->relname)));
                /* Permanent rels cannot inherit from temporary ones */
-               if (relpersistence != RELPERSISTENCE_TEMP
-                       && RelationUsesTempNamespace(relation))
+               if (relpersistence != RELPERSISTENCE_TEMP &&
+                       relation->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
                        ereport(ERROR,
                                        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                         errmsg("cannot inherit from temporary relation \"%s\"",
                                                        parent->relname)));
 
+               /* If existing rel is temp, it must belong to this session */
+               if (relation->rd_rel->relpersistence == RELPERSISTENCE_TEMP &&
+                       !relation->rd_islocaltemp)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                                        errmsg("cannot inherit from temporary relation of another session")));
+
                /*
                 * We should have an UNDER permission flag for this, but for now,
                 * demand that creator of a child table own the parent.
@@ -5504,6 +5511,10 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
                                ereport(ERROR,
                                                (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                                                 errmsg("constraints on temporary tables may reference only temporary tables")));
+                       if (!pkrel->rd_islocaltemp || !rel->rd_islocaltemp)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+                                                errmsg("constraints on temporary tables must involve temporary tables of this session")));
                        break;
        }
 
@@ -8146,13 +8157,27 @@ ATExecAddInherit(Relation child_rel, RangeVar *parent, LOCKMODE lockmode)
        ATSimplePermissions(parent_rel, ATT_TABLE);
 
        /* Permanent rels cannot inherit from temporary ones */
-       if (RelationUsesTempNamespace(parent_rel)
-               && !RelationUsesTempNamespace(child_rel))
+       if (parent_rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP &&
+               child_rel->rd_rel->relpersistence != RELPERSISTENCE_TEMP)
                ereport(ERROR,
                                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                 errmsg("cannot inherit from temporary relation \"%s\"",
                                                RelationGetRelationName(parent_rel))));
 
+       /* If parent rel is temp, it must belong to this session */
+       if (parent_rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP &&
+               !parent_rel->rd_islocaltemp)
+               ereport(ERROR,
+                               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                                errmsg("cannot inherit from temporary relation of another session")));
+
+       /* Ditto for the child */
+       if (child_rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP &&
+               !child_rel->rd_islocaltemp)
+               ereport(ERROR,
+                               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                                errmsg("cannot inherit to temporary relation of another session")));
+
        /*
         * Check for duplicates in the list of parents, and determine the highest
         * inhseqno already present; we'll use the next one for the new parent.
index 2ee59668cd7f3a43c01e7de0f910f4804ad8319a..7381f217251a9d98e2941277b829c396a1173838 100644 (file)
@@ -242,6 +242,9 @@ pg_tablespace_size_name(PG_FUNCTION_ARGS)
 
 /*
  * calculate size of (one fork of) a relation
+ *
+ * Note: we can safely apply this to temp tables of other sessions, so there
+ * is no check here or at the call sites for that.
  */
 static int64
 calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
index 12a02b8c09b22bb21af897609129fdbb5d6e0433..29ae3878ef3c3015c34a9de77eb56dc2b70d216f 100644 (file)
@@ -854,20 +854,33 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
                case RELPERSISTENCE_UNLOGGED:
                case RELPERSISTENCE_PERMANENT:
                        relation->rd_backend = InvalidBackendId;
+                       relation->rd_islocaltemp = false;
                        break;
                case RELPERSISTENCE_TEMP:
                        if (isTempOrToastNamespace(relation->rd_rel->relnamespace))
+                       {
                                relation->rd_backend = MyBackendId;
+                               relation->rd_islocaltemp = true;
+                       }
                        else
                        {
                                /*
-                                * If it's a local temp table, but not one of ours, we have to
-                                * use the slow, grotty method to figure out the owning
-                                * backend.
+                                * If it's a temp table, but not one of ours, we have to use
+                                * the slow, grotty method to figure out the owning backend.
+                                *
+                                * Note: it's possible that rd_backend gets set to MyBackendId
+                                * here, in case we are looking at a pg_class entry left over
+                                * from a crashed backend that coincidentally had the same
+                                * BackendId we're using.  We should *not* consider such a
+                                * table to be "ours"; this is why we need the separate
+                                * rd_islocaltemp flag.  The pg_class entry will get flushed
+                                * if/when we clean out the corresponding temp table namespace
+                                * in preparation for using it.
                                 */
                                relation->rd_backend =
                                        GetTempNamespaceBackendId(relation->rd_rel->relnamespace);
                                Assert(relation->rd_backend != InvalidBackendId);
+                               relation->rd_islocaltemp = false;
                        }
                        break;
                default:
@@ -1388,6 +1401,7 @@ formrdesc(const char *relationName, Oid relationReltype,
        relation->rd_createSubid = InvalidSubTransactionId;
        relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
        relation->rd_backend = InvalidBackendId;
+       relation->rd_islocaltemp = false;
 
        /*
         * initialize relation tuple form
@@ -2535,16 +2549,19 @@ RelationBuildLocalRelation(const char *relname,
        /* needed when bootstrapping: */
        rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;
 
-       /* set up persistence; rd_backend is a function of persistence type */
+       /* set up persistence and relcache fields dependent on it */
        rel->rd_rel->relpersistence = relpersistence;
        switch (relpersistence)
        {
                case RELPERSISTENCE_UNLOGGED:
                case RELPERSISTENCE_PERMANENT:
                        rel->rd_backend = InvalidBackendId;
+                       rel->rd_islocaltemp = false;
                        break;
                case RELPERSISTENCE_TEMP:
+                       Assert(isTempOrToastNamespace(relnamespace));
                        rel->rd_backend = MyBackendId;
+                       rel->rd_islocaltemp = true;
                        break;
                default:
                        elog(ERROR, "invalid relpersistence: %c", relpersistence);
index 57cdc3b94040b9196ce047d978e9209cee0da761..5eb9eaa8a8e4980d696d2009af440096749738db 100644 (file)
@@ -137,6 +137,7 @@ typedef struct RelationData
        bool            rd_isvalid;             /* relcache entry is valid */
        char            rd_indexvalid;  /* state of rd_indexlist: 0 = not valid, 1 =
                                                                 * valid, 2 = temporarily forced */
+       bool            rd_islocaltemp; /* rel is a temp rel of this session */
 
        /*
         * rd_createSubid is the ID of the highest subtransaction the rel has
@@ -405,22 +406,16 @@ typedef struct StdRdOptions
 #define RelationUsesLocalBuffers(relation) \
        ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
 
-/*
- * RelationUsesTempNamespace
- *             True if relation's catalog entries live in a private namespace.
- */
-#define RelationUsesTempNamespace(relation) \
-       ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
-
 /*
  * RELATION_IS_LOCAL
  *             If a rel is either temp or newly created in the current transaction,
- *             it can be assumed to be visible only to the current backend.
+ *             it can be assumed to be accessible only to the current backend.
+ *             This is typically used to decide that we can skip acquiring locks.
  *
  * Beware of multiple eval of argument
  */
 #define RELATION_IS_LOCAL(relation) \
-       ((relation)->rd_backend == MyBackendId || \
+       ((relation)->rd_islocaltemp || \
         (relation)->rd_createSubid != InvalidSubTransactionId)
 
 /*
@@ -430,8 +425,8 @@ typedef struct StdRdOptions
  * Beware of multiple eval of argument
  */
 #define RELATION_IS_OTHER_TEMP(relation) \
-       ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP \
-       && (relation)->rd_backend != MyBackendId)
+       ((relation)->rd_rel->relpersistence == RELPERSISTENCE_TEMP && \
+        !(relation)->rd_islocaltemp)
 
 /* routines in utils/cache/relcache.c */
 extern void RelationIncrementReferenceCount(Relation rel);