]> granicus.if.org Git - postgresql/commitdiff
Combine options for RangeVarGetRelidExtended() into a flags argument.
authorAndres Freund <andres@anarazel.de>
Fri, 30 Mar 2018 23:33:42 +0000 (16:33 -0700)
committerAndres Freund <andres@anarazel.de>
Sat, 31 Mar 2018 00:05:16 +0000 (17:05 -0700)
A followup patch will add a SKIP_LOCKED option. To avoid introducing
evermore arguments, breaking existing callers each time, introduce a
flags argument. This'll no doubt break a few external users...

Also change the MISSING_OK behaviour so a DEBUG1 debug message is
emitted when a relation is not found.

Author: Nathan Bossart
Reviewed-By: Michael Paquier and Andres Freund
Discussion: https://postgr.es/m/20180306005349.b65whmvj7z6hbe2y@alap3.anarazel.de

12 files changed:
src/backend/catalog/namespace.c
src/backend/commands/cluster.c
src/backend/commands/indexcmds.c
src/backend/commands/lockcmds.c
src/backend/commands/matview.c
src/backend/commands/policy.c
src/backend/commands/sequence.c
src/backend/commands/tablecmds.c
src/backend/commands/trigger.c
src/backend/rewrite/rewriteDefine.c
src/backend/tcop/utility.c
src/include/catalog/namespace.h

index 52dd400b9680e31e9f9468ab40def2ba12dfc23b..687d31e083ba511b084374493211fd4ea4e56836 100644 (file)
@@ -206,23 +206,25 @@ static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
  *             Given a RangeVar describing an existing relation,
  *             select the proper namespace and look up the relation OID.
  *
- * If the schema or relation is not found, return InvalidOid if missing_ok
- * = true, otherwise raise an error.
+ * If the schema or relation is not found, return InvalidOid if flags contains
+ * RVR_MISSING_OK, otherwise raise an error.
  *
- * If nowait = true, throw an error if we'd have to wait for a lock.
+ * If flags contains RVR_NOWAIT, throw an error if we'd have to wait for a
+ * lock.
  *
  * Callback allows caller to check permissions or acquire additional locks
  * prior to grabbing the relation lock.
  */
 Oid
 RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
