Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_dump_sort.c
4 : : * Sort the items of a dump into a safe order for dumping
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : *
11 : : * IDENTIFICATION
12 : : * src/bin/pg_dump/pg_dump_sort.c
13 : : *
14 : : *-------------------------------------------------------------------------
15 : : */
16 : : #include "postgres_fe.h"
17 : :
18 : : #include "catalog/pg_class_d.h"
19 : : #include "common/int.h"
20 : : #include "lib/binaryheap.h"
21 : : #include "pg_backup_utils.h"
22 : : #include "pg_dump.h"
23 : :
24 : : /*
25 : : * Sort priority for database object types.
26 : : * Objects are sorted by type, and within a type by name.
27 : : *
28 : : * Triggers, event triggers, and materialized views are intentionally sorted
29 : : * late. Triggers must be restored after all data modifications, so that
30 : : * they don't interfere with loading data. Event triggers are restored
31 : : * next-to-last so that they don't interfere with object creations of any
32 : : * kind. Matview refreshes are last because they should execute in the
33 : : * database's normal state (e.g., they must come after all ACLs are restored;
34 : : * also, if they choose to look at system catalogs, they should see the final
35 : : * restore state). If you think to change this, see also the RestorePass
36 : : * mechanism in pg_backup_archiver.c.
37 : : *
38 : : * On the other hand, casts are intentionally sorted earlier than you might
39 : : * expect; logically they should come after functions, since they usually
40 : : * depend on those. This works around the backend's habit of recording
41 : : * views that use casts as dependent on the cast's underlying function.
42 : : * We initially sort casts first, and then any functions used by casts
43 : : * will be hoisted above the casts, and in turn views that those functions
44 : : * depend on will be hoisted above the functions. But views not used that
45 : : * way won't be hoisted.
46 : : *
47 : : * NOTE: object-type priorities must match the section assignments made in
48 : : * pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUNDARY,
49 : : * POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA objects
50 : : * must sort between them.
51 : : */
52 : :
53 : : /* This enum lists the priority levels in order */
54 : : enum dbObjectTypePriorities
55 : : {
56 : : PRIO_NAMESPACE = 1,
57 : : PRIO_PROCLANG,
58 : : PRIO_COLLATION,
59 : : PRIO_TRANSFORM,
60 : : PRIO_EXTENSION,
61 : : PRIO_TYPE, /* used for DO_TYPE and DO_SHELL_TYPE */
62 : : PRIO_CAST,
63 : : PRIO_FUNC,
64 : : PRIO_AGG,
65 : : PRIO_ACCESS_METHOD,
66 : : PRIO_OPERATOR,
67 : : PRIO_OPFAMILY, /* used for DO_OPFAMILY and DO_OPCLASS */
68 : : PRIO_CONVERSION,
69 : : PRIO_TSPARSER,
70 : : PRIO_TSTEMPLATE,
71 : : PRIO_TSDICT,
72 : : PRIO_TSCONFIG,
73 : : PRIO_FDW,
74 : : PRIO_FOREIGN_SERVER,
75 : : PRIO_TABLE,
76 : : PRIO_TABLE_ATTACH,
77 : : PRIO_DUMMY_TYPE,
78 : : PRIO_ATTRDEF,
79 : : PRIO_PRE_DATA_BOUNDARY, /* boundary! */
80 : : PRIO_TABLE_DATA,
81 : : PRIO_SEQUENCE_SET,
82 : : PRIO_LARGE_OBJECT,
83 : : PRIO_LARGE_OBJECT_DATA,
84 : : PRIO_STATISTICS_DATA_DATA,
85 : : PRIO_POST_DATA_BOUNDARY, /* boundary! */
86 : : PRIO_CONSTRAINT,
87 : : PRIO_INDEX,
88 : : PRIO_INDEX_ATTACH,
89 : : PRIO_STATSEXT,
90 : : PRIO_RULE,
91 : : PRIO_TRIGGER,
92 : : PRIO_FK_CONSTRAINT,
93 : : PRIO_POLICY,
94 : : PRIO_PUBLICATION,
95 : : PRIO_PUBLICATION_REL,
96 : : PRIO_PUBLICATION_TABLE_IN_SCHEMA,
97 : : PRIO_SUBSCRIPTION,
98 : : PRIO_SUBSCRIPTION_REL,
99 : : PRIO_DEFAULT_ACL, /* done in ACL pass */
100 : : PRIO_EVENT_TRIGGER, /* must be next to last! */
101 : : PRIO_REFRESH_MATVIEW /* must be last! */
102 : : };
103 : :
104 : : /* This table is indexed by enum DumpableObjectType */
105 : : static const int dbObjectTypePriority[] =
106 : : {
107 : : [DO_NAMESPACE] = PRIO_NAMESPACE,
108 : : [DO_EXTENSION] = PRIO_EXTENSION,
109 : : [DO_TYPE] = PRIO_TYPE,
110 : : [DO_SHELL_TYPE] = PRIO_TYPE,
111 : : [DO_FUNC] = PRIO_FUNC,
112 : : [DO_AGG] = PRIO_AGG,
113 : : [DO_OPERATOR] = PRIO_OPERATOR,
114 : : [DO_ACCESS_METHOD] = PRIO_ACCESS_METHOD,
115 : : [DO_OPCLASS] = PRIO_OPFAMILY,
116 : : [DO_OPFAMILY] = PRIO_OPFAMILY,
117 : : [DO_COLLATION] = PRIO_COLLATION,
118 : : [DO_CONVERSION] = PRIO_CONVERSION,
119 : : [DO_TABLE] = PRIO_TABLE,
120 : : [DO_TABLE_ATTACH] = PRIO_TABLE_ATTACH,
121 : : [DO_ATTRDEF] = PRIO_ATTRDEF,
122 : : [DO_INDEX] = PRIO_INDEX,
123 : : [DO_INDEX_ATTACH] = PRIO_INDEX_ATTACH,
124 : : [DO_STATSEXT] = PRIO_STATSEXT,
125 : : [DO_RULE] = PRIO_RULE,
126 : : [DO_TRIGGER] = PRIO_TRIGGER,
127 : : [DO_CONSTRAINT] = PRIO_CONSTRAINT,
128 : : [DO_FK_CONSTRAINT] = PRIO_FK_CONSTRAINT,
129 : : [DO_PROCLANG] = PRIO_PROCLANG,
130 : : [DO_CAST] = PRIO_CAST,
131 : : [DO_TABLE_DATA] = PRIO_TABLE_DATA,
132 : : [DO_SEQUENCE_SET] = PRIO_SEQUENCE_SET,
133 : : [DO_DUMMY_TYPE] = PRIO_DUMMY_TYPE,
134 : : [DO_TSPARSER] = PRIO_TSPARSER,
135 : : [DO_TSDICT] = PRIO_TSDICT,
136 : : [DO_TSTEMPLATE] = PRIO_TSTEMPLATE,
137 : : [DO_TSCONFIG] = PRIO_TSCONFIG,
138 : : [DO_FDW] = PRIO_FDW,
139 : : [DO_FOREIGN_SERVER] = PRIO_FOREIGN_SERVER,
140 : : [DO_DEFAULT_ACL] = PRIO_DEFAULT_ACL,
141 : : [DO_TRANSFORM] = PRIO_TRANSFORM,
142 : : [DO_LARGE_OBJECT] = PRIO_LARGE_OBJECT,
143 : : [DO_LARGE_OBJECT_DATA] = PRIO_LARGE_OBJECT_DATA,
144 : : [DO_PRE_DATA_BOUNDARY] = PRIO_PRE_DATA_BOUNDARY,
145 : : [DO_POST_DATA_BOUNDARY] = PRIO_POST_DATA_BOUNDARY,
146 : : [DO_EVENT_TRIGGER] = PRIO_EVENT_TRIGGER,
147 : : [DO_REFRESH_MATVIEW] = PRIO_REFRESH_MATVIEW,
148 : : [DO_POLICY] = PRIO_POLICY,
149 : : [DO_PUBLICATION] = PRIO_PUBLICATION,
150 : : [DO_PUBLICATION_REL] = PRIO_PUBLICATION_REL,
151 : : [DO_PUBLICATION_TABLE_IN_SCHEMA] = PRIO_PUBLICATION_TABLE_IN_SCHEMA,
152 : : [DO_REL_STATS] = PRIO_STATISTICS_DATA_DATA,
153 : : [DO_SUBSCRIPTION] = PRIO_SUBSCRIPTION,
154 : : [DO_SUBSCRIPTION_REL] = PRIO_SUBSCRIPTION_REL,
155 : : };
156 : :
157 : : StaticAssertDecl(lengthof(dbObjectTypePriority) == NUM_DUMPABLE_OBJECT_TYPES,
158 : : "array length mismatch");
159 : :
160 : : static DumpId preDataBoundId;
161 : : static DumpId postDataBoundId;
162 : :
163 : :
164 : : static int DOTypeNameCompare(const void *p1, const void *p2);
165 : : static int pgTypeNameCompare(Oid typid1, Oid typid2);
166 : : static int accessMethodNameCompare(Oid am1, Oid am2);
167 : : static bool TopoSort(DumpableObject **objs,
168 : : int numObjs,
169 : : DumpableObject **ordering,
170 : : int *nOrdering);
171 : : static void findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs);
172 : : static int findLoop(DumpableObject *obj,
173 : : DumpId startPoint,
174 : : bool *processed,
175 : : DumpId *searchFailed,
176 : : DumpableObject **workspace,
177 : : int depth);
178 : : static void repairDependencyLoop(DumpableObject **loop,
179 : : int nLoop);
180 : : static void describeDumpableObject(DumpableObject *obj,
181 : : char *buf, int bufsize);
182 : : static int int_cmp(void *a, void *b, void *arg);
183 : :
184 : :
185 : : /*
186 : : * Sort the given objects into a type/name-based ordering
187 : : *
188 : : * Normally this is just the starting point for the dependency-based
189 : : * ordering.
190 : : */
191 : : void
7857 tgl@sss.pgh.pa.us 192 :CBC 185 : sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs)
193 : : {
194 [ + - ]: 185 : if (numObjs > 1)
942 peter@eisentraut.org 195 : 185 : qsort(objs, numObjs, sizeof(DumpableObject *),
196 : : DOTypeNameCompare);
7857 tgl@sss.pgh.pa.us 197 : 185 : }
198 : :
199 : : static int
200 : 10336383 : DOTypeNameCompare(const void *p1, const void *p2)
201 : : {
4836 bruce@momjian.us 202 : 10336383 : DumpableObject *obj1 = *(DumpableObject *const *) p1;
203 : 10336383 : DumpableObject *obj2 = *(DumpableObject *const *) p2;
204 : : int cmpval;
205 : :
206 : : /* Sort by type's priority */
3251 tgl@sss.pgh.pa.us 207 : 10336383 : cmpval = dbObjectTypePriority[obj1->objType] -
208 : 10336383 : dbObjectTypePriority[obj2->objType];
209 : :
7857 210 [ + + ]: 10336383 : if (cmpval != 0)
211 : 2334759 : return cmpval;
212 : :
213 : : /*
214 : : * Sort by namespace. Typically, all objects of the same priority would
215 : : * either have or not have a namespace link, but there are exceptions.
216 : : * Sort NULL namespace after non-NULL in such cases.
217 : : */
2587 218 [ + + ]: 8001624 : if (obj1->namespace)
219 : : {
220 [ + + ]: 7632055 : if (obj2->namespace)
221 : : {
222 : 7631994 : cmpval = strcmp(obj1->namespace->dobj.name,
223 : 7631994 : obj2->namespace->dobj.name);
224 [ + + ]: 7631994 : if (cmpval != 0)
225 : 306229 : return cmpval;
226 : : }
227 : : else
228 : 61 : return -1;
229 : : }
230 [ + + ]: 369569 : else if (obj2->namespace)
231 : 126 : return 1;
232 : :
233 : : /*
234 : : * Sort by name. With a few exceptions, names here are single catalog
235 : : * columns. To get a fuller picture, grep pg_dump.c for "dobj.name = ".
236 : : * Names here don't match "Name:" in plain format output, which is a
237 : : * _tocEntry.tag. For example, DumpableObject.name of a constraint is
238 : : * pg_constraint.conname, but _tocEntry.tag of a constraint is relname and
239 : : * conname joined with a space.
240 : : */
7857 241 : 7695208 : cmpval = strcmp(obj1->name, obj2->name);
242 [ + + ]: 7695208 : if (cmpval != 0)
243 : 6743670 : return cmpval;
244 : :
245 : : /*
246 : : * Sort by type. This helps types that share a type priority without
247 : : * sharing a unique name constraint, e.g. opclass and opfamily.
248 : : */
37 noah@leadboat.com 249 : 951538 : cmpval = obj1->objType - obj2->objType;
250 [ + + ]: 951538 : if (cmpval != 0)
251 : 34321 : return cmpval;
252 : :
253 : : /*
254 : : * To have a stable sort order, break ties for some object types. Most
255 : : * catalogs have a natural key, e.g. pg_proc_proname_args_nsp_index. Where
256 : : * the above "namespace" and "name" comparisons don't cover all natural
257 : : * key columns, compare the rest here.
258 : : *
259 : : * The natural key usually refers to other catalogs by surrogate keys.
260 : : * Hence, this translates each of those references to the natural key of
261 : : * the referenced catalog. That may descend through multiple levels of
262 : : * catalog references. For example, to sort by pg_proc.proargtypes,
263 : : * descend to each pg_type and then further to its pg_namespace, for an
264 : : * overall sort by (nspname, typname).
265 : : */
5671 bruce@momjian.us 266 [ + + + + ]: 917217 : if (obj1->objType == DO_FUNC || obj1->objType == DO_AGG)
5682 peter_e@gmx.net 267 :UBC 0 : {
4836 bruce@momjian.us 268 :CBC 76 : FuncInfo *fobj1 = *(FuncInfo *const *) p1;
269 : 76 : FuncInfo *fobj2 = *(FuncInfo *const *) p2;
270 : : int i;
271 : :
272 : : /* Sort by number of arguments, then argument type names */
5682 peter_e@gmx.net 273 : 76 : cmpval = fobj1->nargs - fobj2->nargs;
274 [ + + ]: 76 : if (cmpval != 0)
275 : 15 : return cmpval;
3837 tgl@sss.pgh.pa.us 276 [ + - ]: 69 : for (i = 0; i < fobj1->nargs; i++)
277 : : {
37 noah@leadboat.com 278 : 69 : cmpval = pgTypeNameCompare(fobj1->argtypes[i],
279 : 69 : fobj2->argtypes[i]);
280 [ + + ]: 69 : if (cmpval != 0)
281 : 61 : return cmpval;
282 : : }
283 : : }
4993 peter_e@gmx.net 284 [ + + ]: 917141 : else if (obj1->objType == DO_OPERATOR)
285 : : {
4836 bruce@momjian.us 286 : 679697 : OprInfo *oobj1 = *(OprInfo *const *) p1;
287 : 679697 : OprInfo *oobj2 = *(OprInfo *const *) p2;
288 : :
289 : : /* oprkind is 'l', 'r', or 'b'; this sorts prefix, postfix, infix */
4993 peter_e@gmx.net 290 : 679697 : cmpval = (oobj2->oprkind - oobj1->oprkind);
291 [ + + ]: 679697 : if (cmpval != 0)
292 : 20303 : return cmpval;
293 : : /* Within an oprkind, sort by argument type names */
37 noah@leadboat.com 294 : 659394 : cmpval = pgTypeNameCompare(oobj1->oprleft, oobj2->oprleft);
295 [ + + ]: 659394 : if (cmpval != 0)
296 : 581154 : return cmpval;
297 : 78240 : cmpval = pgTypeNameCompare(oobj1->oprright, oobj2->oprright);
298 [ + - ]: 78240 : if (cmpval != 0)
299 : 78240 : return cmpval;
300 : : }
301 [ + + ]: 237444 : else if (obj1->objType == DO_OPCLASS)
302 : : {
303 : 14161 : OpclassInfo *opcobj1 = *(OpclassInfo *const *) p1;
304 : 14161 : OpclassInfo *opcobj2 = *(OpclassInfo *const *) p2;
305 : :
306 : : /* Sort by access method name, per pg_opclass_am_name_nsp_index */
307 : 14161 : cmpval = accessMethodNameCompare(opcobj1->opcmethod,
308 : : opcobj2->opcmethod);
309 [ + - ]: 14161 : if (cmpval != 0)
310 : 14161 : return cmpval;
311 : : }
312 [ + + ]: 223283 : else if (obj1->objType == DO_OPFAMILY)
313 : : {
314 : 11450 : OpfamilyInfo *opfobj1 = *(OpfamilyInfo *const *) p1;
315 : 11450 : OpfamilyInfo *opfobj2 = *(OpfamilyInfo *const *) p2;
316 : :
317 : : /* Sort by access method name, per pg_opfamily_am_name_nsp_index */
318 : 11450 : cmpval = accessMethodNameCompare(opfobj1->opfmethod,
319 : : opfobj2->opfmethod);
320 [ + - ]: 11450 : if (cmpval != 0)
321 : 11450 : return cmpval;
322 : : }
323 [ + + ]: 211833 : else if (obj1->objType == DO_COLLATION)
324 : : {
325 : 33211 : CollInfo *cobj1 = *(CollInfo *const *) p1;
326 : 33211 : CollInfo *cobj2 = *(CollInfo *const *) p2;
327 : :
328 : : /*
329 : : * Sort by encoding, per pg_collation_name_enc_nsp_index. Technically,
330 : : * this is not necessary, because wherever this changes dump order,
331 : : * restoring the dump fails anyway. CREATE COLLATION can't create a
332 : : * tie for this to break, because it imposes restrictions to make
333 : : * (nspname, collname) uniquely identify a collation within a given
334 : : * DatabaseEncoding. While pg_import_system_collations() can create a
335 : : * tie, pg_dump+restore fails after
336 : : * pg_import_system_collations('my_schema') does so. However, there's
337 : : * little to gain by ignoring one natural key column on the basis of
338 : : * those limitations elsewhere, so respect the full natural key like
339 : : * we do for other object types.
340 : : */
341 : 33211 : cmpval = cobj1->collencoding - cobj2->collencoding;
342 [ + - ]: 33211 : if (cmpval != 0)
343 : 33211 : return cmpval;
344 : : }
4957 tgl@sss.pgh.pa.us 345 [ + + ]: 178622 : else if (obj1->objType == DO_ATTRDEF)
346 : : {
4836 bruce@momjian.us 347 : 505 : AttrDefInfo *adobj1 = *(AttrDefInfo *const *) p1;
348 : 505 : AttrDefInfo *adobj2 = *(AttrDefInfo *const *) p2;
349 : :
350 : : /* Sort by attribute number */
4957 tgl@sss.pgh.pa.us 351 : 505 : cmpval = (adobj1->adnum - adobj2->adnum);
352 [ + - ]: 505 : if (cmpval != 0)
353 : 505 : return cmpval;
354 : : }
2133 355 [ + + ]: 178117 : else if (obj1->objType == DO_POLICY)
356 : : {
357 : 22 : PolicyInfo *pobj1 = *(PolicyInfo *const *) p1;
358 : 22 : PolicyInfo *pobj2 = *(PolicyInfo *const *) p2;
359 : :
360 : : /* Sort by table name (table namespace was considered already) */
361 : 22 : cmpval = strcmp(pobj1->poltable->dobj.name,
362 : 22 : pobj2->poltable->dobj.name);
363 [ + - ]: 22 : if (cmpval != 0)
364 : 22 : return cmpval;
365 : : }
306 366 [ + + ]: 178095 : else if (obj1->objType == DO_RULE)
367 : : {
368 : 177008 : RuleInfo *robj1 = *(RuleInfo *const *) p1;
369 : 177008 : RuleInfo *robj2 = *(RuleInfo *const *) p2;
370 : :
371 : : /* Sort by table name (table namespace was considered already) */
372 : 177008 : cmpval = strcmp(robj1->ruletable->dobj.name,
373 : 177008 : robj2->ruletable->dobj.name);
374 [ + - ]: 177008 : if (cmpval != 0)
375 : 177008 : return cmpval;
376 : : }
2133 377 [ + + ]: 1087 : else if (obj1->objType == DO_TRIGGER)
378 : : {
379 : 425 : TriggerInfo *tobj1 = *(TriggerInfo *const *) p1;
380 : 425 : TriggerInfo *tobj2 = *(TriggerInfo *const *) p2;
381 : :
382 : : /* Sort by table name (table namespace was considered already) */
383 : 425 : cmpval = strcmp(tobj1->tgtable->dobj.name,
384 : 425 : tobj2->tgtable->dobj.name);
385 [ + - ]: 425 : if (cmpval != 0)
386 : 425 : return cmpval;
387 : : }
37 noah@leadboat.com 388 [ + + ]: 662 : else if (obj1->objType == DO_CONSTRAINT)
389 : : {
390 : 385 : ConstraintInfo *robj1 = *(ConstraintInfo *const *) p1;
391 : 385 : ConstraintInfo *robj2 = *(ConstraintInfo *const *) p2;
392 : :
393 : : /*
394 : : * Sort domain constraints before table constraints, for consistency
395 : : * with our decision to sort CREATE DOMAIN before CREATE TABLE.
396 : : */
397 [ + + ]: 385 : if (robj1->condomain)
398 : : {
399 [ - + ]: 33 : if (robj2->condomain)
400 : : {
401 : : /* Sort by domain name (domain namespace was considered) */
37 noah@leadboat.com 402 :UBC 0 : cmpval = strcmp(robj1->condomain->dobj.name,
403 : 0 : robj2->condomain->dobj.name);
404 [ # # ]: 0 : if (cmpval != 0)
405 : 0 : return cmpval;
406 : : }
407 : : else
37 noah@leadboat.com 408 :CBC 33 : return PRIO_TYPE - PRIO_TABLE;
409 : : }
410 [ + + ]: 352 : else if (robj2->condomain)
411 : 35 : return PRIO_TABLE - PRIO_TYPE;
412 : : else
413 : : {
414 : : /* Sort by table name (table namespace was considered already) */
415 : 317 : cmpval = strcmp(robj1->contable->dobj.name,
416 : 317 : robj2->contable->dobj.name);
417 [ + - ]: 317 : if (cmpval != 0)
418 : 317 : return cmpval;
419 : : }
420 : : }
15 421 [ + + ]: 277 : else if (obj1->objType == DO_DEFAULT_ACL)
422 : : {
423 : 8 : DefaultACLInfo *daclobj1 = *(DefaultACLInfo *const *) p1;
424 : 8 : DefaultACLInfo *daclobj2 = *(DefaultACLInfo *const *) p2;
425 : :
426 : : /*
427 : : * Sort by defaclrole, per pg_default_acl_role_nsp_obj_index. The
428 : : * (namespace, name) match (defaclnamespace, defaclobjtype).
429 : : */
430 : 8 : cmpval = strcmp(daclobj1->defaclrole, daclobj2->defaclrole);
431 [ + - ]: 8 : if (cmpval != 0)
432 : 8 : return cmpval;
433 : : }
37 434 [ + + ]: 269 : else if (obj1->objType == DO_PUBLICATION_REL)
435 : : {
436 : 235 : PublicationRelInfo *probj1 = *(PublicationRelInfo *const *) p1;
437 : 235 : PublicationRelInfo *probj2 = *(PublicationRelInfo *const *) p2;
438 : :
439 : : /* Sort by publication name, since (namespace, name) match the rel */
440 : 235 : cmpval = strcmp(probj1->publication->dobj.name,
441 : 235 : probj2->publication->dobj.name);
442 [ + - ]: 235 : if (cmpval != 0)
443 : 235 : return cmpval;
444 : : }
445 [ + - ]: 34 : else if (obj1->objType == DO_PUBLICATION_TABLE_IN_SCHEMA)
446 : : {
447 : 34 : PublicationSchemaInfo *psobj1 = *(PublicationSchemaInfo *const *) p1;
448 : 34 : PublicationSchemaInfo *psobj2 = *(PublicationSchemaInfo *const *) p2;
449 : :
450 : : /* Sort by publication name, since ->name is just nspname */
451 : 34 : cmpval = strcmp(psobj1->publication->dobj.name,
452 : 34 : psobj2->publication->dobj.name);
453 [ + - ]: 34 : if (cmpval != 0)
454 : 34 : return cmpval;
455 : : }
456 : :
457 : : /*
458 : : * Shouldn't get here except after catalog corruption, but if we do, sort
459 : : * by OID. This may make logically-identical databases differ in the
460 : : * order of objects in dump output. Users will get spurious schema diffs.
461 : : * Expect flaky failures of 002_pg_upgrade.pl test 'dump outputs from
462 : : * original and restored regression databases match' if the regression
463 : : * database contains objects allowing that test to reach here. That's a
464 : : * consequence of the test using "pg_restore -j", which doesn't fully
465 : : * constrain OID assignment order.
466 : : */
37 noah@leadboat.com 467 :UNC 0 : Assert(false);
7857 tgl@sss.pgh.pa.us 468 [ # # ]:EUB : return oidcmp(obj1->catId.oid, obj2->catId.oid);
469 : : }
470 : :
471 : : /* Compare two OID-identified pg_type values by nspname, then by typname. */
472 : : static int
37 noah@leadboat.com 473 :CBC 737703 : pgTypeNameCompare(Oid typid1, Oid typid2)
474 : : {
475 : : TypeInfo *typobj1;
476 : : TypeInfo *typobj2;
477 : : int cmpval;
478 : :
479 [ + + ]: 737703 : if (typid1 == typid2)
480 : 78248 : return 0;
481 : :
482 : 659455 : typobj1 = findTypeByOid(typid1);
483 : 659455 : typobj2 = findTypeByOid(typid2);
484 : :
485 [ + - - + ]: 659455 : if (!typobj1 || !typobj2)
486 : : {
487 : : /*
488 : : * getTypes() didn't find some OID. Assume catalog corruption, e.g.
489 : : * an oprright value without the corresponding OID in a pg_type row.
490 : : * Report as "equal", so the caller uses the next available basis for
491 : : * comparison, e.g. the next function argument.
492 : : *
493 : : * Unary operators have InvalidOid in oprleft (if oprkind='r') or in
494 : : * oprright (if oprkind='l'). Caller already sorted by oprkind,
495 : : * calling us only for like-kind operators. Hence, "typid1 == typid2"
496 : : * took care of InvalidOid. (v14 removed postfix operator support.
497 : : * Hence, when dumping from v14+, only oprleft can be InvalidOid.)
498 : : */
37 noah@leadboat.com 499 :UBC 0 : Assert(false);
500 : : return 0;
501 : : }
502 : :
37 noah@leadboat.com 503 [ + - - + ]:CBC 659455 : if (!typobj1->dobj.namespace || !typobj2->dobj.namespace)
37 noah@leadboat.com 504 :UBC 0 : Assert(false); /* catalog corruption */
505 : : else
506 : : {
37 noah@leadboat.com 507 :CBC 659455 : cmpval = strcmp(typobj1->dobj.namespace->dobj.name,
508 : 659455 : typobj2->dobj.namespace->dobj.name);
509 [ + + ]: 659455 : if (cmpval != 0)
510 : 35 : return cmpval;
511 : : }
512 : 659420 : return strcmp(typobj1->dobj.name, typobj2->dobj.name);
513 : : }
514 : :
515 : : /* Compare two OID-identified pg_am values by amname. */
516 : : static int
517 : 25611 : accessMethodNameCompare(Oid am1, Oid am2)
518 : : {
519 : : AccessMethodInfo *amobj1;
520 : : AccessMethodInfo *amobj2;
521 : :
522 [ - + ]: 25611 : if (am1 == am2)
37 noah@leadboat.com 523 :UBC 0 : return 0;
524 : :
37 noah@leadboat.com 525 :CBC 25611 : amobj1 = findAccessMethodByOid(am1);
526 : 25611 : amobj2 = findAccessMethodByOid(am2);
527 : :
528 [ + - - + ]: 25611 : if (!amobj1 || !amobj2)
529 : : {
530 : : /* catalog corruption: handle like pgTypeNameCompare() does */
37 noah@leadboat.com 531 :UBC 0 : Assert(false);
532 : : return 0;
533 : : }
534 : :
37 noah@leadboat.com 535 :CBC 25611 : return strcmp(amobj1->dobj.name, amobj2->dobj.name);
536 : : }
537 : :
538 : :
539 : : /*
540 : : * Sort the given objects into a safe dump order using dependency
541 : : * information (to the extent we have it available).
542 : : *
543 : : * The DumpIds of the PRE_DATA_BOUNDARY and POST_DATA_BOUNDARY objects are
544 : : * passed in separately, in case we need them during dependency loop repair.
545 : : */
546 : : void
4821 tgl@sss.pgh.pa.us 547 : 185 : sortDumpableObjects(DumpableObject **objs, int numObjs,
548 : : DumpId preBoundaryId, DumpId postBoundaryId)
549 : : {
550 : : DumpableObject **ordering;
551 : : int nOrdering;
552 : :
553 [ - + ]: 185 : if (numObjs <= 0) /* can't happen anymore ... */
7944 tgl@sss.pgh.pa.us 554 :UBC 0 : return;
555 : :
556 : : /*
557 : : * Saving the boundary IDs in static variables is a bit grotty, but seems
558 : : * better than adding them to parameter lists of subsidiary functions.
559 : : */
4821 tgl@sss.pgh.pa.us 560 :CBC 185 : preDataBoundId = preBoundaryId;
561 : 185 : postDataBoundId = postBoundaryId;
562 : :
5034 bruce@momjian.us 563 : 185 : ordering = (DumpableObject **) pg_malloc(numObjs * sizeof(DumpableObject *));
7945 tgl@sss.pgh.pa.us 564 [ + + ]: 560 : while (!TopoSort(objs, numObjs, ordering, &nOrdering))
7944 565 : 375 : findDependencyLoops(ordering, nOrdering, numObjs);
566 : :
7945 567 : 185 : memcpy(objs, ordering, numObjs * sizeof(DumpableObject *));
568 : :
569 : 185 : free(ordering);
570 : : }
571 : :
572 : : /*
573 : : * TopoSort -- topological sort of a dump list
574 : : *
575 : : * Generate a re-ordering of the dump list that satisfies all the dependency
576 : : * constraints shown in the dump list. (Each such constraint is a fact of a
577 : : * partial ordering.) Minimize rearrangement of the list not needed to
578 : : * achieve the partial ordering.
579 : : *
580 : : * The input is the list of numObjs objects in objs[]. This list is not
581 : : * modified.
582 : : *
583 : : * Returns true if able to build an ordering that satisfies all the
584 : : * constraints, false if not (there are contradictory constraints).
585 : : *
586 : : * On success (true result), ordering[] is filled with a sorted array of
587 : : * DumpableObject pointers, of length equal to the input list length.
588 : : *
589 : : * On failure (false result), ordering[] is filled with an unsorted array of
590 : : * DumpableObject pointers of length *nOrdering, listing the objects that
591 : : * prevented the sort from being completed. In general, these objects either
592 : : * participate directly in a dependency cycle, or are depended on by objects
593 : : * that are in a cycle. (The latter objects are not actually problematic,
594 : : * but it takes further analysis to identify which are which.)
595 : : *
596 : : * The caller is responsible for allocating sufficient space at *ordering.
597 : : */
598 : : static bool
599 : 560 : TopoSort(DumpableObject **objs,
600 : : int numObjs,
601 : : DumpableObject **ordering, /* output argument */
602 : : int *nOrdering) /* output argument */
603 : : {
604 : 560 : DumpId maxDumpId = getMaxDumpId();
605 : : binaryheap *pendingHeap;
606 : : int *beforeConstraints;
607 : : int *idMap;
608 : : DumpableObject *obj;
609 : : int i,
610 : : j,
611 : : k;
612 : :
613 : : /*
614 : : * This is basically the same algorithm shown for topological sorting in
615 : : * Knuth's Volume 1. However, we would like to minimize unnecessary
616 : : * rearrangement of the input ordering; that is, when we have a choice of
617 : : * which item to output next, we always want to take the one highest in
618 : : * the original list. Therefore, instead of maintaining an unordered
619 : : * linked list of items-ready-to-output as Knuth does, we maintain a heap
620 : : * of their item numbers, which we can use as a priority queue. This
621 : : * turns the algorithm from O(N) to O(N log N) because each insertion or
622 : : * removal of a heap item takes O(log N) time. However, that's still
623 : : * plenty fast enough for this application.
624 : : */
625 : :
7678 bruce@momjian.us 626 : 560 : *nOrdering = numObjs; /* for success return */
627 : :
628 : : /* Eliminate the null case */
7945 tgl@sss.pgh.pa.us 629 [ - + ]: 560 : if (numObjs <= 0)
7945 tgl@sss.pgh.pa.us 630 :UBC 0 : return true;
631 : :
632 : : /* Create workspace for the above-described heap */
513 msawada@postgresql.o 633 :CBC 560 : pendingHeap = binaryheap_allocate(numObjs, int_cmp, NULL);
634 : :
635 : : /*
636 : : * Scan the constraints, and for each item in the input, generate a count
637 : : * of the number of constraints that say it must be before something else.
638 : : * The count for the item with dumpId j is stored in beforeConstraints[j].
639 : : * We also make a map showing the input-order index of the item with
640 : : * dumpId j.
641 : : */
2355 michael@paquier.xyz 642 : 560 : beforeConstraints = (int *) pg_malloc0((maxDumpId + 1) * sizeof(int));
5034 bruce@momjian.us 643 : 560 : idMap = (int *) pg_malloc((maxDumpId + 1) * sizeof(int));
7945 tgl@sss.pgh.pa.us 644 [ + + ]: 2612771 : for (i = 0; i < numObjs; i++)
645 : : {
646 : 2612211 : obj = objs[i];
647 : 2612211 : j = obj->dumpId;
648 [ + - - + ]: 2612211 : if (j <= 0 || j > maxDumpId)
1247 tgl@sss.pgh.pa.us 649 :UBC 0 : pg_fatal("invalid dumpId %d", j);
7945 tgl@sss.pgh.pa.us 650 :CBC 2612211 : idMap[j] = i;
651 [ + + ]: 6548475 : for (j = 0; j < obj->nDeps; j++)
652 : : {
653 : 3936264 : k = obj->dependencies[j];
654 [ + - - + ]: 3936264 : if (k <= 0 || k > maxDumpId)
1247 tgl@sss.pgh.pa.us 655 :UBC 0 : pg_fatal("invalid dependency %d", k);
7945 tgl@sss.pgh.pa.us 656 :CBC 3936264 : beforeConstraints[k]++;
657 : : }
658 : : }
659 : :
660 : : /*
661 : : * Now initialize the heap of items-ready-to-output by filling it with the
662 : : * indexes of items that already have beforeConstraints[id] == 0.
663 : : *
664 : : * We enter the indexes into pendingHeap in decreasing order so that the
665 : : * heap invariant is satisfied at the completion of this loop. This
666 : : * reduces the amount of work that binaryheap_build() must do.
667 : : */
7678 bruce@momjian.us 668 [ + + ]: 2612771 : for (i = numObjs; --i >= 0;)
669 : : {
7945 tgl@sss.pgh.pa.us 670 [ + + ]: 2612211 : if (beforeConstraints[objs[i]->dumpId] == 0)
718 nathan@postgresql.or 671 : 33134 : binaryheap_add_unordered(pendingHeap, (void *) (intptr_t) i);
672 : : }
673 : 560 : binaryheap_build(pendingHeap);
674 : :
675 : : /*--------------------
676 : : * Now emit objects, working backwards in the output list. At each step,
677 : : * we use the priority heap to select the last item that has no remaining
678 : : * before-constraints. We remove that item from the heap, output it to
679 : : * ordering[], and decrease the beforeConstraints count of each of the
680 : : * items it was constrained against. Whenever an item's beforeConstraints
681 : : * count is thereby decreased to zero, we insert it into the priority heap
682 : : * to show that it is a candidate to output. We are done when the heap
683 : : * becomes empty; if we have output every element then we succeeded,
684 : : * otherwise we failed.
685 : : * i = number of ordering[] entries left to output
686 : : * j = objs[] index of item we are outputting
687 : : * k = temp for scanning constraint list for item j
688 : : *--------------------
689 : : */
7945 tgl@sss.pgh.pa.us 690 : 560 : i = numObjs;
718 nathan@postgresql.or 691 [ + + ]: 1638112 : while (!binaryheap_empty(pendingHeap))
692 : : {
693 : : /* Select object to output by removing largest heap member */
694 : 1637552 : j = (int) (intptr_t) binaryheap_remove_first(pendingHeap);
7945 tgl@sss.pgh.pa.us 695 : 1637552 : obj = objs[j];
696 : : /* Output candidate to ordering[] */
697 : 1637552 : ordering[--i] = obj;
698 : : /* Update beforeConstraints counts of its predecessors */
699 [ + + ]: 3967052 : for (k = 0; k < obj->nDeps; k++)
700 : : {
7678 bruce@momjian.us 701 : 2329500 : int id = obj->dependencies[k];
702 : :
7945 tgl@sss.pgh.pa.us 703 [ + + ]: 2329500 : if ((--beforeConstraints[id]) == 0)
718 nathan@postgresql.or 704 : 1604418 : binaryheap_add(pendingHeap, (void *) (intptr_t) idMap[id]);
705 : : }
706 : : }
707 : :
708 : : /*
709 : : * If we failed, report the objects that couldn't be output; these are the
710 : : * ones with beforeConstraints[] still nonzero.
711 : : */
7945 tgl@sss.pgh.pa.us 712 [ + + ]: 560 : if (i != 0)
713 : : {
7944 714 : 375 : k = 0;
7945 715 [ + + ]: 1793391 : for (j = 1; j <= maxDumpId; j++)
716 : : {
717 [ + + ]: 1793016 : if (beforeConstraints[j] != 0)
7944 718 : 974659 : ordering[k++] = objs[idMap[j]];
719 : : }
720 : 375 : *nOrdering = k;
721 : : }
722 : :
723 : : /* Done */
718 nathan@postgresql.or 724 : 560 : binaryheap_free(pendingHeap);
7945 tgl@sss.pgh.pa.us 725 : 560 : free(beforeConstraints);
726 : 560 : free(idMap);
727 : :
728 : 560 : return (i == 0);
729 : : }
730 : :
731 : : /*
732 : : * findDependencyLoops - identify loops in TopoSort's failure output,
733 : : * and pass each such loop to repairDependencyLoop() for action
734 : : *
735 : : * In general there may be many loops in the set of objects returned by
736 : : * TopoSort; for speed we should try to repair as many loops as we can
737 : : * before trying TopoSort again. We can safely repair loops that are
738 : : * disjoint (have no members in common); if we find overlapping loops
739 : : * then we repair only the first one found, because the action taken to
740 : : * repair the first might have repaired the other as well. (If not,
741 : : * we'll fix it on the next go-round.)
742 : : *
743 : : * objs[] lists the objects TopoSort couldn't sort
744 : : * nObjs is the number of such objects
745 : : * totObjs is the total number of objects in the universe
746 : : */
747 : : static void
7944 748 : 375 : findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs)
749 : : {
750 : : /*
751 : : * We use three data structures here:
752 : : *
753 : : * processed[] is a bool array indexed by dump ID, marking the objects
754 : : * already processed during this invocation of findDependencyLoops().
755 : : *
756 : : * searchFailed[] is another array indexed by dump ID. searchFailed[j] is
757 : : * set to dump ID k if we have proven that there is no dependency path
758 : : * leading from object j back to start point k. This allows us to skip
759 : : * useless searching when there are multiple dependency paths from k to j,
760 : : * which is a common situation. We could use a simple bool array for
761 : : * this, but then we'd need to re-zero it for each start point, resulting
762 : : * in O(N^2) zeroing work. Using the start point's dump ID as the "true"
763 : : * value lets us skip clearing the array before we consider the next start
764 : : * point.
765 : : *
766 : : * workspace[] is an array of DumpableObject pointers, in which we try to
767 : : * build lists of objects constituting loops. We make workspace[] large
768 : : * enough to hold all the objects in TopoSort's output, which is huge
769 : : * overkill in most cases but could theoretically be necessary if there is
770 : : * a single dependency chain linking all the objects.
771 : : */
772 : : bool *processed;
773 : : DumpId *searchFailed;
774 : : DumpableObject **workspace;
775 : : bool fixedloop;
776 : : int i;
777 : :
4722 778 : 375 : processed = (bool *) pg_malloc0((getMaxDumpId() + 1) * sizeof(bool));
4061 779 : 375 : searchFailed = (DumpId *) pg_malloc0((getMaxDumpId() + 1) * sizeof(DumpId));
5034 bruce@momjian.us 780 : 375 : workspace = (DumpableObject **) pg_malloc(totObjs * sizeof(DumpableObject *));
7944 tgl@sss.pgh.pa.us 781 : 375 : fixedloop = false;
782 : :
783 [ + + ]: 975034 : for (i = 0; i < nObjs; i++)
784 : : {
785 : 974659 : DumpableObject *obj = objs[i];
786 : : int looplen;
787 : : int j;
788 : :
4061 789 : 974659 : looplen = findLoop(obj,
790 : : obj->dumpId,
791 : : processed,
792 : : searchFailed,
793 : : workspace,
794 : : 0);
795 : :
4907 796 [ + + ]: 974659 : if (looplen > 0)
797 : : {
798 : : /* Found a loop, repair it */
799 : 31208 : repairDependencyLoop(workspace, looplen);
7944 800 : 31208 : fixedloop = true;
801 : : /* Mark loop members as processed */
4907 802 [ + + ]: 93818 : for (j = 0; j < looplen; j++)
803 : 62610 : processed[workspace[j]->dumpId] = true;
804 : : }
805 : : else
806 : : {
807 : : /*
808 : : * There's no loop starting at this object, but mark it processed
809 : : * anyway. This is not necessary for correctness, but saves later
810 : : * invocations of findLoop() from uselessly chasing references to
811 : : * such an object.
812 : : */
813 : 943451 : processed[obj->dumpId] = true;
814 : : }
815 : : }
816 : :
817 : : /* We'd better have fixed at least one loop */
7944 818 [ - + ]: 375 : if (!fixedloop)
1247 tgl@sss.pgh.pa.us 819 :UBC 0 : pg_fatal("could not identify dependency loop");
820 : :
7944 tgl@sss.pgh.pa.us 821 :CBC 375 : free(workspace);
4061 822 : 375 : free(searchFailed);
4907 823 : 375 : free(processed);
7944 824 : 375 : }
825 : :
826 : : /*
827 : : * Recursively search for a circular dependency loop that doesn't include
828 : : * any already-processed objects.
829 : : *
830 : : * obj: object we are examining now
831 : : * startPoint: dumpId of starting object for the hoped-for circular loop
832 : : * processed[]: flag array marking already-processed objects
833 : : * searchFailed[]: flag array marking already-unsuccessfully-visited objects
834 : : * workspace[]: work array in which we are building list of loop members
835 : : * depth: number of valid entries in workspace[] at call
836 : : *
837 : : * On success, the length of the loop is returned, and workspace[] is filled
838 : : * with pointers to the members of the loop. On failure, we return 0.
839 : : *
840 : : * Note: it is possible that the given starting object is a member of more
841 : : * than one cycle; if so, we will find an arbitrary one of the cycles.
842 : : */
843 : : static int
7945 844 : 25009329 : findLoop(DumpableObject *obj,
845 : : DumpId startPoint,
846 : : bool *processed,
847 : : DumpId *searchFailed,
848 : : DumpableObject **workspace,
849 : : int depth)
850 : : {
851 : : int i;
852 : :
853 : : /*
854 : : * Reject if obj is already processed. This test prevents us from finding
855 : : * loops that overlap previously-processed loops.
856 : : */
4907 857 [ + + ]: 25009329 : if (processed[obj->dumpId])
858 : 23084963 : return 0;
859 : :
860 : : /*
861 : : * If we've already proven there is no path from this object back to the
862 : : * startPoint, forget it.
863 : : */
4061 864 [ + + ]: 1924366 : if (searchFailed[obj->dumpId] == startPoint)
865 : 177910 : return 0;
866 : :
867 : : /*
868 : : * Reject if obj is already present in workspace. This test prevents us
869 : : * from going into infinite recursion if we are given a startPoint object
870 : : * that links to a cycle it's not a member of, and it guarantees that we
871 : : * can't overflow the allocated size of workspace[].
872 : : */
7944 873 [ + + ]: 3332518 : for (i = 0; i < depth; i++)
874 : : {
875 [ + + ]: 1588980 : if (workspace[i] == obj)
4907 876 : 2918 : return 0;
877 : : }
878 : :
879 : : /*
880 : : * Okay, tentatively add obj to workspace
881 : : */
7944 882 : 1743538 : workspace[depth++] = obj;
883 : :
884 : : /*
885 : : * See if we've found a loop back to the desired startPoint; if so, done
886 : : */
887 [ + + ]: 26566531 : for (i = 0; i < obj->nDeps; i++)
888 : : {
889 [ + + ]: 24854201 : if (obj->dependencies[i] == startPoint)
4907 890 : 31208 : return depth;
891 : : }
892 : :
893 : : /*
894 : : * Recurse down each outgoing branch
895 : : */
7944 896 [ + + ]: 25715598 : for (i = 0; i < obj->nDeps; i++)
897 : : {
898 : 24034670 : DumpableObject *nextobj = findObjectByDumpId(obj->dependencies[i]);
899 : : int newDepth;
900 : :
7945 901 [ - + ]: 24034670 : if (!nextobj)
7945 tgl@sss.pgh.pa.us 902 :UBC 0 : continue; /* ignore dependencies on undumped objects */
4907 tgl@sss.pgh.pa.us 903 :CBC 24034670 : newDepth = findLoop(nextobj,
904 : : startPoint,
905 : : processed,
906 : : searchFailed,
907 : : workspace,
908 : : depth);
909 [ + + ]: 24034670 : if (newDepth > 0)
910 : 31402 : return newDepth;
911 : : }
912 : :
913 : : /*
914 : : * Remember there is no path from here back to startPoint
915 : : */
4061 916 : 1680928 : searchFailed[obj->dumpId] = startPoint;
917 : :
4907 918 : 1680928 : return 0;
919 : : }
920 : :
921 : : /*
922 : : * A user-defined datatype will have a dependency loop with each of its
923 : : * I/O functions (since those have the datatype as input or output).
924 : : * Similarly, a range type will have a loop with its canonicalize function,
925 : : * if any. Break the loop by making the function depend on the associated
926 : : * shell type, instead.
927 : : */
928 : : static void
7945 929 : 200 : repairTypeFuncLoop(DumpableObject *typeobj, DumpableObject *funcobj)
930 : : {
931 : 200 : TypeInfo *typeInfo = (TypeInfo *) typeobj;
932 : :
933 : : /* remove function's dependency on type */
934 : 200 : removeObjectDependency(funcobj, typeobj->dumpId);
935 : :
936 : : /* add function's dependency on shell type, instead */
7128 937 [ + + ]: 200 : if (typeInfo->shellType)
938 : : {
939 : 158 : addObjectDependency(funcobj, typeInfo->shellType->dobj.dumpId);
940 : :
941 : : /*
942 : : * Mark shell type (always including the definition, as we need the
943 : : * shell type defined to identify the function fully) as to be dumped
944 : : * if any such function is
945 : : */
946 [ + - ]: 158 : if (funcobj->dump)
3438 sfrost@snowman.net 947 : 158 : typeInfo->shellType->dobj.dump = funcobj->dump |
948 : : DUMP_COMPONENT_DEFINITION;
949 : : }
7945 tgl@sss.pgh.pa.us 950 : 200 : }
951 : :
952 : : /*
953 : : * Because we force a view to depend on its ON SELECT rule, while there
954 : : * will be an implicit dependency in the other direction, we need to break
955 : : * the loop. If there are no other objects in the loop then we can remove
956 : : * the implicit dependency and leave the ON SELECT rule non-separate.
957 : : * This applies to matviews, as well.
958 : : */
959 : : static void
960 : 27983 : repairViewRuleLoop(DumpableObject *viewobj,
961 : : DumpableObject *ruleobj)
962 : : {
963 : : /* remove rule's dependency on view */
964 : 27983 : removeObjectDependency(ruleobj, viewobj->dumpId);
965 : : /* flags on the two objects are already set correctly for this case */
966 : 27983 : }
967 : :
968 : : /*
969 : : * However, if there are other objects in the loop, we must break the loop
970 : : * by making the ON SELECT rule a separately-dumped object.
971 : : *
972 : : * Because findLoop() finds shorter cycles before longer ones, it's likely
973 : : * that we will have previously fired repairViewRuleLoop() and removed the
974 : : * rule's dependency on the view. Put it back to ensure the rule won't be
975 : : * emitted before the view.
976 : : *
977 : : * Note: this approach does *not* work for matviews, at the moment.
978 : : */
979 : : static void
7571 980 : 10 : repairViewRuleMultiLoop(DumpableObject *viewobj,
981 : : DumpableObject *ruleobj)
982 : : {
4764 983 : 10 : TableInfo *viewinfo = (TableInfo *) viewobj;
984 : 10 : RuleInfo *ruleinfo = (RuleInfo *) ruleobj;
985 : :
986 : : /* remove view's dependency on rule */
7571 987 : 10 : removeObjectDependency(viewobj, ruleobj->dumpId);
988 : : /* mark view to be printed with a dummy definition */
3215 989 : 10 : viewinfo->dummy_view = true;
990 : : /* mark rule as needing its own dump */
4764 991 : 10 : ruleinfo->separate = true;
992 : : /* put back rule's dependency on view */
7571 993 : 10 : addObjectDependency(ruleobj, viewobj->dumpId);
994 : : /* now that rule is separate, it must be post-data */
4821 995 : 10 : addObjectDependency(ruleobj, postDataBoundId);
7571 996 : 10 : }
997 : :
998 : : /*
999 : : * If a matview is involved in a multi-object loop, we can't currently fix
1000 : : * that by splitting off the rule. As a stopgap, we try to fix it by
1001 : : * dropping the constraint that the matview be dumped in the pre-data section.
1002 : : * This is sufficient to handle cases where a matview depends on some unique
1003 : : * index, as can happen if it has a GROUP BY for example.
1004 : : *
1005 : : * Note that the "next object" is not necessarily the matview itself;
1006 : : * it could be the matview's rowtype, for example. We may come through here
1007 : : * several times while removing all the pre-data linkages. In particular,
1008 : : * if there are other matviews that depend on the one with the circularity
1009 : : * problem, we'll come through here for each such matview and mark them all
1010 : : * as postponed. (This works because all MVs have pre-data dependencies
1011 : : * to begin with, so each of them will get visited.)
1012 : : */
1013 : : static void
2406 1014 : 135 : repairMatViewBoundaryMultiLoop(DumpableObject *boundaryobj,
1015 : : DumpableObject *nextobj)
1016 : : {
1017 : : /* remove boundary's dependency on object after it in loop */
4179 1018 : 135 : removeObjectDependency(boundaryobj, nextobj->dumpId);
1019 : :
1020 : : /*
1021 : : * If that object is a matview or matview stats, mark it as postponed into
1022 : : * post-data.
1023 : : */
2406 1024 [ + + ]: 135 : if (nextobj->objType == DO_TABLE)
1025 : : {
1026 : 44 : TableInfo *nextinfo = (TableInfo *) nextobj;
1027 : :
198 jdavis@postgresql.or 1028 [ + - ]: 44 : if (nextinfo->relkind == RELKIND_MATVIEW)
1029 : 44 : nextinfo->postponed_def = true;
1030 : : }
1031 [ + + ]: 91 : else if (nextobj->objType == DO_REL_STATS)
1032 : : {
1033 : 3 : RelStatsInfo *nextinfo = (RelStatsInfo *) nextobj;
1034 : :
2406 tgl@sss.pgh.pa.us 1035 [ + - ]: 3 : if (nextinfo->relkind == RELKIND_MATVIEW)
162 jdavis@postgresql.or 1036 : 3 : nextinfo->section = SECTION_POST_DATA;
1037 : : }
4179 tgl@sss.pgh.pa.us 1038 : 135 : }
1039 : :
1040 : : /*
1041 : : * If a function is involved in a multi-object loop, we can't currently fix
1042 : : * that by splitting it into two DumpableObjects. As a stopgap, we try to fix
1043 : : * it by dropping the constraint that the function be dumped in the pre-data
1044 : : * section. This is sufficient to handle cases where a function depends on
1045 : : * some unique index, as can happen if it has a GROUP BY for example.
1046 : : */
1047 : : static void
825 1048 : 44 : repairFunctionBoundaryMultiLoop(DumpableObject *boundaryobj,
1049 : : DumpableObject *nextobj)
1050 : : {
1051 : : /* remove boundary's dependency on object after it in loop */
1052 : 44 : removeObjectDependency(boundaryobj, nextobj->dumpId);
1053 : : /* if that object is a function, mark it as postponed into post-data */
1054 [ + - ]: 44 : if (nextobj->objType == DO_FUNC)
1055 : : {
1056 : 44 : FuncInfo *nextinfo = (FuncInfo *) nextobj;
1057 : :
1058 : 44 : nextinfo->postponed_def = true;
1059 : : }
1060 : 44 : }
1061 : :
1062 : : /*
1063 : : * Because we make tables depend on their CHECK constraints, while there
1064 : : * will be an automatic dependency in the other direction, we need to break
1065 : : * the loop. If there are no other objects in the loop then we can remove
1066 : : * the automatic dependency and leave the CHECK constraint non-separate.
1067 : : */
1068 : : static void
7945 1069 : 617 : repairTableConstraintLoop(DumpableObject *tableobj,
1070 : : DumpableObject *constraintobj)
1071 : : {
1072 : : /* remove constraint's dependency on table */
1073 : 617 : removeObjectDependency(constraintobj, tableobj->dumpId);
1074 : 617 : }
1075 : :
1076 : : /*
1077 : : * However, if there are other objects in the loop, we must break the loop
1078 : : * by making the CHECK constraint a separately-dumped object.
1079 : : *
1080 : : * Because findLoop() finds shorter cycles before longer ones, it's likely
1081 : : * that we will have previously fired repairTableConstraintLoop() and
1082 : : * removed the constraint's dependency on the table. Put it back to ensure
1083 : : * the constraint won't be emitted before the table...
1084 : : */
1085 : : static void
1086 : 5 : repairTableConstraintMultiLoop(DumpableObject *tableobj,
1087 : : DumpableObject *constraintobj)
1088 : : {
1089 : : /* remove table's dependency on constraint */
1090 : 5 : removeObjectDependency(tableobj, constraintobj->dumpId);
1091 : : /* mark constraint as needing its own dump */
1092 : 5 : ((ConstraintInfo *) constraintobj)->separate = true;
1093 : : /* put back constraint's dependency on table */
1094 : 5 : addObjectDependency(constraintobj, tableobj->dumpId);
1095 : : /* now that constraint is separate, it must be post-data */
4821 1096 : 5 : addObjectDependency(constraintobj, postDataBoundId);
7945 1097 : 5 : }
1098 : :
1099 : : /*
1100 : : * Attribute defaults behave exactly the same as CHECK constraints...
1101 : : */
1102 : : static void
1103 : 1060 : repairTableAttrDefLoop(DumpableObject *tableobj,
1104 : : DumpableObject *attrdefobj)
1105 : : {
1106 : : /* remove attrdef's dependency on table */
1107 : 1060 : removeObjectDependency(attrdefobj, tableobj->dumpId);
1108 : 1060 : }
1109 : :
1110 : : static void
1111 : 164 : repairTableAttrDefMultiLoop(DumpableObject *tableobj,
1112 : : DumpableObject *attrdefobj)
1113 : : {
1114 : : /* remove table's dependency on attrdef */
1115 : 164 : removeObjectDependency(tableobj, attrdefobj->dumpId);
1116 : : /* mark attrdef as needing its own dump */
1117 : 164 : ((AttrDefInfo *) attrdefobj)->separate = true;
1118 : : /* put back attrdef's dependency on table */
1119 : 164 : addObjectDependency(attrdefobj, tableobj->dumpId);
1120 : 164 : }
1121 : :
1122 : : /*
1123 : : * CHECK, NOT NULL constraints on domains work just like those on tables ...
1124 : : */
1125 : : static void
1126 : 173 : repairDomainConstraintLoop(DumpableObject *domainobj,
1127 : : DumpableObject *constraintobj)
1128 : : {
1129 : : /* remove constraint's dependency on domain */
1130 : 173 : removeObjectDependency(constraintobj, domainobj->dumpId);
1131 : 173 : }
1132 : :
1133 : : static void
7945 tgl@sss.pgh.pa.us 1134 :UBC 0 : repairDomainConstraintMultiLoop(DumpableObject *domainobj,
1135 : : DumpableObject *constraintobj)
1136 : : {
1137 : : /* remove domain's dependency on constraint */
1138 : 0 : removeObjectDependency(domainobj, constraintobj->dumpId);
1139 : : /* mark constraint as needing its own dump */
1140 : 0 : ((ConstraintInfo *) constraintobj)->separate = true;
1141 : : /* put back constraint's dependency on domain */
1142 : 0 : addObjectDependency(constraintobj, domainobj->dumpId);
1143 : : /* now that constraint is separate, it must be post-data */
4821 1144 : 0 : addObjectDependency(constraintobj, postDataBoundId);
7945 1145 : 0 : }
1146 : :
1147 : : static void
2787 alvherre@alvh.no-ip. 1148 : 0 : repairIndexLoop(DumpableObject *partedindex,
1149 : : DumpableObject *partindex)
1150 : : {
1151 : 0 : removeObjectDependency(partedindex, partindex->dumpId);
1152 : 0 : }
1153 : :
1154 : : /*
1155 : : * Fix a dependency loop, or die trying ...
1156 : : *
1157 : : * This routine is mainly concerned with reducing the multiple ways that
1158 : : * a loop might appear to common cases, which it passes off to the
1159 : : * "fixer" routines above.
1160 : : */
1161 : : static void
7945 tgl@sss.pgh.pa.us 1162 :CBC 31208 : repairDependencyLoop(DumpableObject **loop,
1163 : : int nLoop)
1164 : : {
1165 : : int i,
1166 : : j;
1167 : :
1168 : : /* Datatype and one of its I/O or canonicalize functions */
1169 [ + + ]: 31208 : if (nLoop == 2 &&
1170 [ + + ]: 30033 : loop[0]->objType == DO_TYPE &&
1171 [ - + ]: 173 : loop[1]->objType == DO_FUNC)
1172 : : {
7945 tgl@sss.pgh.pa.us 1173 :UBC 0 : repairTypeFuncLoop(loop[0], loop[1]);
1174 : 0 : return;
1175 : : }
7945 tgl@sss.pgh.pa.us 1176 [ + + ]:CBC 31208 : if (nLoop == 2 &&
1177 [ + + ]: 30033 : loop[1]->objType == DO_TYPE &&
1178 [ + - ]: 200 : loop[0]->objType == DO_FUNC)
1179 : : {
1180 : 200 : repairTypeFuncLoop(loop[1], loop[0]);
1181 : 200 : return;
1182 : : }
1183 : :
1184 : : /* View (including matview) and its ON SELECT rule */
1185 [ + + ]: 31008 : if (nLoop == 2 &&
1186 [ + + ]: 29833 : loop[0]->objType == DO_TABLE &&
1187 [ + + ]: 29660 : loop[1]->objType == DO_RULE &&
3103 1188 [ + + ]: 27983 : (((TableInfo *) loop[0])->relkind == RELKIND_VIEW ||
1189 [ + - ]: 566 : ((TableInfo *) loop[0])->relkind == RELKIND_MATVIEW) &&
7945 1190 [ + - ]: 27983 : ((RuleInfo *) loop[1])->ev_type == '1' &&
7571 1191 [ + - ]: 27983 : ((RuleInfo *) loop[1])->is_instead &&
1192 [ + - ]: 27983 : ((RuleInfo *) loop[1])->ruletable == (TableInfo *) loop[0])
1193 : : {
7945 1194 : 27983 : repairViewRuleLoop(loop[0], loop[1]);
1195 : 27983 : return;
1196 : : }
1197 [ + + ]: 3025 : if (nLoop == 2 &&
1198 [ - + ]: 1850 : loop[1]->objType == DO_TABLE &&
7945 tgl@sss.pgh.pa.us 1199 [ # # ]:UBC 0 : loop[0]->objType == DO_RULE &&
3103 1200 [ # # ]: 0 : (((TableInfo *) loop[1])->relkind == RELKIND_VIEW ||
1201 [ # # ]: 0 : ((TableInfo *) loop[1])->relkind == RELKIND_MATVIEW) &&
7945 1202 [ # # ]: 0 : ((RuleInfo *) loop[0])->ev_type == '1' &&
7571 1203 [ # # ]: 0 : ((RuleInfo *) loop[0])->is_instead &&
1204 [ # # ]: 0 : ((RuleInfo *) loop[0])->ruletable == (TableInfo *) loop[1])
1205 : : {
7945 1206 : 0 : repairViewRuleLoop(loop[1], loop[0]);
1207 : 0 : return;
1208 : : }
1209 : :
1210 : : /* Indirect loop involving view (but not matview) and ON SELECT rule */
7571 tgl@sss.pgh.pa.us 1211 [ + + ]:CBC 3025 : if (nLoop > 2)
1212 : : {
1213 [ + + ]: 2029 : for (i = 0; i < nLoop; i++)
1214 : : {
4179 1215 [ + + ]: 1681 : if (loop[i]->objType == DO_TABLE &&
3103 1216 [ + + ]: 478 : ((TableInfo *) loop[i])->relkind == RELKIND_VIEW)
1217 : : {
7571 1218 [ + - ]: 24 : for (j = 0; j < nLoop; j++)
1219 : : {
1220 [ + + ]: 24 : if (loop[j]->objType == DO_RULE &&
1221 [ + - ]: 10 : ((RuleInfo *) loop[j])->ev_type == '1' &&
1222 [ + - ]: 10 : ((RuleInfo *) loop[j])->is_instead &&
1223 [ + - ]: 10 : ((RuleInfo *) loop[j])->ruletable == (TableInfo *) loop[i])
1224 : : {
1225 : 10 : repairViewRuleMultiLoop(loop[i], loop[j]);
1226 : 10 : return;
1227 : : }
1228 : : }
1229 : : }
1230 : : }
1231 : : }
1232 : :
1233 : : /* Indirect loop involving matview and data boundary */
4179 1234 [ + + ]: 3015 : if (nLoop > 2)
1235 : : {
1236 [ + + ]: 1433 : for (i = 0; i < nLoop; i++)
1237 : : {
1238 [ + + ]: 1220 : if (loop[i]->objType == DO_TABLE &&
3103 1239 [ + + ]: 468 : ((TableInfo *) loop[i])->relkind == RELKIND_MATVIEW)
1240 : : {
4179 1241 [ + + ]: 351 : for (j = 0; j < nLoop; j++)
1242 : : {
1243 [ + + ]: 348 : if (loop[j]->objType == DO_PRE_DATA_BOUNDARY)
1244 : : {
1245 : : DumpableObject *nextobj;
1246 : :
198 jdavis@postgresql.or 1247 [ + + ]: 132 : nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
1248 : 132 : repairMatViewBoundaryMultiLoop(loop[j], nextobj);
1249 : 132 : return;
1250 : : }
1251 : : }
1252 : : }
1253 [ + + ]: 1085 : else if (loop[i]->objType == DO_REL_STATS &&
1254 [ + + ]: 155 : ((RelStatsInfo *) loop[i])->relkind == RELKIND_MATVIEW)
1255 : : {
1256 [ + - ]: 12 : for (j = 0; j < nLoop; j++)
1257 : : {
1258 [ + + ]: 12 : if (loop[j]->objType == DO_POST_DATA_BOUNDARY)
1259 : : {
1260 : : DumpableObject *nextobj;
1261 : :
4179 tgl@sss.pgh.pa.us 1262 [ + - ]: 3 : nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
2406 1263 : 3 : repairMatViewBoundaryMultiLoop(loop[j], nextobj);
4179 1264 : 3 : return;
1265 : : }
1266 : : }
1267 : : }
1268 : : }
1269 : : }
1270 : :
1271 : : /* Indirect loop involving function and data boundary */
825 1272 [ + + ]: 2880 : if (nLoop > 2)
1273 : : {
1274 [ + + ]: 801 : for (i = 0; i < nLoop; i++)
1275 : : {
1276 [ + + ]: 632 : if (loop[i]->objType == DO_FUNC)
1277 : : {
1278 [ + + ]: 130 : for (j = 0; j < nLoop; j++)
1279 : : {
1280 [ + + ]: 125 : if (loop[j]->objType == DO_PRE_DATA_BOUNDARY)
1281 : : {
1282 : : DumpableObject *nextobj;
1283 : :
1284 [ + + ]: 44 : nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
1285 : 44 : repairFunctionBoundaryMultiLoop(loop[j], nextobj);
1286 : 44 : return;
1287 : : }
1288 : : }
1289 : : }
1290 : : }
1291 : : }
1292 : :
1293 : : /* Table and CHECK constraint */
7945 1294 [ + + ]: 2836 : if (nLoop == 2 &&
1295 [ + + ]: 1850 : loop[0]->objType == DO_TABLE &&
1296 [ + + ]: 1677 : loop[1]->objType == DO_CONSTRAINT &&
1297 [ + - ]: 617 : ((ConstraintInfo *) loop[1])->contype == 'c' &&
1298 [ + - ]: 617 : ((ConstraintInfo *) loop[1])->contable == (TableInfo *) loop[0])
1299 : : {
1300 : 617 : repairTableConstraintLoop(loop[0], loop[1]);
1301 : 617 : return;
1302 : : }
1303 [ + + ]: 2219 : if (nLoop == 2 &&
1304 [ - + ]: 1233 : loop[1]->objType == DO_TABLE &&
7945 tgl@sss.pgh.pa.us 1305 [ # # ]:UBC 0 : loop[0]->objType == DO_CONSTRAINT &&
1306 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->contype == 'c' &&
1307 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->contable == (TableInfo *) loop[1])
1308 : : {
1309 : 0 : repairTableConstraintLoop(loop[1], loop[0]);
1310 : 0 : return;
1311 : : }
1312 : :
1313 : : /* Indirect loop involving table and CHECK constraint */
7945 tgl@sss.pgh.pa.us 1314 [ + + ]:CBC 2219 : if (nLoop > 2)
1315 : : {
1316 [ + + ]: 661 : for (i = 0; i < nLoop; i++)
1317 : : {
1318 [ + + ]: 497 : if (loop[i]->objType == DO_TABLE)
1319 : : {
1320 [ + + ]: 1322 : for (j = 0; j < nLoop; j++)
1321 : : {
1322 [ + + ]: 994 : if (loop[j]->objType == DO_CONSTRAINT &&
1323 [ + - ]: 5 : ((ConstraintInfo *) loop[j])->contype == 'c' &&
1324 [ + - ]: 5 : ((ConstraintInfo *) loop[j])->contable == (TableInfo *) loop[i])
1325 : : {
1326 : 5 : repairTableConstraintMultiLoop(loop[i], loop[j]);
1327 : 5 : return;
1328 : : }
1329 : : }
1330 : : }
1331 : : }
1332 : : }
1333 : :
1334 : : /* Table and attribute default */
1335 [ + + ]: 2214 : if (nLoop == 2 &&
1336 [ + + ]: 1233 : loop[0]->objType == DO_TABLE &&
1337 [ + - ]: 1060 : loop[1]->objType == DO_ATTRDEF &&
1338 [ + - ]: 1060 : ((AttrDefInfo *) loop[1])->adtable == (TableInfo *) loop[0])
1339 : : {
1340 : 1060 : repairTableAttrDefLoop(loop[0], loop[1]);
1341 : 1060 : return;
1342 : : }
1343 [ + + ]: 1154 : if (nLoop == 2 &&
1344 [ - + ]: 173 : loop[1]->objType == DO_TABLE &&
7945 tgl@sss.pgh.pa.us 1345 [ # # ]:UBC 0 : loop[0]->objType == DO_ATTRDEF &&
1346 [ # # ]: 0 : ((AttrDefInfo *) loop[0])->adtable == (TableInfo *) loop[1])
1347 : : {
1348 : 0 : repairTableAttrDefLoop(loop[1], loop[0]);
1349 : 0 : return;
1350 : : }
1351 : :
1352 : : /* index on partitioned table and corresponding index on partition */
2787 alvherre@alvh.no-ip. 1353 [ + + ]:CBC 1154 : if (nLoop == 2 &&
1354 [ - + ]: 173 : loop[0]->objType == DO_INDEX &&
2787 alvherre@alvh.no-ip. 1355 [ # # ]:UBC 0 : loop[1]->objType == DO_INDEX)
1356 : : {
1357 [ # # ]: 0 : if (((IndxInfo *) loop[0])->parentidx == loop[1]->catId.oid)
1358 : : {
1359 : 0 : repairIndexLoop(loop[0], loop[1]);
1360 : 0 : return;
1361 : : }
1362 [ # # ]: 0 : else if (((IndxInfo *) loop[1])->parentidx == loop[0]->catId.oid)
1363 : : {
1364 : 0 : repairIndexLoop(loop[1], loop[0]);
1365 : 0 : return;
1366 : : }
1367 : : }
1368 : :
1369 : : /* Indirect loop involving table and attribute default */
7945 tgl@sss.pgh.pa.us 1370 [ + + ]:CBC 1154 : if (nLoop > 2)
1371 : : {
1372 [ + - ]: 328 : for (i = 0; i < nLoop; i++)
1373 : : {
1374 [ + - ]: 328 : if (loop[i]->objType == DO_TABLE)
1375 : : {
1376 [ + + ]: 1148 : for (j = 0; j < nLoop; j++)
1377 : : {
1378 [ + + ]: 984 : if (loop[j]->objType == DO_ATTRDEF &&
1379 [ + + ]: 328 : ((AttrDefInfo *) loop[j])->adtable == (TableInfo *) loop[i])
1380 : : {
1381 : 164 : repairTableAttrDefMultiLoop(loop[i], loop[j]);
1382 : 164 : return;
1383 : : }
1384 : : }
1385 : : }
1386 : : }
1387 : : }
1388 : :
1389 : : /* Domain and CHECK or NOT NULL constraint */
1390 [ + + ]: 990 : if (nLoop == 2 &&
1391 [ + - ]: 173 : loop[0]->objType == DO_TYPE &&
1392 [ + - ]: 173 : loop[1]->objType == DO_CONSTRAINT &&
47 alvherre@kurilemu.de 1393 [ + + ]: 173 : (((ConstraintInfo *) loop[1])->contype == 'c' ||
1394 [ + - ]: 59 : ((ConstraintInfo *) loop[1])->contype == 'n') &&
7945 tgl@sss.pgh.pa.us 1395 [ + - ]: 173 : ((ConstraintInfo *) loop[1])->condomain == (TypeInfo *) loop[0])
1396 : : {
1397 : 173 : repairDomainConstraintLoop(loop[0], loop[1]);
1398 : 173 : return;
1399 : : }
1400 [ - + ]: 817 : if (nLoop == 2 &&
7945 tgl@sss.pgh.pa.us 1401 [ # # ]:UBC 0 : loop[1]->objType == DO_TYPE &&
1402 [ # # ]: 0 : loop[0]->objType == DO_CONSTRAINT &&
47 alvherre@kurilemu.de 1403 [ # # ]: 0 : (((ConstraintInfo *) loop[0])->contype == 'c' ||
1404 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->contype == 'n') &&
7945 tgl@sss.pgh.pa.us 1405 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->condomain == (TypeInfo *) loop[1])
1406 : : {
1407 : 0 : repairDomainConstraintLoop(loop[1], loop[0]);
1408 : 0 : return;
1409 : : }
1410 : :
1411 : : /* Indirect loop involving domain and CHECK or NOT NULL constraint */
7945 tgl@sss.pgh.pa.us 1412 [ - + ]:CBC 817 : if (nLoop > 2)
1413 : : {
7945 tgl@sss.pgh.pa.us 1414 [ # # ]:UBC 0 : for (i = 0; i < nLoop; i++)
1415 : : {
1416 [ # # ]: 0 : if (loop[i]->objType == DO_TYPE)
1417 : : {
1418 [ # # ]: 0 : for (j = 0; j < nLoop; j++)
1419 : : {
1420 [ # # ]: 0 : if (loop[j]->objType == DO_CONSTRAINT &&
47 alvherre@kurilemu.de 1421 [ # # ]: 0 : (((ConstraintInfo *) loop[j])->contype == 'c' ||
1422 [ # # ]: 0 : ((ConstraintInfo *) loop[j])->contype == 'n') &&
7945 tgl@sss.pgh.pa.us 1423 [ # # ]: 0 : ((ConstraintInfo *) loop[j])->condomain == (TypeInfo *) loop[i])
1424 : : {
1425 : 0 : repairDomainConstraintMultiLoop(loop[i], loop[j]);
1426 : 0 : return;
1427 : : }
1428 : : }
1429 : : }
1430 : : }
1431 : : }
1432 : :
1433 : : /*
1434 : : * Loop of table with itself --- just ignore it.
1435 : : *
1436 : : * (Actually, what this arises from is a dependency of a table column on
1437 : : * another column, which happened with generated columns before v15; or a
1438 : : * dependency of a table column on the whole table, which happens with
1439 : : * partitioning. But we didn't pay attention to sub-object IDs while
1440 : : * collecting the dependency data, so we can't see that here.)
1441 : : */
2352 peter@eisentraut.org 1442 [ + - ]:CBC 817 : if (nLoop == 1)
1443 : : {
1444 [ + - ]: 817 : if (loop[0]->objType == DO_TABLE)
1445 : : {
1446 : 817 : removeObjectDependency(loop[0], loop[0]->dumpId);
1447 : 817 : return;
1448 : : }
1449 : : }
1450 : :
1451 : : /*
1452 : : * If all the objects are TABLE_DATA items, what we must have is a
1453 : : * circular set of foreign key constraints (or a single self-referential
1454 : : * table). Print an appropriate complaint and break the loop arbitrarily.
1455 : : */
6207 tgl@sss.pgh.pa.us 1456 [ # # ]:UBC 0 : for (i = 0; i < nLoop; i++)
1457 : : {
1458 [ # # ]: 0 : if (loop[i]->objType != DO_TABLE_DATA)
1459 : 0 : break;
1460 : : }
1461 [ # # ]: 0 : if (i >= nLoop)
1462 : : {
2350 peter@eisentraut.org 1463 : 0 : pg_log_warning(ngettext("there are circular foreign-key constraints on this table:",
1464 : : "there are circular foreign-key constraints among these tables:",
1465 : : nLoop));
6207 tgl@sss.pgh.pa.us 1466 [ # # ]: 0 : for (i = 0; i < nLoop; i++)
825 1467 : 0 : pg_log_warning_detail("%s", loop[i]->name);
1468 : 0 : pg_log_warning_hint("You might not be able to restore the dump without using --disable-triggers or temporarily dropping the constraints.");
1469 : 0 : pg_log_warning_hint("Consider using a full dump instead of a --data-only dump to avoid this problem.");
6207 1470 [ # # ]: 0 : if (nLoop > 1)
1471 : 0 : removeObjectDependency(loop[0], loop[1]->dumpId);
1472 : : else /* must be a self-dependency */
1473 : 0 : removeObjectDependency(loop[0], loop[0]->dumpId);
1474 : 0 : return;
1475 : : }
1476 : :
1477 : : /*
1478 : : * If we can't find a principled way to break the loop, complain and break
1479 : : * it in an arbitrary fashion.
1480 : : */
2350 peter@eisentraut.org 1481 : 0 : pg_log_warning("could not resolve dependency loop among these items:");
7945 tgl@sss.pgh.pa.us 1482 [ # # ]: 0 : for (i = 0; i < nLoop; i++)
1483 : : {
1484 : : char buf[1024];
1485 : :
1486 : 0 : describeDumpableObject(loop[i], buf, sizeof(buf));
825 1487 : 0 : pg_log_warning_detail("%s", buf);
1488 : : }
1489 : :
6207 1490 [ # # ]: 0 : if (nLoop > 1)
1491 : 0 : removeObjectDependency(loop[0], loop[1]->dumpId);
1492 : : else /* must be a self-dependency */
1493 : 0 : removeObjectDependency(loop[0], loop[0]->dumpId);
1494 : : }
1495 : :
1496 : : /*
1497 : : * Describe a dumpable object usefully for errors
1498 : : *
1499 : : * This should probably go somewhere else...
1500 : : */
1501 : : static void
7945 1502 : 0 : describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
1503 : : {
1504 [ # # # # : 0 : switch (obj->objType)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
1505 : : {
1506 : 0 : case DO_NAMESPACE:
1507 : 0 : snprintf(buf, bufsize,
1508 : : "SCHEMA %s (ID %d OID %u)",
1509 : : obj->name, obj->dumpId, obj->catId.oid);
1510 : 0 : return;
5324 1511 : 0 : case DO_EXTENSION:
1512 : 0 : snprintf(buf, bufsize,
1513 : : "EXTENSION %s (ID %d OID %u)",
1514 : : obj->name, obj->dumpId, obj->catId.oid);
1515 : 0 : return;
7945 1516 : 0 : case DO_TYPE:
1517 : 0 : snprintf(buf, bufsize,
1518 : : "TYPE %s (ID %d OID %u)",
1519 : : obj->name, obj->dumpId, obj->catId.oid);
1520 : 0 : return;
7128 1521 : 0 : case DO_SHELL_TYPE:
1522 : 0 : snprintf(buf, bufsize,
1523 : : "SHELL TYPE %s (ID %d OID %u)",
1524 : : obj->name, obj->dumpId, obj->catId.oid);
1525 : 0 : return;
7945 1526 : 0 : case DO_FUNC:
1527 : 0 : snprintf(buf, bufsize,
1528 : : "FUNCTION %s (ID %d OID %u)",
1529 : : obj->name, obj->dumpId, obj->catId.oid);
1530 : 0 : return;
1531 : 0 : case DO_AGG:
1532 : 0 : snprintf(buf, bufsize,
1533 : : "AGGREGATE %s (ID %d OID %u)",
1534 : : obj->name, obj->dumpId, obj->catId.oid);
1535 : 0 : return;
1536 : 0 : case DO_OPERATOR:
1537 : 0 : snprintf(buf, bufsize,
1538 : : "OPERATOR %s (ID %d OID %u)",
1539 : : obj->name, obj->dumpId, obj->catId.oid);
1540 : 0 : return;
3454 alvherre@alvh.no-ip. 1541 : 0 : case DO_ACCESS_METHOD:
1542 : 0 : snprintf(buf, bufsize,
1543 : : "ACCESS METHOD %s (ID %d OID %u)",
1544 : : obj->name, obj->dumpId, obj->catId.oid);
1545 : 0 : return;
7945 tgl@sss.pgh.pa.us 1546 : 0 : case DO_OPCLASS:
1547 : 0 : snprintf(buf, bufsize,
1548 : : "OPERATOR CLASS %s (ID %d OID %u)",
1549 : : obj->name, obj->dumpId, obj->catId.oid);
1550 : 0 : return;
6801 1551 : 0 : case DO_OPFAMILY:
1552 : 0 : snprintf(buf, bufsize,
1553 : : "OPERATOR FAMILY %s (ID %d OID %u)",
1554 : : obj->name, obj->dumpId, obj->catId.oid);
1555 : 0 : return;
5320 peter_e@gmx.net 1556 : 0 : case DO_COLLATION:
1557 : 0 : snprintf(buf, bufsize,
1558 : : "COLLATION %s (ID %d OID %u)",
1559 : : obj->name, obj->dumpId, obj->catId.oid);
1560 : 0 : return;
7945 tgl@sss.pgh.pa.us 1561 : 0 : case DO_CONVERSION:
1562 : 0 : snprintf(buf, bufsize,
1563 : : "CONVERSION %s (ID %d OID %u)",
1564 : : obj->name, obj->dumpId, obj->catId.oid);
1565 : 0 : return;
1566 : 0 : case DO_TABLE:
1567 : 0 : snprintf(buf, bufsize,
1568 : : "TABLE %s (ID %d OID %u)",
1569 : : obj->name, obj->dumpId, obj->catId.oid);
1570 : 0 : return;
1699 1571 : 0 : case DO_TABLE_ATTACH:
1572 : 0 : snprintf(buf, bufsize,
1573 : : "TABLE ATTACH %s (ID %d)",
1574 : : obj->name, obj->dumpId);
1575 : 0 : return;
7945 1576 : 0 : case DO_ATTRDEF:
1577 : 0 : snprintf(buf, bufsize,
1578 : : "ATTRDEF %s.%s (ID %d OID %u)",
7857 1579 : 0 : ((AttrDefInfo *) obj)->adtable->dobj.name,
7945 1580 : 0 : ((AttrDefInfo *) obj)->adtable->attnames[((AttrDefInfo *) obj)->adnum - 1],
1581 : : obj->dumpId, obj->catId.oid);
1582 : 0 : return;
1583 : 0 : case DO_INDEX:
1584 : 0 : snprintf(buf, bufsize,
1585 : : "INDEX %s (ID %d OID %u)",
1586 : : obj->name, obj->dumpId, obj->catId.oid);
4570 kgrittn@postgresql.o 1587 : 0 : return;
2787 alvherre@alvh.no-ip. 1588 : 0 : case DO_INDEX_ATTACH:
1589 : 0 : snprintf(buf, bufsize,
1590 : : "INDEX ATTACH %s (ID %d)",
1591 : : obj->name, obj->dumpId);
1592 : 0 : return;
3088 1593 : 0 : case DO_STATSEXT:
1594 : 0 : snprintf(buf, bufsize,
1595 : : "STATISTICS %s (ID %d OID %u)",
1596 : : obj->name, obj->dumpId, obj->catId.oid);
1597 : 0 : return;
4570 kgrittn@postgresql.o 1598 : 0 : case DO_REFRESH_MATVIEW:
1599 : 0 : snprintf(buf, bufsize,
1600 : : "REFRESH MATERIALIZED VIEW %s (ID %d OID %u)",
1601 : : obj->name, obj->dumpId, obj->catId.oid);
7945 tgl@sss.pgh.pa.us 1602 : 0 : return;
1603 : 0 : case DO_RULE:
1604 : 0 : snprintf(buf, bufsize,
1605 : : "RULE %s (ID %d OID %u)",
1606 : : obj->name, obj->dumpId, obj->catId.oid);
1607 : 0 : return;
1608 : 0 : case DO_TRIGGER:
1609 : 0 : snprintf(buf, bufsize,
1610 : : "TRIGGER %s (ID %d OID %u)",
1611 : : obj->name, obj->dumpId, obj->catId.oid);
1612 : 0 : return;
4798 rhaas@postgresql.org 1613 : 0 : case DO_EVENT_TRIGGER:
1614 : 0 : snprintf(buf, bufsize,
1615 : : "EVENT TRIGGER %s (ID %d OID %u)",
1616 : : obj->name, obj->dumpId, obj->catId.oid);
1617 : 0 : return;
7945 tgl@sss.pgh.pa.us 1618 : 0 : case DO_CONSTRAINT:
1619 : 0 : snprintf(buf, bufsize,
1620 : : "CONSTRAINT %s (ID %d OID %u)",
1621 : : obj->name, obj->dumpId, obj->catId.oid);
1622 : 0 : return;
1623 : 0 : case DO_FK_CONSTRAINT:
1624 : 0 : snprintf(buf, bufsize,
1625 : : "FK CONSTRAINT %s (ID %d OID %u)",
1626 : : obj->name, obj->dumpId, obj->catId.oid);
1627 : 0 : return;
1628 : 0 : case DO_PROCLANG:
1629 : 0 : snprintf(buf, bufsize,
1630 : : "PROCEDURAL LANGUAGE %s (ID %d OID %u)",
1631 : : obj->name, obj->dumpId, obj->catId.oid);
1632 : 0 : return;
1633 : 0 : case DO_CAST:
1634 : 0 : snprintf(buf, bufsize,
1635 : : "CAST %u to %u (ID %d OID %u)",
1636 : : ((CastInfo *) obj)->castsource,
1637 : : ((CastInfo *) obj)->casttarget,
1638 : : obj->dumpId, obj->catId.oid);
1639 : 0 : return;
3786 peter_e@gmx.net 1640 : 0 : case DO_TRANSFORM:
1641 : 0 : snprintf(buf, bufsize,
1642 : : "TRANSFORM %u lang %u (ID %d OID %u)",
1643 : : ((TransformInfo *) obj)->trftype,
1644 : : ((TransformInfo *) obj)->trflang,
1645 : : obj->dumpId, obj->catId.oid);
1646 : 0 : return;
7945 tgl@sss.pgh.pa.us 1647 : 0 : case DO_TABLE_DATA:
1648 : 0 : snprintf(buf, bufsize,
1649 : : "TABLE DATA %s (ID %d OID %u)",
1650 : : obj->name, obj->dumpId, obj->catId.oid);
7857 1651 : 0 : return;
3301 peter_e@gmx.net 1652 : 0 : case DO_SEQUENCE_SET:
1653 : 0 : snprintf(buf, bufsize,
1654 : : "SEQUENCE SET %s (ID %d OID %u)",
1655 : : obj->name, obj->dumpId, obj->catId.oid);
1656 : 0 : return;
6075 tgl@sss.pgh.pa.us 1657 : 0 : case DO_DUMMY_TYPE:
7857 1658 : 0 : snprintf(buf, bufsize,
1659 : : "DUMMY TYPE %s (ID %d OID %u)",
1660 : : obj->name, obj->dumpId, obj->catId.oid);
1661 : 0 : return;
6591 1662 : 0 : case DO_TSPARSER:
1663 : 0 : snprintf(buf, bufsize,
1664 : : "TEXT SEARCH PARSER %s (ID %d OID %u)",
1665 : : obj->name, obj->dumpId, obj->catId.oid);
1666 : 0 : return;
1667 : 0 : case DO_TSDICT:
1668 : 0 : snprintf(buf, bufsize,
1669 : : "TEXT SEARCH DICTIONARY %s (ID %d OID %u)",
1670 : : obj->name, obj->dumpId, obj->catId.oid);
1671 : 0 : return;
1672 : 0 : case DO_TSTEMPLATE:
1673 : 0 : snprintf(buf, bufsize,
1674 : : "TEXT SEARCH TEMPLATE %s (ID %d OID %u)",
1675 : : obj->name, obj->dumpId, obj->catId.oid);
1676 : 0 : return;
1677 : 0 : case DO_TSCONFIG:
1678 : 0 : snprintf(buf, bufsize,
1679 : : "TEXT SEARCH CONFIGURATION %s (ID %d OID %u)",
1680 : : obj->name, obj->dumpId, obj->catId.oid);
1681 : 0 : return;
6105 peter_e@gmx.net 1682 : 0 : case DO_FDW:
1683 : 0 : snprintf(buf, bufsize,
1684 : : "FOREIGN DATA WRAPPER %s (ID %d OID %u)",
1685 : : obj->name, obj->dumpId, obj->catId.oid);
1686 : 0 : return;
1687 : 0 : case DO_FOREIGN_SERVER:
1688 : 0 : snprintf(buf, bufsize,
1689 : : "FOREIGN SERVER %s (ID %d OID %u)",
1690 : : obj->name, obj->dumpId, obj->catId.oid);
1691 : 0 : return;
5815 tgl@sss.pgh.pa.us 1692 : 0 : case DO_DEFAULT_ACL:
1693 : 0 : snprintf(buf, bufsize,
1694 : : "DEFAULT ACL %s (ID %d OID %u)",
1695 : : obj->name, obj->dumpId, obj->catId.oid);
1696 : 0 : return;
1006 peter@eisentraut.org 1697 : 0 : case DO_LARGE_OBJECT:
7857 tgl@sss.pgh.pa.us 1698 : 0 : snprintf(buf, bufsize,
1699 : : "LARGE OBJECT (ID %d OID %u)",
1700 : : obj->dumpId, obj->catId.oid);
7945 1701 : 0 : return;
1006 peter@eisentraut.org 1702 : 0 : case DO_LARGE_OBJECT_DATA:
7373 tgl@sss.pgh.pa.us 1703 : 0 : snprintf(buf, bufsize,
1704 : : "LARGE OBJECT DATA (ID %d)",
1705 : : obj->dumpId);
1706 : 0 : return;
3936 sfrost@snowman.net 1707 : 0 : case DO_POLICY:
4005 1708 : 0 : snprintf(buf, bufsize,
1709 : : "POLICY (ID %d OID %u)",
1710 : : obj->dumpId, obj->catId.oid);
1711 : 0 : return;
3152 peter_e@gmx.net 1712 : 0 : case DO_PUBLICATION:
1713 : 0 : snprintf(buf, bufsize,
1714 : : "PUBLICATION (ID %d OID %u)",
1715 : : obj->dumpId, obj->catId.oid);
1716 : 0 : return;
1717 : 0 : case DO_PUBLICATION_REL:
1718 : 0 : snprintf(buf, bufsize,
1719 : : "PUBLICATION TABLE (ID %d OID %u)",
1720 : : obj->dumpId, obj->catId.oid);
1721 : 0 : return;
1397 akapila@postgresql.o 1722 : 0 : case DO_PUBLICATION_TABLE_IN_SCHEMA:
1410 1723 : 0 : snprintf(buf, bufsize,
1724 : : "PUBLICATION TABLES IN SCHEMA (ID %d OID %u)",
1725 : : obj->dumpId, obj->catId.oid);
1726 : 0 : return;
3152 peter_e@gmx.net 1727 : 0 : case DO_SUBSCRIPTION:
1728 : 0 : snprintf(buf, bufsize,
1729 : : "SUBSCRIPTION (ID %d OID %u)",
1730 : : obj->dumpId, obj->catId.oid);
1731 : 0 : return;
613 akapila@postgresql.o 1732 : 0 : case DO_SUBSCRIPTION_REL:
1733 : 0 : snprintf(buf, bufsize,
1734 : : "SUBSCRIPTION TABLE (ID %d OID %u)",
1735 : : obj->dumpId, obj->catId.oid);
1736 : 0 : return;
4821 tgl@sss.pgh.pa.us 1737 : 0 : case DO_PRE_DATA_BOUNDARY:
1738 : 0 : snprintf(buf, bufsize,
1739 : : "PRE-DATA BOUNDARY (ID %d)",
1740 : : obj->dumpId);
1741 : 0 : return;
1742 : 0 : case DO_POST_DATA_BOUNDARY:
1743 : 0 : snprintf(buf, bufsize,
1744 : : "POST-DATA BOUNDARY (ID %d)",
1745 : : obj->dumpId);
1746 : 0 : return;
198 jdavis@postgresql.or 1747 : 0 : case DO_REL_STATS:
1748 : 0 : snprintf(buf, bufsize,
1749 : : "RELATION STATISTICS FOR %s (ID %d OID %u)",
1750 : : obj->name, obj->dumpId, obj->catId.oid);
1751 : 0 : return;
1752 : : }
1753 : : /* shouldn't get here */
7945 tgl@sss.pgh.pa.us 1754 : 0 : snprintf(buf, bufsize,
1755 : : "object type %d (ID %d OID %u)",
1756 : 0 : (int) obj->objType,
1757 : : obj->dumpId, obj->catId.oid);
1758 : : }
1759 : :
1760 : : /* binaryheap comparator that compares "a" and "b" as integers */
1761 : : static int
718 nathan@postgresql.or 1762 :CBC 34216953 : int_cmp(void *a, void *b, void *arg)
1763 : : {
1764 : 34216953 : int ai = (int) (intptr_t) a;
1765 : 34216953 : int bi = (int) (intptr_t) b;
1766 : :
568 1767 : 34216953 : return pg_cmp_s32(ai, bi);
1768 : : }
|