Skip to content

Commit d87510a

Browse files
committed
Combine options for RangeVarGetRelidExtended() into a flags argument.
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
1 parent 9a89546 commit d87510a

File tree

12 files changed

+45
-32
lines changed

12 files changed

+45
-32
lines changed

src/backend/catalog/namespace.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,25 @@ static bool MatchNamedCall(HeapTuple proctup, int nargs, List *argnames,
206206
* Given a RangeVar describing an existing relation,
207207
* select the proper namespace and look up the relation OID.
208208
*
209-
* If the schema or relation is not found, return InvalidOid if missing_ok
210-
* = true, otherwise raise an error.
209+
* If the schema or relation is not found, return InvalidOid if flags contains
210+
* RVR_MISSING_OK, otherwise raise an error.
211211
*
212-
* If nowait = true, throw an error if we'd have to wait for a lock.
212+
* If flags contains RVR_NOWAIT, throw an error if we'd have to wait for a
213+
* lock.
213214
*
214215
* Callback allows caller to check permissions or acquire additional locks
215216
* prior to grabbing the relation lock.
216217
*/
217218
Oid
218219
RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
219-
bool missing_ok, bool nowait,
220+
uint32 flags,
220221
RangeVarGetRelidCallback callback, void *callback_arg)
221222
{
222223
uint64 inval_count;
223224
Oid relId;
224225
Oid oldRelId = InvalidOid;
225226
bool retry = false;
227+
bool missing_ok = (flags & RVR_MISSING_OK) != 0;
226228

227229
/*
228230
* We check the catalog name and then ignore it.
@@ -361,7 +363,7 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
361363
*/
362364
if (!OidIsValid(relId))
363365
AcceptInvalidationMessages();
364-
else if (!nowait)
366+
else if (!(flags & RVR_NOWAIT))
365367
LockRelationOid(relId, lockmode);
366368
else if (!ConditionalLockRelationOid(relId, lockmode))
367369
{
@@ -392,15 +394,17 @@ RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode,
392394
oldRelId = relId;
393395
}
394396

395-
if (!OidIsValid(relId) && !missing_ok)
397+
if (!OidIsValid(relId))
396398
{
399+
int elevel = missing_ok ? DEBUG1 : ERROR;
400+
397401
if (relation->schemaname)
398-
ereport(ERROR,
402+
ereport(elevel,
399403
(errcode(ERRCODE_UNDEFINED_TABLE),
400404
errmsg("relation \"%s.%s\" does not exist",
401405
relation->schemaname, relation->relname)));
402406
else
403-
ereport(ERROR,
407+
ereport(elevel,
404408
(errcode(ERRCODE_UNDEFINED_TABLE),
405409
errmsg("relation \"%s\" does not exist",
406410
relation->relname)));

src/backend/commands/cluster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ cluster(ClusterStmt *stmt, bool isTopLevel)
115115
/* Find, lock, and check permissions on the table */
116116
tableOid = RangeVarGetRelidExtended(stmt->relation,
117117
AccessExclusiveLock,
118-
false, false,
118+
0,
119119
RangeVarCallbackOwnsTable, NULL);
120120
rel = heap_open(tableOid, NoLock);
121121

src/backend/commands/indexcmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ ReindexIndex(RangeVar *indexRelation, int options)
20912091
* used here must match the index lock obtained in reindex_index().
20922092
*/
20932093
indOid = RangeVarGetRelidExtended(indexRelation, AccessExclusiveLock,
2094-
false, false,
2094+
0,
20952095
RangeVarCallbackForReindexIndex,
20962096
(void *) &heapOid);
20972097

@@ -2183,7 +2183,7 @@ ReindexTable(RangeVar *relation, int options)
21832183
Oid heapOid;
21842184

21852185
/* The lock level used here should match reindex_relation(). */
2186-
heapOid = RangeVarGetRelidExtended(relation, ShareLock, false, false,
2186+
heapOid = RangeVarGetRelidExtended(relation, ShareLock, 0,
21872187
RangeVarCallbackOwnsTable, NULL);
21882188

21892189
if (!reindex_relation(heapOid,

src/backend/commands/lockcmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ LockTableCommand(LockStmt *lockstmt)
6161
bool recurse = rv->inh;
6262
Oid reloid;
6363

64-
reloid = RangeVarGetRelidExtended(rv, lockstmt->mode, false,
65-
lockstmt->nowait,
64+
reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
65+
lockstmt->nowait ? RVR_NOWAIT : 0,
6666
RangeVarCallbackForLockTable,
6767
(void *) &lockstmt->mode);
6868

src/backend/commands/matview.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
161161
* Get a lock until end of transaction.
162162
*/
163163
matviewOid = RangeVarGetRelidExtended(stmt->relation,
164-
lockmode, false, false,
164+
lockmode, 0,
165165
RangeVarCallbackOwnsTable, NULL);
166166
matviewRel = heap_open(matviewOid, NoLock);
167167

src/backend/commands/policy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ CreatePolicy(CreatePolicyStmt *stmt)
743743

744744
/* Get id of table. Also handles permissions checks. */
745745
table_id = RangeVarGetRelidExtended(stmt->table, AccessExclusiveLock,
746-
false, false,
746+
0,
747747
RangeVarCallbackForPolicy,
748748
(void *) stmt);
749749

@@ -915,7 +915,7 @@ AlterPolicy(AlterPolicyStmt *stmt)
915915

916916
/* Get id of table. Also handles permissions checks. */
917917
table_id = RangeVarGetRelidExtended(stmt->table, AccessExclusiveLock,
918-
false, false,
918+
0,
919919
RangeVarCallbackForPolicy,
920920
(void *) stmt);
921921

@@ -1215,7 +1215,7 @@ rename_policy(RenameStmt *stmt)
12151215

12161216
/* Get id of table. Also handles permissions checks. */
12171217
table_id = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
1218-
false, false,
1218+
0,
12191219
RangeVarCallbackForPolicy,
12201220
(void *) stmt);
12211221

src/backend/commands/sequence.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
432432
/* Open and lock sequence, and check for ownership along the way. */
433433
relid = RangeVarGetRelidExtended(stmt->sequence,
434434
ShareRowExclusiveLock,
435-
stmt->missing_ok,
436-
false,
435+
stmt->missing_ok ? RVR_MISSING_OK : 0,
437436
RangeVarCallbackOwnsRelation,
438437
NULL);
439438
if (relid == InvalidOid)

src/backend/commands/tablecmds.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,7 @@ RemoveRelations(DropStmt *drop)
11591159
state.heapOid = InvalidOid;
11601160
state.partParentOid = InvalidOid;
11611161
state.concurrent = drop->concurrent;
1162-
relOid = RangeVarGetRelidExtended(rel, lockmode, true,
1163-
false,
1162+
relOid = RangeVarGetRelidExtended(rel, lockmode, RVR_MISSING_OK,
11641163
RangeVarCallbackForDropRelation,
11651164
(void *) &state);
11661165

@@ -2793,7 +2792,7 @@ renameatt(RenameStmt *stmt)
27932792

27942793
/* lock level taken here should match renameatt_internal */
27952794
relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
2796-
stmt->missing_ok, false,
2795+
stmt->missing_ok ? RVR_MISSING_OK : 0,
27972796
RangeVarCallbackForRenameAttribute,
27982797
NULL);
27992798

@@ -2945,7 +2944,7 @@ RenameConstraint(RenameStmt *stmt)
29452944
{
29462945
/* lock level taken here should match rename_constraint_internal */
29472946
relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
2948-
stmt->missing_ok, false,
2947+
stmt->missing_ok ? RVR_MISSING_OK : 0,
29492948
RangeVarCallbackForRenameAttribute,
29502949
NULL);
29512950
if (!OidIsValid(relid))
@@ -2987,7 +2986,7 @@ RenameRelation(RenameStmt *stmt)
29872986
* escalation.
29882987
*/
29892988
relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
2990-
stmt->missing_ok, false,
2989+
stmt->missing_ok ? RVR_MISSING_OK : 0,
29912990
RangeVarCallbackForAlterRelation,
29922991
(void *) stmt);
29932992

@@ -3146,7 +3145,8 @@ CheckTableNotInUse(Relation rel, const char *stmt)
31463145
Oid
31473146
AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode)
31483147
{
3149-
return RangeVarGetRelidExtended(stmt->relation, lockmode, stmt->missing_ok, false,
3148+
return RangeVarGetRelidExtended(stmt->relation, lockmode,
3149+
stmt->missing_ok ? RVR_MISSING_OK : 0,
31503150
RangeVarCallbackForAlterRelation,
31513151
(void *) stmt);
31523152
}
@@ -12683,7 +12683,7 @@ AlterTableNamespace(AlterObjectSchemaStmt *stmt, Oid *oldschema)
1268312683
ObjectAddress myself;
1268412684

1268512685
relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
12686-
stmt->missing_ok, false,
12686+
stmt->missing_ok ? RVR_MISSING_OK : 0,
1268712687
RangeVarCallbackForAlterRelation,
1268812688
(void *) stmt);
1268912689

@@ -14613,7 +14613,7 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name)
1461314613
state.parentTblOid = parentIdx->rd_index->indrelid;
1461414614
state.lockedParentTbl = false;
1461514615
partIdxId =
14616-
RangeVarGetRelidExtended(name, AccessExclusiveLock, false, false,
14616+
RangeVarGetRelidExtended(name, AccessExclusiveLock, 0,
1461714617
RangeVarCallbackForAttachIndex,
1461814618
(void *) &state);
1461914619
/* Not there? */

src/backend/commands/trigger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ renametrig(RenameStmt *stmt)
16671667
* release until end of transaction).
16681668
*/
16691669
relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
1670-
false, false,
1670+
0,
16711671
RangeVarCallbackForRenameTrigger,
16721672
NULL);
16731673

src/backend/rewrite/rewriteDefine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ RenameRewriteRule(RangeVar *relation, const char *oldName,
951951
* release until end of transaction).
952952
*/
953953
relid = RangeVarGetRelidExtended(relation, AccessExclusiveLock,
954-
false, false,
954+
0,
955955
RangeVarCallbackForRenameRule,
956956
NULL);
957957

src/backend/tcop/utility.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ ProcessUtilitySlow(ParseState *pstate,
13051305
: ShareLock;
13061306
relid =
13071307
RangeVarGetRelidExtended(stmt->relation, lockmode,
1308-
false, false,
1308+
0,
13091309
RangeVarCallbackOwnsRelation,
13101310
NULL);
13111311

src/include/catalog/namespace.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,24 @@ typedef struct OverrideSearchPath
4747
bool addTemp; /* implicitly prepend temp schema? */
4848
} OverrideSearchPath;
4949

50+
/*
51+
* Option flag bits for RangeVarGetRelidExtended().
52+
*/
53+
typedef enum RVROption
54+
{
55+
RVR_MISSING_OK = 1 << 0, /* don't error if relation doesn't exist */
56+
RVR_NOWAIT = 1 << 1 /* error if relation cannot be locked */
57+
} RVROption;
58+
5059
typedef void (*RangeVarGetRelidCallback) (const RangeVar *relation, Oid relId,
5160
Oid oldRelId, void *callback_arg);
5261

5362
#define RangeVarGetRelid(relation, lockmode, missing_ok) \
54-
RangeVarGetRelidExtended(relation, lockmode, missing_ok, false, NULL, NULL)
63+
RangeVarGetRelidExtended(relation, lockmode, \
64+
(missing_ok) ? RVR_MISSING_OK : 0, NULL, NULL)
5565

5666
extern Oid RangeVarGetRelidExtended(const RangeVar *relation,
57-
LOCKMODE lockmode, bool missing_ok, bool nowait,
67+
LOCKMODE lockmode, uint32 flags,
5868
RangeVarGetRelidCallback callback,
5969
void *callback_arg);
6070
extern Oid RangeVarGetCreationNamespace(const RangeVar *newRelation);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy