Skip to content

Commit 0e54969

Browse files
committed
Classify DROP operations by whether or not they are user-initiated.
This doesn't do anything useful just yet, but is intended as supporting infrastructure for allowing sepgsql to sensibly check DROP permissions. KaiGai Kohei and Robert Haas
1 parent bc33474 commit 0e54969

File tree

14 files changed

+61
-30
lines changed

14 files changed

+61
-30
lines changed

src/backend/catalog/aclchk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ SetDefaultACL(InternalDefaultACL *iacls)
12111211
myself.objectId = HeapTupleGetOid(tuple);
12121212
myself.objectSubId = 0;
12131213

1214-
performDeletion(&myself, DROP_RESTRICT);
1214+
performDeletion(&myself, DROP_RESTRICT, 0);
12151215
}
12161216
}
12171217
else

src/backend/catalog/dependency.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ static void reportDependentObjects(const ObjectAddresses *targetObjects,
171171
DropBehavior behavior,
172172
int msglevel,
173173
const ObjectAddress *origObject);
174-
static void deleteOneObject(const ObjectAddress *object, Relation depRel);
174+
static void deleteOneObject(const ObjectAddress *object,
175+
Relation depRel, int32 flags);
175176
static void doDeletion(const ObjectAddress *object);
176177
static void AcquireDeletionLock(const ObjectAddress *object);
177178
static void ReleaseDeletionLock(const ObjectAddress *object);
@@ -205,10 +206,17 @@ static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
205206
* that can participate in dependencies. Note that the next two routines
206207
* are variants on the same theme; if you change anything here you'll likely
207208
* need to fix them too.
209+
*
210+
* flags should include PERFORM_DELETION_INTERNAL when the drop operation is
211+
* not the direct result of a user-initiated action. For example, when a
212+
* temporary schema is cleaned out so that a new backend can use it, or when
213+
* a column default is dropped as an intermediate step while adding a new one,
214+
* that's an internal operation. On the other hand, when the we drop something
215+
* because the user issued a DROP statement against it, that's not internal.
208216
*/
209217
void
210218
performDeletion(const ObjectAddress *object,
211-
DropBehavior behavior)
219+
DropBehavior behavior, int flags)
212220
{
213221
Relation depRel;
214222
ObjectAddresses *targetObjects;
@@ -254,7 +262,7 @@ performDeletion(const ObjectAddress *object,
254262
{
255263
ObjectAddress *thisobj = targetObjects->refs + i;
256264

257-
deleteOneObject(thisobj, depRel);
265+
deleteOneObject(thisobj, depRel, flags);
258266
}
259267

260268
/* And clean up */
@@ -274,7 +282,7 @@ performDeletion(const ObjectAddress *object,
274282
*/
275283
void
276284
performMultipleDeletions(const ObjectAddresses *objects,
277-
DropBehavior behavior)
285+
DropBehavior behavior, int flags)
278286
{
279287
Relation depRel;
280288
ObjectAddresses *targetObjects;
@@ -336,7 +344,7 @@ performMultipleDeletions(const ObjectAddresses *objects,
336344
{
337345
ObjectAddress *thisobj = targetObjects->refs + i;
338346

339-
deleteOneObject(thisobj, depRel);
347+
deleteOneObject(thisobj, depRel, flags);
340348
}
341349

342350
/* And clean up */
@@ -407,7 +415,14 @@ deleteWhatDependsOn(const ObjectAddress *object,
407415
if (thisextra->flags & DEPFLAG_ORIGINAL)
408416
continue;
409417

410-
deleteOneObject(thisobj, depRel);
418+
/*
419+
* Since this function is currently only used to clean out temporary
420+
* schemas, we pass PERFORM_DELETION_INTERNAL here, indicating that
421+
* the operation is an automatic system operation rather than a user
422+
* action. If, in the future, this function is used for other
423+
* purposes, we might need to revisit this.
424+
*/
425+
deleteOneObject(thisobj, depRel, PERFORM_DELETION_INTERNAL);
411426
}
412427

413428
/* And clean up */
@@ -950,7 +965,7 @@ reportDependentObjects(const ObjectAddresses *targetObjects,
950965
* depRel is the already-open pg_depend relation.
951966
*/
952967
static void
953-
deleteOneObject(const ObjectAddress *object, Relation depRel)
968+
deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
954969
{
955970
ScanKeyData key[3];
956971
int nkeys;

src/backend/catalog/heap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
15281528
*/
15291529
void
15301530
RemoveAttrDefault(Oid relid, AttrNumber attnum,
1531-
DropBehavior behavior, bool complain)
1531+
DropBehavior behavior, bool complain, bool internal)
15321532
{
15331533
Relation attrdef_rel;
15341534
ScanKeyData scankeys[2];
@@ -1559,7 +1559,8 @@ RemoveAttrDefault(Oid relid, AttrNumber attnum,
15591559
object.objectId = HeapTupleGetOid(tuple);
15601560
object.objectSubId = 0;
15611561

1562-
performDeletion(&object, behavior);
1562+
performDeletion(&object, behavior,
1563+
internal ? PERFORM_DELETION_INTERNAL : 0);
15631564

15641565
found = true;
15651566
}

src/backend/catalog/pg_shdepend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
12401240
}
12411241

12421242
/* the dependency mechanism does the actual work */
1243-
performMultipleDeletions(deleteobjs, behavior);
1243+
performMultipleDeletions(deleteobjs, behavior, 0);
12441244

12451245
heap_close(sdepRel, RowExclusiveLock);
12461246

src/backend/commands/cluster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
14431443
* The new relation is local to our transaction and we know nothing
14441444
* depends on it, so DROP_RESTRICT should be OK.
14451445
*/
1446-
performDeletion(&object, DROP_RESTRICT);
1446+
performDeletion(&object, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
14471447

14481448
/* performDeletion does CommandCounterIncrement at end */
14491449

src/backend/commands/dropcmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ RemoveObjects(DropStmt *stmt)
119119
}
120120

121121
/* Here we really delete them. */
122-
performMultipleDeletions(objects, stmt->behavior);
122+
performMultipleDeletions(objects, stmt->behavior, 0);
123123

124124
free_object_addresses(objects);
125125
}

src/backend/commands/foreigncmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ RemoveUserMapping(DropUserMappingStmt *stmt)
12861286
object.objectId = umId;
12871287
object.objectSubId = 0;
12881288

1289-
performDeletion(&object, DROP_CASCADE);
1289+
performDeletion(&object, DROP_CASCADE, 0);
12901290
}
12911291

12921292

src/backend/commands/opclasscmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ dropOperators(List *opfamilyname, Oid amoid, Oid opfamilyoid,
15191519
object.objectId = amopid;
15201520
object.objectSubId = 0;
15211521

1522-
performDeletion(&object, DROP_RESTRICT);
1522+
performDeletion(&object, DROP_RESTRICT, 0);
15231523
}
15241524
}
15251525

@@ -1559,7 +1559,7 @@ dropProcedures(List *opfamilyname, Oid amoid, Oid opfamilyoid,
15591559
object.objectId = amprocid;
15601560
object.objectSubId = 0;
15611561

1562-
performDeletion(&object, DROP_RESTRICT);
1562+
performDeletion(&object, DROP_RESTRICT, 0);
15631563
}
15641564
}
15651565

src/backend/commands/tablecmds.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ RemoveRelations(DropStmt *drop)
816816
add_exact_object_address(&obj, objects);
817817
}
818818

