Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * dependency.c
4 : : * Routines to support inter-object dependencies.
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/catalog/dependency.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/genam.h"
18 : : #include "access/htup_details.h"
19 : : #include "access/table.h"
20 : : #include "access/xact.h"
21 : : #include "catalog/catalog.h"
22 : : #include "catalog/dependency.h"
23 : : #include "catalog/heap.h"
24 : : #include "catalog/index.h"
25 : : #include "catalog/namespace.h"
26 : : #include "catalog/objectaccess.h"
27 : : #include "catalog/pg_am.h"
28 : : #include "catalog/pg_amop.h"
29 : : #include "catalog/pg_amproc.h"
30 : : #include "catalog/pg_attrdef.h"
31 : : #include "catalog/pg_authid.h"
32 : : #include "catalog/pg_auth_members.h"
33 : : #include "catalog/pg_cast.h"
34 : : #include "catalog/pg_collation.h"
35 : : #include "catalog/pg_constraint.h"
36 : : #include "catalog/pg_conversion.h"
37 : : #include "catalog/pg_database.h"
38 : : #include "catalog/pg_default_acl.h"
39 : : #include "catalog/pg_depend.h"
40 : : #include "catalog/pg_event_trigger.h"
41 : : #include "catalog/pg_extension.h"
42 : : #include "catalog/pg_foreign_data_wrapper.h"
43 : : #include "catalog/pg_foreign_server.h"
44 : : #include "catalog/pg_init_privs.h"
45 : : #include "catalog/pg_language.h"
46 : : #include "catalog/pg_largeobject.h"
47 : : #include "catalog/pg_namespace.h"
48 : : #include "catalog/pg_opclass.h"
49 : : #include "catalog/pg_operator.h"
50 : : #include "catalog/pg_opfamily.h"
51 : : #include "catalog/pg_parameter_acl.h"
52 : : #include "catalog/pg_policy.h"
53 : : #include "catalog/pg_proc.h"
54 : : #include "catalog/pg_publication.h"
55 : : #include "catalog/pg_publication_namespace.h"
56 : : #include "catalog/pg_publication_rel.h"
57 : : #include "catalog/pg_rewrite.h"
58 : : #include "catalog/pg_statistic_ext.h"
59 : : #include "catalog/pg_subscription.h"
60 : : #include "catalog/pg_tablespace.h"
61 : : #include "catalog/pg_transform.h"
62 : : #include "catalog/pg_trigger.h"
63 : : #include "catalog/pg_ts_config.h"
64 : : #include "catalog/pg_ts_dict.h"
65 : : #include "catalog/pg_ts_parser.h"
66 : : #include "catalog/pg_ts_template.h"
67 : : #include "catalog/pg_type.h"
68 : : #include "catalog/pg_user_mapping.h"
69 : : #include "commands/comment.h"
70 : : #include "commands/defrem.h"
71 : : #include "commands/event_trigger.h"
72 : : #include "commands/extension.h"
73 : : #include "commands/policy.h"
74 : : #include "commands/publicationcmds.h"
75 : : #include "commands/seclabel.h"
76 : : #include "commands/sequence.h"
77 : : #include "commands/trigger.h"
78 : : #include "commands/typecmds.h"
79 : : #include "funcapi.h"
80 : : #include "miscadmin.h"
81 : : #include "nodes/nodeFuncs.h"
82 : : #include "parser/parsetree.h"
83 : : #include "rewrite/rewriteRemove.h"
84 : : #include "storage/lmgr.h"
85 : : #include "utils/fmgroids.h"
86 : : #include "utils/lsyscache.h"
87 : : #include "utils/syscache.h"
88 : :
89 : :
90 : : /*
91 : : * Deletion processing requires additional state for each ObjectAddress that
92 : : * it's planning to delete. For simplicity and code-sharing we make the
93 : : * ObjectAddresses code support arrays with or without this extra state.
94 : : */
95 : : typedef struct
96 : : {
97 : : int flags; /* bitmask, see bit definitions below */
98 : : ObjectAddress dependee; /* object whose deletion forced this one */
99 : : } ObjectAddressExtra;
100 : :
101 : : /* ObjectAddressExtra flag bits */
102 : : #define DEPFLAG_ORIGINAL 0x0001 /* an original deletion target */
103 : : #define DEPFLAG_NORMAL 0x0002 /* reached via normal dependency */
104 : : #define DEPFLAG_AUTO 0x0004 /* reached via auto dependency */
105 : : #define DEPFLAG_INTERNAL 0x0008 /* reached via internal dependency */
106 : : #define DEPFLAG_PARTITION 0x0010 /* reached via partition dependency */
107 : : #define DEPFLAG_EXTENSION 0x0020 /* reached via extension dependency */
108 : : #define DEPFLAG_REVERSE 0x0040 /* reverse internal/extension link */
109 : : #define DEPFLAG_IS_PART 0x0080 /* has a partition dependency */
110 : : #define DEPFLAG_SUBOBJECT 0x0100 /* subobject of another deletable object */
111 : :
112 : :
113 : : /* expansible list of ObjectAddresses */
114 : : struct ObjectAddresses
115 : : {
116 : : ObjectAddress *refs; /* => palloc'd array */
117 : : ObjectAddressExtra *extras; /* => palloc'd array, or NULL if not used */
118 : : int numrefs; /* current number of references */
119 : : int maxrefs; /* current size of palloc'd array(s) */
120 : : };
121 : :
122 : : /* typedef ObjectAddresses appears in dependency.h */
123 : :
124 : : /* threaded list of ObjectAddresses, for recursion detection */
125 : : typedef struct ObjectAddressStack
126 : : {
127 : : const ObjectAddress *object; /* object being visited */
128 : : int flags; /* its current flag bits */
129 : : struct ObjectAddressStack *next; /* next outer stack level */
130 : : } ObjectAddressStack;
131 : :
132 : : /* temporary storage in findDependentObjects */
133 : : typedef struct
134 : : {
135 : : ObjectAddress obj; /* object to be deleted --- MUST BE FIRST */
136 : : int subflags; /* flags to pass down when recursing to obj */
137 : : } ObjectAddressAndFlags;
138 : :
139 : : /* for find_expr_references_walker */
140 : : typedef struct
141 : : {
142 : : ObjectAddresses *addrs; /* addresses being accumulated */
143 : : List *rtables; /* list of rangetables to resolve Vars */
144 : : } find_expr_references_context;
145 : :
146 : :
147 : : static void findDependentObjects(const ObjectAddress *object,
148 : : int objflags,
149 : : int flags,
150 : : ObjectAddressStack *stack,
151 : : ObjectAddresses *targetObjects,
152 : : const ObjectAddresses *pendingObjects,
153 : : Relation *depRel);
154 : : static void reportDependentObjects(const ObjectAddresses *targetObjects,
155 : : DropBehavior behavior,
156 : : int flags,
157 : : const ObjectAddress *origObject);
158 : : static void deleteOneObject(const ObjectAddress *object,
159 : : Relation *depRel, int32 flags);
160 : : static void doDeletion(const ObjectAddress *object, int flags);
161 : : static void add_target_column_dependencies(List *targetList, Oid relid,
162 : : find_expr_references_context *context);
163 : : static bool find_expr_references_walker(Node *node,
164 : : find_expr_references_context *context);
165 : : static void process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum,
166 : : find_expr_references_context *context);
167 : : static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
168 : : static int object_address_comparator(const void *a, const void *b);
169 : : static void add_object_address(Oid classId, Oid objectId, int32 subId,
170 : : ObjectAddresses *addrs);
171 : : static void add_exact_object_address_extra(const ObjectAddress *object,
172 : : const ObjectAddressExtra *extra,
173 : : ObjectAddresses *addrs);
174 : : static bool object_address_present_add_flags(const ObjectAddress *object,
175 : : int flags,
176 : : ObjectAddresses *addrs);
177 : : static bool stack_address_present_add_flags(const ObjectAddress *object,
178 : : int flags,
179 : : ObjectAddressStack *stack);
180 : : static void DeleteInitPrivs(const ObjectAddress *object);
181 : :
182 : :
183 : : /*
184 : : * Go through the objects given running the final actions on them, and execute
185 : : * the actual deletion.
186 : : */
187 : : static void
4994 alvherre@alvh.no-ip. 188 :CBC 24119 : deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel,
189 : : int flags)
190 : : {
191 : : int i;
192 : :
193 : : /*
194 : : * Keep track of objects for event triggers, if necessary.
195 : : */
4362 196 [ + + + + ]: 24119 : if (trackDroppedObjectsNeeded() && !(flags & PERFORM_DELETION_INTERNAL))
197 : : {
4994 198 [ + + ]: 3139 : for (i = 0; i < targetObjects->numrefs; i++)
199 : : {
4362 200 : 2652 : const ObjectAddress *thisobj = &targetObjects->refs[i];
201 : 2652 : const ObjectAddressExtra *extra = &targetObjects->extras[i];
4207 bruce@momjian.us 202 : 2652 : bool original = false;
203 : 2652 : bool normal = false;
204 : :
4362 alvherre@alvh.no-ip. 205 [ + + ]: 2652 : if (extra->flags & DEPFLAG_ORIGINAL)
206 : 542 : original = true;
207 [ + + ]: 2652 : if (extra->flags & DEPFLAG_NORMAL)
208 : 236 : normal = true;
209 [ - + ]: 2652 : if (extra->flags & DEPFLAG_REVERSE)
4362 alvherre@alvh.no-ip. 210 :UBC 0 : normal = true;
211 : :
977 peter@eisentraut.org 212 [ + + ]:CBC 2652 : if (EventTriggerSupportsObject(thisobj))
213 : : {
4362 alvherre@alvh.no-ip. 214 : 2578 : EventTriggerSQLDropAddObject(thisobj, original, normal);
215 : : }
216 : : }
217 : : }
218 : :
219 : : /*
220 : : * Delete all the objects in the proper order, except that if told to, we
221 : : * should skip the original object(s).
222 : : */
4994 223 [ + + ]: 178188 : for (i = 0; i < targetObjects->numrefs; i++)
224 : : {
225 : 154083 : ObjectAddress *thisobj = targetObjects->refs + i;
3648 tgl@sss.pgh.pa.us 226 : 154083 : ObjectAddressExtra *thisextra = targetObjects->extras + i;
227 : :
228 [ + + ]: 154083 : if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
229 [ + + ]: 6670 : (thisextra->flags & DEPFLAG_ORIGINAL))
230 : 593 : continue;
231 : :
4994 alvherre@alvh.no-ip. 232 : 153490 : deleteOneObject(thisobj, depRel, flags);
233 : : }
234 : 24105 : }
235 : :
236 : : /*
237 : : * performDeletion: attempt to drop the specified object. If CASCADE
238 : : * behavior is specified, also drop any dependent objects (recursively).
239 : : * If RESTRICT behavior is specified, error out if there are any dependent
240 : : * objects, except for those that should be implicitly dropped anyway
241 : : * according to the dependency type.
242 : : *
243 : : * This is the outer control routine for all forms of DROP that drop objects
244 : : * that can participate in dependencies. Note that performMultipleDeletions
245 : : * is a variant on the same theme; if you change anything here you'll likely
246 : : * need to fix that too.
247 : : *
248 : : * Bits in the flags argument can include:
249 : : *
250 : : * PERFORM_DELETION_INTERNAL: indicates that the drop operation is not the
251 : : * direct result of a user-initiated action. For example, when a temporary
252 : : * schema is cleaned out so that a new backend can use it, or when a column
253 : : * default is dropped as an intermediate step while adding a new one, that's
254 : : * an internal operation. On the other hand, when we drop something because
255 : : * the user issued a DROP statement against it, that's not internal. Currently
256 : : * this suppresses calling event triggers and making some permissions checks.
257 : : *
258 : : * PERFORM_DELETION_CONCURRENTLY: perform the drop concurrently. This does
259 : : * not currently work for anything except dropping indexes; don't set it for
260 : : * other object types or you may get strange results.
261 : : *
262 : : * PERFORM_DELETION_QUIETLY: reduce message level from NOTICE to DEBUG2.
263 : : *
264 : : * PERFORM_DELETION_SKIP_ORIGINAL: do not delete the specified object(s),
265 : : * but only what depends on it/them.
266 : : *
267 : : * PERFORM_DELETION_SKIP_EXTENSIONS: do not delete extensions, even when
268 : : * deleting objects that are part of an extension. This should generally
269 : : * be used only when dropping temporary objects.
270 : : *
271 : : * PERFORM_DELETION_CONCURRENT_LOCK: perform the drop normally but with a lock
272 : : * as if it were concurrent. This is used by REINDEX CONCURRENTLY.
273 : : *
274 : : */
275 : : void
8905 tgl@sss.pgh.pa.us 276 : 4552 : performDeletion(const ObjectAddress *object,
277 : : DropBehavior behavior, int flags)
278 : : {
279 : : Relation depRel;
280 : : ObjectAddresses *targetObjects;
281 : :
282 : : /*
283 : : * We save some cycles by opening pg_depend just once and passing the
284 : : * Relation pointer down to all the recursive deletion steps.
285 : : */
2868 andres@anarazel.de 286 : 4552 : depRel = table_open(DependRelationId, RowExclusiveLock);
287 : :
288 : : /*
289 : : * Acquire deletion lock on the target object. (Ideally the caller has
290 : : * done this already, but many places are sloppy about it.)
291 : : */
5349 simon@2ndQuadrant.co 292 : 4552 : AcquireDeletionLock(object, 0);
293 : :
294 : : /*
295 : : * Construct a list of objects to delete (ie, the given object plus
296 : : * everything directly or indirectly dependent on it).
297 : : */
6747 tgl@sss.pgh.pa.us 298 : 4552 : targetObjects = new_object_addresses();
299 : :
300 : 4552 : findDependentObjects(object,
301 : : DEPFLAG_ORIGINAL,
302 : : flags,
303 : : NULL, /* empty stack */
304 : : targetObjects,
305 : : NULL, /* no pendingObjects */
306 : : &depRel);
307 : :
308 : : /*
309 : : * Check if deletion is allowed, and report about cascaded deletes.
310 : : */
311 : 4552 : reportDependentObjects(targetObjects,
312 : : behavior,
313 : : flags,
314 : : object);
315 : :
316 : : /* do the deed */
4994 alvherre@alvh.no-ip. 317 : 4516 : deleteObjectsInList(targetObjects, &depRel, flags);
318 : :
319 : : /* And clean up */
6747 tgl@sss.pgh.pa.us 320 : 4515 : free_object_addresses(targetObjects);
321 : :
2868 andres@anarazel.de 322 : 4515 : table_close(depRel, RowExclusiveLock);
7405 alvherre@alvh.no-ip. 323 : 4515 : }
324 : :
325 : : /*
326 : : * performMultipleDeletions: Similar to performDeletion, but act on multiple
327 : : * objects at once.
328 : : *
329 : : * The main difference from issuing multiple performDeletion calls is that the
330 : : * list of objects that would be implicitly dropped, for each object to be
331 : : * dropped, is the union of the implicit-object list for all objects. This
332 : : * makes each check be more relaxed.
333 : : */
334 : : void
335 : 21960 : performMultipleDeletions(const ObjectAddresses *objects,
336 : : DropBehavior behavior, int flags)
337 : : {
338 : : Relation depRel;
339 : : ObjectAddresses *targetObjects;
340 : : int i;
341 : :
342 : : /* No work if no objects... */
6741 tgl@sss.pgh.pa.us 343 [ + + ]: 21960 : if (objects->numrefs <= 0)
344 : 2084 : return;
345 : :
346 : : /*
347 : : * We save some cycles by opening pg_depend just once and passing the
348 : : * Relation pointer down to all the recursive deletion steps.
349 : : */
2868 andres@anarazel.de 350 : 19876 : depRel = table_open(DependRelationId, RowExclusiveLock);
351 : :
352 : : /*
353 : : * Construct a list of objects to delete (ie, the given objects plus
354 : : * everything directly or indirectly dependent on them). Note that
355 : : * because we pass the whole objects list as pendingObjects context, we
356 : : * won't get a failure from trying to delete an object that is internally
357 : : * dependent on another one in the list; we'll just skip that object and
358 : : * delete it when we reach its owner.
359 : : */
6747 tgl@sss.pgh.pa.us 360 : 19876 : targetObjects = new_object_addresses();
361 : :
7405 alvherre@alvh.no-ip. 362 [ + + ]: 43754 : for (i = 0; i < objects->numrefs; i++)
363 : : {
6747 tgl@sss.pgh.pa.us 364 : 23909 : const ObjectAddress *thisobj = objects->refs + i;
365 : :
366 : : /*
367 : : * Acquire deletion lock on each target object. (Ideally the caller
368 : : * has done this already, but many places are sloppy about it.)
369 : : */
5349 simon@2ndQuadrant.co 370 : 23909 : AcquireDeletionLock(thisobj, flags);
371 : :
6747 tgl@sss.pgh.pa.us 372 : 23909 : findDependentObjects(thisobj,
373 : : DEPFLAG_ORIGINAL,
374 : : flags,
375 : : NULL, /* empty stack */
376 : : targetObjects,
377 : : objects,
378 : : &depRel);
379 : : }
380 : :
381 : : /*
382 : : * Check if deletion is allowed, and report about cascaded deletes.
383 : : *
384 : : * If there's exactly one object being deleted, report it the same way as
385 : : * in performDeletion(), else we have to be vaguer.
386 : : */
387 : 19845 : reportDependentObjects(targetObjects,
388 : : behavior,
389 : : flags,
6741 390 [ + + ]: 19845 : (objects->numrefs == 1 ? objects->refs : NULL));
391 : :
392 : : /* do the deed */
4994 alvherre@alvh.no-ip. 393 : 19603 : deleteObjectsInList(targetObjects, &depRel, flags);
394 : :
395 : : /* And clean up */
6747 tgl@sss.pgh.pa.us 396 : 19590 : free_object_addresses(targetObjects);
397 : :
2868 andres@anarazel.de 398 : 19590 : table_close(depRel, RowExclusiveLock);
399 : : }
400 : :
401 : : /*
402 : : * findDependentObjects - find all objects that depend on 'object'
403 : : *
404 : : * For every object that depends on the starting object, acquire a deletion
405 : : * lock on the object, add it to targetObjects (if not already there),
406 : : * and recursively find objects that depend on it. An object's dependencies
407 : : * will be placed into targetObjects before the object itself; this means
408 : : * that the finished list's order represents a safe deletion order.
409 : : *
410 : : * The caller must already have a deletion lock on 'object' itself,
411 : : * but must not have added it to targetObjects. (Note: there are corner
412 : : * cases where we won't add the object either, and will also release the
413 : : * caller-taken lock. This is a bit ugly, but the API is set up this way
414 : : * to allow easy rechecking of an object's liveness after we lock it. See
415 : : * notes within the function.)
416 : : *
417 : : * When dropping a whole object (subId = 0), we find dependencies for
418 : : * its sub-objects too.
419 : : *
420 : : * object: the object to add to targetObjects and find dependencies on
421 : : * objflags: flags to be ORed into the object's targetObjects entry
422 : : * flags: PERFORM_DELETION_xxx flags for the deletion operation as a whole
423 : : * stack: list of objects being visited in current recursion; topmost item
424 : : * is the object that we recursed from (NULL for external callers)
425 : : * targetObjects: list of objects that are scheduled to be deleted
426 : : * pendingObjects: list of other objects slated for destruction, but
427 : : * not necessarily in targetObjects yet (can be NULL if none)
428 : : * *depRel: already opened pg_depend relation
429 : : *
430 : : * Note: objflags describes the reason for visiting this particular object
431 : : * at this time, and is not passed down when recursing. The flags argument
432 : : * is passed down, since it describes what we're doing overall.
433 : : */
434 : : static void
6747 tgl@sss.pgh.pa.us 435 : 191156 : findDependentObjects(const ObjectAddress *object,
436 : : int objflags,
437 : : int flags,
438 : : ObjectAddressStack *stack,
439 : : ObjectAddresses *targetObjects,
440 : : const ObjectAddresses *pendingObjects,
441 : : Relation *depRel)
442 : : {
443 : : ScanKeyData key[3];
444 : : int nkeys;
445 : : SysScanDesc scan;
446 : : HeapTuple tup;
447 : : ObjectAddress otherObject;
448 : : ObjectAddress owningObject;
449 : : ObjectAddress partitionObject;
450 : : ObjectAddressAndFlags *dependentObjects;
451 : : int numDependentObjects;
452 : : int maxDependentObjects;
453 : : ObjectAddressStack mystack;
454 : : ObjectAddressExtra extra;
455 : :
456 : : /*
457 : : * If the target object is already being visited in an outer recursion
458 : : * level, just report the current objflags back to that level and exit.
459 : : * This is needed to avoid infinite recursion in the face of circular
460 : : * dependencies.
461 : : *
462 : : * The stack check alone would result in dependency loops being broken at
463 : : * an arbitrary point, ie, the first member object of the loop to be
464 : : * visited is the last one to be deleted. This is obviously unworkable.
465 : : * However, the check for internal dependency below guarantees that we
466 : : * will not break a loop at an internal dependency: if we enter the loop
467 : : * at an "owned" object we will switch and start at the "owning" object
468 : : * instead. We could probably hack something up to avoid breaking at an
469 : : * auto dependency, too, if we had to. However there are no known cases
470 : : * where that would be necessary.
471 : : */
3648 472 [ + + ]: 191156 : if (stack_address_present_add_flags(object, objflags, stack))
5575 473 : 34128 : return;
474 : :
475 : : /*
476 : : * since this function recurses, it could be driven to stack overflow,
477 : : * because of the deep dependency tree, not only due to dependency loops.
478 : : */
1016 akorotkov@postgresql 479 : 190934 : check_stack_depth();
480 : :
481 : : /*
482 : : * It's also possible that the target object has already been completely
483 : : * processed and put into targetObjects. If so, again we just add the
484 : : * specified objflags to its entry and return.
485 : : *
486 : : * (Note: in these early-exit cases we could release the caller-taken
487 : : * lock, since the object is presumably now locked multiple times; but it
488 : : * seems not worth the cycles.)
489 : : */
3648 tgl@sss.pgh.pa.us 490 [ + + ]: 190934 : if (object_address_present_add_flags(object, objflags, targetObjects))
6747 491 : 32660 : return;
492 : :
493 : : /*
494 : : * If the target object is pinned, we can just error out immediately; it
495 : : * won't have any objects recorded as depending on it.
496 : : */
1962 497 [ + + ]: 158274 : if (IsPinnedObject(object->classId, object->objectId))
498 [ + - ]: 1 : ereport(ERROR,
499 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
500 : : errmsg("cannot drop %s because it is required by the database system",
501 : : getObjectDescription(object, false))));
502 : :
503 : : /*
504 : : * The target object might be internally dependent on some other object
505 : : * (its "owner"), and/or be a member of an extension (also considered its
506 : : * owner). If so, and if we aren't recursing from the owning object, we
507 : : * have to transform this deletion request into a deletion request of the
508 : : * owning object. (We'll eventually recurse back to this object, but the
509 : : * owning object has to be visited first so it will be deleted after.) The
510 : : * way to find out about this is to scan the pg_depend entries that show
511 : : * what this object depends on.
512 : : */
8417 513 : 158273 : ScanKeyInit(&key[0],
514 : : Anum_pg_depend_classid,
515 : : BTEqualStrategyNumber, F_OIDEQ,
516 : 158273 : ObjectIdGetDatum(object->classId));
517 : 158273 : ScanKeyInit(&key[1],
518 : : Anum_pg_depend_objid,
519 : : BTEqualStrategyNumber, F_OIDEQ,
520 : 158273 : ObjectIdGetDatum(object->objectId));
8905 521 [ + + ]: 158273 : if (object->objectSubId != 0)
522 : : {
523 : : /* Consider only dependencies of this sub-object */
8417 524 : 1574 : ScanKeyInit(&key[2],
525 : : Anum_pg_depend_objsubid,
526 : : BTEqualStrategyNumber, F_INT4EQ,
527 : 1574 : Int32GetDatum(object->objectSubId));
8905 528 : 1574 : nkeys = 3;
529 : : }
530 : : else
531 : : {
532 : : /* Consider dependencies of this object and any sub-objects it has */
533 : 156699 : nkeys = 2;
534 : : }
535 : :
5106 536 : 158273 : scan = systable_beginscan(*depRel, DependDependerIndexId, true,
537 : : NULL, nkeys, key);
538 : :
539 : : /* initialize variables that loop may fill */
2847 540 : 158273 : memset(&owningObject, 0, sizeof(owningObject));
541 : 158273 : memset(&partitionObject, 0, sizeof(partitionObject));
542 : :
8905 543 [ + + ]: 376629 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
544 : : {
8851 bruce@momjian.us 545 : 219602 : Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
546 : :
8905 tgl@sss.pgh.pa.us 547 : 219602 : otherObject.classId = foundDep->refclassid;
548 : 219602 : otherObject.objectId = foundDep->refobjid;
549 : 219602 : otherObject.objectSubId = foundDep->refobjsubid;
550 : :
551 : : /*
552 : : * When scanning dependencies of a whole object, we may find rows
553 : : * linking sub-objects of the object to the object itself. (Normally,
554 : : * such a dependency is implicit, but we must make explicit ones in
555 : : * some cases involving partitioning.) We must ignore such rows to
556 : : * avoid infinite recursion.
557 : : */
2686 558 [ + + ]: 219602 : if (otherObject.classId == object->classId &&
559 [ + + ]: 73341 : otherObject.objectId == object->objectId &&
560 [ + + ]: 3100 : object->objectSubId == 0)
561 : 3084 : continue;
562 : :
8905 563 [ + + + + : 216518 : switch (foundDep->deptype)
+ - ]
564 : : {
565 : 124703 : case DEPENDENCY_NORMAL:
566 : : case DEPENDENCY_AUTO:
567 : : case DEPENDENCY_AUTO_EXTENSION:
568 : : /* no problem */
569 : 124703 : break;
570 : :
5772 571 : 3211 : case DEPENDENCY_EXTENSION:
572 : :
573 : : /*
574 : : * If told to, ignore EXTENSION dependencies altogether. This
575 : : * flag is normally used to prevent dropping extensions during
576 : : * temporary-object cleanup, even if a temp object was created
577 : : * during an extension script.
578 : : */
3648 579 [ + + ]: 3211 : if (flags & PERFORM_DELETION_SKIP_EXTENSIONS)
580 : 4 : break;
581 : :
582 : : /*
583 : : * If the other object is the extension currently being
584 : : * created/altered, ignore this dependency and continue with
585 : : * the deletion. This allows dropping of an extension's
586 : : * objects within the extension's scripts, as well as corner
587 : : * cases such as dropping a transient object created within
588 : : * such a script.
589 : : */
3654 590 [ + + ]: 3207 : if (creating_extension &&
591 [ + - ]: 201 : otherObject.classId == ExtensionRelationId &&
592 [ + - ]: 201 : otherObject.objectId == CurrentExtensionObject)
593 : 201 : break;
594 : :
595 : : /* Otherwise, treat this like an internal dependency */
596 : : pg_fallthrough;
597 : :
598 : : case DEPENDENCY_INTERNAL:
599 : :
600 : : /*
601 : : * This object is part of the internal implementation of
602 : : * another object, or is part of the extension that is the
603 : : * other object. We have three cases:
604 : : *
605 : : * 1. At the outermost recursion level, we must disallow the
606 : : * DROP. However, if the owning object is listed in
607 : : * pendingObjects, just release the caller's lock and return;
608 : : * we'll eventually complete the DROP when we reach that entry
609 : : * in the pending list.
610 : : *
611 : : * Note: the above statement is true only if this pg_depend
612 : : * entry still exists by then; in principle, therefore, we
613 : : * could miss deleting an item the user told us to delete.
614 : : * However, no inconsistency can result: since we're at outer
615 : : * level, there is no object depending on this one.
616 : : */
6747 617 [ + + ]: 84936 : if (stack == NULL)
618 : : {
6276 619 [ + - - + ]: 52 : if (pendingObjects &&
620 : 26 : object_address_present(&otherObject, pendingObjects))
621 : : {
6747 tgl@sss.pgh.pa.us 622 :UBC 0 : systable_endscan(scan);
623 : : /* need to release caller's lock; see notes below */
624 : 0 : ReleaseDeletionLock(object);
625 : 0 : return;
626 : : }
627 : :
628 : : /*
629 : : * We postpone actually issuing the error message until
630 : : * after this loop, so that we can make the behavior
631 : : * independent of the ordering of pg_depend entries, at
632 : : * least if there's not more than one INTERNAL and one
633 : : * EXTENSION dependency. (If there's more, we'll complain
634 : : * about a random one of them.) Prefer to complain about
635 : : * EXTENSION, since that's generally a more important
636 : : * dependency.
637 : : */
2847 tgl@sss.pgh.pa.us 638 [ - + ]:CBC 26 : if (!OidIsValid(owningObject.classId) ||
2847 tgl@sss.pgh.pa.us 639 [ # # ]:UBC 0 : foundDep->deptype == DEPENDENCY_EXTENSION)
2847 tgl@sss.pgh.pa.us 640 :CBC 26 : owningObject = otherObject;
641 : 26 : break;
642 : : }
643 : :
644 : : /*
645 : : * 2. When recursing from the other end of this dependency,
646 : : * it's okay to continue with the deletion. This holds when
647 : : * recursing from a whole object that includes the nominal
648 : : * other end as a component, too. Since there can be more
649 : : * than one "owning" object, we have to allow matches that are
650 : : * more than one level down in the stack.
651 : : */
5575 652 [ + + ]: 84910 : if (stack_address_present_add_flags(&otherObject, 0, stack))
8901 653 : 83664 : break;
654 : :
655 : : /*
656 : : * 3. Not all the owning objects have been visited, so
657 : : * transform this deletion request into a delete of this
658 : : * owning object.
659 : : *
660 : : * First, release caller's lock on this object and get
661 : : * deletion lock on the owning object. (We must release
662 : : * caller's lock to avoid deadlock against a concurrent
663 : : * deletion of the owning object.)
664 : : */
6747 665 : 1246 : ReleaseDeletionLock(object);
5349 simon@2ndQuadrant.co 666 : 1246 : AcquireDeletionLock(&otherObject, 0);
667 : :
668 : : /*
669 : : * The owning object might have been deleted while we waited
670 : : * to lock it; if so, neither it nor the current object are
671 : : * interesting anymore. We test this by checking the
672 : : * pg_depend entry (see notes below).
673 : : */
6747 tgl@sss.pgh.pa.us 674 [ - + ]: 1246 : if (!systable_recheck_tuple(scan, tup))
675 : : {
6747 tgl@sss.pgh.pa.us 676 :UBC 0 : systable_endscan(scan);
677 : 0 : ReleaseDeletionLock(&otherObject);
678 : 0 : return;
679 : : }
680 : :
681 : : /*
682 : : * One way or the other, we're done with the scan; might as
683 : : * well close it down before recursing, to reduce peak
684 : : * resource consumption.
685 : : */
2847 tgl@sss.pgh.pa.us 686 :CBC 1246 : systable_endscan(scan);
687 : :
688 : : /*
689 : : * Okay, recurse to the owning object instead of proceeding.
690 : : *
691 : : * We do not need to stack the current object; we want the
692 : : * traversal order to be as if the original reference had
693 : : * linked to the owning object instead of this one.
694 : : *
695 : : * The dependency type is a "reverse" dependency: we need to
696 : : * delete the owning object if this one is to be deleted, but
697 : : * this linkage is never a reason for an automatic deletion.
698 : : */
6747 699 : 1246 : findDependentObjects(&otherObject,
700 : : DEPFLAG_REVERSE,
701 : : flags,
702 : : stack,
703 : : targetObjects,
704 : : pendingObjects,
705 : : depRel);
706 : :
707 : : /*
708 : : * The current target object should have been added to
709 : : * targetObjects while processing the owning object; but it
710 : : * probably got only the flag bits associated with the
711 : : * dependency we're looking at. We need to add the objflags
712 : : * that were passed to this recursion level, too, else we may
713 : : * get a bogus failure in reportDependentObjects (if, for
714 : : * example, we were called due to a partition dependency).
715 : : *
716 : : * If somehow the current object didn't get scheduled for
717 : : * deletion, bleat. (That would imply that somebody deleted
718 : : * this dependency record before the recursion got to it.)
719 : : * Another idea would be to reacquire lock on the current
720 : : * object and resume trying to delete it, but it seems not
721 : : * worth dealing with the race conditions inherent in that.
722 : : */
2847 723 [ - + ]: 1246 : if (!object_address_present_add_flags(object, objflags,
724 : : targetObjects))
2847 tgl@sss.pgh.pa.us 725 [ # # ]:UBC 0 : elog(ERROR, "deletion of owning object %s failed to delete %s",
726 : : getObjectDescription(&otherObject, false),
727 : : getObjectDescription(object, false));
728 : :
729 : : /* And we're done here. */
6747 tgl@sss.pgh.pa.us 730 :CBC 1246 : return;
731 : :
2847 732 : 3337 : case DEPENDENCY_PARTITION_PRI:
733 : :
734 : : /*
735 : : * Remember that this object has a partition-type dependency.
736 : : * After the dependency scan, we'll complain if we didn't find
737 : : * a reason to delete one of its partition dependencies.
738 : : */
739 : 3337 : objflags |= DEPFLAG_IS_PART;
740 : :
741 : : /*
742 : : * Also remember the primary partition owner, for error
743 : : * messages. If there are multiple primary owners (which
744 : : * there should not be), we'll report a random one of them.
745 : : */
746 : 3337 : partitionObject = otherObject;
747 : 3337 : break;
748 : :
749 : 3337 : case DEPENDENCY_PARTITION_SEC:
750 : :
751 : : /*
752 : : * Only use secondary partition owners in error messages if we
753 : : * find no primary owner (which probably shouldn't happen).
754 : : */
755 [ + + ]: 3337 : if (!(objflags & DEPFLAG_IS_PART))
2847 tgl@sss.pgh.pa.us 756 :GBC 4 : partitionObject = otherObject;
757 : :
758 : : /*
759 : : * Remember that this object has a partition-type dependency.
760 : : * After the dependency scan, we'll complain if we didn't find
761 : : * a reason to delete one of its partition dependencies.
762 : : */
2847 tgl@sss.pgh.pa.us 763 :CBC 3337 : objflags |= DEPFLAG_IS_PART;
764 : 3337 : break;
765 : :
8905 tgl@sss.pgh.pa.us 766 :UBC 0 : default:
8531 767 [ # # ]: 0 : elog(ERROR, "unrecognized dependency type '%c' for %s",
768 : : foundDep->deptype, getObjectDescription(object, false));
769 : : break;
770 : : }
771 : : }
772 : :
8905 tgl@sss.pgh.pa.us 773 :CBC 157027 : systable_endscan(scan);
774 : :
775 : : /*
776 : : * If we found an INTERNAL or EXTENSION dependency when we're at outer
777 : : * level, complain about it now. If we also found a PARTITION dependency,
778 : : * we prefer to report the PARTITION dependency. This is arbitrary but
779 : : * seems to be more useful in practice.
780 : : */
2847 781 [ + + ]: 157027 : if (OidIsValid(owningObject.classId))
782 : : {
783 : : char *otherObjDesc;
784 : :
785 [ + + ]: 26 : if (OidIsValid(partitionObject.classId))
2327 michael@paquier.xyz 786 : 8 : otherObjDesc = getObjectDescription(&partitionObject, false);
787 : : else
788 : 18 : otherObjDesc = getObjectDescription(&owningObject, false);
789 : :
2847 tgl@sss.pgh.pa.us 790 [ + - ]: 26 : ereport(ERROR,
791 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
792 : : errmsg("cannot drop %s because %s requires it",
793 : : getObjectDescription(object, false), otherObjDesc),
794 : : errhint("You can drop %s instead.", otherObjDesc)));
795 : : }
796 : :
797 : : /*
798 : : * Next, identify all objects that directly depend on the current object.
799 : : * To ensure predictable deletion order, we collect them up in
800 : : * dependentObjects and sort the list before actually recursing. (The
801 : : * deletion order would be valid in any case, but doing this ensures
802 : : * consistent output from DROP CASCADE commands, which is helpful for
803 : : * regression testing.)
804 : : */
2868 805 : 157001 : maxDependentObjects = 128; /* arbitrary initial allocation */
353 michael@paquier.xyz 806 : 157001 : dependentObjects = palloc_array(ObjectAddressAndFlags, maxDependentObjects);
2868 tgl@sss.pgh.pa.us 807 : 157001 : numDependentObjects = 0;
808 : :
8417 809 : 157001 : ScanKeyInit(&key[0],
810 : : Anum_pg_depend_refclassid,
811 : : BTEqualStrategyNumber, F_OIDEQ,
812 : 157001 : ObjectIdGetDatum(object->classId));
813 : 157001 : ScanKeyInit(&key[1],
814 : : Anum_pg_depend_refobjid,
815 : : BTEqualStrategyNumber, F_OIDEQ,
816 : 157001 : ObjectIdGetDatum(object->objectId));
8905 817 [ + + ]: 157001 : if (object->objectSubId != 0)
818 : : {
8417 819 : 1558 : ScanKeyInit(&key[2],
820 : : Anum_pg_depend_refobjsubid,
821 : : BTEqualStrategyNumber, F_INT4EQ,
822 : 1558 : Int32GetDatum(object->objectSubId));
8905 823 : 1558 : nkeys = 3;
824 : : }
825 : : else
826 : 155443 : nkeys = 2;
827 : :
5106 828 : 157001 : scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
829 : : NULL, nkeys, key);
830 : :
8905 831 [ + + ]: 321538 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
832 : : {
8851 bruce@momjian.us 833 : 164541 : Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
834 : : int subflags;
835 : :
8905 tgl@sss.pgh.pa.us 836 : 164541 : otherObject.classId = foundDep->classid;
837 : 164541 : otherObject.objectId = foundDep->objid;
838 : 164541 : otherObject.objectSubId = foundDep->objsubid;
839 : :
840 : : /*
841 : : * If what we found is a sub-object of the current object, just ignore
842 : : * it. (Normally, such a dependency is implicit, but we must make
843 : : * explicit ones in some cases involving partitioning.)
844 : : */
2686 845 [ + + ]: 164541 : if (otherObject.classId == object->classId &&
846 [ + + ]: 70194 : otherObject.objectId == object->objectId &&
847 [ + - ]: 3084 : object->objectSubId == 0)
848 : 3084 : continue;
849 : :
850 : : /*
851 : : * Must lock the dependent object before recursing to it.
852 : : */
5349 simon@2ndQuadrant.co 853 : 161457 : AcquireDeletionLock(&otherObject, 0);
854 : :
855 : : /*
856 : : * The dependent object might have been deleted while we waited to
857 : : * lock it; if so, we don't need to do anything more with it. We can
858 : : * test this cheaply and independently of the object's type by seeing
859 : : * if the pg_depend tuple we are looking at is still live. (If the
860 : : * object got deleted, the tuple would have been deleted too.)
861 : : */
6747 tgl@sss.pgh.pa.us 862 [ - + ]: 161457 : if (!systable_recheck_tuple(scan, tup))
863 : : {
864 : : /* release the now-useless lock */
6747 tgl@sss.pgh.pa.us 865 :UBC 0 : ReleaseDeletionLock(&otherObject);
866 : : /* and continue scanning for dependencies */
867 : 0 : continue;
868 : : }
869 : :
870 : : /*
871 : : * Check that the dependent object is not in a shared catalog, which
872 : : * is not supported by doDeletion().
873 : : */
117 jdavis@postgresql.or 874 [ + + ]:CBC 161457 : if (IsSharedRelation(otherObject.classId))
875 : : {
876 : 4 : char *otherObjDesc = getObjectDescription(&otherObject,
877 : : false);
878 : :
879 [ + - ]: 4 : ereport(ERROR,
880 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
881 : : errmsg("cannot drop %s because %s depends on it",
882 : : getObjectDescription(object, false), otherObjDesc),
883 : : errhint("Drop %s first.", otherObjDesc)));
884 : : }
885 : :
886 : : /*
887 : : * We do need to delete it, so identify objflags to be passed down,
888 : : * which depend on the dependency type.
889 : : */
8905 tgl@sss.pgh.pa.us 890 [ + + + + : 161453 : switch (foundDep->deptype)
+ - ]
891 : : {
892 : 21864 : case DEPENDENCY_NORMAL:
6747 893 : 21864 : subflags = DEPFLAG_NORMAL;
8905 894 : 21864 : break;
895 : 49869 : case DEPENDENCY_AUTO:
896 : : case DEPENDENCY_AUTO_EXTENSION:
6747 897 : 49869 : subflags = DEPFLAG_AUTO;
898 : 49869 : break;
8905 899 : 80663 : case DEPENDENCY_INTERNAL:
6747 900 : 80663 : subflags = DEPFLAG_INTERNAL;
8905 901 : 80663 : break;
2847 902 : 6099 : case DEPENDENCY_PARTITION_PRI:
903 : : case DEPENDENCY_PARTITION_SEC:
904 : 6099 : subflags = DEPFLAG_PARTITION;
905 : 6099 : break;
5772 906 : 2958 : case DEPENDENCY_EXTENSION:
907 : 2958 : subflags = DEPFLAG_EXTENSION;
908 : 2958 : break;
8905 tgl@sss.pgh.pa.us 909 :UBC 0 : default:
8531 910 [ # # ]: 0 : elog(ERROR, "unrecognized dependency type '%c' for %s",
911 : : foundDep->deptype, getObjectDescription(object, false));
912 : : subflags = 0; /* keep compiler quiet */
913 : : break;
914 : : }
915 : :
916 : : /* And add it to the pending-objects list */
2868 tgl@sss.pgh.pa.us 917 [ + + ]:CBC 161453 : if (numDependentObjects >= maxDependentObjects)
918 : : {
919 : : /* enlarge array if needed */
920 : 24 : maxDependentObjects *= 2;
103 michael@paquier.xyz 921 :GNC 24 : dependentObjects = repalloc_array(dependentObjects,
922 : : ObjectAddressAndFlags,
923 : : maxDependentObjects);
924 : : }
925 : :
2868 tgl@sss.pgh.pa.us 926 :CBC 161453 : dependentObjects[numDependentObjects].obj = otherObject;
927 : 161453 : dependentObjects[numDependentObjects].subflags = subflags;
928 : 161453 : numDependentObjects++;
929 : : }
930 : :
931 : 156997 : systable_endscan(scan);
932 : :
933 : : /*
934 : : * Now we can sort the dependent objects into a stable visitation order.
935 : : * It's safe to use object_address_comparator here since the obj field is
936 : : * first within ObjectAddressAndFlags.
937 : : */
938 [ + + ]: 156997 : if (numDependentObjects > 1)
1390 peter@eisentraut.org 939 : 34303 : qsort(dependentObjects, numDependentObjects,
940 : : sizeof(ObjectAddressAndFlags),
941 : : object_address_comparator);
942 : :
943 : : /*
944 : : * Now recurse to the dependent objects. We must visit them first since
945 : : * they have to be deleted before the current object.
946 : : */
2868 tgl@sss.pgh.pa.us 947 : 156997 : mystack.object = object; /* set up a new stack level */
948 : 156997 : mystack.flags = objflags;
949 : 156997 : mystack.next = stack;
950 : :
951 [ + + ]: 318446 : for (int i = 0; i < numDependentObjects; i++)
952 : : {
953 : 161449 : ObjectAddressAndFlags *depObj = dependentObjects + i;
954 : :
955 : 161449 : findDependentObjects(&depObj->obj,
956 : : depObj->subflags,
957 : : flags,
958 : : &mystack,
959 : : targetObjects,
960 : : pendingObjects,
961 : : depRel);
962 : : }
963 : :
964 : 156997 : pfree(dependentObjects);
965 : :
966 : : /*
967 : : * Finally, we can add the target object to targetObjects. Be careful to
968 : : * include any flags that were passed back down to us from inner recursion
969 : : * levels. Record the "dependee" as being either the most important
970 : : * partition owner if there is one, else the object we recursed from, if
971 : : * any. (The logic in reportDependentObjects() is such that it can only
972 : : * need one of those objects.)
973 : : */
6747 974 : 156997 : extra.flags = mystack.flags;
2847 975 [ + + ]: 156997 : if (extra.flags & DEPFLAG_IS_PART)
976 : 3329 : extra.dependee = partitionObject;
977 [ + + ]: 153668 : else if (stack)
6747 978 : 125824 : extra.dependee = *stack->object;
979 : : else
980 : 27844 : memset(&extra.dependee, 0, sizeof(extra.dependee));
981 : 156997 : add_exact_object_address_extra(object, &extra, targetObjects);
982 : : }
983 : :
984 : : /*
985 : : * reportDependentObjects - report about dependencies, and fail if RESTRICT
986 : : *
987 : : * Tell the user about dependent objects that we are going to delete
988 : : * (or would need to delete, but are prevented by RESTRICT mode);
989 : : * then error out if there are any and it's not CASCADE mode.
990 : : *
991 : : * targetObjects: list of objects that are scheduled to be deleted
992 : : * behavior: RESTRICT or CASCADE
993 : : * flags: other flags for the deletion operation
994 : : * origObject: base object of deletion, or NULL if not available
995 : : * (the latter case occurs in DROP OWNED)
996 : : */
997 : : static void
998 : 24397 : reportDependentObjects(const ObjectAddresses *targetObjects,
999 : : DropBehavior behavior,
1000 : : int flags,
1001 : : const ObjectAddress *origObject)
1002 : : {
3648 1003 [ + + ]: 24397 : int msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
6747 1004 : 24397 : bool ok = true;
1005 : : StringInfoData clientdetail;
1006 : : StringInfoData logdetail;
6744 1007 : 24397 : int numReportedClient = 0;
1008 : 24397 : int numNotReportedClient = 0;
1009 : : int i;
1010 : :
1011 : : /*
1012 : : * If we need to delete any partition-dependent objects, make sure that
1013 : : * we're deleting at least one of their partition dependencies, too. That
1014 : : * can be detected by checking that we reached them by a PARTITION
1015 : : * dependency at some point.
1016 : : *
1017 : : * We just report the first such object, as in most cases the only way to
1018 : : * trigger this complaint is to explicitly try to delete one partition of
1019 : : * a partitioned object.
1020 : : */
2847 1021 [ + + ]: 181374 : for (i = 0; i < targetObjects->numrefs; i++)
1022 : : {
1023 : 156997 : const ObjectAddressExtra *extra = &targetObjects->extras[i];
1024 : :
1025 [ + + ]: 156997 : if ((extra->flags & DEPFLAG_IS_PART) &&
1026 [ + + ]: 3329 : !(extra->flags & DEPFLAG_PARTITION))
1027 : : {
1028 : 20 : const ObjectAddress *object = &targetObjects->refs[i];
2327 michael@paquier.xyz 1029 : 20 : char *otherObjDesc = getObjectDescription(&extra->dependee,
1030 : : false);
1031 : :
2847 tgl@sss.pgh.pa.us 1032 [ + - ]: 20 : ereport(ERROR,
1033 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1034 : : errmsg("cannot drop %s because %s requires it",
1035 : : getObjectDescription(object, false), otherObjDesc),
1036 : : errhint("You can drop %s instead.", otherObjDesc)));
1037 : : }
1038 : : }
1039 : :
1040 : : /*
1041 : : * If no error is to be thrown, and the msglevel is too low to be shown to
1042 : : * either client or server log, there's no need to do any of the rest of
1043 : : * the work.
1044 : : */
6744 1045 [ + + ]: 24377 : if (behavior == DROP_CASCADE &&
2196 1046 [ + + ]: 2328 : !message_level_is_interesting(msglevel))
6744 1047 : 646 : return;
1048 : :
1049 : : /*
1050 : : * We limit the number of dependencies reported to the client to
1051 : : * MAX_REPORTED_DEPS, since client software may not deal well with
1052 : : * enormous error strings. The server log always gets a full report.
1053 : : */
1054 : : #define MAX_REPORTED_DEPS 100
1055 : :
1056 : 23731 : initStringInfo(&clientdetail);
1057 : 23731 : initStringInfo(&logdetail);
1058 : :
1059 : : /*
1060 : : * We process the list back to front (ie, in dependency order not deletion
1061 : : * order), since this makes for a more understandable display.
1062 : : */
6747 1063 [ + + ]: 172151 : for (i = targetObjects->numrefs - 1; i >= 0; i--)
1064 : : {
1065 : 148420 : const ObjectAddress *obj = &targetObjects->refs[i];
1066 : 148420 : const ObjectAddressExtra *extra = &targetObjects->extras[i];
1067 : : char *objDesc;
1068 : :
1069 : : /* Ignore the original deletion target(s) */
1070 [ + + ]: 148420 : if (extra->flags & DEPFLAG_ORIGINAL)
1071 : 27780 : continue;
1072 : :
1073 : : /* Also ignore sub-objects; we'll report the whole object elsewhere */
2871 1074 [ + + ]: 120640 : if (extra->flags & DEPFLAG_SUBOBJECT)
1075 : 24 : continue;
1076 : :
2327 michael@paquier.xyz 1077 : 120616 : objDesc = getObjectDescription(obj, false);
1078 : :
1079 : : /* An object being dropped concurrently doesn't need to be reported */
1849 alvherre@alvh.no-ip. 1080 [ - + ]: 120616 : if (objDesc == NULL)
1849 alvherre@alvh.no-ip. 1081 :UBC 0 : continue;
1082 : :
1083 : : /*
1084 : : * If, at any stage of the recursive search, we reached the object via
1085 : : * an AUTO, INTERNAL, PARTITION, or EXTENSION dependency, then it's
1086 : : * okay to delete it even in RESTRICT mode.
1087 : : */
5772 tgl@sss.pgh.pa.us 1088 [ + + ]:CBC 120616 : if (extra->flags & (DEPFLAG_AUTO |
1089 : : DEPFLAG_INTERNAL |
1090 : : DEPFLAG_PARTITION |
1091 : : DEPFLAG_EXTENSION))
1092 : : {
1093 : : /*
1094 : : * auto-cascades are reported at DEBUG2, not msglevel. We don't
1095 : : * try to combine them with the regular message because the
1096 : : * results are too confusing when client_min_messages and
1097 : : * log_min_messages are different.
1098 : : */
6747 1099 [ + + ]: 115056 : ereport(DEBUG2,
1100 : : (errmsg_internal("drop auto-cascades to %s",
1101 : : objDesc)));
1102 : : }
1103 [ + + ]: 5560 : else if (behavior == DROP_RESTRICT)
1104 : : {
2327 michael@paquier.xyz 1105 : 403 : char *otherDesc = getObjectDescription(&extra->dependee,
1106 : : false);
1107 : :
1849 alvherre@alvh.no-ip. 1108 [ + - ]: 403 : if (otherDesc)
1109 : : {
1110 [ + - ]: 403 : if (numReportedClient < MAX_REPORTED_DEPS)
1111 : : {
1112 : : /* separate entries with a newline */
1113 [ + + ]: 403 : if (clientdetail.len != 0)
1114 : 145 : appendStringInfoChar(&clientdetail, '\n');
1115 : 403 : appendStringInfo(&clientdetail, _("%s depends on %s"),
1116 : : objDesc, otherDesc);
1117 : 403 : numReportedClient++;
1118 : : }
1119 : : else
1849 alvherre@alvh.no-ip. 1120 :UBC 0 : numNotReportedClient++;
1121 : : /* separate entries with a newline */
1849 alvherre@alvh.no-ip. 1122 [ + + ]:CBC 403 : if (logdetail.len != 0)
1123 : 145 : appendStringInfoChar(&logdetail, '\n');
1124 : 403 : appendStringInfo(&logdetail, _("%s depends on %s"),
1125 : : objDesc, otherDesc);
1126 : 403 : pfree(otherDesc);
1127 : : }
1128 : : else
6744 tgl@sss.pgh.pa.us 1129 :UBC 0 : numNotReportedClient++;
6747 tgl@sss.pgh.pa.us 1130 :CBC 403 : ok = false;
1131 : : }
1132 : : else
1133 : : {
6744 1134 [ + + ]: 5157 : if (numReportedClient < MAX_REPORTED_DEPS)
1135 : : {
1136 : : /* separate entries with a newline */
1137 [ + + ]: 4238 : if (clientdetail.len != 0)
1138 : 3221 : appendStringInfoChar(&clientdetail, '\n');
1139 : 4238 : appendStringInfo(&clientdetail, _("drop cascades to %s"),
1140 : : objDesc);
1141 : 4238 : numReportedClient++;
1142 : : }
1143 : : else
1144 : 919 : numNotReportedClient++;
1145 : : /* separate entries with a newline */
1146 [ + + ]: 5157 : if (logdetail.len != 0)
1147 : 4140 : appendStringInfoChar(&logdetail, '\n');
1148 : 5157 : appendStringInfo(&logdetail, _("drop cascades to %s"),
1149 : : objDesc);
1150 : : }
1151 : :
1152 : 120616 : pfree(objDesc);
1153 : : }
1154 : :
1155 [ + + ]: 23731 : if (numNotReportedClient > 0)
6456 peter_e@gmx.net 1156 : 10 : appendStringInfo(&clientdetail, ngettext("\nand %d other object "
1157 : : "(see server log for list)",
1158 : : "\nand %d other objects "
1159 : : "(see server log for list)",
1160 : : numNotReportedClient),
1161 : : numNotReportedClient);
1162 : :
6747 tgl@sss.pgh.pa.us 1163 [ + + ]: 23731 : if (!ok)
1164 : : {
1165 [ + + ]: 258 : if (origObject)
1166 [ + - ]: 254 : ereport(ERROR,
1167 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1168 : : errmsg("cannot drop %s because other objects depend on it",
1169 : : getObjectDescription(origObject, false)),
1170 : : errdetail_internal("%s", clientdetail.data),
1171 : : errdetail_log("%s", logdetail.data),
1172 : : errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
1173 : : else
1174 [ + - ]: 4 : ereport(ERROR,
1175 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1176 : : errmsg("cannot drop desired object(s) because other objects depend on them"),
1177 : : errdetail_internal("%s", clientdetail.data),
1178 : : errdetail_log("%s", logdetail.data),
1179 : : errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
1180 : : }
6744 1181 [ + + ]: 23473 : else if (numReportedClient > 1)
1182 : : {
1183 [ + - ]: 472 : ereport(msglevel,
1184 : : (errmsg_plural("drop cascades to %d other object",
1185 : : "drop cascades to %d other objects",
1186 : : numReportedClient + numNotReportedClient,
1187 : : numReportedClient + numNotReportedClient),
1188 : : errdetail_internal("%s", clientdetail.data),
1189 : : errdetail_log("%s", logdetail.data)));
1190 : : }
1191 [ + + ]: 23001 : else if (numReportedClient == 1)
1192 : : {
1193 : : /* we just use the single item as-is */
1194 [ + - ]: 545 : ereport(msglevel,
1195 : : (errmsg_internal("%s", clientdetail.data)));
1196 : : }
1197 : :
1198 : 23473 : pfree(clientdetail.data);
1199 : 23473 : pfree(logdetail.data);
1200 : : }
1201 : :
1202 : : /*
1203 : : * Drop an object by OID. Works for most catalogs, if no special processing
1204 : : * is needed.
1205 : : */
1206 : : static void
2363 peter@eisentraut.org 1207 : 3636 : DropObjectById(const ObjectAddress *object)
1208 : : {
1209 : : SysCacheIdentifier cacheId;
1210 : : Relation rel;
1211 : : HeapTuple tup;
1212 : :
1213 : 3636 : cacheId = get_object_catcache_oid(object->classId);
1214 : :
1215 : 3636 : rel = table_open(object->classId, RowExclusiveLock);
1216 : :
1217 : : /*
1218 : : * Use the system cache for the oid column, if one exists.
1219 : : */
1220 [ + + ]: 3636 : if (cacheId >= 0)
1221 : : {
1222 : 1419 : tup = SearchSysCache1(cacheId, ObjectIdGetDatum(object->objectId));
1223 [ - + ]: 1419 : if (!HeapTupleIsValid(tup))
2363 peter@eisentraut.org 1224 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for %s %u",
1225 : : get_object_class_descr(object->classId), object->objectId);
1226 : :
2363 peter@eisentraut.org 1227 :CBC 1419 : CatalogTupleDelete(rel, &tup->t_self);
1228 : :
1229 : 1419 : ReleaseSysCache(tup);
1230 : : }
1231 : : else
1232 : : {
1233 : : ScanKeyData skey[1];
1234 : : SysScanDesc scan;
1235 : :
1236 : 2217 : ScanKeyInit(&skey[0],
1237 : 2217 : get_object_attnum_oid(object->classId),
1238 : : BTEqualStrategyNumber, F_OIDEQ,
1239 : 2217 : ObjectIdGetDatum(object->objectId));
1240 : :
1241 : 2217 : scan = systable_beginscan(rel, get_object_oid_index(object->classId), true,
1242 : : NULL, 1, skey);
1243 : :
1244 : : /* we expect exactly one match */
1245 : 2217 : tup = systable_getnext(scan);
1246 [ - + ]: 2217 : if (!HeapTupleIsValid(tup))
2363 peter@eisentraut.org 1247 [ # # ]:UBC 0 : elog(ERROR, "could not find tuple for %s %u",
1248 : : get_object_class_descr(object->classId), object->objectId);
1249 : :
2363 peter@eisentraut.org 1250 :CBC 2217 : CatalogTupleDelete(rel, &tup->t_self);
1251 : :
1252 : 2217 : systable_endscan(scan);
1253 : : }
1254 : :
1255 : 3636 : table_close(rel, RowExclusiveLock);
1256 : 3636 : }
1257 : :
1258 : : /*
1259 : : * deleteOneObject: delete a single object for performDeletion.
1260 : : *
1261 : : * *depRel is the already-open pg_depend relation.
1262 : : */
1263 : : static void
5106 tgl@sss.pgh.pa.us 1264 : 153490 : deleteOneObject(const ObjectAddress *object, Relation *depRel, int flags)
1265 : : {
1266 : : ScanKeyData key[3];
1267 : : int nkeys;
1268 : : SysScanDesc scan;
1269 : : HeapTuple tup;
1270 : :
1271 : : /* DROP hook of the objects being removed */
5015 rhaas@postgresql.org 1272 [ + + ]: 153490 : InvokeObjectDropHookArg(object->classId, object->objectId,
1273 : : object->objectSubId, flags);
1274 : :
1275 : : /*
1276 : : * Close depRel if we are doing a drop concurrently. The object deletion
1277 : : * subroutine will commit the current transaction, so we can't keep the
1278 : : * relation open across doDeletion().
1279 : : */
5153 simon@2ndQuadrant.co 1280 [ + + ]: 153490 : if (flags & PERFORM_DELETION_CONCURRENTLY)
2868 andres@anarazel.de 1281 : 76 : table_close(*depRel, RowExclusiveLock);
1282 : :
1283 : : /*
1284 : : * Delete the object itself, in an object-type-dependent way.
1285 : : *
1286 : : * We used to do this after removing the outgoing dependency links, but it
1287 : : * seems just as reasonable to do it beforehand. In the concurrent case
1288 : : * we *must* do it in this order, because we can't make any transactional
1289 : : * updates before calling doDeletion() --- they'd get committed right
1290 : : * away, which is not cool if the deletion then fails.
1291 : : */
5153 simon@2ndQuadrant.co 1292 : 153490 : doDeletion(object, flags);
1293 : :
1294 : : /*
1295 : : * Reopen depRel if we closed it above
1296 : : */
1297 [ + + ]: 153476 : if (flags & PERFORM_DELETION_CONCURRENTLY)
2868 andres@anarazel.de 1298 : 76 : *depRel = table_open(DependRelationId, RowExclusiveLock);
1299 : :
1300 : : /*
1301 : : * Now remove any pg_depend records that link from this object to others.
1302 : : * (Any records linking to this object should be gone already.)
1303 : : *
1304 : : * When dropping a whole object (subId = 0), remove all pg_depend records
1305 : : * for its sub-objects too.
1306 : : */
6747 tgl@sss.pgh.pa.us 1307 : 153476 : ScanKeyInit(&key[0],
1308 : : Anum_pg_depend_classid,
1309 : : BTEqualStrategyNumber, F_OIDEQ,
1310 : 153476 : ObjectIdGetDatum(object->classId));
1311 : 153476 : ScanKeyInit(&key[1],
1312 : : Anum_pg_depend_objid,
1313 : : BTEqualStrategyNumber, F_OIDEQ,
1314 : 153476 : ObjectIdGetDatum(object->objectId));
1315 [ + + ]: 153476 : if (object->objectSubId != 0)
1316 : : {
1317 : 1475 : ScanKeyInit(&key[2],
1318 : : Anum_pg_depend_objsubid,
1319 : : BTEqualStrategyNumber, F_INT4EQ,
1320 : 1475 : Int32GetDatum(object->objectSubId));
1321 : 1475 : nkeys = 3;
1322 : : }
1323 : : else
1324 : 152001 : nkeys = 2;
1325 : :
5106 1326 : 153476 : scan = systable_beginscan(*depRel, DependDependerIndexId, true,
1327 : : NULL, nkeys, key);
1328 : :
6747 1329 [ + + ]: 365233 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
1330 : : {
3587 1331 : 211757 : CatalogTupleDelete(*depRel, &tup->t_self);
1332 : : }
1333 : :
6747 1334 : 153476 : systable_endscan(scan);
1335 : :
1336 : : /*
1337 : : * Delete shared dependency references related to this object. Again, if
1338 : : * subId = 0, remove records for sub-objects too.
1339 : : */
6519 1340 : 153476 : deleteSharedDependencyRecordsFor(object->classId, object->objectId,
1341 : 153476 : object->objectSubId);
1342 : :
1343 : :
1344 : : /*
1345 : : * Delete any comments, security labels, or initial privileges associated
1346 : : * with this object. (This is a convenient place to do these things,
1347 : : * rather than having every object type know to do it.) As above, all
1348 : : * these functions must remove records for sub-objects too if the subid is
1349 : : * zero.
1350 : : */
6747 1351 : 153476 : DeleteComments(object->objectId, object->classId, object->objectSubId);
5906 rhaas@postgresql.org 1352 : 153476 : DeleteSecurityLabel(object);
3888 sfrost@snowman.net 1353 : 153476 : DeleteInitPrivs(object);
1354 : :
1355 : : /*
1356 : : * CommandCounterIncrement here to ensure that preceding changes are all
1357 : : * visible to the next deletion step.
1358 : : */
6747 tgl@sss.pgh.pa.us 1359 : 153476 : CommandCounterIncrement();
1360 : :
1361 : : /*
1362 : : * And we're done!
1363 : : */
1364 : 153476 : }
1365 : :
1366 : : /*
1367 : : * doDeletion: actually delete a single object
1368 : : */
1369 : : static void
5349 simon@2ndQuadrant.co 1370 : 153490 : doDeletion(const ObjectAddress *object, int flags)
1371 : : {
977 peter@eisentraut.org 1372 [ + + + + : 153490 : switch (object->classId)
+ + + + +
+ + + + +
+ + + -
- ]
1373 : : {
1374 : 52847 : case RelationRelationId:
1375 : : {
8836 tgl@sss.pgh.pa.us 1376 : 52847 : char relKind = get_rel_relkind(object->objectId);
1377 : :
3235 alvherre@alvh.no-ip. 1378 [ + + + + ]: 52847 : if (relKind == RELKIND_INDEX ||
1379 : : relKind == RELKIND_PARTITIONED_INDEX)
8851 bruce@momjian.us 1380 : 16622 : {
3648 tgl@sss.pgh.pa.us 1381 : 16630 : bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
2801 peter@eisentraut.org 1382 : 16630 : bool concurrent_lock_mode = ((flags & PERFORM_DELETION_CONCURRENT_LOCK) != 0);
1383 : :
8851 bruce@momjian.us 1384 [ - + ]: 16630 : Assert(object->objectSubId == 0);
2801 peter@eisentraut.org 1385 : 16630 : index_drop(object->objectId, concurrent, concurrent_lock_mode);
1386 : : }
1387 : : else
1388 : : {
8851 bruce@momjian.us 1389 [ + + ]: 36217 : if (object->objectSubId != 0)
1390 : 1475 : RemoveAttributeById(object->objectId,
1391 : 1475 : object->objectSubId);
1392 : : else
1393 : 34742 : heap_drop_with_catalog(object->objectId);
1394 : : }
1395 : :
1396 : : /*
1397 : : * for a sequence, in addition to dropping the heap, also
1398 : : * delete pg_sequence tuple
1399 : : */
3630 peter_e@gmx.net 1400 [ + + ]: 52835 : if (relKind == RELKIND_SEQUENCE)
1401 : 684 : DeleteSequenceTuple(object->objectId);
8851 bruce@momjian.us 1402 : 52835 : break;
1403 : : }
1404 : :
977 peter@eisentraut.org 1405 : 5974 : case ProcedureRelationId:
8905 tgl@sss.pgh.pa.us 1406 : 5974 : RemoveFunctionById(object->objectId);
1407 : 5974 : break;
1408 : :
977 peter@eisentraut.org 1409 : 55515 : case TypeRelationId:
8905 tgl@sss.pgh.pa.us 1410 : 55515 : RemoveTypeById(object->objectId);
1411 : 55515 : break;
1412 : :
977 peter@eisentraut.org 1413 : 18245 : case ConstraintRelationId:
8905 tgl@sss.pgh.pa.us 1414 : 18245 : RemoveConstraintById(object->objectId);
1415 : 18244 : break;
1416 : :
977 peter@eisentraut.org 1417 : 2427 : case AttrDefaultRelationId:
8902 tgl@sss.pgh.pa.us 1418 : 2427 : RemoveAttrDefaultById(object->objectId);
1419 : 2427 : break;
1420 : :
977 peter@eisentraut.org 1421 : 61 : case LargeObjectRelationId:
6196 itagaki.takahiro@gma 1422 : 61 : LargeObjectDrop(object->objectId);
1423 : 61 : break;
1424 : :
977 peter@eisentraut.org 1425 : 490 : case OperatorRelationId:
8905 tgl@sss.pgh.pa.us 1426 : 490 : RemoveOperatorById(object->objectId);
1427 : 490 : break;
1428 : :
977 peter@eisentraut.org 1429 : 2179 : case RewriteRelationId:
8905 tgl@sss.pgh.pa.us 1430 : 2179 : RemoveRewriteRuleById(object->objectId);
1431 : 2178 : break;
1432 : :
977 peter@eisentraut.org 1433 : 9711 : case TriggerRelationId:
8905 tgl@sss.pgh.pa.us 1434 : 9711 : RemoveTriggerById(object->objectId);
1435 : 9711 : break;
1436 : :
977 peter@eisentraut.org 1437 : 585 : case StatisticExtRelationId:
3485 tgl@sss.pgh.pa.us 1438 : 585 : RemoveStatisticsById(object->objectId);
1439 : 585 : break;
1440 : :
977 peter@eisentraut.org 1441 : 32 : case TSConfigRelationId:
7039 tgl@sss.pgh.pa.us 1442 : 32 : RemoveTSConfigurationById(object->objectId);
1443 : 32 : break;
1444 : :
977 peter@eisentraut.org 1445 : 124 : case ExtensionRelationId:
5772 tgl@sss.pgh.pa.us 1446 : 124 : RemoveExtensionById(object->objectId);
1447 : 124 : break;
1448 : :
977 peter@eisentraut.org 1449 : 466 : case PolicyRelationId:
4453 sfrost@snowman.net 1450 : 466 : RemovePolicyById(object->objectId);
1451 : 466 : break;
1452 : :
977 peter@eisentraut.org 1453 : 132 : case PublicationNamespaceRelationId:
1858 akapila@postgresql.o 1454 : 132 : RemovePublicationSchemaById(object->objectId);
1455 : 132 : break;
1456 : :
977 peter@eisentraut.org 1457 : 659 : case PublicationRelRelationId:
3600 peter_e@gmx.net 1458 : 659 : RemovePublicationRelById(object->objectId);
1459 : 659 : break;
1460 : :
977 peter@eisentraut.org 1461 : 407 : case PublicationRelationId:
1907 akapila@postgresql.o 1462 : 407 : RemovePublicationById(object->objectId);
1463 : 407 : break;
1464 : :
977 peter@eisentraut.org 1465 : 3636 : case CastRelationId:
1466 : : case CollationRelationId:
1467 : : case ConversionRelationId:
1468 : : case LanguageRelationId:
1469 : : case OperatorClassRelationId:
1470 : : case OperatorFamilyRelationId:
1471 : : case AccessMethodRelationId:
1472 : : case AccessMethodOperatorRelationId:
1473 : : case AccessMethodProcedureRelationId:
1474 : : case NamespaceRelationId:
1475 : : case TSParserRelationId:
1476 : : case TSDictionaryRelationId:
1477 : : case TSTemplateRelationId:
1478 : : case ForeignDataWrapperRelationId:
1479 : : case ForeignServerRelationId:
1480 : : case UserMappingRelationId:
1481 : : case DefaultAclRelationId:
1482 : : case EventTriggerRelationId:
1483 : : case TransformRelationId:
1484 : : case AuthMemRelationId:
2363 1485 : 3636 : DropObjectById(object);
4234 peter_e@gmx.net 1486 : 3636 : break;
1487 : :
1488 : : /*
1489 : : * These global object types are not supported here.
1490 : : */
977 peter@eisentraut.org 1491 :UBC 0 : case AuthIdRelationId:
1492 : : case DatabaseRelationId:
1493 : : case TableSpaceRelationId:
1494 : : case SubscriptionRelationId:
1495 : : case ParameterAclRelationId:
3485 tgl@sss.pgh.pa.us 1496 [ # # ]: 0 : elog(ERROR, "global objects cannot be deleted by doDeletion");
1497 : : break;
1498 : :
977 peter@eisentraut.org 1499 : 0 : default:
1500 [ # # ]: 0 : elog(ERROR, "unsupported object class: %u", object->classId);
1501 : : }
8905 tgl@sss.pgh.pa.us 1502 :CBC 153476 : }
1503 : :
1504 : : /*
1505 : : * AcquireDeletionLock - acquire a suitable lock for deleting an object
1506 : : *
1507 : : * Accepts the same flags as performDeletion (though currently only
1508 : : * PERFORM_DELETION_CONCURRENTLY does anything).
1509 : : *
1510 : : * We use LockRelation for relations, and otherwise LockSharedObject or
1511 : : * LockDatabaseObject as appropriate for the object type.
1512 : : */
1513 : : void
5349 simon@2ndQuadrant.co 1514 : 191431 : AcquireDeletionLock(const ObjectAddress *object, int flags)
1515 : : {
6747 tgl@sss.pgh.pa.us 1516 [ + + ]: 191431 : if (object->classId == RelationRelationId)
1517 : : {
1518 : : /*
1519 : : * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1520 : : * the index for the moment. index_drop() will promote the lock once
1521 : : * it's safe to do so. In all other cases we need full exclusive
1522 : : * lock.
1523 : : */
5153 simon@2ndQuadrant.co 1524 [ + + ]: 66696 : if (flags & PERFORM_DELETION_CONCURRENTLY)
5349 1525 : 76 : LockRelationOid(object->objectId, ShareUpdateExclusiveLock);
1526 : : else
1527 : 66620 : LockRelationOid(object->objectId, AccessExclusiveLock);
1528 : : }
117 jdavis@postgresql.or 1529 [ + + ]: 124735 : else if (IsSharedRelation(object->classId))
1563 rhaas@postgresql.org 1530 : 35 : LockSharedObject(object->classId, object->objectId, 0,
1531 : : AccessExclusiveLock);
1532 : : else
1533 : : {
1534 : : /* assume we should lock the whole object not a sub-object */
6747 tgl@sss.pgh.pa.us 1535 : 124700 : LockDatabaseObject(object->classId, object->objectId, 0,
1536 : : AccessExclusiveLock);
1537 : : }
1538 : 191431 : }
1539 : :
1540 : : /*
1541 : : * ReleaseDeletionLock - release an object deletion lock
1542 : : *
1543 : : * Companion to AcquireDeletionLock.
1544 : : */
1545 : : void
1546 : 1247 : ReleaseDeletionLock(const ObjectAddress *object)
1547 : : {
1548 [ + + ]: 1247 : if (object->classId == RelationRelationId)
1549 : 37 : UnlockRelationOid(object->objectId, AccessExclusiveLock);
117 jdavis@postgresql.or 1550 [ + + ]: 1210 : else if (IsSharedRelation(object->classId))
1551 : 1 : UnlockSharedObject(object->classId, object->objectId, 0,
1552 : : AccessExclusiveLock);
1553 : : else
1554 : : /* assume we should lock the whole object not a sub-object */
6747 tgl@sss.pgh.pa.us 1555 : 1209 : UnlockDatabaseObject(object->classId, object->objectId, 0,
1556 : : AccessExclusiveLock);
1557 : 1247 : }
1558 : :
1559 : : /*
1560 : : * recordDependencyOnExpr - find expression dependencies
1561 : : *
1562 : : * This is used to find the dependencies of rules, constraint expressions,
1563 : : * etc.
1564 : : *
1565 : : * Given an expression or query in node-tree form, find all the objects
1566 : : * it refers to (tables, columns, operators, functions, etc). Record
1567 : : * a dependency of the specified type from the given depender object
1568 : : * to each object mentioned in the expression.
1569 : : *
1570 : : * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
1571 : : * It can be NIL if no such variables are expected.
1572 : : */
1573 : : void
8901 1574 : 13530 : recordDependencyOnExpr(const ObjectAddress *depender,
1575 : : Node *expr, List *rtable,
1576 : : DependencyType behavior)
1577 : : {
1578 : : ObjectAddresses *addrs;
1579 : :
370 1580 : 13530 : addrs = new_object_addresses();
1581 : :
1582 : : /* Collect all dependencies from the expression */
1583 : 13530 : collectDependenciesOfExpr(addrs, expr, rtable);
1584 : :
1585 : : /* Remove duplicates */
1586 : 13530 : eliminate_duplicate_dependencies(addrs);
1587 : :
1588 : : /* And record 'em */
8585 1589 : 13530 : recordMultipleDependencies(depender,
370 1590 : 13530 : addrs->refs, addrs->numrefs,
1591 : : behavior);
1592 : :
1593 : 13530 : free_object_addresses(addrs);
1594 : 13530 : }
1595 : :
1596 : : /*
1597 : : * collectDependenciesOfExpr - collect expression dependencies
1598 : : *
1599 : : * This function analyzes an expression or query in node-tree form to
1600 : : * find all the objects it refers to (tables, columns, operators,
1601 : : * functions, etc.) and adds them to the provided ObjectAddresses
1602 : : * structure. Unlike recordDependencyOnExpr, this function does not
1603 : : * immediately record the dependencies, allowing the caller to add to,
1604 : : * filter, or modify the collected dependencies before recording them.
1605 : : *
1606 : : * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
1607 : : * It can be NIL if no such variables are expected.
1608 : : *
1609 : : * Note: the returned list may well contain duplicates. The caller should
1610 : : * de-duplicate before recording the dependencies. Within this file, callers
1611 : : * must call eliminate_duplicate_dependencies(). External callers typically
1612 : : * go through record_object_address_dependencies() which will see to that.
1613 : : * This choice allows collecting dependencies from multiple sources without
1614 : : * redundant de-duplication work.
1615 : : */
1616 : : void
1617 : 46630 : collectDependenciesOfExpr(ObjectAddresses *addrs,
1618 : : Node *expr, List *rtable)
1619 : : {
1620 : : find_expr_references_context context;
1621 : :
1622 : 46630 : context.addrs = addrs;
1623 : :
1624 : : /* Set up interpretation for Vars at varlevelsup = 0 */
1625 : 46630 : context.rtables = list_make1(rtable);
1626 : :
1627 : : /* Scan the expression tree for referenceable objects */
1628 : 46630 : find_expr_references_walker(expr, &context);
8585 1629 : 46626 : }
1630 : :
1631 : : /*
1632 : : * recordDependencyOnSingleRelExpr - find expression dependencies
1633 : : *
1634 : : * As above, but only one relation is expected to be referenced (with
1635 : : * varno = 1 and varlevelsup = 0). Pass the relation OID instead of a
1636 : : * range table. An additional frammish is that dependencies on that
1637 : : * relation's component columns will be marked with 'self_behavior',
1638 : : * whereas 'behavior' is used for everything else; also, if 'reverse_self'
1639 : : * is true, those dependencies are reversed so that the columns are made
1640 : : * to depend on the table not vice versa.
1641 : : *
1642 : : * NOTE: the caller should ensure that a whole-table dependency on the
1643 : : * specified relation is created separately, if one is needed. In particular,
1644 : : * a whole-row Var "relation.*" will not cause this routine to emit any
1645 : : * dependency item. This is appropriate behavior for subexpressions of an
1646 : : * ordinary query, so other cases need to cope as necessary.
1647 : : */
1648 : : void
1649 : 7704 : recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
1650 : : Node *expr, Oid relId,
1651 : : DependencyType behavior,
1652 : : DependencyType self_behavior,
1653 : : bool reverse_self)
1654 : : {
1655 : : find_expr_references_context context;
1596 peter@eisentraut.org 1656 : 7704 : RangeTblEntry rte = {0};
1657 : :
19 heikki.linnakangas@i 1658 [ - + ]: 7704 : if (expr == NULL)
19 heikki.linnakangas@i 1659 :UBC 0 : return;
1660 : :
7405 alvherre@alvh.no-ip. 1661 :CBC 7704 : context.addrs = new_object_addresses();
1662 : :
1663 : : /* We gin up a rather bogus rangetable list to handle Vars */
8585 tgl@sss.pgh.pa.us 1664 : 7704 : rte.type = T_RangeTblEntry;
1665 : 7704 : rte.rtekind = RTE_RELATION;
1666 : 7704 : rte.relid = relId;
3447 1667 : 7704 : rte.relkind = RELKIND_RELATION; /* no need for exactness here */
2981 1668 : 7704 : rte.rellockmode = AccessShareLock;
1669 : :
8221 neilc@samurai.com 1670 : 7704 : context.rtables = list_make1(list_make1(&rte));
1671 : :
1672 : : /* Scan the expression tree for referenceable objects */
8585 tgl@sss.pgh.pa.us 1673 : 7704 : find_expr_references_walker(expr, &context);
1674 : :
1675 : : /* Also add a dependency to the result type */
19 heikki.linnakangas@i 1676 [ + + ]: 7704 : if (IsA(expr, List))
1677 : : {
1678 : : ListCell *l;
1679 : :
1680 [ + - + + : 3293 : foreach(l, (List *) expr)
+ + ]
1681 : : {
1682 : 1739 : add_object_address(TypeRelationId, exprType(lfirst(l)), 0,
1683 : : context.addrs);
1684 : : }
1685 : : }
1686 : : else
1687 : 6150 : add_object_address(TypeRelationId, exprType(expr), 0,
1688 : : context.addrs);
1689 : :
1690 : : /* Remove any duplicates */
7405 alvherre@alvh.no-ip. 1691 : 7704 : eliminate_duplicate_dependencies(context.addrs);
1692 : :
1693 : : /* Separate self-dependencies if necessary */
2686 tgl@sss.pgh.pa.us 1694 [ + + - + ]: 7704 : if ((behavior != self_behavior || reverse_self) &&
1695 [ + - ]: 1554 : context.addrs->numrefs > 0)
1696 : : {
1697 : : ObjectAddresses *self_addrs;
1698 : : ObjectAddress *outobj;
1699 : : int oldref,
1700 : : outrefs;
1701 : :
7405 alvherre@alvh.no-ip. 1702 : 1554 : self_addrs = new_object_addresses();
1703 : :
1704 : 1554 : outobj = context.addrs->refs;
8585 tgl@sss.pgh.pa.us 1705 : 1554 : outrefs = 0;
7405 alvherre@alvh.no-ip. 1706 [ + + ]: 7597 : for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1707 : : {
1708 : 6043 : ObjectAddress *thisobj = context.addrs->refs + oldref;
1709 : :
7898 tgl@sss.pgh.pa.us 1710 [ + + ]: 6043 : if (thisobj->classId == RelationRelationId &&
8585 1711 [ + + ]: 1976 : thisobj->objectId == relId)
1712 : : {
1713 : : /* Move this ref into self_addrs */
6747 1714 : 1884 : add_exact_object_address(thisobj, self_addrs);
1715 : : }
1716 : : else
1717 : : {
1718 : : /* Keep it in context.addrs */
1719 : 4159 : *outobj = *thisobj;
8585 1720 : 4159 : outobj++;
1721 : 4159 : outrefs++;
1722 : : }
1723 : : }
7405 alvherre@alvh.no-ip. 1724 : 1554 : context.addrs->numrefs = outrefs;
1725 : :
1726 : : /* Record the self-dependencies with the appropriate direction */
2686 tgl@sss.pgh.pa.us 1727 [ + + ]: 1554 : if (!reverse_self)
3643 rhaas@postgresql.org 1728 : 1380 : recordMultipleDependencies(depender,
2031 tmunro@postgresql.or 1729 : 1380 : self_addrs->refs, self_addrs->numrefs,
1730 : : self_behavior);
1731 : : else
1732 : : {
1733 : : /* Can't use recordMultipleDependencies, so do it the hard way */
1734 : : int selfref;
1735 : :
2686 tgl@sss.pgh.pa.us 1736 [ + + ]: 397 : for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
1737 : : {
1738 : 223 : ObjectAddress *thisobj = self_addrs->refs + selfref;
1739 : :
1740 : 223 : recordDependencyOn(thisobj, depender, self_behavior);
1741 : : }
1742 : : }
1743 : :
7405 alvherre@alvh.no-ip. 1744 : 1554 : free_object_addresses(self_addrs);
1745 : : }
1746 : :
1747 : : /* Record the external dependencies */
8901 tgl@sss.pgh.pa.us 1748 : 7704 : recordMultipleDependencies(depender,
2031 tmunro@postgresql.or 1749 : 7704 : context.addrs->refs, context.addrs->numrefs,
1750 : : behavior);
1751 : :
7405 alvherre@alvh.no-ip. 1752 : 7704 : free_object_addresses(context.addrs);
1753 : : }
1754 : :
1755 : : /*
1756 : : * We require USAGE on a type to store a dependency on it. This helper
1757 : : * function does the appropriate privilege checks.
1758 : : *
1759 : : * NB: Other objects have privileges of their own, but recording those
1760 : : * dependencies doesn't require holding them. For example, an expression may
1761 : : * reference a function for which the user lacks EXECUTE. Instead, EXECUTE is
1762 : : * checked when the function is executed.
1763 : : */
1764 : : static void
110 nathan@postgresql.or 1765 : 24271 : check_usage_on_types(ObjectAddresses *addrs, Oid roleid)
1766 : : {
1767 [ + + ]: 236650 : for (int i = 0; i < addrs->numrefs; i++)
1768 : : {
1769 : 212431 : ObjectAddress *ref = &addrs->refs[i];
1770 : : AclResult aclresult;
1771 : :
1772 [ + + ]: 212431 : if (ref->classId != TypeRelationId)
1773 : 164814 : continue;
1774 : :
1775 : : /* we don't record dependencies on pinned types */
1776 [ + + ]: 47617 : if (IsPinnedObject(ref->classId, ref->objectId))
1777 : 40251 : continue;
1778 : :
1779 : 7366 : aclresult = object_aclcheck(ref->classId, ref->objectId,
1780 : : roleid, ACL_USAGE);
1781 [ + + ]: 7366 : if (aclresult != ACLCHECK_OK)
1782 : 52 : aclcheck_error_type(aclresult, ref->objectId);
1783 : : }
1784 : 24219 : }
1785 : :
1786 : : /*
1787 : : * CheckUsageOnTypesInExpr - require USAGE on all types named by an expression
1788 : : *
1789 : : * rtable is the rangetable for interpreting Vars (or NIL if none are
1790 : : * expected). roleid is the role whose USAGE is required.
1791 : : */
1792 : : void
1793 : 17924 : CheckUsageOnTypesInExpr(Node *expr, List *rtable, Oid roleid)
1794 : : {
1795 : 17924 : ObjectAddresses *addrs = new_object_addresses();
1796 : :
1797 : 17924 : collectDependenciesOfExpr(addrs, expr, rtable);
1798 : 17924 : eliminate_duplicate_dependencies(addrs);
1799 : 17924 : check_usage_on_types(addrs, roleid);
1800 : 17904 : free_object_addresses(addrs);
1801 : 17904 : }
1802 : :
1803 : : /*
1804 : : * CheckUsageOnTypesInSingleRelExpr - as above, for a single-rel expression
1805 : : *
1806 : : * Like recordDependencyOnSingleRelExpr(), this handles expressions whose Vars
1807 : : * all refer to one relation. roleid is the role whose USAGE is required.
1808 : : */
1809 : : void
1810 : 6355 : CheckUsageOnTypesInSingleRelExpr(Node *expr, Oid relId, Oid roleid)
1811 : : {
1812 : : find_expr_references_context context;
1813 : 6355 : RangeTblEntry rte = {0};
1814 : :
19 heikki.linnakangas@i 1815 [ - + ]: 6355 : if (expr == NULL)
19 heikki.linnakangas@i 1816 :UBC 0 : return;
1817 : :
110 nathan@postgresql.or 1818 :CBC 6355 : context.addrs = new_object_addresses();
1819 : :
1820 : : /* We gin up a rather bogus rangetable list to handle Vars */
1821 : 6355 : rte.type = T_RangeTblEntry;
1822 : 6355 : rte.rtekind = RTE_RELATION;
1823 : 6355 : rte.relid = relId;
1824 : 6355 : rte.relkind = RELKIND_RELATION;
1825 : 6355 : rte.rellockmode = AccessShareLock;
1826 : 6355 : context.rtables = list_make1(list_make1(&rte));
1827 : :
1828 : 6355 : find_expr_references_walker(expr, &context);
1829 : :
1830 : : /* Also check USAGE on the result type */
19 heikki.linnakangas@i 1831 [ + + ]: 6347 : if (IsA(expr, List))
1832 : : {
1833 : : ListCell *l;
1834 : :
1835 [ + - + + : 2962 : foreach(l, (List *) expr)
+ + ]
1836 : : {
1837 : 1575 : add_object_address(TypeRelationId, exprType(lfirst(l)), 0,
1838 : : context.addrs);
1839 : : }
1840 : : }
1841 : : else
1842 : 4960 : add_object_address(TypeRelationId, exprType(expr), 0,
1843 : : context.addrs);
1844 : :
110 nathan@postgresql.or 1845 : 6347 : eliminate_duplicate_dependencies(context.addrs);
1846 : 6347 : check_usage_on_types(context.addrs, roleid);
1847 : 6315 : free_object_addresses(context.addrs);
1848 : : }
1849 : :
1850 : : /*
1851 : : * CheckTypeDependencyInExpr - does 'expr' depend on the given type?
1852 : : */
1853 : : bool
19 heikki.linnakangas@i 1854 : 58 : CheckTypeDependencyInExpr(Node *expr, List *rtable, Oid typeId)
1855 : : {
1856 : 58 : ObjectAddresses *addrs = new_object_addresses();
1857 : : ObjectAddress typeAddr;
1858 : : bool result;
1859 : :
1860 : 58 : collectDependenciesOfExpr(addrs, expr, rtable);
1861 : :
1862 : 58 : ObjectAddressSet(typeAddr, TypeRelationId, typeId);
1863 : 58 : result = object_address_present(&typeAddr, addrs);
1864 : 58 : free_object_addresses(addrs);
1865 : :
1866 : 58 : return result;
1867 : : }
1868 : :
1869 : : /*
1870 : : * CheckTypeDependencyInSingleRelExpr - as above, for a single-rel expression
1871 : : *
1872 : : * One difference is that this also checks the return type of the expression.
1873 : : * That's consistent with recordDependencyOnSingleRelExpr(), which adds a
1874 : : * dependency on the return type.
1875 : : */
1876 : : bool
1877 : 36 : CheckTypeDependencyInSingleRelExpr(Node *expr, Oid relId, Oid typeId)
1878 : : {
1879 : 36 : RangeTblEntry rte = {0};
1880 : :
2 1881 [ - + ]: 36 : if (expr == NULL)
2 heikki.linnakangas@i 1882 :UBC 0 : return false;
1883 : :
1884 : : /* Also check the result type, matching recordDependencyOnSingleRelExpr */
2 heikki.linnakangas@i 1885 [ + - ]:CBC 36 : if (IsA(expr, List))
1886 : : {
1887 : : ListCell *l;
1888 : :
1889 [ + - + + : 52 : foreach(l, (List *) expr)
+ + ]
1890 : : {
1891 [ + + ]: 36 : if (exprType(lfirst(l)) == typeId)
1892 : 20 : return true;
1893 : : }
1894 : : }
2 heikki.linnakangas@i 1895 [ # # ]:UBC 0 : else if (exprType(expr) == typeId)
1896 : 0 : return true;
1897 : :
1898 : : /* We gin up a rather bogus rangetable list to handle Vars */
19 heikki.linnakangas@i 1899 :CBC 16 : rte.type = T_RangeTblEntry;
1900 : 16 : rte.rtekind = RTE_RELATION;
1901 : 16 : rte.relid = relId;
1902 : 16 : rte.relkind = RELKIND_RELATION;
1903 : 16 : rte.rellockmode = AccessShareLock;
1904 : :
1905 : 16 : return CheckTypeDependencyInExpr(expr, list_make1(&rte), typeId);
1906 : : }
1907 : :
1908 : : /*
1909 : : * Record dependencies on the target columns named in the target list of an
1910 : : * INSERT or UPDATE query, or of an INSERT or UPDATE action of a MERGE query.
1911 : : */
1912 : : static void
5 dean.a.rasheed@gmail 1913 : 1062 : add_target_column_dependencies(List *targetList, Oid relid,
1914 : : find_expr_references_context *context)
1915 : : {
1916 [ + + + + : 4218 : foreach_node(TargetEntry, tle, targetList)
+ + ]
1917 : : {
1918 [ + + ]: 2094 : if (tle->resjunk)
1919 : 8 : continue; /* ignore junk tlist items */
1920 : 2086 : add_object_address(RelationRelationId, relid, tle->resno,
1921 : : context->addrs);
1922 : : }
1923 : 1062 : }
1924 : :
1925 : : /*
1926 : : * Recursively search an expression tree for object references.
1927 : : *
1928 : : * Note: in many cases we do not need to create dependencies on the datatypes
1929 : : * involved in an expression, because we'll have an indirect dependency via
1930 : : * some other object. For instance Var nodes depend on a column which depends
1931 : : * on the datatype, and OpExpr nodes depend on the operator which depends on
1932 : : * the datatype. However we do need a type dependency if there is no such
1933 : : * indirect dependency, as for example in Const and CoerceToDomain nodes.
1934 : : *
1935 : : * Similarly, we don't need to create dependencies on collations except where
1936 : : * the collation is being freshly introduced to the expression.
1937 : : */
1938 : : static bool
8901 tgl@sss.pgh.pa.us 1939 : 3674359 : find_expr_references_walker(Node *node,
1940 : : find_expr_references_context *context)
1941 : : {
1942 [ + + ]: 3674359 : if (node == NULL)
1943 : 1242071 : return false;
1944 [ + + ]: 2432288 : if (IsA(node, Var))
1945 : : {
1946 : 616734 : Var *var = (Var *) node;
1947 : : List *rtable;
1948 : : RangeTblEntry *rte;
1949 : :
1950 : : /* Find matching rtable entry, or complain if not found */
8221 neilc@samurai.com 1951 [ - + ]: 616734 : if (var->varlevelsup >= list_length(context->rtables))
8531 tgl@sss.pgh.pa.us 1952 [ # # ]:UBC 0 : elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
8221 neilc@samurai.com 1953 :CBC 616734 : rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1954 [ + - - + ]: 616734 : if (var->varno <= 0 || var->varno > list_length(rtable))
8531 tgl@sss.pgh.pa.us 1955 [ # # ]:UBC 0 : elog(ERROR, "invalid varno %d", var->varno);
8901 tgl@sss.pgh.pa.us 1956 :CBC 616734 : rte = rt_fetch(var->varno, rtable);
1957 : :
1958 : : /*
1959 : : * A whole-row Var references no specific columns, so adds no new
1960 : : * dependency. (We assume that there is a whole-table dependency
1961 : : * arising from each underlying rangetable entry. While we could
1962 : : * record such a dependency when finding a whole-row Var that
1963 : : * references a relation directly, it's quite unclear how to extend
1964 : : * that to whole-row Vars for JOINs, so it seems better to leave the
1965 : : * responsibility with the range table. Note that this poses some
1966 : : * risks for identifying dependencies of stand-alone expressions:
1967 : : * whole-table references may need to be created separately.)
1968 : : */
8136 1969 [ + + ]: 616734 : if (var->varattno == InvalidAttrNumber)
1970 : 10268 : return false;
8901 1971 [ + + ]: 606466 : if (rte->rtekind == RTE_RELATION)
1972 : : {
1973 : : /* If it's a plain relation, reference this column */
1005 michael@paquier.xyz 1974 : 424528 : add_object_address(RelationRelationId, rte->relid, var->varattno,
1975 : : context->addrs);
1976 : : }
1590 tgl@sss.pgh.pa.us 1977 [ + + ]: 181938 : else if (rte->rtekind == RTE_FUNCTION)
1978 : : {
1979 : : /* Might need to add a dependency on a composite type's column */
1980 : : /* (done out of line, because it's a bit bulky) */
1981 : 93894 : process_function_rte_ref(rte, var->varattno, context);
1982 : : }
1983 : :
1984 : : /*
1985 : : * Vars referencing other RTE types require no additional work. In
1986 : : * particular, a join alias Var can be ignored, because it must
1987 : : * reference a merged USING column. The relevant join input columns
1988 : : * will also be referenced in the join qual, and any type coercion
1989 : : * functions involved in the alias expression will be dealt with when
1990 : : * we scan the RTE itself.
1991 : : */
8901 1992 : 606466 : return false;
1993 : : }
6692 1994 [ + + ]: 1815554 : else if (IsA(node, Const))
1995 : : {
7727 1996 : 295630 : Const *con = (Const *) node;
1997 : : Oid objoid;
1998 : :
1999 : : /* A constant must depend on the constant's datatype */
1005 michael@paquier.xyz 2000 : 295630 : add_object_address(TypeRelationId, con->consttype, 0,
2001 : : context->addrs);
2002 : :
2003 : : /*
2004 : : * We must also depend on the constant's collation: it could be
2005 : : * different from the datatype's, if a CollateExpr was const-folded to
2006 : : * a simple constant. However we can save work in the most common
2007 : : * case where the collation is "default", since we know that's pinned.
2008 : : */
2031 tmunro@postgresql.or 2009 [ + + ]: 295630 : if (OidIsValid(con->constcollid) &&
2010 [ + + ]: 118397 : con->constcollid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2011 : 28542 : add_object_address(CollationRelationId, con->constcollid, 0,
2012 : : context->addrs);
2013 : :
2014 : : /*
2015 : : * If it's a regclass or similar literal referring to an existing
2016 : : * object, add a reference to that object. (Currently, only the
2017 : : * regclass and regconfig cases have any likely use, but we may as
2018 : : * well handle all the OID-alias datatypes consistently.)
2019 : : */
7727 tgl@sss.pgh.pa.us 2020 [ + + ]: 295630 : if (!con->constisnull)
2021 : : {
2022 [ - - + - : 247382 : switch (con->consttype)
- - - + +
+ + ]
2023 : : {
7727 tgl@sss.pgh.pa.us 2024 :UBC 0 : case REGPROCOID:
2025 : : case REGPROCEDUREOID:
2026 : 0 : objoid = DatumGetObjectId(con->constvalue);
6131 rhaas@postgresql.org 2027 [ # # ]: 0 : if (SearchSysCacheExists1(PROCOID,
2028 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2029 : 0 : add_object_address(ProcedureRelationId, objoid, 0,
2030 : : context->addrs);
7727 tgl@sss.pgh.pa.us 2031 : 0 : break;
2032 : 0 : case REGOPEROID:
2033 : : case REGOPERATOROID:
2034 : 0 : objoid = DatumGetObjectId(con->constvalue);
6131 rhaas@postgresql.org 2035 [ # # ]: 0 : if (SearchSysCacheExists1(OPEROID,
2036 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2037 : 0 : add_object_address(OperatorRelationId, objoid, 0,
2038 : : context->addrs);
7727 tgl@sss.pgh.pa.us 2039 : 0 : break;
7727 tgl@sss.pgh.pa.us 2040 :CBC 7363 : case REGCLASSOID:
2041 : 7363 : objoid = DatumGetObjectId(con->constvalue);
6131 rhaas@postgresql.org 2042 [ + - ]: 7363 : if (SearchSysCacheExists1(RELOID,
2043 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2044 : 7363 : add_object_address(RelationRelationId, objoid, 0,
2045 : : context->addrs);
7727 tgl@sss.pgh.pa.us 2046 : 7363 : break;
7727 tgl@sss.pgh.pa.us 2047 :UBC 0 : case REGTYPEOID:
2048 : 0 : objoid = DatumGetObjectId(con->constvalue);
6131 rhaas@postgresql.org 2049 [ # # ]: 0 : if (SearchSysCacheExists1(TYPEOID,
2050 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2051 : 0 : add_object_address(TypeRelationId, objoid, 0,
2052 : : context->addrs);
7727 tgl@sss.pgh.pa.us 2053 : 0 : break;
1595 2054 : 0 : case REGCOLLATIONOID:
2055 : 0 : objoid = DatumGetObjectId(con->constvalue);
2056 [ # # ]: 0 : if (SearchSysCacheExists1(COLLOID,
2057 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2058 : 0 : add_object_address(CollationRelationId, objoid, 0,
2059 : : context->addrs);
1595 tgl@sss.pgh.pa.us 2060 : 0 : break;
7039 2061 : 0 : case REGCONFIGOID:
2062 : 0 : objoid = DatumGetObjectId(con->constvalue);
6131 rhaas@postgresql.org 2063 [ # # ]: 0 : if (SearchSysCacheExists1(TSCONFIGOID,
2064 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2065 : 0 : add_object_address(TSConfigRelationId, objoid, 0,
2066 : : context->addrs);
7039 tgl@sss.pgh.pa.us 2067 : 0 : break;
2068 : 0 : case REGDICTIONARYOID:
2069 : 0 : objoid = DatumGetObjectId(con->constvalue);
6131 rhaas@postgresql.org 2070 [ # # ]: 0 : if (SearchSysCacheExists1(TSDICTOID,
2071 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2072 : 0 : add_object_address(TSDictionaryRelationId, objoid, 0,
2073 : : context->addrs);
7039 tgl@sss.pgh.pa.us 2074 : 0 : break;
2075 : :
4221 andrew@dunslane.net 2076 :CBC 240 : case REGNAMESPACEOID:
2077 : 240 : objoid = DatumGetObjectId(con->constvalue);
2078 [ + - ]: 240 : if (SearchSysCacheExists1(NAMESPACEOID,
2079 : : ObjectIdGetDatum(objoid)))
1005 michael@paquier.xyz 2080 : 240 : add_object_address(NamespaceRelationId, objoid, 0,
2081 : : context->addrs);
4221 andrew@dunslane.net 2082 : 240 : break;
2083 : :
2084 : : /*
2085 : : * Dependencies for regrole should be shared among all
2086 : : * databases, so explicitly inhibit to have dependencies.
2087 : : */
2088 : 4 : case REGROLEOID:
2089 [ + - ]: 4 : ereport(ERROR,
2090 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2091 : : errmsg("constant of the type %s cannot be used here",
2092 : : "regrole")));
2093 : : break;
2094 : :
2095 : : /*
2096 : : * Dependencies for regdatabase should be shared among all
2097 : : * databases, so explicitly inhibit to have dependencies.
2098 : : */
516 nathan@postgresql.or 2099 : 4 : case REGDATABASEOID:
2100 [ + - ]: 4 : ereport(ERROR,
2101 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2102 : : errmsg("constant of the type %s cannot be used here",
2103 : : "regdatabase")));
2104 : : break;
2105 : : }
2106 : : }
7727 tgl@sss.pgh.pa.us 2107 : 295622 : return false;
2108 : : }
6692 2109 [ + + ]: 1519924 : else if (IsA(node, Param))
2110 : : {
7562 2111 : 20484 : Param *param = (Param *) node;
2112 : :
2113 : : /* A parameter must depend on the parameter's datatype */
1005 michael@paquier.xyz 2114 : 20484 : add_object_address(TypeRelationId, param->paramtype, 0,
2115 : : context->addrs);
2116 : : /* and its collation, just as for Consts */
2031 tmunro@postgresql.or 2117 [ + + ]: 20484 : if (OidIsValid(param->paramcollid) &&
2118 [ + + ]: 4356 : param->paramcollid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2119 : 2668 : add_object_address(CollationRelationId, param->paramcollid, 0,
2120 : : context->addrs);
2121 : : }
6692 tgl@sss.pgh.pa.us 2122 [ + + ]: 1499440 : else if (IsA(node, FuncExpr))
2123 : : {
8752 2124 : 143091 : FuncExpr *funcexpr = (FuncExpr *) node;
2125 : :
1005 michael@paquier.xyz 2126 : 143091 : add_object_address(ProcedureRelationId, funcexpr->funcid, 0,
2127 : : context->addrs);
2128 : : /* fall through to examine arguments */
2129 : : }
6692 tgl@sss.pgh.pa.us 2130 [ + + ]: 1356349 : else if (IsA(node, OpExpr))
2131 : : {
8517 bruce@momjian.us 2132 : 168755 : OpExpr *opexpr = (OpExpr *) node;
2133 : :
1005 michael@paquier.xyz 2134 : 168755 : add_object_address(OperatorRelationId, opexpr->opno, 0,
2135 : : context->addrs);
2136 : : /* fall through to examine arguments */
2137 : : }
6692 tgl@sss.pgh.pa.us 2138 [ + + ]: 1187594 : else if (IsA(node, DistinctExpr))
2139 : : {
8517 bruce@momjian.us 2140 : 16 : DistinctExpr *distinctexpr = (DistinctExpr *) node;
2141 : :
1005 michael@paquier.xyz 2142 : 16 : add_object_address(OperatorRelationId, distinctexpr->opno, 0,
2143 : : context->addrs);
2144 : : /* fall through to examine arguments */
2145 : : }
5733 tgl@sss.pgh.pa.us 2146 [ + + ]: 1187578 : else if (IsA(node, NullIfExpr))
2147 : : {
2148 : 782 : NullIfExpr *nullifexpr = (NullIfExpr *) node;
2149 : :
1005 michael@paquier.xyz 2150 : 782 : add_object_address(OperatorRelationId, nullifexpr->opno, 0,
2151 : : context->addrs);
2152 : : /* fall through to examine arguments */
2153 : : }
5733 tgl@sss.pgh.pa.us 2154 [ + + ]: 1186796 : else if (IsA(node, ScalarArrayOpExpr))
2155 : : {
2156 : 11908 : ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
2157 : :
1005 michael@paquier.xyz 2158 : 11908 : add_object_address(OperatorRelationId, opexpr->opno, 0,
2159 : : context->addrs);
2160 : : /* fall through to examine arguments */
2161 : : }
6692 tgl@sss.pgh.pa.us 2162 [ + + ]: 1174888 : else if (IsA(node, Aggref))
2163 : : {
8901 2164 : 3343 : Aggref *aggref = (Aggref *) node;
2165 : :
1005 michael@paquier.xyz 2166 : 3343 : add_object_address(ProcedureRelationId, aggref->aggfnoid, 0,
2167 : : context->addrs);
2168 : : /* fall through to examine arguments */
2169 : : }
6544 tgl@sss.pgh.pa.us 2170 [ + + ]: 1171545 : else if (IsA(node, WindowFunc))
2171 : : {
2172 : 294 : WindowFunc *wfunc = (WindowFunc *) node;
2173 : :
1005 michael@paquier.xyz 2174 : 294 : add_object_address(ProcedureRelationId, wfunc->winfnoid, 0,
2175 : : context->addrs);
2176 : : /* fall through to examine arguments */
2177 : : }
2180 tgl@sss.pgh.pa.us 2178 [ + + ]: 1171251 : else if (IsA(node, SubscriptingRef))
2179 : : {
2180 : 5536 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
2181 : :
2182 : : /*
2183 : : * The refexpr should provide adequate dependency on refcontainertype,
2184 : : * and that type in turn depends on refelemtype. However, a custom
2185 : : * subscripting handler might set refrestype to something different
2186 : : * from either of those, in which case we'd better record it.
2187 : : */
2188 [ + + ]: 5536 : if (sbsref->refrestype != sbsref->refcontainertype &&
2189 [ - + ]: 5368 : sbsref->refrestype != sbsref->refelemtype)
1005 michael@paquier.xyz 2190 :UBC 0 : add_object_address(TypeRelationId, sbsref->refrestype, 0,
2191 : : context->addrs);
2192 : : /* fall through to examine arguments */
2193 : : }
6672 tgl@sss.pgh.pa.us 2194 [ - + ]:CBC 1165715 : else if (IsA(node, SubPlan))
2195 : : {
2196 : : /* Extra work needed here if we ever need this case */
7640 tgl@sss.pgh.pa.us 2197 [ # # ]:UBC 0 : elog(ERROR, "already-planned subqueries not supported");
2198 : : }
3323 tgl@sss.pgh.pa.us 2199 [ + + ]:CBC 1165715 : else if (IsA(node, FieldSelect))
2200 : : {
2201 : 30112 : FieldSelect *fselect = (FieldSelect *) node;
3319 2202 : 30112 : Oid argtype = getBaseType(exprType((Node *) fselect->arg));
2203 : 30112 : Oid reltype = get_typ_typrelid(argtype);
2204 : :
2205 : : /*
2206 : : * We need a dependency on the specific column named in FieldSelect,
2207 : : * assuming we can identify the pg_class OID for it. (Probably we
2208 : : * always can at the moment, but in future it might be possible for
2209 : : * argtype to be RECORDOID.) If we can make a column dependency then
2210 : : * we shouldn't need a dependency on the column's type; but if we
2211 : : * can't, make a dependency on the type, as it might not appear
2212 : : * anywhere else in the expression.
2213 : : */
2214 [ + + ]: 30112 : if (OidIsValid(reltype))
1005 michael@paquier.xyz 2215 : 19194 : add_object_address(RelationRelationId, reltype, fselect->fieldnum,
2216 : : context->addrs);
2217 : : else
2218 : 10918 : add_object_address(TypeRelationId, fselect->resulttype, 0,
2219 : : context->addrs);
2220 : : /* the collation might not be referenced anywhere else, either */
2031 tmunro@postgresql.or 2221 [ + + ]: 30112 : if (OidIsValid(fselect->resultcollid) &&
2222 [ - + ]: 3068 : fselect->resultcollid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2223 :UBC 0 : add_object_address(CollationRelationId, fselect->resultcollid, 0,
2224 : : context->addrs);
2225 : : }
3323 tgl@sss.pgh.pa.us 2226 [ + + ]:CBC 1135603 : else if (IsA(node, FieldStore))
2227 : : {
2228 : 128 : FieldStore *fstore = (FieldStore *) node;
3319 2229 : 128 : Oid reltype = get_typ_typrelid(fstore->resulttype);
2230 : :
2231 : : /* similar considerations to FieldSelect, but multiple column(s) */
2232 [ + - ]: 128 : if (OidIsValid(reltype))
2233 : : {
2234 : : ListCell *l;
2235 : :
2236 [ + - + + : 256 : foreach(l, fstore->fieldnums)
+ + ]
1005 michael@paquier.xyz 2237 : 128 : add_object_address(RelationRelationId, reltype, lfirst_int(l),
2238 : : context->addrs);
2239 : : }
2240 : : else
1005 michael@paquier.xyz 2241 :UBC 0 : add_object_address(TypeRelationId, fstore->resulttype, 0,
2242 : : context->addrs);
2243 : : }
6692 tgl@sss.pgh.pa.us 2244 [ + + ]:CBC 1135475 : else if (IsA(node, RelabelType))
2245 : : {
7360 bruce@momjian.us 2246 : 22867 : RelabelType *relab = (RelabelType *) node;
2247 : :
2248 : : /* since there is no function dependency, need to depend on type */
1005 michael@paquier.xyz 2249 : 22867 : add_object_address(TypeRelationId, relab->resulttype, 0,
2250 : : context->addrs);
2251 : : /* the collation might not be referenced anywhere else, either */
2031 tmunro@postgresql.or 2252 [ + + ]: 22867 : if (OidIsValid(relab->resultcollid) &&
2253 [ + + ]: 5571 : relab->resultcollid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2254 : 5046 : add_object_address(CollationRelationId, relab->resultcollid, 0,
2255 : : context->addrs);
2256 : : }
6692 tgl@sss.pgh.pa.us 2257 [ + + ]: 1112608 : else if (IsA(node, CoerceViaIO))
2258 : : {
7116 2259 : 4760 : CoerceViaIO *iocoerce = (CoerceViaIO *) node;
2260 : :
2261 : : /* since there is no exposed function, need to depend on type */
1005 michael@paquier.xyz 2262 : 4760 : add_object_address(TypeRelationId, iocoerce->resulttype, 0,
2263 : : context->addrs);
2264 : : /* the collation might not be referenced anywhere else, either */
2031 tmunro@postgresql.or 2265 [ + + ]: 4760 : if (OidIsValid(iocoerce->resultcollid) &&
2266 [ + + ]: 3514 : iocoerce->resultcollid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2267 : 1218 : add_object_address(CollationRelationId, iocoerce->resultcollid, 0,
2268 : : context->addrs);
2269 : : }
6692 tgl@sss.pgh.pa.us 2270 [ + + ]: 1107848 : else if (IsA(node, ArrayCoerceExpr))
2271 : : {
7186 2272 : 820 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
2273 : :
2274 : : /* as above, depend on type */
1005 michael@paquier.xyz 2275 : 820 : add_object_address(TypeRelationId, acoerce->resulttype, 0,
2276 : : context->addrs);
2277 : : /* the collation might not be referenced anywhere else, either */
2031 tmunro@postgresql.or 2278 [ + + ]: 820 : if (OidIsValid(acoerce->resultcollid) &&
2279 [ + + ]: 298 : acoerce->resultcollid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2280 : 174 : add_object_address(CollationRelationId, acoerce->resultcollid, 0,
2281 : : context->addrs);
2282 : : /* fall through to examine arguments */
2283 : : }
6692 tgl@sss.pgh.pa.us 2284 [ - + ]: 1107028 : else if (IsA(node, ConvertRowtypeExpr))
2285 : : {
7562 tgl@sss.pgh.pa.us 2286 :UBC 0 : ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
2287 : :
2288 : : /* since there is no function dependency, need to depend on type */
1005 michael@paquier.xyz 2289 : 0 : add_object_address(TypeRelationId, cvt->resulttype, 0,
2290 : : context->addrs);
2291 : : }
5741 tgl@sss.pgh.pa.us 2292 [ + + ]:CBC 1107028 : else if (IsA(node, CollateExpr))
2293 : : {
2294 : 152 : CollateExpr *coll = (CollateExpr *) node;
2295 : :
1005 michael@paquier.xyz 2296 : 152 : add_object_address(CollationRelationId, coll->collOid, 0,
2297 : : context->addrs);
2298 : : }
6692 tgl@sss.pgh.pa.us 2299 [ + + ]: 1106876 : else if (IsA(node, RowExpr))
2300 : : {
7360 bruce@momjian.us 2301 : 304 : RowExpr *rowexpr = (RowExpr *) node;
2302 : :
1005 michael@paquier.xyz 2303 : 304 : add_object_address(TypeRelationId, rowexpr->row_typeid, 0,
2304 : : context->addrs);
2305 : : }
6692 tgl@sss.pgh.pa.us 2306 [ + + ]: 1106572 : else if (IsA(node, RowCompareExpr))
2307 : : {
7640 2308 : 28 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2309 : : ListCell *l;
2310 : :
2311 [ + - + + : 84 : foreach(l, rcexpr->opnos)
+ + ]
2312 : : {
1005 michael@paquier.xyz 2313 : 56 : add_object_address(OperatorRelationId, lfirst_oid(l), 0,
2314 : : context->addrs);
2315 : : }
7280 tgl@sss.pgh.pa.us 2316 [ + - + + : 84 : foreach(l, rcexpr->opfamilies)
+ + ]
2317 : : {
1005 michael@paquier.xyz 2318 : 56 : add_object_address(OperatorFamilyRelationId, lfirst_oid(l), 0,
2319 : : context->addrs);
2320 : : }
2321 : : /* fall through to examine arguments */
2322 : : }
6692 tgl@sss.pgh.pa.us 2323 [ + + ]: 1106544 : else if (IsA(node, CoerceToDomain))
2324 : : {
7562 2325 : 110102 : CoerceToDomain *cd = (CoerceToDomain *) node;
2326 : :
1005 michael@paquier.xyz 2327 : 110102 : add_object_address(TypeRelationId, cd->resulttype, 0,
2328 : : context->addrs);
2329 : : }
3424 tgl@sss.pgh.pa.us 2330 [ - + ]: 996442 : else if (IsA(node, NextValueExpr))
2331 : : {
3424 tgl@sss.pgh.pa.us 2332 :UBC 0 : NextValueExpr *nve = (NextValueExpr *) node;
2333 : :
1005 michael@paquier.xyz 2334 : 0 : add_object_address(RelationRelationId, nve->seqid, 0,
2335 : : context->addrs);
2336 : : }
3853 tgl@sss.pgh.pa.us 2337 [ + + ]:CBC 996442 : else if (IsA(node, OnConflictExpr))
2338 : : {
2339 : 32 : OnConflictExpr *onconflict = (OnConflictExpr *) node;
2340 : :
2341 [ - + ]: 32 : if (OidIsValid(onconflict->constraint))
1005 michael@paquier.xyz 2342 :UBC 0 : add_object_address(ConstraintRelationId, onconflict->constraint, 0,
2343 : : context->addrs);
2344 : : /* fall through to examine arguments */
2345 : : }
6692 tgl@sss.pgh.pa.us 2346 [ + + ]:CBC 996410 : else if (IsA(node, SortGroupClause))
2347 : : {
2348 : 23298 : SortGroupClause *sgc = (SortGroupClause *) node;
2349 : :
1005 michael@paquier.xyz 2350 : 23298 : add_object_address(OperatorRelationId, sgc->eqop, 0,
2351 : : context->addrs);
6692 tgl@sss.pgh.pa.us 2352 [ + - ]: 23298 : if (OidIsValid(sgc->sortop))
1005 michael@paquier.xyz 2353 : 23298 : add_object_address(OperatorRelationId, sgc->sortop, 0,
2354 : : context->addrs);
6692 tgl@sss.pgh.pa.us 2355 : 23298 : return false;
2356 : : }
3216 2357 [ + + ]: 973112 : else if (IsA(node, WindowClause))
2358 : : {
2359 : 270 : WindowClause *wc = (WindowClause *) node;
2360 : :
2361 [ + + ]: 270 : if (OidIsValid(wc->startInRangeFunc))
1005 michael@paquier.xyz 2362 : 8 : add_object_address(ProcedureRelationId, wc->startInRangeFunc, 0,
2363 : : context->addrs);
3216 tgl@sss.pgh.pa.us 2364 [ + + ]: 270 : if (OidIsValid(wc->endInRangeFunc))
1005 michael@paquier.xyz 2365 : 8 : add_object_address(ProcedureRelationId, wc->endInRangeFunc, 0,
2366 : : context->addrs);
2031 tmunro@postgresql.or 2367 [ - + ]: 270 : if (OidIsValid(wc->inRangeColl) &&
2031 tmunro@postgresql.or 2368 [ # # ]:UBC 0 : wc->inRangeColl != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2369 : 0 : add_object_address(CollationRelationId, wc->inRangeColl, 0,
2370 : : context->addrs);
2371 : : /* fall through to examine substructure */
2372 : : }
2126 peter@eisentraut.org 2373 [ + + ]:CBC 972842 : else if (IsA(node, CTECycleClause))
2374 : : {
2375 : 16 : CTECycleClause *cc = (CTECycleClause *) node;
2376 : :
2377 [ + - ]: 16 : if (OidIsValid(cc->cycle_mark_type))
1005 michael@paquier.xyz 2378 : 16 : add_object_address(TypeRelationId, cc->cycle_mark_type, 0,
2379 : : context->addrs);
2126 peter@eisentraut.org 2380 [ + + ]: 16 : if (OidIsValid(cc->cycle_mark_collation))
1005 michael@paquier.xyz 2381 : 8 : add_object_address(CollationRelationId, cc->cycle_mark_collation, 0,
2382 : : context->addrs);
2126 peter@eisentraut.org 2383 [ + - ]: 16 : if (OidIsValid(cc->cycle_mark_neop))
1005 michael@paquier.xyz 2384 : 16 : add_object_address(OperatorRelationId, cc->cycle_mark_neop, 0,
2385 : : context->addrs);
2386 : : /* fall through to examine substructure */
2387 : : }
6692 tgl@sss.pgh.pa.us 2388 [ + + ]: 972826 : else if (IsA(node, Query))
2389 : : {
2390 : : /* Recurse into RTE subquery or not-yet-planned sublink subquery */
8901 2391 : 66379 : Query *query = (Query *) node;
2392 : : ListCell *lc;
2393 : : bool result;
2394 : :
2395 : : /*
2396 : : * Add whole-relation refs for each plain relation mentioned in the
2397 : : * subquery's rtable, and ensure we add refs for any type-coercion
2398 : : * functions used in join alias lists.
2399 : : *
2400 : : * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
2401 : : * RTEs, subqueries, etc, so no need to do that here. But we must
2402 : : * tell it not to visit join alias lists, or we'll add refs for join
2403 : : * input columns whether or not they are actually used in our query.
2404 : : *
2405 : : * Note: we don't need to worry about collations mentioned in
2406 : : * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
2407 : : * collations referenced in other parts of the Query. We do have to
2408 : : * worry about collations mentioned in RTE_FUNCTION, but we take care
2409 : : * of those when we recurse to the RangeTblFunction node(s).
2410 : : */
5957 2411 [ + + + + : 214237 : foreach(lc, query->rtable)
+ + ]
2412 : : {
2413 : 147862 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2414 : :
7562 2415 [ + + + + ]: 147862 : switch (rte->rtekind)
2416 : : {
2417 : 91038 : case RTE_RELATION:
1005 michael@paquier.xyz 2418 : 91038 : add_object_address(RelationRelationId, rte->relid, 0,
2419 : : context->addrs);
7562 tgl@sss.pgh.pa.us 2420 : 91038 : break;
2515 2421 : 27156 : case RTE_JOIN:
2422 : :
2423 : : /*
2424 : : * Examine joinaliasvars entries only for merged JOIN
2425 : : * USING columns. Only those entries could contain
2426 : : * type-coercion functions. Also, their join input
2427 : : * columns must be referenced in the join quals, so this
2428 : : * won't accidentally add refs to otherwise-unused join
2429 : : * input columns. (We want to ref the type coercion
2430 : : * functions even if the merged column isn't explicitly
2431 : : * used anywhere, to protect possible expansion of the
2432 : : * join RTE as a whole-row var, and because it seems like
2433 : : * a bad idea to allow dropping a function that's present
2434 : : * in our query tree, whether or not it could get called.)
2435 : : */
2436 : 27156 : context->rtables = lcons(query->rtable, context->rtables);
2437 [ + + ]: 27560 : for (int i = 0; i < rte->joinmergedcols; i++)
2438 : : {
2439 : 404 : Node *aliasvar = list_nth(rte->joinaliasvars, i);
2440 : :
2441 [ + + ]: 404 : if (!IsA(aliasvar, Var))
2442 : 104 : find_expr_references_walker(aliasvar, context);
2443 : : }
2444 : 27156 : context->rtables = list_delete_first(context->rtables);
2445 : 27156 : break;
689 2446 : 4 : case RTE_NAMEDTUPLESTORE:
2447 : :
2448 : : /*
2449 : : * Cataloged objects cannot depend on tuplestores, because
2450 : : * those have no cataloged representation. For now we can
2451 : : * call the tuplestore a "transition table" because that's
2452 : : * the only kind exposed to SQL, but someday we might have
2453 : : * to work harder.
2454 : : */
2455 [ + - ]: 4 : ereport(ERROR,
2456 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2457 : : errmsg("transition table \"%s\" cannot be referenced in a persistent object",
2458 : : rte->eref->aliasname)));
2459 : : break;
7562 2460 : 29664 : default:
2461 : : /* Other RTE types can be ignored here */
2462 : 29664 : break;
2463 : : }
2464 : : }
2465 : :
2466 : : /*
2467 : : * If the query is an INSERT, UPDATE, or MERGE, we should create a
2468 : : * dependency on each target column, to prevent the specific target
2469 : : * column from being dropped. Although we will visit the TargetEntry
2470 : : * nodes again during query_tree_walker, we won't have enough context
2471 : : * to do this conveniently, so do it here. (For MERGE, the target
2472 : : * columns are those of its INSERT and UPDATE actions.)
2473 : : */
5375 2474 [ + + ]: 66375 : if (query->commandType == CMD_INSERT ||
5 dean.a.rasheed@gmail 2475 [ + + ]: 65725 : query->commandType == CMD_UPDATE ||
2476 [ + + ]: 65425 : query->commandType == CMD_MERGE)
2477 : : {
2478 : : RangeTblEntry *rte;
2479 : :
5375 tgl@sss.pgh.pa.us 2480 [ + - - + ]: 1964 : if (query->resultRelation <= 0 ||
2481 : 982 : query->resultRelation > list_length(query->rtable))
5375 tgl@sss.pgh.pa.us 2482 [ # # ]:UBC 0 : elog(ERROR, "invalid resultRelation %d",
2483 : : query->resultRelation);
5375 tgl@sss.pgh.pa.us 2484 :CBC 982 : rte = rt_fetch(query->resultRelation, query->rtable);
2485 [ + - ]: 982 : if (rte->rtekind == RTE_RELATION)
2486 : : {
5 dean.a.rasheed@gmail 2487 [ + + ]: 982 : if (query->commandType == CMD_MERGE)
2488 : : {
2489 [ + - + + : 200 : foreach_node(MergeAction, action, query->mergeActionList)
+ + ]
2490 : : {
2491 [ + + ]: 136 : if (action->commandType == CMD_INSERT ||
2492 [ + + ]: 72 : action->commandType == CMD_UPDATE)
2493 : 112 : add_target_column_dependencies(action->targetList,
2494 : : rte->relid,
2495 : : context);
2496 : : }
2497 : : }
2498 : : else
2499 : 950 : add_target_column_dependencies(query->targetList,
2500 : : rte->relid, context);
2501 : : }
2502 : : }
2503 : :
2504 : : /*
2505 : : * Add dependencies on constraints listed in query's constraintDeps
2506 : : */
5957 tgl@sss.pgh.pa.us 2507 [ + + + + : 66500 : foreach(lc, query->constraintDeps)
+ + ]
2508 : : {
1005 michael@paquier.xyz 2509 : 125 : add_object_address(ConstraintRelationId, lfirst_oid(lc), 0,
2510 : : context->addrs);
2511 : : }
2512 : :
2513 : : /* Examine substructure of query */
8901 tgl@sss.pgh.pa.us 2514 : 66375 : context->rtables = lcons(query->rtable, context->rtables);
2515 : 66375 : result = query_tree_walker(query,
2516 : : find_expr_references_walker,
2517 : : context,
2518 : : QTW_IGNORE_JOINALIASES |
2519 : : QTW_EXAMINE_SORTGROUP);
8221 neilc@samurai.com 2520 : 66375 : context->rtables = list_delete_first(context->rtables);
8901 tgl@sss.pgh.pa.us 2521 : 66375 : return result;
2522 : : }
6687 2523 [ + + ]: 906447 : else if (IsA(node, SetOperationStmt))
2524 : : {
2525 : 7051 : SetOperationStmt *setop = (SetOperationStmt *) node;
2526 : :
2527 : : /* we need to look at the groupClauses for operator references */
2528 : 7051 : find_expr_references_walker((Node *) setop->groupClauses, context);
2529 : : /* fall through to examine child nodes */
2530 : : }
4755 2531 [ + + ]: 899396 : else if (IsA(node, RangeTblFunction))
2532 : : {
2533 : 10654 : RangeTblFunction *rtfunc = (RangeTblFunction *) node;
2534 : : ListCell *ct;
2535 : :
2536 : : /*
2537 : : * Add refs for any datatypes and collations used in a column
2538 : : * definition list for a RECORD function. (For other cases, it should
2539 : : * be enough to depend on the function itself.)
2540 : : */
2541 [ + + + + : 10858 : foreach(ct, rtfunc->funccoltypes)
+ + ]
2542 : : {
1005 michael@paquier.xyz 2543 : 204 : add_object_address(TypeRelationId, lfirst_oid(ct), 0,
2544 : : context->addrs);
2545 : : }
4755 tgl@sss.pgh.pa.us 2546 [ + + + + : 10858 : foreach(ct, rtfunc->funccolcollations)
+ + ]
2547 : : {
2548 : 204 : Oid collid = lfirst_oid(ct);
2549 : :
2031 tmunro@postgresql.or 2550 [ + + - + ]: 204 : if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2551 :UBC 0 : add_object_address(CollationRelationId, collid, 0,
2552 : : context->addrs);
2553 : : }
2554 : : }
2572 tgl@sss.pgh.pa.us 2555 [ + + ]:CBC 888742 : else if (IsA(node, TableFunc))
2556 : : {
2557 : 210 : TableFunc *tf = (TableFunc *) node;
2558 : : ListCell *ct;
2559 : :
2560 : : /*
2561 : : * Add refs for the datatypes and collations used in the TableFunc.
2562 : : */
2563 [ + - + + : 1221 : foreach(ct, tf->coltypes)
+ + ]
2564 : : {
1005 michael@paquier.xyz 2565 : 1011 : add_object_address(TypeRelationId, lfirst_oid(ct), 0,
2566 : : context->addrs);
2567 : : }
2572 tgl@sss.pgh.pa.us 2568 [ + - + + : 1221 : foreach(ct, tf->colcollations)
+ + ]
2569 : : {
2570 : 1011 : Oid collid = lfirst_oid(ct);
2571 : :
2031 tmunro@postgresql.or 2572 [ + + - + ]: 1011 : if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
1005 michael@paquier.xyz 2573 :UBC 0 : add_object_address(CollationRelationId, collid, 0,
2574 : : context->addrs);
2575 : : }
2576 : : }
4144 tgl@sss.pgh.pa.us 2577 [ + + ]:CBC 888532 : else if (IsA(node, TableSampleClause))
2578 : : {
2579 : 36 : TableSampleClause *tsc = (TableSampleClause *) node;
2580 : :
1005 michael@paquier.xyz 2581 : 36 : add_object_address(ProcedureRelationId, tsc->tsmhandler, 0,
2582 : : context->addrs);
2583 : : /* fall through to examine arguments */
2584 : : }
2585 : :
8901 tgl@sss.pgh.pa.us 2586 : 1430247 : return expression_tree_walker(node, find_expr_references_walker,
2587 : : context);
2588 : : }
2589 : :
2590 : : /*
2591 : : * find_expr_references_walker subroutine: handle a Var reference
2592 : : * to an RTE_FUNCTION RTE
2593 : : */
2594 : : static void
1590 2595 : 93894 : process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum,
2596 : : find_expr_references_context *context)
2597 : : {
2598 : 93894 : int atts_done = 0;
2599 : : ListCell *lc;
2600 : :
2601 : : /*
2602 : : * Identify which RangeTblFunction produces this attnum, and see if it
2603 : : * returns a composite type. If so, we'd better make a dependency on the
2604 : : * referenced column of the composite type (or actually, of its associated
2605 : : * relation).
2606 : : */
2607 [ + - + + : 94226 : foreach(lc, rte->functions)
+ + ]
2608 : : {
2609 : 94070 : RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2610 : :
2611 [ + - ]: 94070 : if (attnum > atts_done &&
2612 [ + + ]: 94070 : attnum <= atts_done + rtfunc->funccolcount)
2613 : : {
2614 : : TupleDesc tupdesc;
2615 : :
2616 : : /* If it has a coldeflist, it certainly returns RECORD */
957 2617 [ + + ]: 93738 : if (rtfunc->funccolnames != NIL)
2618 : 204 : tupdesc = NULL; /* no need to work hard */
2619 : : else
2620 : 93534 : tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr, true);
1590 2621 [ + + + + ]: 93738 : if (tupdesc && tupdesc->tdtypeid != RECORDOID)
2622 : : {
2623 : : /*
2624 : : * Named composite type, so individual columns could get
2625 : : * dropped. Make a dependency on this specific column.
2626 : : */
2627 : 1194 : Oid reltype = get_typ_typrelid(tupdesc->tdtypeid);
2628 : :
2629 [ - + ]: 1194 : Assert(attnum - atts_done <= tupdesc->natts);
2630 [ + - ]: 1194 : if (OidIsValid(reltype)) /* can this fail? */
1005 michael@paquier.xyz 2631 : 1194 : add_object_address(RelationRelationId, reltype,
2632 : : attnum - atts_done,
2633 : : context->addrs);
1590 tgl@sss.pgh.pa.us 2634 : 93738 : return;
2635 : : }
2636 : : /* Nothing to do; function's result type is handled elsewhere */
2637 : 92544 : return;
2638 : : }
2639 : 332 : atts_done += rtfunc->funccolcount;
2640 : : }
2641 : :
2642 : : /* If we get here, must be looking for the ordinality column */
2643 [ + - + - ]: 156 : if (rte->funcordinality && attnum == atts_done + 1)
2644 : 156 : return;
2645 : :
2646 : : /* this probably can't happen ... */
1590 tgl@sss.pgh.pa.us 2647 [ # # ]:UBC 0 : ereport(ERROR,
2648 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2649 : : errmsg("column %d of relation \"%s\" does not exist",
2650 : : attnum, rte->eref->aliasname)));
2651 : : }
2652 : :
2653 : : /*
2654 : : * find_temp_object - search an array of dependency references for temp objects
2655 : : *
2656 : : * Scan an ObjectAddresses array for references to temporary objects (objects
2657 : : * in temporary namespaces), ignoring those in our own temp namespace if
2658 : : * local_temp_okay is true. If one is found, return true after storing its
2659 : : * address in *foundobj.
2660 : : *
2661 : : * Current callers only use this to deliver helpful notices, so reporting
2662 : : * one such object seems sufficient. We return the first one, which should
2663 : : * be a stable result for a given query since it depends only on the order
2664 : : * in which this module searches query trees. (However, it's important to
2665 : : * call this before de-duplicating the objects, else OID order would affect
2666 : : * the result.)
2667 : : */
2668 : : bool
370 tgl@sss.pgh.pa.us 2669 :CBC 26228 : find_temp_object(const ObjectAddresses *addrs, bool local_temp_okay,
2670 : : ObjectAddress *foundobj)
2671 : : {
2672 [ + + ]: 563421 : for (int i = 0; i < addrs->numrefs; i++)
2673 : : {
2674 : 537316 : const ObjectAddress *thisobj = addrs->refs + i;
2675 : : Oid objnamespace;
2676 : :
2677 : : /*
2678 : : * Use get_object_namespace() to see if this object belongs to a
2679 : : * schema. If not, we can skip it.
2680 : : */
2681 : 537316 : objnamespace = get_object_namespace(thisobj);
2682 : :
2683 : : /*
2684 : : * If the object is in a temporary namespace, complain, except if
2685 : : * local_temp_okay and it's our own temp namespace.
2686 : : */
2687 [ + + + + ]: 537316 : if (OidIsValid(objnamespace) && isAnyTempNamespace(objnamespace) &&
2688 [ + + - + ]: 124 : !(local_temp_okay && isTempNamespace(objnamespace)))
2689 : : {
2690 : 123 : *foundobj = *thisobj;
2691 : 123 : return true;
2692 : : }
2693 : : }
2694 : 26105 : return false;
2695 : : }
2696 : :
2697 : : /*
2698 : : * query_uses_temp_object - convenience wrapper for find_temp_object
2699 : : *
2700 : : * If the Query includes any use of a temporary object, fill *temp_object
2701 : : * with the address of one such object and return true.
2702 : : */
2703 : : bool
369 2704 : 11216 : query_uses_temp_object(Query *query, ObjectAddress *temp_object)
2705 : : {
2706 : : bool result;
2707 : : ObjectAddresses *addrs;
2708 : :
2709 : 11216 : addrs = new_object_addresses();
2710 : :
2711 : : /* Collect all dependencies from the Query */
2712 : 11216 : collectDependenciesOfExpr(addrs, (Node *) query, NIL);
2713 : :
2714 : : /* Look for one that is temp */
2715 : 11212 : result = find_temp_object(addrs, false, temp_object);
2716 : :
2717 : 11212 : free_object_addresses(addrs);
2718 : :
2719 : 11212 : return result;
2720 : : }
2721 : :
2722 : : /*
2723 : : * Given an array of dependency references, eliminate any duplicates.
2724 : : */
2725 : : static void
8901 2726 : 313474 : eliminate_duplicate_dependencies(ObjectAddresses *addrs)
2727 : : {
2728 : : ObjectAddress *priorobj;
2729 : : int oldref,
2730 : : newrefs;
2731 : :
2732 : : /*
2733 : : * We can't sort if the array has "extra" data, because there's no way to
2734 : : * keep it in sync. Fortunately that combination of features is not
2735 : : * needed.
2736 : : */
6747 2737 [ - + ]: 313474 : Assert(!addrs->extras);
2738 : :
8901 2739 [ + + ]: 313474 : if (addrs->numrefs <= 1)
2740 : 100389 : return; /* nothing to do */
2741 : :
2742 : : /* Sort the refs so that duplicates are adjacent */
1390 peter@eisentraut.org 2743 : 213085 : qsort(addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
2744 : : object_address_comparator);
2745 : :
2746 : : /* Remove dups */
8901 tgl@sss.pgh.pa.us 2747 : 213085 : priorobj = addrs->refs;
2748 : 213085 : newrefs = 1;
2749 [ + + ]: 1773426 : for (oldref = 1; oldref < addrs->numrefs; oldref++)
2750 : : {
8851 bruce@momjian.us 2751 : 1560341 : ObjectAddress *thisobj = addrs->refs + oldref;
2752 : :
8901 tgl@sss.pgh.pa.us 2753 [ + + ]: 1560341 : if (priorobj->classId == thisobj->classId &&
2754 [ + + ]: 1346200 : priorobj->objectId == thisobj->objectId)
2755 : : {
2756 [ + + ]: 835575 : if (priorobj->objectSubId == thisobj->objectSubId)
2757 : 637412 : continue; /* identical, so drop thisobj */
2758 : :
2759 : : /*
2760 : : * If we have a whole-object reference and a reference to a part
2761 : : * of the same object, we don't need the whole-object reference
2762 : : * (for example, we don't need to reference both table foo and
2763 : : * column foo.bar). The whole-object reference will always appear
2764 : : * first in the sorted list.
2765 : : */
2766 [ + + ]: 198163 : if (priorobj->objectSubId == 0)
2767 : : {
2768 : : /* replace whole ref with partial */
2769 : 46325 : priorobj->objectSubId = thisobj->objectSubId;
2770 : 46325 : continue;
2771 : : }
2772 : : }
2773 : : /* Not identical, so add thisobj to output set */
2774 : 876604 : priorobj++;
6747 2775 : 876604 : *priorobj = *thisobj;
8901 2776 : 876604 : newrefs++;
2777 : : }
2778 : :
2779 : 213085 : addrs->numrefs = newrefs;
2780 : : }
2781 : :
2782 : : /*
2783 : : * qsort comparator for ObjectAddress items
2784 : : */
2785 : : static int
2786 : 5807478 : object_address_comparator(const void *a, const void *b)
2787 : : {
2788 : 5807478 : const ObjectAddress *obja = (const ObjectAddress *) a;
2789 : 5807478 : const ObjectAddress *objb = (const ObjectAddress *) b;
2790 : :
2791 : : /*
2792 : : * Primary sort key is OID descending. Most of the time, this will result
2793 : : * in putting newer objects before older ones, which is likely to be the
2794 : : * right order to delete in.
2795 : : */
2868 2796 [ + + ]: 5807478 : if (obja->objectId > objb->objectId)
8901 2797 : 1510693 : return -1;
2798 [ + + ]: 4296785 : if (obja->objectId < objb->objectId)
2868 2799 : 2750689 : return 1;
2800 : :
2801 : : /*
2802 : : * Next sort on catalog ID, in case identical OIDs appear in different
2803 : : * catalogs. Sort direction is pretty arbitrary here.
2804 : : */
2805 [ - + ]: 1546096 : if (obja->classId < objb->classId)
8901 tgl@sss.pgh.pa.us 2806 :UBC 0 : return -1;
2868 tgl@sss.pgh.pa.us 2807 [ - + ]:CBC 1546096 : if (obja->classId > objb->classId)
8901 tgl@sss.pgh.pa.us 2808 :UBC 0 : return 1;
2809 : :
2810 : : /*
2811 : : * Last, sort on object subId.
2812 : : *
2813 : : * We sort the subId as an unsigned int so that 0 (the whole object) will
2814 : : * come first. This is essential for eliminate_duplicate_dependencies,
2815 : : * and is also the best order for findDependentObjects.
2816 : : */
8901 tgl@sss.pgh.pa.us 2817 [ + + ]:CBC 1546096 : if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2818 : 384804 : return -1;
2819 [ + + ]: 1161292 : if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2820 : 375216 : return 1;
2821 : 786076 : return 0;
2822 : : }
2823 : :
2824 : : /*
2825 : : * Routines for handling an expansible array of ObjectAddress items.
2826 : : *
2827 : : * new_object_addresses: create a new ObjectAddresses array.
2828 : : */
2829 : : ObjectAddresses *
7405 alvherre@alvh.no-ip. 2830 : 373324 : new_object_addresses(void)
2831 : : {
2832 : : ObjectAddresses *addrs;
2833 : :
353 michael@paquier.xyz 2834 : 373324 : addrs = palloc_object(ObjectAddresses);
2835 : :
8901 tgl@sss.pgh.pa.us 2836 : 373324 : addrs->numrefs = 0;
7405 alvherre@alvh.no-ip. 2837 : 373324 : addrs->maxrefs = 32;
353 michael@paquier.xyz 2838 : 373324 : addrs->refs = palloc_array(ObjectAddress, addrs->maxrefs);
6747 tgl@sss.pgh.pa.us 2839 : 373324 : addrs->extras = NULL; /* until/unless needed */
2840 : :
7405 alvherre@alvh.no-ip. 2841 : 373324 : return addrs;
2842 : : }
2843 : :
2844 : : /*
2845 : : * Add an entry to an ObjectAddresses array.
2846 : : */
2847 : : static void
1005 michael@paquier.xyz 2848 : 1440209 : add_object_address(Oid classId, Oid objectId, int32 subId,
2849 : : ObjectAddresses *addrs)
2850 : : {
2851 : : ObjectAddress *item;
2852 : :
2853 : : /* enlarge array if needed */
8901 tgl@sss.pgh.pa.us 2854 [ + + ]: 1440209 : if (addrs->numrefs >= addrs->maxrefs)
2855 : : {
2856 : 20038 : addrs->maxrefs *= 2;
103 michael@paquier.xyz 2857 :GNC 20038 : addrs->refs = repalloc_array(addrs->refs, ObjectAddress, addrs->maxrefs);
6747 tgl@sss.pgh.pa.us 2858 [ - + ]:CBC 20038 : Assert(!addrs->extras);
2859 : : }
2860 : : /* record this item */
8901 2861 : 1440209 : item = addrs->refs + addrs->numrefs;
1005 michael@paquier.xyz 2862 : 1440209 : item->classId = classId;
8901 tgl@sss.pgh.pa.us 2863 : 1440209 : item->objectId = objectId;
2864 : 1440209 : item->objectSubId = subId;
2865 : 1440209 : addrs->numrefs++;
2866 : 1440209 : }
2867 : :
2868 : : /*
2869 : : * Add an entry to an ObjectAddresses array.
2870 : : *
2871 : : * As above, but specify entry exactly.
2872 : : */
2873 : : void
2874 : 857535 : add_exact_object_address(const ObjectAddress *object,
2875 : : ObjectAddresses *addrs)
2876 : : {
2877 : : ObjectAddress *item;
2878 : :
2879 : : /* enlarge array if needed */
2880 [ + + ]: 857535 : if (addrs->numrefs >= addrs->maxrefs)
2881 : : {
2882 : 37 : addrs->maxrefs *= 2;
103 michael@paquier.xyz 2883 :GNC 37 : addrs->refs = repalloc_array(addrs->refs, ObjectAddress, addrs->maxrefs);
6747 tgl@sss.pgh.pa.us 2884 [ - + ]:CBC 37 : Assert(!addrs->extras);
2885 : : }
2886 : : /* record this item */
8901 2887 : 857535 : item = addrs->refs + addrs->numrefs;
2888 : 857535 : *item = *object;
2889 : 857535 : addrs->numrefs++;
2890 : 857535 : }
2891 : :
2892 : : /*
2893 : : * Add an entry to an ObjectAddresses array.
2894 : : *
2895 : : * As above, but specify entry exactly and provide some "extra" data too.
2896 : : */
2897 : : static void
6747 2898 : 156997 : add_exact_object_address_extra(const ObjectAddress *object,
2899 : : const ObjectAddressExtra *extra,
2900 : : ObjectAddresses *addrs)
2901 : : {
2902 : : ObjectAddress *item;
2903 : : ObjectAddressExtra *itemextra;
2904 : :
2905 : : /* allocate extra space if first time */
2906 [ + + ]: 156997 : if (!addrs->extras)
103 michael@paquier.xyz 2907 :GNC 24397 : addrs->extras = palloc_array(ObjectAddressExtra, addrs->maxrefs);
2908 : :
2909 : : /* enlarge array if needed */
6747 tgl@sss.pgh.pa.us 2910 [ + + ]:CBC 156997 : if (addrs->numrefs >= addrs->maxrefs)
2911 : : {
2912 : 592 : addrs->maxrefs *= 2;
103 michael@paquier.xyz 2913 :GNC 592 : addrs->refs = repalloc_array(addrs->refs, ObjectAddress, addrs->maxrefs);
2914 : 592 : addrs->extras = repalloc_array(addrs->extras, ObjectAddressExtra, addrs->maxrefs);
2915 : : }
2916 : : /* record this item */
6747 tgl@sss.pgh.pa.us 2917 :CBC 156997 : item = addrs->refs + addrs->numrefs;
2918 : 156997 : *item = *object;
2919 : 156997 : itemextra = addrs->extras + addrs->numrefs;
2920 : 156997 : *itemextra = *extra;
2921 : 156997 : addrs->numrefs++;
2922 : 156997 : }
2923 : :
2924 : : /*
2925 : : * Test whether an object is present in an ObjectAddresses array.
2926 : : *
2927 : : * We return "true" if object is a subobject of something in the array, too.
2928 : : */
2929 : : bool
8833 2930 : 512 : object_address_present(const ObjectAddress *object,
2931 : : const ObjectAddresses *addrs)
2932 : : {
2933 : : int i;
2934 : :
2935 [ + + ]: 1835 : for (i = addrs->numrefs - 1; i >= 0; i--)
2936 : : {
6747 2937 : 1360 : const ObjectAddress *thisobj = addrs->refs + i;
2938 : :
8833 2939 [ + + ]: 1360 : if (object->classId == thisobj->classId &&
2940 [ + + ]: 404 : object->objectId == thisobj->objectId)
2941 : : {
2942 [ - + ]: 37 : if (object->objectSubId == thisobj->objectSubId ||
8833 tgl@sss.pgh.pa.us 2943 [ # # ]:UBC 0 : thisobj->objectSubId == 0)
8833 tgl@sss.pgh.pa.us 2944 :CBC 37 : return true;
2945 : : }
2946 : : }
2947 : :
2948 : 475 : return false;
2949 : : }
2950 : :
2951 : : /*
2952 : : * As above, except that if the object is present then also OR the given
2953 : : * flags into its associated extra data (which must exist).
2954 : : */
2955 : : static bool
6747 2956 : 192180 : object_address_present_add_flags(const ObjectAddress *object,
2957 : : int flags,
2958 : : ObjectAddresses *addrs)
2959 : : {
4400 2960 : 192180 : bool result = false;
2961 : : int i;
2962 : :
6747 2963 [ + + ]: 7039997 : for (i = addrs->numrefs - 1; i >= 0; i--)
2964 : : {
2965 : 6847817 : ObjectAddress *thisobj = addrs->refs + i;
2966 : :
2967 [ + + ]: 6847817 : if (object->classId == thisobj->classId &&
2968 [ + + ]: 2649862 : object->objectId == thisobj->objectId)
2969 : : {
2970 [ + + ]: 33970 : if (object->objectSubId == thisobj->objectSubId)
2971 : : {
2972 : 33593 : ObjectAddressExtra *thisextra = addrs->extras + i;
2973 : :
2974 : 33593 : thisextra->flags |= flags;
4400 2975 : 33593 : result = true;
2976 : : }
2977 [ + + ]: 377 : else if (thisobj->objectSubId == 0)
2978 : : {
2979 : : /*
2980 : : * We get here if we find a need to delete a column after
2981 : : * having already decided to drop its whole table. Obviously
2982 : : * we no longer need to drop the subobject, so report that we
2983 : : * found the subobject in the array. But don't plaster its
2984 : : * flags on the whole object.
2985 : : */
2986 : 313 : result = true;
2987 : : }
2988 [ + + ]: 64 : else if (object->objectSubId == 0)
2989 : : {
2990 : : /*
2991 : : * We get here if we find a need to delete a whole table after
2992 : : * having already decided to drop one of its columns. We
2993 : : * can't report that the whole object is in the array, but we
2994 : : * should mark the subobject with the whole object's flags.
2995 : : *
2996 : : * It might seem attractive to physically delete the column's
2997 : : * array entry, or at least mark it as no longer needing
2998 : : * separate deletion. But that could lead to, e.g., dropping
2999 : : * the column's datatype before we drop the table, which does
3000 : : * not seem like a good idea. This is a very rare situation
3001 : : * in practice, so we just take the hit of doing a separate
3002 : : * DROP COLUMN action even though we know we're gonna delete
3003 : : * the table later.
3004 : : *
3005 : : * What we can do, though, is mark this as a subobject so that
3006 : : * we don't report it separately, which is confusing because
3007 : : * it's unpredictable whether it happens or not. But do so
3008 : : * only if flags != 0 (flags == 0 is a read-only probe).
3009 : : *
3010 : : * Because there could be other subobjects of this object in
3011 : : * the array, this case means we always have to loop through
3012 : : * the whole array; we cannot exit early on a match.
3013 : : */
3014 : 52 : ObjectAddressExtra *thisextra = addrs->extras + i;
3015 : :
2871 3016 [ + - ]: 52 : if (flags)
3017 : 52 : thisextra->flags |= (flags | DEPFLAG_SUBOBJECT);
3018 : : }
3019 : : }
3020 : : }
3021 : :
4400 3022 : 192180 : return result;
3023 : : }
3024 : :
3025 : : /*
3026 : : * Similar to above, except we search an ObjectAddressStack.
3027 : : */
3028 : : static bool
5575 3029 : 276066 : stack_address_present_add_flags(const ObjectAddress *object,
3030 : : int flags,
3031 : : ObjectAddressStack *stack)
3032 : : {
4400 3033 : 276066 : bool result = false;
3034 : : ObjectAddressStack *stackptr;
3035 : :
5575 3036 [ + + ]: 728962 : for (stackptr = stack; stackptr; stackptr = stackptr->next)
3037 : : {
3038 : 452896 : const ObjectAddress *thisobj = stackptr->object;
3039 : :
3040 [ + + ]: 452896 : if (object->classId == thisobj->classId &&
3041 [ + + ]: 201603 : object->objectId == thisobj->objectId)
3042 : : {
3043 [ + + ]: 83934 : if (object->objectSubId == thisobj->objectSubId)
3044 : : {
3045 : 83212 : stackptr->flags |= flags;
4400 3046 : 83212 : result = true;
3047 : : }
3048 [ + + ]: 722 : else if (thisobj->objectSubId == 0)
3049 : : {
3050 : : /*
3051 : : * We're visiting a column with whole table already on stack.
3052 : : * As in object_address_present_add_flags(), we can skip
3053 : : * further processing of the subobject, but we don't want to
3054 : : * propagate flags for the subobject to the whole object.
3055 : : */
3056 : 674 : result = true;
3057 : : }
3058 [ - + ]: 48 : else if (object->objectSubId == 0)
3059 : : {
3060 : : /*
3061 : : * We're visiting a table with column already on stack. As in
3062 : : * object_address_present_add_flags(), we should propagate
3063 : : * flags for the whole object to each of its subobjects.
3064 : : */
2871 tgl@sss.pgh.pa.us 3065 [ # # ]:UBC 0 : if (flags)
3066 : 0 : stackptr->flags |= (flags | DEPFLAG_SUBOBJECT);
3067 : : }
3068 : : }
3069 : : }
3070 : :
4400 tgl@sss.pgh.pa.us 3071 :CBC 276066 : return result;
3072 : : }
3073 : :
3074 : : /*
3075 : : * Record multiple dependencies from an ObjectAddresses array, after first
3076 : : * removing any duplicates.
3077 : : */
3078 : : void
7039 3079 : 267969 : record_object_address_dependencies(const ObjectAddress *depender,
3080 : : ObjectAddresses *referenced,
3081 : : DependencyType behavior)
3082 : : {
3083 : 267969 : eliminate_duplicate_dependencies(referenced);
3084 : 267969 : recordMultipleDependencies(depender,
2217 tmunro@postgresql.or 3085 : 267969 : referenced->refs, referenced->numrefs,
3086 : : behavior);
7039 tgl@sss.pgh.pa.us 3087 : 267964 : }
3088 : :
3089 : : /*
3090 : : * Sort the items in an ObjectAddresses array.
3091 : : *
3092 : : * The major sort key is OID-descending, so that newer objects will be listed
3093 : : * first in most cases. This is primarily useful for ensuring stable outputs
3094 : : * from regression tests; it's not recommended if the order of the objects is
3095 : : * determined by user input, such as the order of targets in a DROP command.
3096 : : */
3097 : : void
2810 3098 : 101 : sort_object_addresses(ObjectAddresses *addrs)
3099 : : {
3100 [ + + ]: 101 : if (addrs->numrefs > 1)
1390 peter@eisentraut.org 3101 : 50 : qsort(addrs->refs, addrs->numrefs,
3102 : : sizeof(ObjectAddress),
3103 : : object_address_comparator);
2810 tgl@sss.pgh.pa.us 3104 : 101 : }
3105 : :
3106 : : /*
3107 : : * Clean up when done with an ObjectAddresses array.
3108 : : */
3109 : : void
7405 alvherre@alvh.no-ip. 3110 : 371760 : free_object_addresses(ObjectAddresses *addrs)
3111 : : {
8901 tgl@sss.pgh.pa.us 3112 : 371760 : pfree(addrs->refs);
6747 3113 [ + + ]: 371760 : if (addrs->extras)
3114 : 24105 : pfree(addrs->extras);
7405 alvherre@alvh.no-ip. 3115 : 371760 : pfree(addrs);
8901 tgl@sss.pgh.pa.us 3116 : 371760 : }
3117 : :
3118 : : /*
3119 : : * delete initial ACL for extension objects
3120 : : */
3121 : : static void
3888 sfrost@snowman.net 3122 : 153476 : DeleteInitPrivs(const ObjectAddress *object)
3123 : : {
3124 : : Relation relation;
3125 : : ScanKeyData key[3];
3126 : : int nkeys;
3127 : : SysScanDesc scan;
3128 : : HeapTuple oldtuple;
3129 : :
2868 andres@anarazel.de 3130 : 153476 : relation = table_open(InitPrivsRelationId, RowExclusiveLock);
3131 : :
3888 sfrost@snowman.net 3132 : 153476 : ScanKeyInit(&key[0],
3133 : : Anum_pg_init_privs_objoid,
3134 : : BTEqualStrategyNumber, F_OIDEQ,
3135 : 153476 : ObjectIdGetDatum(object->objectId));
3136 : 153476 : ScanKeyInit(&key[1],
3137 : : Anum_pg_init_privs_classoid,
3138 : : BTEqualStrategyNumber, F_OIDEQ,
3139 : 153476 : ObjectIdGetDatum(object->classId));
897 tgl@sss.pgh.pa.us 3140 [ + + ]: 153476 : if (object->objectSubId != 0)
3141 : : {
3142 : 1475 : ScanKeyInit(&key[2],
3143 : : Anum_pg_init_privs_objsubid,
3144 : : BTEqualStrategyNumber, F_INT4EQ,
3145 : 1475 : Int32GetDatum(object->objectSubId));
3146 : 1475 : nkeys = 3;
3147 : : }
3148 : : else
3149 : 152001 : nkeys = 2;
3150 : :
3888 sfrost@snowman.net 3151 : 153476 : scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
3152 : : NULL, nkeys, key);
3153 : :
3154 [ + + ]: 153557 : while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
3587 tgl@sss.pgh.pa.us 3155 : 81 : CatalogTupleDelete(relation, &oldtuple->t_self);
3156 : :
3888 sfrost@snowman.net 3157 : 153476 : systable_endscan(scan);
3158 : :
2868 andres@anarazel.de 3159 : 153476 : table_close(relation, RowExclusiveLock);
3888 sfrost@snowman.net 3160 : 153476 : }
|