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-2026, 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
8047 tgl@sss.pgh.pa.us 192 :CBC 260 : sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs)
193 : : {
194 [ + - ]: 260 : if (numObjs > 1)
1132 peter@eisentraut.org 195 : 260 : qsort(objs, numObjs, sizeof(DumpableObject *),
196 : : DOTypeNameCompare);
8047 tgl@sss.pgh.pa.us 197 : 260 : }
198 : :
199 : : static int
200 : 14198878 : DOTypeNameCompare(const void *p1, const void *p2)
201 : : {
5026 bruce@momjian.us 202 : 14198878 : DumpableObject *obj1 = *(DumpableObject *const *) p1;
203 : 14198878 : DumpableObject *obj2 = *(DumpableObject *const *) p2;
204 : : int cmpval;
205 : :
206 : : /* Sort by type's priority */
3441 tgl@sss.pgh.pa.us 207 : 14198878 : cmpval = dbObjectTypePriority[obj1->objType] -
208 : 14198878 : dbObjectTypePriority[obj2->objType];
209 : :
8047 210 [ + + ]: 14198878 : if (cmpval != 0)
211 : 3145307 : 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 : : */
2777 218 [ + + ]: 11053571 : if (obj1->namespace)
219 : : {
220 [ + + ]: 10507546 : if (obj2->namespace)
221 : : {
222 : 10507469 : cmpval = strcmp(obj1->namespace->dobj.name,
223 : 10507469 : obj2->namespace->dobj.name);
224 [ + + ]: 10507469 : if (cmpval != 0)
225 : 394038 : return cmpval;
226 : : }
227 : : else
228 : 77 : return -1;
229 : : }
230 [ + + ]: 546025 : else if (obj2->namespace)
231 : 116 : 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 : : */
8047 241 : 10659340 : cmpval = strcmp(obj1->name, obj2->name);
242 [ + + ]: 10659340 : if (cmpval != 0)
243 : 9302000 : 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 : : */
227 noah@leadboat.com 249 : 1357340 : cmpval = obj1->objType - obj2->objType;
250 [ + + ]: 1357340 : if (cmpval != 0)
251 : 49073 : 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 : : */
5861 bruce@momjian.us 266 [ + + + + ]: 1308267 : if (obj1->objType == DO_FUNC || obj1->objType == DO_AGG)
5872 peter_e@gmx.net 267 :UBC 0 : {
5026 bruce@momjian.us 268 :CBC 84 : FuncInfo *fobj1 = *(FuncInfo *const *) p1;
269 : 84 : FuncInfo *fobj2 = *(FuncInfo *const *) p2;
270 : : int i;
271 : :
272 : : /* Sort by number of arguments, then argument type names */
5872 peter_e@gmx.net 273 : 84 : cmpval = fobj1->nargs - fobj2->nargs;
274 [ + + ]: 84 : if (cmpval != 0)
275 : 16 : return cmpval;
4027 tgl@sss.pgh.pa.us 276 [ + - ]: 75 : for (i = 0; i < fobj1->nargs; i++)
277 : : {
227 noah@leadboat.com 278 : 75 : cmpval = pgTypeNameCompare(fobj1->argtypes[i],
279 : 75 : fobj2->argtypes[i]);
280 [ + + ]: 75 : if (cmpval != 0)
281 : 68 : return cmpval;
282 : : }
283 : : }
5183 peter_e@gmx.net 284 [ + + ]: 1308183 : else if (obj1->objType == DO_OPERATOR)
285 : : {
5026 bruce@momjian.us 286 : 973148 : OprInfo *oobj1 = *(OprInfo *const *) p1;
287 : 973148 : OprInfo *oobj2 = *(OprInfo *const *) p2;
288 : :
289 : : /* oprkind is 'l', 'r', or 'b'; this sorts prefix, postfix, infix */
5183 peter_e@gmx.net 290 : 973148 : cmpval = (oobj2->oprkind - oobj1->oprkind);
291 [ + + ]: 973148 : if (cmpval != 0)
292 : 29664 : return cmpval;
293 : : /* Within an oprkind, sort by argument type names */
227 noah@leadboat.com 294 : 943484 : cmpval = pgTypeNameCompare(oobj1->oprleft, oobj2->oprleft);
295 [ + + ]: 943484 : if (cmpval != 0)
296 : 832604 : return cmpval;
297 : 110880 : cmpval = pgTypeNameCompare(oobj1->oprright, oobj2->oprright);
298 [ + - ]: 110880 : if (cmpval != 0)
299 : 110880 : return cmpval;
300 : : }
301 [ + + ]: 335035 : else if (obj1->objType == DO_OPCLASS)
302 : : {
303 : 19833 : OpclassInfo *opcobj1 = *(OpclassInfo *const *) p1;
304 : 19833 : OpclassInfo *opcobj2 = *(OpclassInfo *const *) p2;
305 : :
306 : : /* Sort by access method name, per pg_opclass_am_name_nsp_index */
307 : 19833 : cmpval = accessMethodNameCompare(opcobj1->opcmethod,
308 : : opcobj2->opcmethod);
309 [ + - ]: 19833 : if (cmpval != 0)
310 : 19833 : return cmpval;
311 : : }
312 [ + + ]: 315202 : else if (obj1->objType == DO_OPFAMILY)
313 : : {
314 : 15840 : OpfamilyInfo *opfobj1 = *(OpfamilyInfo *const *) p1;
315 : 15840 : OpfamilyInfo *opfobj2 = *(OpfamilyInfo *const *) p2;
316 : :
317 : : /* Sort by access method name, per pg_opfamily_am_name_nsp_index */
318 : 15840 : cmpval = accessMethodNameCompare(opfobj1->opfmethod,
319 : : opfobj2->opfmethod);
320 [ + - ]: 15840 : if (cmpval != 0)
321 : 15840 : return cmpval;
322 : : }
323 [ + + ]: 299362 : else if (obj1->objType == DO_COLLATION)
324 : : {
325 : 46232 : CollInfo *cobj1 = *(CollInfo *const *) p1;
326 : 46232 : 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 : 46232 : cmpval = cobj1->collencoding - cobj2->collencoding;
342 [ + - ]: 46232 : if (cmpval != 0)
343 : 46232 : return cmpval;
344 : : }
5147 tgl@sss.pgh.pa.us 345 [ + + ]: 253130 : else if (obj1->objType == DO_ATTRDEF)
346 : : {
5026 bruce@momjian.us 347 : 503 : AttrDefInfo *adobj1 = *(AttrDefInfo *const *) p1;
348 : 503 : AttrDefInfo *adobj2 = *(AttrDefInfo *const *) p2;
349 : :
350 : : /* Sort by attribute number */
5147 tgl@sss.pgh.pa.us 351 : 503 : cmpval = (adobj1->adnum - adobj2->adnum);
352 [ + - ]: 503 : if (cmpval != 0)
353 : 503 : return cmpval;
354 : : }
2323 355 [ + + ]: 252627 : else if (obj1->objType == DO_POLICY)
356 : : {
357 : 24 : PolicyInfo *pobj1 = *(PolicyInfo *const *) p1;
358 : 24 : PolicyInfo *pobj2 = *(PolicyInfo *const *) p2;
359 : :
360 : : /* Sort by table name (table namespace was considered already) */
361 : 24 : cmpval = strcmp(pobj1->poltable->dobj.name,
362 : 24 : pobj2->poltable->dobj.name);
363 [ + - ]: 24 : if (cmpval != 0)
364 : 24 : return cmpval;
365 : : }
496 366 [ + + ]: 252603 : else if (obj1->objType == DO_RULE)
367 : : {
368 : 251516 : RuleInfo *robj1 = *(RuleInfo *const *) p1;
369 : 251516 : RuleInfo *robj2 = *(RuleInfo *const *) p2;
370 : :
371 : : /* Sort by table name (table namespace was considered already) */
372 : 251516 : cmpval = strcmp(robj1->ruletable->dobj.name,
373 : 251516 : robj2->ruletable->dobj.name);
374 [ + - ]: 251516 : if (cmpval != 0)
375 : 251516 : return cmpval;
376 : : }
2323 377 [ + + ]: 1087 : else if (obj1->objType == DO_TRIGGER)
378 : : {
379 : 381 : TriggerInfo *tobj1 = *(TriggerInfo *const *) p1;
380 : 381 : TriggerInfo *tobj2 = *(TriggerInfo *const *) p2;
381 : :
382 : : /* Sort by table name (table namespace was considered already) */
383 : 381 : cmpval = strcmp(tobj1->tgtable->dobj.name,
384 : 381 : tobj2->tgtable->dobj.name);
385 [ + - ]: 381 : if (cmpval != 0)
386 : 381 : return cmpval;
387 : : }
148 alvherre@kurilemu.de 388 [ + + ]: 706 : else if (obj1->objType == DO_CONSTRAINT ||
389 [ - + ]: 295 : obj1->objType == DO_FK_CONSTRAINT)
227 noah@leadboat.com 390 :UBC 0 : {
227 noah@leadboat.com 391 :CBC 411 : ConstraintInfo *robj1 = *(ConstraintInfo *const *) p1;
392 : 411 : ConstraintInfo *robj2 = *(ConstraintInfo *const *) p2;
393 : :
394 : : /*
395 : : * Sort domain constraints before table constraints, for consistency
396 : : * with our decision to sort CREATE DOMAIN before CREATE TABLE.
397 : : */
398 [ + + ]: 411 : if (robj1->condomain)
399 : : {
400 [ - + ]: 28 : if (robj2->condomain)
401 : : {
402 : : /* Sort by domain name (domain namespace was considered) */
227 noah@leadboat.com 403 :UBC 0 : cmpval = strcmp(robj1->condomain->dobj.name,
404 : 0 : robj2->condomain->dobj.name);
405 [ # # ]: 0 : if (cmpval != 0)
406 : 0 : return cmpval;
407 : : }
408 : : else
227 noah@leadboat.com 409 :CBC 28 : return PRIO_TYPE - PRIO_TABLE;
410 : : }
411 [ + + ]: 383 : else if (robj2->condomain)
412 : 26 : return PRIO_TABLE - PRIO_TYPE;
413 : : else
414 : : {
415 : : /* Sort by table name (table namespace was considered already) */
416 : 357 : cmpval = strcmp(robj1->contable->dobj.name,
417 : 357 : robj2->contable->dobj.name);
418 [ + - ]: 357 : if (cmpval != 0)
419 : 357 : return cmpval;
420 : : }
421 : : }
205 422 [ + + ]: 295 : else if (obj1->objType == DO_DEFAULT_ACL)
423 : : {
424 : 9 : DefaultACLInfo *daclobj1 = *(DefaultACLInfo *const *) p1;
425 : 9 : DefaultACLInfo *daclobj2 = *(DefaultACLInfo *const *) p2;
426 : :
427 : : /*
428 : : * Sort by defaclrole, per pg_default_acl_role_nsp_obj_index. The
429 : : * (namespace, name) match (defaclnamespace, defaclobjtype).
430 : : */
431 : 9 : cmpval = strcmp(daclobj1->defaclrole, daclobj2->defaclrole);
432 [ + - ]: 9 : if (cmpval != 0)
433 : 9 : return cmpval;
434 : : }
227 435 [ + + ]: 286 : else if (obj1->objType == DO_PUBLICATION_REL)
436 : : {
437 : 256 : PublicationRelInfo *probj1 = *(PublicationRelInfo *const *) p1;
438 : 256 : PublicationRelInfo *probj2 = *(PublicationRelInfo *const *) p2;
439 : :
440 : : /* Sort by publication name, since (namespace, name) match the rel */
441 : 256 : cmpval = strcmp(probj1->publication->dobj.name,
442 : 256 : probj2->publication->dobj.name);
443 [ + - ]: 256 : if (cmpval != 0)
444 : 256 : return cmpval;
445 : : }
446 [ + - ]: 30 : else if (obj1->objType == DO_PUBLICATION_TABLE_IN_SCHEMA)
447 : : {
448 : 30 : PublicationSchemaInfo *psobj1 = *(PublicationSchemaInfo *const *) p1;
449 : 30 : PublicationSchemaInfo *psobj2 = *(PublicationSchemaInfo *const *) p2;
450 : :
451 : : /* Sort by publication name, since ->name is just nspname */
452 : 30 : cmpval = strcmp(psobj1->publication->dobj.name,
453 : 30 : psobj2->publication->dobj.name);
454 [ + - ]: 30 : if (cmpval != 0)
455 : 30 : return cmpval;
456 : : }
87 noah@leadboat.com 457 [ # # ]:UBC 0 : else if (obj1->objType == DO_SUBSCRIPTION_REL)
458 : : {
459 : 0 : SubRelInfo *srobj1 = *(SubRelInfo *const *) p1;
460 : 0 : SubRelInfo *srobj2 = *(SubRelInfo *const *) p2;
461 : :
462 : : /* Sort by subscription name, since (namespace, name) match the rel */
463 : 0 : cmpval = strcmp(srobj1->subinfo->dobj.name,
464 : 0 : srobj2->subinfo->dobj.name);
465 [ # # ]: 0 : if (cmpval != 0)
466 : 0 : return cmpval;
467 : : }
468 : :
469 : : /*
470 : : * Shouldn't get here except after catalog corruption, but if we do, sort
471 : : * by OID. This may make logically-identical databases differ in the
472 : : * order of objects in dump output. Users will get spurious schema diffs.
473 : : * Expect flaky failures of 002_pg_upgrade.pl test 'dump outputs from
474 : : * original and restored regression databases match' if the regression
475 : : * database contains objects allowing that test to reach here. That's a
476 : : * consequence of the test using "pg_restore -j", which doesn't fully
477 : : * constrain OID assignment order.
478 : : */
227 noah@leadboat.com 479 :UNC 0 : Assert(false);
8047 tgl@sss.pgh.pa.us 480 [ # # ]:EUB : return oidcmp(obj1->catId.oid, obj2->catId.oid);
481 : : }
482 : :
483 : : /* Compare two OID-identified pg_type values by nspname, then by typname. */
484 : : static int
227 noah@leadboat.com 485 :CBC 1054439 : pgTypeNameCompare(Oid typid1, Oid typid2)
486 : : {
487 : : TypeInfo *typobj1;
488 : : TypeInfo *typobj2;
489 : : int cmpval;
490 : :
491 [ + + ]: 1054439 : if (typid1 == typid2)
492 : 110887 : return 0;
493 : :
494 : 943552 : typobj1 = findTypeByOid(typid1);
495 : 943552 : typobj2 = findTypeByOid(typid2);
496 : :
497 [ + - - + ]: 943552 : if (!typobj1 || !typobj2)
498 : : {
499 : : /*
500 : : * getTypes() didn't find some OID. Assume catalog corruption, e.g.
501 : : * an oprright value without the corresponding OID in a pg_type row.
502 : : * Report as "equal", so the caller uses the next available basis for
503 : : * comparison, e.g. the next function argument.
504 : : *
505 : : * Unary operators have InvalidOid in oprleft (if oprkind='r') or in
506 : : * oprright (if oprkind='l'). Caller already sorted by oprkind,
507 : : * calling us only for like-kind operators. Hence, "typid1 == typid2"
508 : : * took care of InvalidOid. (v14 removed postfix operator support.
509 : : * Hence, when dumping from v14+, only oprleft can be InvalidOid.)
510 : : */
227 noah@leadboat.com 511 :UBC 0 : Assert(false);
512 : : return 0;
513 : : }
514 : :
227 noah@leadboat.com 515 [ + - - + ]:CBC 943552 : if (!typobj1->dobj.namespace || !typobj2->dobj.namespace)
227 noah@leadboat.com 516 :UBC 0 : Assert(false); /* catalog corruption */
517 : : else
518 : : {
227 noah@leadboat.com 519 :CBC 943552 : cmpval = strcmp(typobj1->dobj.namespace->dobj.name,
520 : 943552 : typobj2->dobj.namespace->dobj.name);
521 [ + + ]: 943552 : if (cmpval != 0)
522 : 32 : return cmpval;
523 : : }
524 : 943520 : return strcmp(typobj1->dobj.name, typobj2->dobj.name);
525 : : }
526 : :
527 : : /* Compare two OID-identified pg_am values by amname. */
528 : : static int
529 : 35673 : accessMethodNameCompare(Oid am1, Oid am2)
530 : : {
531 : : AccessMethodInfo *amobj1;
532 : : AccessMethodInfo *amobj2;
533 : :
534 [ - + ]: 35673 : if (am1 == am2)
227 noah@leadboat.com 535 :UBC 0 : return 0;
536 : :
227 noah@leadboat.com 537 :CBC 35673 : amobj1 = findAccessMethodByOid(am1);
538 : 35673 : amobj2 = findAccessMethodByOid(am2);
539 : :
540 [ + - - + ]: 35673 : if (!amobj1 || !amobj2)
541 : : {
542 : : /* catalog corruption: handle like pgTypeNameCompare() does */
227 noah@leadboat.com 543 :UBC 0 : Assert(false);
544 : : return 0;
545 : : }
546 : :
227 noah@leadboat.com 547 :CBC 35673 : return strcmp(amobj1->dobj.name, amobj2->dobj.name);
548 : : }
549 : :
550 : :
551 : : /*
552 : : * Sort the given objects into a safe dump order using dependency
553 : : * information (to the extent we have it available).
554 : : *
555 : : * The DumpIds of the PRE_DATA_BOUNDARY and POST_DATA_BOUNDARY objects are
556 : : * passed in separately, in case we need them during dependency loop repair.
557 : : */
558 : : void
5011 tgl@sss.pgh.pa.us 559 : 260 : sortDumpableObjects(DumpableObject **objs, int numObjs,
560 : : DumpId preBoundaryId, DumpId postBoundaryId)
561 : : {
562 : : DumpableObject **ordering;
563 : : int nOrdering;
564 : :
565 [ - + ]: 260 : if (numObjs <= 0) /* can't happen anymore ... */
8134 tgl@sss.pgh.pa.us 566 :UBC 0 : return;
567 : :
568 : : /*
569 : : * Saving the boundary IDs in static variables is a bit grotty, but seems
570 : : * better than adding them to parameter lists of subsidiary functions.
571 : : */
5011 tgl@sss.pgh.pa.us 572 :CBC 260 : preDataBoundId = preBoundaryId;
573 : 260 : postDataBoundId = postBoundaryId;
574 : :
30 michael@paquier.xyz 575 :GNC 260 : ordering = pg_malloc_array(DumpableObject *, numObjs);
8135 tgl@sss.pgh.pa.us 576 [ + + ]:CBC 692 : while (!TopoSort(objs, numObjs, ordering, &nOrdering))
8134 577 : 432 : findDependencyLoops(ordering, nOrdering, numObjs);
578 : :
8135 579 : 260 : memcpy(objs, ordering, numObjs * sizeof(DumpableObject *));
580 : :
581 : 260 : free(ordering);
582 : : }
583 : :
584 : : /*
585 : : * TopoSort -- topological sort of a dump list
586 : : *
587 : : * Generate a re-ordering of the dump list that satisfies all the dependency
588 : : * constraints shown in the dump list. (Each such constraint is a fact of a
589 : : * partial ordering.) Minimize rearrangement of the list not needed to
590 : : * achieve the partial ordering.
591 : : *
592 : : * The input is the list of numObjs objects in objs[]. This list is not
593 : : * modified.
594 : : *
595 : : * Returns true if able to build an ordering that satisfies all the
596 : : * constraints, false if not (there are contradictory constraints).
597 : : *
598 : : * On success (true result), ordering[] is filled with a sorted array of
599 : : * DumpableObject pointers, of length equal to the input list length.
600 : : *
601 : : * On failure (false result), ordering[] is filled with an unsorted array of
602 : : * DumpableObject pointers of length *nOrdering, listing the objects that
603 : : * prevented the sort from being completed. In general, these objects either
604 : : * participate directly in a dependency cycle, or are depended on by objects
605 : : * that are in a cycle. (The latter objects are not actually problematic,
606 : : * but it takes further analysis to identify which are which.)
607 : : *
608 : : * The caller is responsible for allocating sufficient space at *ordering.
609 : : */
610 : : static bool
611 : 692 : TopoSort(DumpableObject **objs,
612 : : int numObjs,
613 : : DumpableObject **ordering, /* output argument */
614 : : int *nOrdering) /* output argument */
615 : : {
616 : 692 : DumpId maxDumpId = getMaxDumpId();
617 : : binaryheap *pendingHeap;
618 : : int *beforeConstraints;
619 : : int *idMap;
620 : : DumpableObject *obj;
621 : : int i,
622 : : j,
623 : : k;
624 : :
625 : : /*
626 : : * This is basically the same algorithm shown for topological sorting in
627 : : * Knuth's Volume 1. However, we would like to minimize unnecessary
628 : : * rearrangement of the input ordering; that is, when we have a choice of
629 : : * which item to output next, we always want to take the one highest in
630 : : * the original list. Therefore, instead of maintaining an unordered
631 : : * linked list of items-ready-to-output as Knuth does, we maintain a heap
632 : : * of their item numbers, which we can use as a priority queue. This
633 : : * turns the algorithm from O(N) to O(N log N) because each insertion or
634 : : * removal of a heap item takes O(log N) time. However, that's still
635 : : * plenty fast enough for this application.
636 : : */
637 : :
7868 bruce@momjian.us 638 : 692 : *nOrdering = numObjs; /* for success return */
639 : :
640 : : /* Eliminate the null case */
8135 tgl@sss.pgh.pa.us 641 [ - + ]: 692 : if (numObjs <= 0)
8135 tgl@sss.pgh.pa.us 642 :UBC 0 : return true;
643 : :
644 : : /* Create workspace for the above-described heap */
703 msawada@postgresql.o 645 :CBC 692 : pendingHeap = binaryheap_allocate(numObjs, int_cmp, NULL);
646 : :
647 : : /*
648 : : * Scan the constraints, and for each item in the input, generate a count
649 : : * of the number of constraints that say it must be before something else.
650 : : * The count for the item with dumpId j is stored in beforeConstraints[j].
651 : : * We also make a map showing the input-order index of the item with
652 : : * dumpId j.
653 : : */
30 michael@paquier.xyz 654 :GNC 692 : beforeConstraints = pg_malloc0_array(int, (maxDumpId + 1));
655 : 692 : idMap = pg_malloc_array(int, (maxDumpId + 1));
8135 tgl@sss.pgh.pa.us 656 [ + + ]:CBC 3162662 : for (i = 0; i < numObjs; i++)
657 : : {
658 : 3161970 : obj = objs[i];
659 : 3161970 : j = obj->dumpId;
660 [ + - - + ]: 3161970 : if (j <= 0 || j > maxDumpId)
1437 tgl@sss.pgh.pa.us 661 :UBC 0 : pg_fatal("invalid dumpId %d", j);
8135 tgl@sss.pgh.pa.us 662 :CBC 3161970 : idMap[j] = i;
663 [ + + ]: 7868817 : for (j = 0; j < obj->nDeps; j++)
664 : : {
665 : 4706847 : k = obj->dependencies[j];
666 [ + - - + ]: 4706847 : if (k <= 0 || k > maxDumpId)
1437 tgl@sss.pgh.pa.us 667 :UBC 0 : pg_fatal("invalid dependency %d", k);
8135 tgl@sss.pgh.pa.us 668 :CBC 4706847 : beforeConstraints[k]++;
669 : : }
670 : : }
671 : :
672 : : /*
673 : : * Now initialize the heap of items-ready-to-output by filling it with the
674 : : * indexes of items that already have beforeConstraints[id] == 0.
675 : : *
676 : : * We enter the indexes into pendingHeap in decreasing order so that the
677 : : * heap invariant is satisfied at the completion of this loop. This
678 : : * reduces the amount of work that binaryheap_build() must do.
679 : : */
7868 bruce@momjian.us 680 [ + + ]: 3162662 : for (i = numObjs; --i >= 0;)
681 : : {
8135 tgl@sss.pgh.pa.us 682 [ + + ]: 3161970 : if (beforeConstraints[objs[i]->dumpId] == 0)
908 nathan@postgresql.or 683 : 32493 : binaryheap_add_unordered(pendingHeap, (void *) (intptr_t) i);
684 : : }
685 : 692 : binaryheap_build(pendingHeap);
686 : :
687 : : /*--------------------
688 : : * Now emit objects, working backwards in the output list. At each step,
689 : : * we use the priority heap to select the last item that has no remaining
690 : : * before-constraints. We remove that item from the heap, output it to
691 : : * ordering[], and decrease the beforeConstraints count of each of the
692 : : * items it was constrained against. Whenever an item's beforeConstraints
693 : : * count is thereby decreased to zero, we insert it into the priority heap
694 : : * to show that it is a candidate to output. We are done when the heap
695 : : * becomes empty; if we have output every element then we succeeded,
696 : : * otherwise we failed.
697 : : * i = number of ordering[] entries left to output
698 : : * j = objs[] index of item we are outputting
699 : : * k = temp for scanning constraint list for item j
700 : : *--------------------
701 : : */
8135 tgl@sss.pgh.pa.us 702 : 692 : i = numObjs;
908 nathan@postgresql.or 703 [ + + ]: 2263763 : while (!binaryheap_empty(pendingHeap))
704 : : {
705 : : /* Select object to output by removing largest heap member */
706 : 2263071 : j = (int) (intptr_t) binaryheap_remove_first(pendingHeap);
8135 tgl@sss.pgh.pa.us 707 : 2263071 : obj = objs[j];
708 : : /* Output candidate to ordering[] */
709 : 2263071 : ordering[--i] = obj;
710 : : /* Update beforeConstraints counts of its predecessors */
711 [ + + ]: 5385963 : for (k = 0; k < obj->nDeps; k++)
712 : : {
7868 bruce@momjian.us 713 : 3122892 : int id = obj->dependencies[k];
714 : :
8135 tgl@sss.pgh.pa.us 715 [ + + ]: 3122892 : if ((--beforeConstraints[id]) == 0)
908 nathan@postgresql.or 716 : 2230578 : binaryheap_add(pendingHeap, (void *) (intptr_t) idMap[id]);
717 : : }
718 : : }
719 : :
720 : : /*
721 : : * If we failed, report the objects that couldn't be output; these are the
722 : : * ones with beforeConstraints[] still nonzero.
723 : : */
8135 tgl@sss.pgh.pa.us 724 [ + + ]: 692 : if (i != 0)
725 : : {
8134 726 : 432 : k = 0;
8135 727 [ + + ]: 2030243 : for (j = 1; j <= maxDumpId; j++)
728 : : {
729 [ + + ]: 2029811 : if (beforeConstraints[j] != 0)
8134 730 : 898899 : ordering[k++] = objs[idMap[j]];
731 : : }
732 : 432 : *nOrdering = k;
733 : : }
734 : :
735 : : /* Done */
908 nathan@postgresql.or 736 : 692 : binaryheap_free(pendingHeap);
8135 tgl@sss.pgh.pa.us 737 : 692 : free(beforeConstraints);
738 : 692 : free(idMap);
739 : :
740 : 692 : return (i == 0);
741 : : }
742 : :
743 : : /*
744 : : * findDependencyLoops - identify loops in TopoSort's failure output,
745 : : * and pass each such loop to repairDependencyLoop() for action
746 : : *
747 : : * In general there may be many loops in the set of objects returned by
748 : : * TopoSort; for speed we should try to repair as many loops as we can
749 : : * before trying TopoSort again. We can safely repair loops that are
750 : : * disjoint (have no members in common); if we find overlapping loops
751 : : * then we repair only the first one found, because the action taken to
752 : : * repair the first might have repaired the other as well. (If not,
753 : : * we'll fix it on the next go-round.)
754 : : *
755 : : * objs[] lists the objects TopoSort couldn't sort
756 : : * nObjs is the number of such objects
757 : : * totObjs is the total number of objects in the universe
758 : : */
759 : : static void
8134 760 : 432 : findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs)
761 : : {
762 : : /*
763 : : * We use three data structures here:
764 : : *
765 : : * processed[] is a bool array indexed by dump ID, marking the objects
766 : : * already processed during this invocation of findDependencyLoops().
767 : : *
768 : : * searchFailed[] is another array indexed by dump ID. searchFailed[j] is
769 : : * set to dump ID k if we have proven that there is no dependency path
770 : : * leading from object j back to start point k. This allows us to skip
771 : : * useless searching when there are multiple dependency paths from k to j,
772 : : * which is a common situation. We could use a simple bool array for
773 : : * this, but then we'd need to re-zero it for each start point, resulting
774 : : * in O(N^2) zeroing work. Using the start point's dump ID as the "true"
775 : : * value lets us skip clearing the array before we consider the next start
776 : : * point.
777 : : *
778 : : * workspace[] is an array of DumpableObject pointers, in which we try to
779 : : * build lists of objects constituting loops. We make workspace[] large
780 : : * enough to hold all the objects in TopoSort's output, which is huge
781 : : * overkill in most cases but could theoretically be necessary if there is
782 : : * a single dependency chain linking all the objects.
783 : : */
784 : : bool *processed;
785 : : DumpId *searchFailed;
786 : : DumpableObject **workspace;
787 : : bool fixedloop;
788 : : int i;
789 : :
30 michael@paquier.xyz 790 :GNC 432 : processed = pg_malloc0_array(bool, (getMaxDumpId() + 1));
791 : 432 : searchFailed = pg_malloc0_array(DumpId, (getMaxDumpId() + 1));
792 : 432 : workspace = pg_malloc_array(DumpableObject *, totObjs);
8134 tgl@sss.pgh.pa.us 793 :CBC 432 : fixedloop = false;
794 : :
795 [ + + ]: 899331 : for (i = 0; i < nObjs; i++)
796 : : {
797 : 898899 : DumpableObject *obj = objs[i];
798 : : int looplen;
799 : : int j;
800 : :
4251 801 : 898899 : looplen = findLoop(obj,
802 : : obj->dumpId,
803 : : processed,
804 : : searchFailed,
805 : : workspace,
806 : : 0);
807 : :
5097 808 [ + + ]: 898899 : if (looplen > 0)
809 : : {
810 : : /* Found a loop, repair it */
811 : 42624 : repairDependencyLoop(workspace, looplen);
8134 812 : 42624 : fixedloop = true;
813 : : /* Mark loop members as processed */
5097 814 [ + + ]: 127956 : for (j = 0; j < looplen; j++)
815 : 85332 : processed[workspace[j]->dumpId] = true;
816 : : }
817 : : else
818 : : {
819 : : /*
820 : : * There's no loop starting at this object, but mark it processed
821 : : * anyway. This is not necessary for correctness, but saves later
822 : : * invocations of findLoop() from uselessly chasing references to
823 : : * such an object.
824 : : */
825 : 856275 : processed[obj->dumpId] = true;
826 : : }
827 : : }
828 : :
829 : : /* We'd better have fixed at least one loop */
8134 830 [ - + ]: 432 : if (!fixedloop)
1437 tgl@sss.pgh.pa.us 831 :UBC 0 : pg_fatal("could not identify dependency loop");
832 : :
8134 tgl@sss.pgh.pa.us 833 :CBC 432 : free(workspace);
4251 834 : 432 : free(searchFailed);
5097 835 : 432 : free(processed);
8134 836 : 432 : }
837 : :
838 : : /*
839 : : * Recursively search for a circular dependency loop that doesn't include
840 : : * any already-processed objects.
841 : : *
842 : : * obj: object we are examining now
843 : : * startPoint: dumpId of starting object for the hoped-for circular loop
844 : : * processed[]: flag array marking already-processed objects
845 : : * searchFailed[]: flag array marking already-unsuccessfully-visited objects
846 : : * workspace[]: work array in which we are building list of loop members
847 : : * depth: number of valid entries in workspace[] at call
848 : : *
849 : : * On success, the length of the loop is returned, and workspace[] is filled
850 : : * with pointers to the members of the loop. On failure, we return 0.
851 : : *
852 : : * Note: it is possible that the given starting object is a member of more
853 : : * than one cycle; if so, we will find an arbitrary one of the cycles.
854 : : */
855 : : static int
8135 856 : 25083605 : findLoop(DumpableObject *obj,
857 : : DumpId startPoint,
858 : : bool *processed,
859 : : DumpId *searchFailed,
860 : : DumpableObject **workspace,
861 : : int depth)
862 : : {
863 : : int i;
864 : :
865 : : /*
866 : : * Reject if obj is already processed. This test prevents us from finding
867 : : * loops that overlap previously-processed loops.
868 : : */
5097 869 [ + + ]: 25083605 : if (processed[obj->dumpId])
870 : 23263507 : return 0;
871 : :
872 : : /*
873 : : * If we've already proven there is no path from this object back to the
874 : : * startPoint, forget it.
875 : : */
4251 876 [ + + ]: 1820098 : if (searchFailed[obj->dumpId] == startPoint)
877 : 163639 : return 0;
878 : :
879 : : /*
880 : : * Reject if obj is already present in workspace. This test prevents us
881 : : * from going into infinite recursion if we are given a startPoint object
882 : : * that links to a cycle it's not a member of, and it guarantees that we
883 : : * can't overflow the allocated size of workspace[].
884 : : */
8134 885 [ + + ]: 3227688 : for (i = 0; i < depth; i++)
886 : : {
887 [ + + ]: 1573663 : if (workspace[i] == obj)
5097 888 : 2434 : return 0;
889 : : }
890 : :
891 : : /*
892 : : * Okay, tentatively add obj to workspace
893 : : */
8134 894 : 1654025 : workspace[depth++] = obj;
895 : :
896 : : /*
897 : : * See if we've found a loop back to the desired startPoint; if so, done
898 : : */
899 [ + + ]: 26621205 : for (i = 0; i < obj->nDeps; i++)
900 : : {
901 [ + + ]: 25009804 : if (obj->dependencies[i] == startPoint)
5097 902 : 42624 : return depth;
903 : : }
904 : :
905 : : /*
906 : : * Recurse down each outgoing branch
907 : : */
8134 908 [ + + ]: 25753399 : for (i = 0; i < obj->nDeps; i++)
909 : : {
910 : 24184706 : DumpableObject *nextobj = findObjectByDumpId(obj->dependencies[i]);
911 : : int newDepth;
912 : :
8135 913 [ - + ]: 24184706 : if (!nextobj)
8135 tgl@sss.pgh.pa.us 914 :UBC 0 : continue; /* ignore dependencies on undumped objects */
5097 tgl@sss.pgh.pa.us 915 :CBC 24184706 : newDepth = findLoop(nextobj,
916 : : startPoint,
917 : : processed,
918 : : searchFailed,
919 : : workspace,
920 : : depth);
921 [ + + ]: 24184706 : if (newDepth > 0)
922 : 42708 : return newDepth;
923 : : }
924 : :
925 : : /*
926 : : * Remember there is no path from here back to startPoint
927 : : */
4251 928 : 1568693 : searchFailed[obj->dumpId] = startPoint;
929 : :
5097 930 : 1568693 : return 0;
931 : : }
932 : :
933 : : /*
934 : : * A user-defined datatype will have a dependency loop with each of its
935 : : * I/O functions (since those have the datatype as input or output).
936 : : * Similarly, a range type will have a loop with its canonicalize function,
937 : : * if any. Break the loop by making the function depend on the associated
938 : : * shell type, instead.
939 : : */
940 : : static void
8135 941 : 188 : repairTypeFuncLoop(DumpableObject *typeobj, DumpableObject *funcobj)
942 : : {
943 : 188 : TypeInfo *typeInfo = (TypeInfo *) typeobj;
944 : :
945 : : /* remove function's dependency on type */
946 : 188 : removeObjectDependency(funcobj, typeobj->dumpId);
947 : :
948 : : /* add function's dependency on shell type, instead */
7318 949 [ + + ]: 188 : if (typeInfo->shellType)
950 : : {
951 : 146 : addObjectDependency(funcobj, typeInfo->shellType->dobj.dumpId);
952 : :
953 : : /*
954 : : * Mark shell type (always including the definition, as we need the
955 : : * shell type defined to identify the function fully) as to be dumped
956 : : * if any such function is
957 : : */
958 [ + - ]: 146 : if (funcobj->dump)
3628 sfrost@snowman.net 959 : 146 : typeInfo->shellType->dobj.dump = funcobj->dump |
960 : : DUMP_COMPONENT_DEFINITION;
961 : : }
8135 tgl@sss.pgh.pa.us 962 : 188 : }
963 : :
964 : : /*
965 : : * Because we force a view to depend on its ON SELECT rule, while there
966 : : * will be an implicit dependency in the other direction, we need to break
967 : : * the loop. If there are no other objects in the loop then we can remove
968 : : * the implicit dependency and leave the ON SELECT rule non-separate.
969 : : * This applies to matviews, as well.
970 : : */
971 : : static void
972 : 39612 : repairViewRuleLoop(DumpableObject *viewobj,
973 : : DumpableObject *ruleobj)
974 : : {
975 : : /* remove rule's dependency on view */
976 : 39612 : removeObjectDependency(ruleobj, viewobj->dumpId);
977 : : /* flags on the two objects are already set correctly for this case */
978 : 39612 : }
979 : :
980 : : /*
981 : : * However, if there are other objects in the loop, we must break the loop
982 : : * by making the ON SELECT rule a separately-dumped object.
983 : : *
984 : : * Because findLoop() finds shorter cycles before longer ones, it's likely
985 : : * that we will have previously fired repairViewRuleLoop() and removed the
986 : : * rule's dependency on the view. Put it back to ensure the rule won't be
987 : : * emitted before the view.
988 : : *
989 : : * Note: this approach does *not* work for matviews, at the moment.
990 : : */
991 : : static void
7761 992 : 10 : repairViewRuleMultiLoop(DumpableObject *viewobj,
993 : : DumpableObject *ruleobj)
994 : : {
4954 995 : 10 : TableInfo *viewinfo = (TableInfo *) viewobj;
996 : 10 : RuleInfo *ruleinfo = (RuleInfo *) ruleobj;
997 : :
998 : : /* remove view's dependency on rule */
7761 999 : 10 : removeObjectDependency(viewobj, ruleobj->dumpId);
1000 : : /* mark view to be printed with a dummy definition */
3405 1001 : 10 : viewinfo->dummy_view = true;
1002 : : /* mark rule as needing its own dump */
4954 1003 : 10 : ruleinfo->separate = true;
1004 : : /* put back rule's dependency on view */
7761 1005 : 10 : addObjectDependency(ruleobj, viewobj->dumpId);
1006 : : /* now that rule is separate, it must be post-data */
5011 1007 : 10 : addObjectDependency(ruleobj, postDataBoundId);
7761 1008 : 10 : }
1009 : :
1010 : : /*
1011 : : * If a matview is involved in a multi-object loop, we can't currently fix
1012 : : * that by splitting off the rule. As a stopgap, we try to fix it by
1013 : : * dropping the constraint that the matview be dumped in the pre-data section.
1014 : : * This is sufficient to handle cases where a matview depends on some unique
1015 : : * index, as can happen if it has a GROUP BY for example.
1016 : : *
1017 : : * Note that the "next object" is not necessarily the matview itself;
1018 : : * it could be the matview's rowtype, for example. We may come through here
1019 : : * several times while removing all the pre-data linkages. In particular,
1020 : : * if there are other matviews that depend on the one with the circularity
1021 : : * problem, we'll come through here for each such matview and mark them all
1022 : : * as postponed. (This works because all MVs have pre-data dependencies
1023 : : * to begin with, so each of them will get visited.)
1024 : : */
1025 : : static void
2596 1026 : 117 : repairMatViewBoundaryMultiLoop(DumpableObject *boundaryobj,
1027 : : DumpableObject *nextobj)
1028 : : {
1029 : : /* remove boundary's dependency on object after it in loop */
4369 1030 : 117 : removeObjectDependency(boundaryobj, nextobj->dumpId);
1031 : :
1032 : : /*
1033 : : * If that object is a matview or matview stats, mark it as postponed into
1034 : : * post-data.
1035 : : */
2596 1036 [ + + ]: 117 : if (nextobj->objType == DO_TABLE)
1037 : : {
1038 : 38 : TableInfo *nextinfo = (TableInfo *) nextobj;
1039 : :
388 jdavis@postgresql.or 1040 [ + - ]: 38 : if (nextinfo->relkind == RELKIND_MATVIEW)
1041 : 38 : nextinfo->postponed_def = true;
1042 : : }
1043 [ + + ]: 79 : else if (nextobj->objType == DO_REL_STATS)
1044 : : {
1045 : 3 : RelStatsInfo *nextinfo = (RelStatsInfo *) nextobj;
1046 : :
2596 tgl@sss.pgh.pa.us 1047 [ + - ]: 3 : if (nextinfo->relkind == RELKIND_MATVIEW)
352 jdavis@postgresql.or 1048 : 3 : nextinfo->section = SECTION_POST_DATA;
1049 : : }
4369 tgl@sss.pgh.pa.us 1050 : 117 : }
1051 : :
1052 : : /*
1053 : : * If a function is involved in a multi-object loop, we can't currently fix
1054 : : * that by splitting it into two DumpableObjects. As a stopgap, we try to fix
1055 : : * it by dropping the constraint that the function be dumped in the pre-data
1056 : : * section. This is sufficient to handle cases where a function depends on
1057 : : * some unique index, as can happen if it has a GROUP BY for example.
1058 : : */
1059 : : static void
1015 1060 : 38 : repairFunctionBoundaryMultiLoop(DumpableObject *boundaryobj,
1061 : : DumpableObject *nextobj)
1062 : : {
1063 : : /* remove boundary's dependency on object after it in loop */
1064 : 38 : removeObjectDependency(boundaryobj, nextobj->dumpId);
1065 : : /* if that object is a function, mark it as postponed into post-data */
1066 [ + - ]: 38 : if (nextobj->objType == DO_FUNC)
1067 : : {
1068 : 38 : FuncInfo *nextinfo = (FuncInfo *) nextobj;
1069 : :
1070 : 38 : nextinfo->postponed_def = true;
1071 : : }
1072 : 38 : }
1073 : :
1074 : : /*
1075 : : * Because we make tables depend on their CHECK constraints, while there
1076 : : * will be an automatic dependency in the other direction, we need to break
1077 : : * the loop. If there are no other objects in the loop then we can remove
1078 : : * the automatic dependency and leave the CHECK constraint non-separate.
1079 : : */
1080 : : static void
8135 1081 : 547 : repairTableConstraintLoop(DumpableObject *tableobj,
1082 : : DumpableObject *constraintobj)
1083 : : {
1084 : : /* remove constraint's dependency on table */
1085 : 547 : removeObjectDependency(constraintobj, tableobj->dumpId);
1086 : 547 : }
1087 : :
1088 : : /*
1089 : : * However, if there are other objects in the loop, we must break the loop
1090 : : * by making the CHECK constraint a separately-dumped object.
1091 : : *
1092 : : * Because findLoop() finds shorter cycles before longer ones, it's likely
1093 : : * that we will have previously fired repairTableConstraintLoop() and
1094 : : * removed the constraint's dependency on the table. Put it back to ensure
1095 : : * the constraint won't be emitted before the table...
1096 : : */
1097 : : static void
1098 : 5 : repairTableConstraintMultiLoop(DumpableObject *tableobj,
1099 : : DumpableObject *constraintobj)
1100 : : {
1101 : : /* remove table's dependency on constraint */
1102 : 5 : removeObjectDependency(tableobj, constraintobj->dumpId);
1103 : : /* mark constraint as needing its own dump */
1104 : 5 : ((ConstraintInfo *) constraintobj)->separate = true;
1105 : : /* put back constraint's dependency on table */
1106 : 5 : addObjectDependency(constraintobj, tableobj->dumpId);
1107 : : /* now that constraint is separate, it must be post-data */
5011 1108 : 5 : addObjectDependency(constraintobj, postDataBoundId);
8135 1109 : 5 : }
1110 : :
1111 : : /*
1112 : : * Attribute defaults behave exactly the same as CHECK constraints...
1113 : : */
1114 : : static void
1115 : 987 : repairTableAttrDefLoop(DumpableObject *tableobj,
1116 : : DumpableObject *attrdefobj)
1117 : : {
1118 : : /* remove attrdef's dependency on table */
1119 : 987 : removeObjectDependency(attrdefobj, tableobj->dumpId);
1120 : 987 : }
1121 : :
1122 : : static void
1123 : 152 : repairTableAttrDefMultiLoop(DumpableObject *tableobj,
1124 : : DumpableObject *attrdefobj)
1125 : : {
1126 : : /* remove table's dependency on attrdef */
1127 : 152 : removeObjectDependency(tableobj, attrdefobj->dumpId);
1128 : : /* mark attrdef as needing its own dump */
1129 : 152 : ((AttrDefInfo *) attrdefobj)->separate = true;
1130 : : /* put back attrdef's dependency on table */
1131 : 152 : addObjectDependency(attrdefobj, tableobj->dumpId);
1132 : 152 : }
1133 : :
1134 : : /*
1135 : : * CHECK, NOT NULL constraints on domains work just like those on tables ...
1136 : : */
1137 : : static void
1138 : 161 : repairDomainConstraintLoop(DumpableObject *domainobj,
1139 : : DumpableObject *constraintobj)
1140 : : {
1141 : : /* remove constraint's dependency on domain */
1142 : 161 : removeObjectDependency(constraintobj, domainobj->dumpId);
1143 : 161 : }
1144 : :
1145 : : static void
8135 tgl@sss.pgh.pa.us 1146 :UBC 0 : repairDomainConstraintMultiLoop(DumpableObject *domainobj,
1147 : : DumpableObject *constraintobj)
1148 : : {
1149 : : /* remove domain's dependency on constraint */
1150 : 0 : removeObjectDependency(domainobj, constraintobj->dumpId);
1151 : : /* mark constraint as needing its own dump */
1152 : 0 : ((ConstraintInfo *) constraintobj)->separate = true;
1153 : : /* put back constraint's dependency on domain */
1154 : 0 : addObjectDependency(constraintobj, domainobj->dumpId);
1155 : : /* now that constraint is separate, it must be post-data */
5011 1156 : 0 : addObjectDependency(constraintobj, postDataBoundId);
8135 1157 : 0 : }
1158 : :
1159 : : static void
2977 alvherre@alvh.no-ip. 1160 : 0 : repairIndexLoop(DumpableObject *partedindex,
1161 : : DumpableObject *partindex)
1162 : : {
1163 : 0 : removeObjectDependency(partedindex, partindex->dumpId);
1164 : 0 : }
1165 : :
1166 : : /*
1167 : : * Fix a dependency loop, or die trying ...
1168 : : *
1169 : : * This routine is mainly concerned with reducing the multiple ways that
1170 : : * a loop might appear to common cases, which it passes off to the
1171 : : * "fixer" routines above.
1172 : : */
1173 : : static void
8135 tgl@sss.pgh.pa.us 1174 :CBC 42624 : repairDependencyLoop(DumpableObject **loop,
1175 : : int nLoop)
1176 : : {
1177 : : int i,
1178 : : j;
1179 : :
1180 : : /* Datatype and one of its I/O or canonicalize functions */
1181 [ + + ]: 42624 : if (nLoop == 2 &&
1182 [ + + ]: 41495 : loop[0]->objType == DO_TYPE &&
1183 [ - + ]: 161 : loop[1]->objType == DO_FUNC)
1184 : : {
8135 tgl@sss.pgh.pa.us 1185 :UBC 0 : repairTypeFuncLoop(loop[0], loop[1]);
1186 : 0 : return;
1187 : : }
8135 tgl@sss.pgh.pa.us 1188 [ + + ]:CBC 42624 : if (nLoop == 2 &&
1189 [ + + ]: 41495 : loop[1]->objType == DO_TYPE &&
1190 [ + - ]: 188 : loop[0]->objType == DO_FUNC)
1191 : : {
1192 : 188 : repairTypeFuncLoop(loop[1], loop[0]);
1193 : 188 : return;
1194 : : }
1195 : :
1196 : : /* View (including matview) and its ON SELECT rule */
1197 [ + + ]: 42436 : if (nLoop == 2 &&
1198 [ + + ]: 41307 : loop[0]->objType == DO_TABLE &&
1199 [ + + ]: 41146 : loop[1]->objType == DO_RULE &&
3293 1200 [ + + ]: 39612 : (((TableInfo *) loop[0])->relkind == RELKIND_VIEW ||
1201 [ + - ]: 471 : ((TableInfo *) loop[0])->relkind == RELKIND_MATVIEW) &&
8135 1202 [ + - ]: 39612 : ((RuleInfo *) loop[1])->ev_type == '1' &&
7761 1203 [ + - ]: 39612 : ((RuleInfo *) loop[1])->is_instead &&
1204 [ + - ]: 39612 : ((RuleInfo *) loop[1])->ruletable == (TableInfo *) loop[0])
1205 : : {
8135 1206 : 39612 : repairViewRuleLoop(loop[0], loop[1]);
1207 : 39612 : return;
1208 : : }
1209 [ + + ]: 2824 : if (nLoop == 2 &&
1210 [ - + ]: 1695 : loop[1]->objType == DO_TABLE &&
8135 tgl@sss.pgh.pa.us 1211 [ # # ]:UBC 0 : loop[0]->objType == DO_RULE &&
3293 1212 [ # # ]: 0 : (((TableInfo *) loop[1])->relkind == RELKIND_VIEW ||
1213 [ # # ]: 0 : ((TableInfo *) loop[1])->relkind == RELKIND_MATVIEW) &&
8135 1214 [ # # ]: 0 : ((RuleInfo *) loop[0])->ev_type == '1' &&
7761 1215 [ # # ]: 0 : ((RuleInfo *) loop[0])->is_instead &&
1216 [ # # ]: 0 : ((RuleInfo *) loop[0])->ruletable == (TableInfo *) loop[1])
1217 : : {
8135 1218 : 0 : repairViewRuleLoop(loop[1], loop[0]);
1219 : 0 : return;
1220 : : }
1221 : :
1222 : : /* Indirect loop involving view (but not matview) and ON SELECT rule */
7761 tgl@sss.pgh.pa.us 1223 [ + + ]:CBC 2824 : if (nLoop > 2)
1224 : : {
1225 [ + + ]: 1801 : for (i = 0; i < nLoop; i++)
1226 : : {
4369 1227 [ + + ]: 1489 : if (loop[i]->objType == DO_TABLE &&
3293 1228 [ + + ]: 436 : ((TableInfo *) loop[i])->relkind == RELKIND_VIEW)
1229 : : {
7761 1230 [ + - ]: 24 : for (j = 0; j < nLoop; j++)
1231 : : {
1232 [ + + ]: 24 : if (loop[j]->objType == DO_RULE &&
1233 [ + - ]: 10 : ((RuleInfo *) loop[j])->ev_type == '1' &&
1234 [ + - ]: 10 : ((RuleInfo *) loop[j])->is_instead &&
1235 [ + - ]: 10 : ((RuleInfo *) loop[j])->ruletable == (TableInfo *) loop[i])
1236 : : {
1237 : 10 : repairViewRuleMultiLoop(loop[i], loop[j]);
1238 : 10 : return;
1239 : : }
1240 : : }
1241 : : }
1242 : : }
1243 : : }
1244 : :
1245 : : /* Indirect loop involving matview and data boundary */
4369 1246 [ + + ]: 2814 : if (nLoop > 2)
1247 : : {
1248 [ + + ]: 1277 : for (i = 0; i < nLoop; i++)
1249 : : {
1250 [ + + ]: 1082 : if (loop[i]->objType == DO_TABLE &&
3293 1251 [ + + ]: 426 : ((TableInfo *) loop[i])->relkind == RELKIND_MATVIEW)
1252 : : {
4369 1253 [ + + ]: 315 : for (j = 0; j < nLoop; j++)
1254 : : {
1255 [ + + ]: 312 : if (loop[j]->objType == DO_PRE_DATA_BOUNDARY)
1256 : : {
1257 : : DumpableObject *nextobj;
1258 : :
388 jdavis@postgresql.or 1259 [ + + ]: 114 : nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
1260 : 114 : repairMatViewBoundaryMultiLoop(loop[j], nextobj);
1261 : 114 : return;
1262 : : }
1263 : : }
1264 : : }
1265 [ + + ]: 965 : else if (loop[i]->objType == DO_REL_STATS &&
1266 [ + + ]: 131 : ((RelStatsInfo *) loop[i])->relkind == RELKIND_MATVIEW)
1267 : : {
1268 [ + - ]: 12 : for (j = 0; j < nLoop; j++)
1269 : : {
1270 [ + + ]: 12 : if (loop[j]->objType == DO_POST_DATA_BOUNDARY)
1271 : : {
1272 : : DumpableObject *nextobj;
1273 : :
4369 tgl@sss.pgh.pa.us 1274 [ + - ]: 3 : nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
2596 1275 : 3 : repairMatViewBoundaryMultiLoop(loop[j], nextobj);
4369 1276 : 3 : return;
1277 : : }
1278 : : }
1279 : : }
1280 : : }
1281 : : }
1282 : :
1283 : : /* Indirect loop involving function and data boundary */
1015 1284 [ + + ]: 2697 : if (nLoop > 2)
1285 : : {
1286 [ + + ]: 735 : for (i = 0; i < nLoop; i++)
1287 : : {
1288 [ + + ]: 578 : if (loop[i]->objType == DO_FUNC)
1289 : : {
1290 [ + + ]: 118 : for (j = 0; j < nLoop; j++)
1291 : : {
1292 [ + + ]: 113 : if (loop[j]->objType == DO_PRE_DATA_BOUNDARY)
1293 : : {
1294 : : DumpableObject *nextobj;
1295 : :
1296 [ + + ]: 38 : nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
1297 : 38 : repairFunctionBoundaryMultiLoop(loop[j], nextobj);
1298 : 38 : return;
1299 : : }
1300 : : }
1301 : : }
1302 : : }
1303 : : }
1304 : :
1305 : : /* Table and CHECK constraint */
8135 1306 [ + + ]: 2659 : if (nLoop == 2 &&
1307 [ + + ]: 1695 : loop[0]->objType == DO_TABLE &&
1308 [ + + ]: 1534 : loop[1]->objType == DO_CONSTRAINT &&
1309 [ + - ]: 547 : ((ConstraintInfo *) loop[1])->contype == 'c' &&
1310 [ + - ]: 547 : ((ConstraintInfo *) loop[1])->contable == (TableInfo *) loop[0])
1311 : : {
1312 : 547 : repairTableConstraintLoop(loop[0], loop[1]);
1313 : 547 : return;
1314 : : }
1315 [ + + ]: 2112 : if (nLoop == 2 &&
1316 [ - + ]: 1148 : loop[1]->objType == DO_TABLE &&
8135 tgl@sss.pgh.pa.us 1317 [ # # ]:UBC 0 : loop[0]->objType == DO_CONSTRAINT &&
1318 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->contype == 'c' &&
1319 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->contable == (TableInfo *) loop[1])
1320 : : {
1321 : 0 : repairTableConstraintLoop(loop[1], loop[0]);
1322 : 0 : return;
1323 : : }
1324 : :
1325 : : /* Indirect loop involving table and CHECK constraint */
8135 tgl@sss.pgh.pa.us 1326 [ + + ]:CBC 2112 : if (nLoop > 2)
1327 : : {
1328 [ + + ]: 613 : for (i = 0; i < nLoop; i++)
1329 : : {
1330 [ + + ]: 461 : if (loop[i]->objType == DO_TABLE)
1331 : : {
1332 [ + + ]: 1226 : for (j = 0; j < nLoop; j++)
1333 : : {
1334 [ + + ]: 922 : if (loop[j]->objType == DO_CONSTRAINT &&
1335 [ + - ]: 5 : ((ConstraintInfo *) loop[j])->contype == 'c' &&
1336 [ + - ]: 5 : ((ConstraintInfo *) loop[j])->contable == (TableInfo *) loop[i])
1337 : : {
1338 : 5 : repairTableConstraintMultiLoop(loop[i], loop[j]);
1339 : 5 : return;
1340 : : }
1341 : : }
1342 : : }
1343 : : }
1344 : : }
1345 : :
1346 : : /* Table and attribute default */
1347 [ + + ]: 2107 : if (nLoop == 2 &&
1348 [ + + ]: 1148 : loop[0]->objType == DO_TABLE &&
1349 [ + - ]: 987 : loop[1]->objType == DO_ATTRDEF &&
1350 [ + - ]: 987 : ((AttrDefInfo *) loop[1])->adtable == (TableInfo *) loop[0])
1351 : : {
1352 : 987 : repairTableAttrDefLoop(loop[0], loop[1]);
1353 : 987 : return;
1354 : : }
1355 [ + + ]: 1120 : if (nLoop == 2 &&
1356 [ - + ]: 161 : loop[1]->objType == DO_TABLE &&
8135 tgl@sss.pgh.pa.us 1357 [ # # ]:UBC 0 : loop[0]->objType == DO_ATTRDEF &&
1358 [ # # ]: 0 : ((AttrDefInfo *) loop[0])->adtable == (TableInfo *) loop[1])
1359 : : {
1360 : 0 : repairTableAttrDefLoop(loop[1], loop[0]);
1361 : 0 : return;
1362 : : }
1363 : :
1364 : : /* index on partitioned table and corresponding index on partition */
2977 alvherre@alvh.no-ip. 1365 [ + + ]:CBC 1120 : if (nLoop == 2 &&
1366 [ - + ]: 161 : loop[0]->objType == DO_INDEX &&
2977 alvherre@alvh.no-ip. 1367 [ # # ]:UBC 0 : loop[1]->objType == DO_INDEX)
1368 : : {
1369 [ # # ]: 0 : if (((IndxInfo *) loop[0])->parentidx == loop[1]->catId.oid)
1370 : : {
1371 : 0 : repairIndexLoop(loop[0], loop[1]);
1372 : 0 : return;
1373 : : }
1374 [ # # ]: 0 : else if (((IndxInfo *) loop[1])->parentidx == loop[0]->catId.oid)
1375 : : {
1376 : 0 : repairIndexLoop(loop[1], loop[0]);
1377 : 0 : return;
1378 : : }
1379 : : }
1380 : :
1381 : : /* Indirect loop involving table and attribute default */
8135 tgl@sss.pgh.pa.us 1382 [ + + ]:CBC 1120 : if (nLoop > 2)
1383 : : {
1384 [ + - ]: 304 : for (i = 0; i < nLoop; i++)
1385 : : {
1386 [ + - ]: 304 : if (loop[i]->objType == DO_TABLE)
1387 : : {
1388 [ + + ]: 1064 : for (j = 0; j < nLoop; j++)
1389 : : {
1390 [ + + ]: 912 : if (loop[j]->objType == DO_ATTRDEF &&
1391 [ + + ]: 304 : ((AttrDefInfo *) loop[j])->adtable == (TableInfo *) loop[i])
1392 : : {
1393 : 152 : repairTableAttrDefMultiLoop(loop[i], loop[j]);
1394 : 152 : return;
1395 : : }
1396 : : }
1397 : : }
1398 : : }
1399 : : }
1400 : :
1401 : : /* Domain and CHECK or NOT NULL constraint */
1402 [ + + ]: 968 : if (nLoop == 2 &&
1403 [ + - ]: 161 : loop[0]->objType == DO_TYPE &&
1404 [ + - ]: 161 : loop[1]->objType == DO_CONSTRAINT &&
237 alvherre@kurilemu.de 1405 [ + + ]: 161 : (((ConstraintInfo *) loop[1])->contype == 'c' ||
1406 [ + - ]: 53 : ((ConstraintInfo *) loop[1])->contype == 'n') &&
8135 tgl@sss.pgh.pa.us 1407 [ + - ]: 161 : ((ConstraintInfo *) loop[1])->condomain == (TypeInfo *) loop[0])
1408 : : {
1409 : 161 : repairDomainConstraintLoop(loop[0], loop[1]);
1410 : 161 : return;
1411 : : }
1412 [ - + ]: 807 : if (nLoop == 2 &&
8135 tgl@sss.pgh.pa.us 1413 [ # # ]:UBC 0 : loop[1]->objType == DO_TYPE &&
1414 [ # # ]: 0 : loop[0]->objType == DO_CONSTRAINT &&
237 alvherre@kurilemu.de 1415 [ # # ]: 0 : (((ConstraintInfo *) loop[0])->contype == 'c' ||
1416 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->contype == 'n') &&
8135 tgl@sss.pgh.pa.us 1417 [ # # ]: 0 : ((ConstraintInfo *) loop[0])->condomain == (TypeInfo *) loop[1])
1418 : : {
1419 : 0 : repairDomainConstraintLoop(loop[1], loop[0]);
1420 : 0 : return;
1421 : : }
1422 : :
1423 : : /* Indirect loop involving domain and CHECK or NOT NULL constraint */
8135 tgl@sss.pgh.pa.us 1424 [ - + ]:CBC 807 : if (nLoop > 2)
1425 : : {
8135 tgl@sss.pgh.pa.us 1426 [ # # ]:UBC 0 : for (i = 0; i < nLoop; i++)
1427 : : {
1428 [ # # ]: 0 : if (loop[i]->objType == DO_TYPE)
1429 : : {
1430 [ # # ]: 0 : for (j = 0; j < nLoop; j++)
1431 : : {
1432 [ # # ]: 0 : if (loop[j]->objType == DO_CONSTRAINT &&
237 alvherre@kurilemu.de 1433 [ # # ]: 0 : (((ConstraintInfo *) loop[j])->contype == 'c' ||
1434 [ # # ]: 0 : ((ConstraintInfo *) loop[j])->contype == 'n') &&
8135 tgl@sss.pgh.pa.us 1435 [ # # ]: 0 : ((ConstraintInfo *) loop[j])->condomain == (TypeInfo *) loop[i])
1436 : : {
1437 : 0 : repairDomainConstraintMultiLoop(loop[i], loop[j]);
1438 : 0 : return;
1439 : : }
1440 : : }
1441 : : }
1442 : : }
1443 : : }
1444 : :
1445 : : /*
1446 : : * Loop of table with itself --- just ignore it.
1447 : : *
1448 : : * (Actually, what this arises from is a dependency of a table column on
1449 : : * another column, which happened with generated columns before v15; or a
1450 : : * dependency of a table column on the whole table, which happens with
1451 : : * partitioning. But we didn't pay attention to sub-object IDs while
1452 : : * collecting the dependency data, so we can't see that here.)
1453 : : */
2542 peter@eisentraut.org 1454 [ + - ]:CBC 807 : if (nLoop == 1)
1455 : : {
1456 [ + - ]: 807 : if (loop[0]->objType == DO_TABLE)
1457 : : {
1458 : 807 : removeObjectDependency(loop[0], loop[0]->dumpId);
1459 : 807 : return;
1460 : : }
1461 : : }
1462 : :
1463 : : /*
1464 : : * If all the objects are TABLE_DATA items, what we must have is a
1465 : : * circular set of foreign key constraints (or a single self-referential
1466 : : * table). Print an appropriate complaint and break the loop arbitrarily.
1467 : : */
6397 tgl@sss.pgh.pa.us 1468 [ # # ]:UBC 0 : for (i = 0; i < nLoop; i++)
1469 : : {
1470 [ # # ]: 0 : if (loop[i]->objType != DO_TABLE_DATA)
1471 : 0 : break;
1472 : : }
1473 [ # # ]: 0 : if (i >= nLoop)
1474 : : {
2540 peter@eisentraut.org 1475 : 0 : pg_log_warning(ngettext("there are circular foreign-key constraints on this table:",
1476 : : "there are circular foreign-key constraints among these tables:",
1477 : : nLoop));
6397 tgl@sss.pgh.pa.us 1478 [ # # ]: 0 : for (i = 0; i < nLoop; i++)
1015 1479 : 0 : pg_log_warning_detail("%s", loop[i]->name);
1480 : 0 : pg_log_warning_hint("You might not be able to restore the dump without using --disable-triggers or temporarily dropping the constraints.");
1481 : 0 : pg_log_warning_hint("Consider using a full dump instead of a --data-only dump to avoid this problem.");
6397 1482 [ # # ]: 0 : if (nLoop > 1)
1483 : 0 : removeObjectDependency(loop[0], loop[1]->dumpId);
1484 : : else /* must be a self-dependency */
1485 : 0 : removeObjectDependency(loop[0], loop[0]->dumpId);
1486 : 0 : return;
1487 : : }
1488 : :
1489 : : /*
1490 : : * If we can't find a principled way to break the loop, complain and break
1491 : : * it in an arbitrary fashion.
1492 : : */
2540 peter@eisentraut.org 1493 : 0 : pg_log_warning("could not resolve dependency loop among these items:");
8135 tgl@sss.pgh.pa.us 1494 [ # # ]: 0 : for (i = 0; i < nLoop; i++)
1495 : : {
1496 : : char buf[1024];
1497 : :
1498 : 0 : describeDumpableObject(loop[i], buf, sizeof(buf));
1015 1499 : 0 : pg_log_warning_detail("%s", buf);
1500 : : }
1501 : :
6397 1502 [ # # ]: 0 : if (nLoop > 1)
1503 : 0 : removeObjectDependency(loop[0], loop[1]->dumpId);
1504 : : else /* must be a self-dependency */
1505 : 0 : removeObjectDependency(loop[0], loop[0]->dumpId);
1506 : : }
1507 : :
1508 : : /*
1509 : : * Describe a dumpable object usefully for errors
1510 : : *
1511 : : * This should probably go somewhere else...
1512 : : */
1513 : : static void
8135 1514 : 0 : describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
1515 : : {
1516 [ # # # # : 0 : switch (obj->objType)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
1517 : : {
1518 : 0 : case DO_NAMESPACE:
1519 : 0 : snprintf(buf, bufsize,
1520 : : "SCHEMA %s (ID %d OID %u)",
1521 : : obj->name, obj->dumpId, obj->catId.oid);
1522 : 0 : return;
5514 1523 : 0 : case DO_EXTENSION:
1524 : 0 : snprintf(buf, bufsize,
1525 : : "EXTENSION %s (ID %d OID %u)",
1526 : : obj->name, obj->dumpId, obj->catId.oid);
1527 : 0 : return;
8135 1528 : 0 : case DO_TYPE:
1529 : 0 : snprintf(buf, bufsize,
1530 : : "TYPE %s (ID %d OID %u)",
1531 : : obj->name, obj->dumpId, obj->catId.oid);
1532 : 0 : return;
7318 1533 : 0 : case DO_SHELL_TYPE:
1534 : 0 : snprintf(buf, bufsize,
1535 : : "SHELL TYPE %s (ID %d OID %u)",
1536 : : obj->name, obj->dumpId, obj->catId.oid);
1537 : 0 : return;
8135 1538 : 0 : case DO_FUNC:
1539 : 0 : snprintf(buf, bufsize,
1540 : : "FUNCTION %s (ID %d OID %u)",
1541 : : obj->name, obj->dumpId, obj->catId.oid);
1542 : 0 : return;
1543 : 0 : case DO_AGG:
1544 : 0 : snprintf(buf, bufsize,
1545 : : "AGGREGATE %s (ID %d OID %u)",
1546 : : obj->name, obj->dumpId, obj->catId.oid);
1547 : 0 : return;
1548 : 0 : case DO_OPERATOR:
1549 : 0 : snprintf(buf, bufsize,
1550 : : "OPERATOR %s (ID %d OID %u)",
1551 : : obj->name, obj->dumpId, obj->catId.oid);
1552 : 0 : return;
3644 alvherre@alvh.no-ip. 1553 : 0 : case DO_ACCESS_METHOD:
1554 : 0 : snprintf(buf, bufsize,
1555 : : "ACCESS METHOD %s (ID %d OID %u)",
1556 : : obj->name, obj->dumpId, obj->catId.oid);
1557 : 0 : return;
8135 tgl@sss.pgh.pa.us 1558 : 0 : case DO_OPCLASS:
1559 : 0 : snprintf(buf, bufsize,
1560 : : "OPERATOR CLASS %s (ID %d OID %u)",
1561 : : obj->name, obj->dumpId, obj->catId.oid);
1562 : 0 : return;
6991 1563 : 0 : case DO_OPFAMILY:
1564 : 0 : snprintf(buf, bufsize,
1565 : : "OPERATOR FAMILY %s (ID %d OID %u)",
1566 : : obj->name, obj->dumpId, obj->catId.oid);
1567 : 0 : return;
5510 peter_e@gmx.net 1568 : 0 : case DO_COLLATION:
1569 : 0 : snprintf(buf, bufsize,
1570 : : "COLLATION %s (ID %d OID %u)",
1571 : : obj->name, obj->dumpId, obj->catId.oid);
1572 : 0 : return;
8135 tgl@sss.pgh.pa.us 1573 : 0 : case DO_CONVERSION:
1574 : 0 : snprintf(buf, bufsize,
1575 : : "CONVERSION %s (ID %d OID %u)",
1576 : : obj->name, obj->dumpId, obj->catId.oid);
1577 : 0 : return;
1578 : 0 : case DO_TABLE:
1579 : 0 : snprintf(buf, bufsize,
1580 : : "TABLE %s (ID %d OID %u)",
1581 : : obj->name, obj->dumpId, obj->catId.oid);
1582 : 0 : return;
1889 1583 : 0 : case DO_TABLE_ATTACH:
1584 : 0 : snprintf(buf, bufsize,
1585 : : "TABLE ATTACH %s (ID %d)",
1586 : : obj->name, obj->dumpId);
1587 : 0 : return;
8135 1588 : 0 : case DO_ATTRDEF:
1589 : 0 : snprintf(buf, bufsize,
1590 : : "ATTRDEF %s.%s (ID %d OID %u)",
8047 1591 : 0 : ((AttrDefInfo *) obj)->adtable->dobj.name,
8135 1592 : 0 : ((AttrDefInfo *) obj)->adtable->attnames[((AttrDefInfo *) obj)->adnum - 1],
1593 : : obj->dumpId, obj->catId.oid);
1594 : 0 : return;
1595 : 0 : case DO_INDEX:
1596 : 0 : snprintf(buf, bufsize,
1597 : : "INDEX %s (ID %d OID %u)",
1598 : : obj->name, obj->dumpId, obj->catId.oid);
4760 kgrittn@postgresql.o 1599 : 0 : return;
2977 alvherre@alvh.no-ip. 1600 : 0 : case DO_INDEX_ATTACH:
1601 : 0 : snprintf(buf, bufsize,
1602 : : "INDEX ATTACH %s (ID %d)",
1603 : : obj->name, obj->dumpId);
1604 : 0 : return;
3278 1605 : 0 : case DO_STATSEXT:
1606 : 0 : snprintf(buf, bufsize,
1607 : : "STATISTICS %s (ID %d OID %u)",
1608 : : obj->name, obj->dumpId, obj->catId.oid);
1609 : 0 : return;
4760 kgrittn@postgresql.o 1610 : 0 : case DO_REFRESH_MATVIEW:
1611 : 0 : snprintf(buf, bufsize,
1612 : : "REFRESH MATERIALIZED VIEW %s (ID %d OID %u)",
1613 : : obj->name, obj->dumpId, obj->catId.oid);
8135 tgl@sss.pgh.pa.us 1614 : 0 : return;
1615 : 0 : case DO_RULE:
1616 : 0 : snprintf(buf, bufsize,
1617 : : "RULE %s (ID %d OID %u)",
1618 : : obj->name, obj->dumpId, obj->catId.oid);
1619 : 0 : return;
1620 : 0 : case DO_TRIGGER:
1621 : 0 : snprintf(buf, bufsize,
1622 : : "TRIGGER %s (ID %d OID %u)",
1623 : : obj->name, obj->dumpId, obj->catId.oid);
1624 : 0 : return;
4988 rhaas@postgresql.org 1625 : 0 : case DO_EVENT_TRIGGER:
1626 : 0 : snprintf(buf, bufsize,
1627 : : "EVENT TRIGGER %s (ID %d OID %u)",
1628 : : obj->name, obj->dumpId, obj->catId.oid);
1629 : 0 : return;
8135 tgl@sss.pgh.pa.us 1630 : 0 : case DO_CONSTRAINT:
1631 : 0 : snprintf(buf, bufsize,
1632 : : "CONSTRAINT %s (ID %d OID %u)",
1633 : : obj->name, obj->dumpId, obj->catId.oid);
1634 : 0 : return;
1635 : 0 : case DO_FK_CONSTRAINT:
1636 : 0 : snprintf(buf, bufsize,
1637 : : "FK CONSTRAINT %s (ID %d OID %u)",
1638 : : obj->name, obj->dumpId, obj->catId.oid);
1639 : 0 : return;
1640 : 0 : case DO_PROCLANG:
1641 : 0 : snprintf(buf, bufsize,
1642 : : "PROCEDURAL LANGUAGE %s (ID %d OID %u)",
1643 : : obj->name, obj->dumpId, obj->catId.oid);
1644 : 0 : return;
1645 : 0 : case DO_CAST:
1646 : 0 : snprintf(buf, bufsize,
1647 : : "CAST %u to %u (ID %d OID %u)",
1648 : : ((CastInfo *) obj)->castsource,
1649 : : ((CastInfo *) obj)->casttarget,
1650 : : obj->dumpId, obj->catId.oid);
1651 : 0 : return;
3976 peter_e@gmx.net 1652 : 0 : case DO_TRANSFORM:
1653 : 0 : snprintf(buf, bufsize,
1654 : : "TRANSFORM %u lang %u (ID %d OID %u)",
1655 : : ((TransformInfo *) obj)->trftype,
1656 : : ((TransformInfo *) obj)->trflang,
1657 : : obj->dumpId, obj->catId.oid);
1658 : 0 : return;
8135 tgl@sss.pgh.pa.us 1659 : 0 : case DO_TABLE_DATA:
1660 : 0 : snprintf(buf, bufsize,
1661 : : "TABLE DATA %s (ID %d OID %u)",
1662 : : obj->name, obj->dumpId, obj->catId.oid);
8047 1663 : 0 : return;
3491 peter_e@gmx.net 1664 : 0 : case DO_SEQUENCE_SET:
1665 : 0 : snprintf(buf, bufsize,
1666 : : "SEQUENCE SET %s (ID %d OID %u)",
1667 : : obj->name, obj->dumpId, obj->catId.oid);
1668 : 0 : return;
6265 tgl@sss.pgh.pa.us 1669 : 0 : case DO_DUMMY_TYPE:
8047 1670 : 0 : snprintf(buf, bufsize,
1671 : : "DUMMY TYPE %s (ID %d OID %u)",
1672 : : obj->name, obj->dumpId, obj->catId.oid);
1673 : 0 : return;
6781 1674 : 0 : case DO_TSPARSER:
1675 : 0 : snprintf(buf, bufsize,
1676 : : "TEXT SEARCH PARSER %s (ID %d OID %u)",
1677 : : obj->name, obj->dumpId, obj->catId.oid);
1678 : 0 : return;
1679 : 0 : case DO_TSDICT:
1680 : 0 : snprintf(buf, bufsize,
1681 : : "TEXT SEARCH DICTIONARY %s (ID %d OID %u)",
1682 : : obj->name, obj->dumpId, obj->catId.oid);
1683 : 0 : return;
1684 : 0 : case DO_TSTEMPLATE:
1685 : 0 : snprintf(buf, bufsize,
1686 : : "TEXT SEARCH TEMPLATE %s (ID %d OID %u)",
1687 : : obj->name, obj->dumpId, obj->catId.oid);
1688 : 0 : return;
1689 : 0 : case DO_TSCONFIG:
1690 : 0 : snprintf(buf, bufsize,
1691 : : "TEXT SEARCH CONFIGURATION %s (ID %d OID %u)",
1692 : : obj->name, obj->dumpId, obj->catId.oid);
1693 : 0 : return;
6295 peter_e@gmx.net 1694 : 0 : case DO_FDW:
1695 : 0 : snprintf(buf, bufsize,
1696 : : "FOREIGN DATA WRAPPER %s (ID %d OID %u)",
1697 : : obj->name, obj->dumpId, obj->catId.oid);
1698 : 0 : return;
1699 : 0 : case DO_FOREIGN_SERVER:
1700 : 0 : snprintf(buf, bufsize,
1701 : : "FOREIGN SERVER %s (ID %d OID %u)",
1702 : : obj->name, obj->dumpId, obj->catId.oid);
1703 : 0 : return;
6005 tgl@sss.pgh.pa.us 1704 : 0 : case DO_DEFAULT_ACL:
1705 : 0 : snprintf(buf, bufsize,
1706 : : "DEFAULT ACL %s (ID %d OID %u)",
1707 : : obj->name, obj->dumpId, obj->catId.oid);
1708 : 0 : return;
1196 peter@eisentraut.org 1709 : 0 : case DO_LARGE_OBJECT:
8047 tgl@sss.pgh.pa.us 1710 : 0 : snprintf(buf, bufsize,
1711 : : "LARGE OBJECT (ID %d OID %u)",
1712 : : obj->dumpId, obj->catId.oid);
8135 1713 : 0 : return;
1196 peter@eisentraut.org 1714 : 0 : case DO_LARGE_OBJECT_DATA:
7563 tgl@sss.pgh.pa.us 1715 : 0 : snprintf(buf, bufsize,
1716 : : "LARGE OBJECT DATA (ID %d)",
1717 : : obj->dumpId);
1718 : 0 : return;
4126 sfrost@snowman.net 1719 : 0 : case DO_POLICY:
4195 1720 : 0 : snprintf(buf, bufsize,
1721 : : "POLICY (ID %d OID %u)",
1722 : : obj->dumpId, obj->catId.oid);
1723 : 0 : return;
3342 peter_e@gmx.net 1724 : 0 : case DO_PUBLICATION:
1725 : 0 : snprintf(buf, bufsize,
1726 : : "PUBLICATION (ID %d OID %u)",
1727 : : obj->dumpId, obj->catId.oid);
1728 : 0 : return;
1729 : 0 : case DO_PUBLICATION_REL:
1730 : 0 : snprintf(buf, bufsize,
1731 : : "PUBLICATION TABLE (ID %d OID %u)",
1732 : : obj->dumpId, obj->catId.oid);
1733 : 0 : return;
1587 akapila@postgresql.o 1734 : 0 : case DO_PUBLICATION_TABLE_IN_SCHEMA:
1600 1735 : 0 : snprintf(buf, bufsize,
1736 : : "PUBLICATION TABLES IN SCHEMA (ID %d OID %u)",
1737 : : obj->dumpId, obj->catId.oid);
1738 : 0 : return;
3342 peter_e@gmx.net 1739 : 0 : case DO_SUBSCRIPTION:
1740 : 0 : snprintf(buf, bufsize,
1741 : : "SUBSCRIPTION (ID %d OID %u)",
1742 : : obj->dumpId, obj->catId.oid);
1743 : 0 : return;
803 akapila@postgresql.o 1744 : 0 : case DO_SUBSCRIPTION_REL:
1745 : 0 : snprintf(buf, bufsize,
1746 : : "SUBSCRIPTION TABLE (ID %d OID %u)",
1747 : : obj->dumpId, obj->catId.oid);
1748 : 0 : return;
5011 tgl@sss.pgh.pa.us 1749 : 0 : case DO_PRE_DATA_BOUNDARY:
1750 : 0 : snprintf(buf, bufsize,
1751 : : "PRE-DATA BOUNDARY (ID %d)",
1752 : : obj->dumpId);
1753 : 0 : return;
1754 : 0 : case DO_POST_DATA_BOUNDARY:
1755 : 0 : snprintf(buf, bufsize,
1756 : : "POST-DATA BOUNDARY (ID %d)",
1757 : : obj->dumpId);
1758 : 0 : return;
388 jdavis@postgresql.or 1759 : 0 : case DO_REL_STATS:
1760 : 0 : snprintf(buf, bufsize,
1761 : : "RELATION STATISTICS FOR %s (ID %d OID %u)",
1762 : : obj->name, obj->dumpId, obj->catId.oid);
1763 : 0 : return;
1764 : : }
1765 : : /* shouldn't get here */
8135 tgl@sss.pgh.pa.us 1766 : 0 : snprintf(buf, bufsize,
1767 : : "object type %d (ID %d OID %u)",
1768 : 0 : (int) obj->objType,
1769 : : obj->dumpId, obj->catId.oid);
1770 : : }
1771 : :
1772 : : /* binaryheap comparator that compares "a" and "b" as integers */
1773 : : static int
908 nathan@postgresql.or 1774 :CBC 47285494 : int_cmp(void *a, void *b, void *arg)
1775 : : {
1776 : 47285494 : int ai = (int) (intptr_t) a;
1777 : 47285494 : int bi = (int) (intptr_t) b;
1778 : :
758 1779 : 47285494 : return pg_cmp_s32(ai, bi);
1780 : : }
|