819-
performMultipleDeletions(objects, drop->behavior);
819+
performMultipleDeletions(objects, drop->behavior, 0);
820820

821821
free_object_addresses(objects);
822822
}
@@ -4803,8 +4803,13 @@ ATExecColumnDefault(Relation rel, const char *colName,
48034803
* Remove any old default for the column. We use RESTRICT here for
48044804
* safety, but at present we do not expect anything to depend on the
48054805
* default.
4806+
*
4807+
* We treat removing the existing default as an internal operation when
4808+
* it is preparatory to adding a new default, but as a user-initiated
4809+
* operation when the user asked for a drop.
48064810
*/
4807-
RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, false);
4811+
RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, false,
4812+
newDefault == NULL ? false : true);
48084813

48094814
if (newDefault)
48104815
{
@@ -5217,7 +5222,7 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
52175222
object.objectId = RelationGetRelid(rel);
52185223
object.objectSubId = attnum;
52195224

5220-
performDeletion(&object, behavior);
5225+
performDeletion(&object, behavior, 0);
52215226

52225227
/*
52235228
* If we dropped the OID column, must adjust pg_class.relhasoids and tell
@@ -6731,7 +6736,7 @@ ATExecDropConstraint(Relation rel, const char *constrName,
67316736
conobj.objectId = HeapTupleGetOid(tuple);
67326737
conobj.objectSubId = 0;
67336738

6734-
performDeletion(&conobj, behavior);
6739+
performDeletion(&conobj, behavior, 0);
67356740

67366741
found = true;
67376742

@@ -7453,7 +7458,8 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
74537458
* We use RESTRICT here for safety, but at present we do not expect
74547459
* anything to depend on the default.
74557460
*/
7456-
RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, true);
7461+
RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, true,
7462+
true);
74577463