-                                                bool missing_ok, bool nowait,
+                                                uint32 flags,
                                                 RangeVarGetRelidCallback callback, void *callback_arg)
 {
        uint64          inval_count;
        Oid                     relId;
        Oid                     oldRelId = InvalidOid;
        bool            retry = false;
+       bool            missing_ok = (flags & RVR_MISSING_OK) != 0;
 
        /*
         * We check the catalog name and then ignore it.
@@ -361,7 +363,7 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
                 */
                if (!OidIsValid(relId))
                        AcceptInvalidationMessages();
-               else if (!nowait)
+               else if (!(flags & RVR_NOWAIT))
                        LockRelationOid(relId, lockmode);
                else if (!ConditionalLockRelationOid(relId, lockmode))
                {
@@ -392,15 +394,17 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
                oldRelId = relId;
        }
 
-       if (!OidIsValid(relId) && !missing_ok)
+       if (!OidIsValid(relId))
        {
+               int                     elevel = missing_ok ? DEBUG1 : ERROR;
+
                if (relation->schemaname)
-                       ereport(ERROR,
+                       ereport(elevel,
                                        (errcode(ERRCODE_UNDEFINED_TABLE),
                                         errmsg("relation \"%s.%s\" does not exist",
                                                        relation->schemaname, relation->relname)));
                else
-                       ereport(ERROR,
+                       ereport(elevel,
                                        (errcode(ERRCODE_UNDEFINED_TABLE),
                                         errmsg("relation \"%s\" does not exist",
                                                        relation->relname)));
index 0f844c00c83e66e067059385a12d910859769b05..d088dc11a67363d81c493572de2d3b1e8127be71 100644 (file)
@@ -115,7 +115,7 @@ cluster(ClusterStmt *stmt, bool isTopLevel)
                /* Find, lock, and check permissions on the table */
                tableOid = RangeVarGetRelidExtended(stmt->relation,
                                                                                        AccessExclusiveLock,
-                                                                                       false, false,
+                                                                                       0,
                                                                                        RangeVarCallbackOwnsTable, NULL);
                rel = heap_open(tableOid, NoLock);
 
index 01859707940917521900fb0d3eeab547a7a87493..e224b91f53bf7e2e5951949b7752df1758074a9b 100644 (file)
@@ -2091,7 +2091,7 @@ ReindexIndex(RangeVar *indexRelation, int options)
         * used here must match the index lock obtained in reindex_index().
         */
        indOid = RangeVarGetRelidExtended(indexRelation, AccessExclusiveLock,
-                                                                         false, false,
+                                                                         0,
                                                                          RangeVarCallbackForReindexIndex,
                                                                          (void *) &heapOid);
 
@@ -2183,7 +2183,7 @@ ReindexTable(RangeVar *relation, int options)
        Oid                     heapOid;
 
        /* The lock level used here should match reindex_relation(). */
-       heapOid = RangeVarGetRelidExtended(relation, ShareLock, false, false,
+       heapOid = RangeVarGetRelidExtended(relation, ShareLock, 0,
                                                                           RangeVarCallbackOwnsTable, NULL);
 
        if (!reindex_relation(heapOid,
index 7f25d7498a414c453a434ff9fe0f22f0016b5fce..1dbb35f6315d574b84c156cf8f39383c5da29d73 100644 (file)
@@ -61,8 +61,8 @@ LockTableCommand(LockStmt *lockstmt)
                bool            recurse = rv->inh;
                Oid                     reloid;
 
-               reloid = RangeVarGetRelidExtended(rv, lockstmt->mode, false,
-                                                                                 lockstmt->nowait,
+               reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
+                                                                                 lockstmt->nowait ? RVR_NOWAIT : 0,
                                                                                  RangeVarCallbackForLockTable,
                                                                                  (void *) &lockstmt->mode);
 
index 23892b1b81d39f55a8f8527d97261aa0aee6de7d..410d4e5a3808f7204dc7f6c0e3b36a704d3bb3b5 100644 (file)
@@ -161,7 +161,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
         * Get a lock until end of transaction.
         */
        matviewOid = RangeVarGetRelidExtended(stmt->relation,
-                                                                                 lockmode, false, false,
+                                                                                 lockmode, 0,
                                                                                  RangeVarCallbackOwnsTable, NULL);
        matviewRel = heap_open(matviewOid, NoLock);
 
index cfaf32ccbd7bebc14e555160db6a516bd8ec3160..00841b3b8a0cdb3808ad31b9f3e65d409390804d 100644 (file)
@@ -743,7 +743,7 @@ CreatePolicy(CreatePolicyStmt *stmt)
 
        /* Get id of table.  Also handles permissions checks. */
        table_id = RangeVarGetRelidExtended(stmt->table, AccessExclusiveLock,
-                                                                               false, false,
+                                                                               0,
                                                                                RangeVarCallbackForPolicy,
                                                                                (void *) stmt);
 
@@ -915,7 +915,7 @@ AlterPolicy(AlterPolicyStmt *stmt)
 
        /* Get id of table.  Also handles permissions checks. */
        table_id = RangeVarGetRelidExtended(stmt->table, AccessExclusiveLock,
-                                                                               false, false,
+                                                                               0,
                                                                                RangeVarCallbackForPolicy,
                                                                                (void *) stmt);
 
@@ -1215,7 +1215,7 @@ rename_policy(RenameStmt *stmt)
 
        /* Get id of table.  Also handles permissions checks. */
        table_id = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                               false, false,
+                                                                               0,
                                                                                RangeVarCallbackForPolicy,
                                                                                (void *) stmt);
 
index dcc0aa536a0cd8f90ad3364e58b134981a90cc2e..89122d4ad7560a72e2142c893bdbe5c4df699bd8 100644 (file)
@@ -432,8 +432,7 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
        /* Open and lock sequence, and check for ownership along the way. */
        relid = RangeVarGetRelidExtended(stmt->sequence,
                                                                         ShareRowExclusiveLock,
-                                                                        stmt->missing_ok,
-                                                                        false,
+                                                                        stmt->missing_ok ? RVR_MISSING_OK : 0,
                                                                         RangeVarCallbackOwnsRelation,
                                                                         NULL);
        if (relid == InvalidOid)
index 83a881eff38d241c51952662ae8f54ff69fe5fd2..c8da82217d4f7d9cea7572a3652f388b287278fd 100644 (file)
@@ -1159,8 +1159,7 @@ RemoveRelations(DropStmt *drop)
                state.heapOid = InvalidOid;
                state.partParentOid = InvalidOid;
                state.concurrent = drop->concurrent;
-               relOid = RangeVarGetRelidExtended(rel, lockmode, true,
-                                                                                 false,
+               relOid = RangeVarGetRelidExtended(rel, lockmode, RVR_MISSING_OK,
                                                                                  RangeVarCallbackForDropRelation,
                                                                                  (void *) &state);
 
@@ -2793,7 +2792,7 @@ renameatt(RenameStmt *stmt)
 
        /* lock level taken here should match renameatt_internal */
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        stmt->missing_ok, false,
+                                                                        stmt->missing_ok ? RVR_MISSING_OK : 0,
                                                                         RangeVarCallbackForRenameAttribute,
                                                                         NULL);
 
@@ -2945,7 +2944,7 @@ RenameConstraint(RenameStmt *stmt)
        {
                /* lock level taken here should match rename_constraint_internal */
                relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                                stmt->missing_ok, false,
+                                                                                stmt->missing_ok ? RVR_MISSING_OK : 0,
                                                                                 RangeVarCallbackForRenameAttribute,
                                                                                 NULL);
                if (!OidIsValid(relid))
@@ -2987,7 +2986,7 @@ RenameRelation(RenameStmt *stmt)
         * escalation.
         */
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        stmt->missing_ok, false,
+                                                                        stmt->missing_ok ? RVR_MISSING_OK : 0,
                                                                         RangeVarCallbackForAlterRelation,
                                                                         (void *) stmt);
 
@@ -3146,7 +3145,8 @@ CheckTableNotInUse(Relation rel, const char *stmt)
 Oid
 AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode)
 {
-       return RangeVarGetRelidExtended(stmt->relation, lockmode, stmt->missing_ok, false,
+       return RangeVarGetRelidExtended(stmt->relation, lockmode,
+                                                                       stmt->missing_ok ? RVR_MISSING_OK : 0,
                                                                        RangeVarCallbackForAlterRelation,
                                                                        (void *) stmt);
 }
@@ -12683,7 +12683,7 @@ AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
        ObjectAddress myself;
 
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        stmt->missing_ok, false,
+                                                                        stmt->missing_ok ? RVR_MISSING_OK : 0,
                                                                         RangeVarCallbackForAlterRelation,
                                                                         (void *) stmt);
 
@@ -14613,7 +14613,7 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name)
        state.parentTblOid = parentIdx->rd_index->indrelid;
        state.lockedParentTbl = false;
        partIdxId =
-               RangeVarGetRelidExtended(name, AccessExclusiveLock, false, false,
+               RangeVarGetRelidExtended(name, AccessExclusiveLock, 0,
                                                                 RangeVarCallbackForAttachIndex,
                                                                 (void *) &state);
        /* Not there? */
index 9d8df5986ecafe2cfb294991c25df30d9724be7e..a6593f939cab987311667bd997cd97777fce41c5 100644 (file)
@@ -1667,7 +1667,7 @@ renametrig(RenameStmt *stmt)
         * release until end of transaction).
         */
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        false, false,
+                                                                        0,
                                                                         RangeVarCallbackForRenameTrigger,
                                                                         NULL);
 
index 679be605f14bf25b34e0b877093db0743ba6404a..d81a2ea342b75c44c813cffbff531f0c4e449fd0 100644 (file)
@@ -951,7 +951,7 @@ RenameRewriteRule(RangeVar *relation, const char *oldName,
         * release until end of transaction).
         */
        relid = RangeVarGetRelidExtended(relation, AccessExclusiveLock,
-                                                                        false, false,
+                                                                        0,
                                                                         RangeVarCallbackForRenameRule,
                                                                         NULL);
 
index d355bef6060092fc30b31c7f8f2858fa306f0157..b2dc9d18eac5413f2688fc1b205eca24f5671b56 100644 (file)
@@ -1305,7 +1305,7 @@ ProcessUtilitySlow(ParseState *pstate,
                                                : ShareLock;
                                        relid =
                                                RangeVarGetRelidExtended(stmt->relation, lockmode,
-                                                                                                false, false,
+                                                                                                0,
                                                                                                 RangeVarCallbackOwnsRelation,
                                                                                                 NULL);
 
index 5f8cf4992ea68dd0f3a02471869a55065fec7dd4..0bccf910a9ec50dbb726097d2682a384de6ecf4d 100644 (file)
@@ -47,14 +47,24 @@ typedef struct OverrideSearchPath
        bool            addTemp;                /* implicitly prepend temp schema? */
 } OverrideSearchPath;
 
+/*
+ * Option flag bits for RangeVarGetRelidExtended().
+ */
+typedef enum RVROption
+{
+       RVR_MISSING_OK = 1 << 0,        /* don't error if relation doesn't exist */
+       RVR_NOWAIT = 1 << 1                     /* error if relation cannot be locked */
+} RVROption;
+
 typedef void (*RangeVarGetRelidCallback) (const RangeVar *relation, Oid relId,
                                                                                  Oid oldRelId, void *callback_arg);
 
 #define RangeVarGetRelid(relation, lockmode, missing_ok) \
-       RangeVarGetRelidExtended(relation, lockmode, missing_ok, false, NULL, NULL)
+       RangeVarGetRelidExtended(relation, lockmode, \
+                                                        (missing_ok) ? RVR_MISSING_OK : 0, NULL, NULL)
 
 extern Oid RangeVarGetRelidExtended(const RangeVar *relation,
-                                                LOCKMODE lockmode, bool missing_ok, bool nowait,
+                                                LOCKMODE lockmode, uint32 flags,
                                                 RangeVarGetRelidCallback callback,
                                                 void *callback_arg);
 extern Oid     RangeVarGetCreationNamespace(const RangeVar *newRelation);