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