74587464
StoreAttrDefault(rel, attnum, defaultexpr);
74597465
}
@@ -7598,15 +7604,15 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
75987604
obj.classId = ConstraintRelationId;
75997605
obj.objectId = lfirst_oid(oid_item);
76007606
obj.objectSubId = 0;
7601-
performDeletion(&obj, DROP_RESTRICT);
7607+
performDeletion(&obj, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
76027608
}
76037609

76047610
foreach(oid_item, tab->changedIndexOids)
76057611
{
76067612
obj.classId = RelationRelationId;
76077613
obj.objectId = lfirst_oid(oid_item);
76087614
obj.objectSubId = 0;
7609-
performDeletion(&obj, DROP_RESTRICT);
7615+
performDeletion(&obj, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
76107616
}
76117617

76127618
/*
@@ -9764,7 +9770,14 @@ PreCommit_on_commit_actions(void)
97649770
object.classId = RelationRelationId;
97659771
object.objectId = oc->relid;
97669772
object.objectSubId = 0;
9767-
performDeletion(&object, DROP_CASCADE);
9773+
9774+
/*
9775+
* Since this is an automatic drop, rather than one
9776+
* directly initiated by the user, we pass the
9777+
* PERFORM_DELETION_INTERNAL flag.
9778+
*/
9779+
performDeletion(&object,
9780+
DROP_CASCADE, PERFORM_DELETION_INTERNAL);
97689781

97699782
/*
97709783
* Note that table deletion will call

src/backend/commands/typecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2318,7 +2318,7 @@ AlterDomainDropConstraint(List *names, const char *constrName,
23182318
conobj.objectId = HeapTupleGetOid(contup);
23192319
conobj.objectSubId = 0;
23202320

2321-
performDeletion(&conobj, behavior);
2321+
performDeletion(&conobj, behavior, 0);
23222322
found = true;
23232323
}
23242324
}

src/backend/postmaster/autovacuum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ do_autovacuum(void)
20442044
object.classId = RelationRelationId;
20452045
object.objectId = relid;
20462046
object.objectSubId = 0;
2047-
performDeletion(&object, DROP_CASCADE);
2047+
performDeletion(&object, DROP_CASCADE, PERFORM_DELETION_INTERNAL);
20482048
}
20492049
else
20502050
{

src/backend/storage/large_object/inv_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ inv_drop(Oid lobjId)
307307
object.classId = LargeObjectRelationId;
308308
object.objectId = lobjId;
309309
object.objectSubId = 0;
310-
performDeletion(&object, DROP_CASCADE);
310+
performDeletion(&object, DROP_CASCADE, 0);
311311

312312
/*
313313
* Advance command counter so that tuple removal will be seen by later

src/include/catalog/dependency.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ typedef enum ObjectClass
152152

153153
/* in dependency.c */
154154

155+
#define PERFORM_DELETION_INTERNAL 0x0001
156+
155157
extern void performDeletion(const ObjectAddress *object,
156-
DropBehavior behavior);
158+
DropBehavior behavior, int flags);
157159

158160
extern void performMultipleDeletions(const ObjectAddresses *objects,
159-
DropBehavior behavior);
161+
DropBehavior behavior, int flags);
160162

161163
extern void deleteWhatDependsOn(const ObjectAddress *object,
162164
bool showNotices);

src/include/catalog/heap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extern void DeleteRelationTuple(Oid relid);
107107
extern void DeleteAttributeTuples(Oid relid);
108108
extern void RemoveAttributeById(Oid relid, AttrNumber attnum);
109109
extern void RemoveAttrDefault(Oid relid, AttrNumber attnum,
110-
DropBehavior behavior, bool complain);
110+
DropBehavior behavior, bool complain, bool internal);
111111
extern void RemoveAttrDefaultById(Oid attrdefId);
112112
extern void RemoveStatistics(Oid relid, AttrNumber attnum);
113113

0 commit comments

Comments
 (0)