Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5d4b60f2f2
Fetching contributors…

Cannot retrieve contributors at this time

file 1407 lines (1217 sloc) 39.202 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
/*-------------------------------------------------------------------------
*
* pg_shdepend.c
* routines to support manipulation of the pg_shdepend relation
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/catalog/pg_shdepend.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"

#include "access/genam.h"
#include "access/heapam.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_database.h"
#include "catalog/pg_default_acl.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_language.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_shdepend.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "commands/collationcmds.h"
#include "commands/conversioncmds.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "commands/schemacmds.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
#include "storage/lmgr.h"
#include "miscadmin.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/syscache.h"
#include "utils/tqual.h"


typedef enum
{
LOCAL_OBJECT,
SHARED_OBJECT,
REMOTE_OBJECT
} objectType;

static void getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2);
static Oid classIdGetDbId(Oid classId);
static void shdepChangeDep(Relation sdepRel,
Oid classid, Oid objid, int32 objsubid,
Oid refclassid, Oid refobjid,
SharedDependencyType deptype);
static void shdepAddDependency(Relation sdepRel,
Oid classId, Oid objectId, int32 objsubId,
Oid refclassId, Oid refobjId,
SharedDependencyType deptype);
static void shdepDropDependency(Relation sdepRel,
Oid classId, Oid objectId, int32 objsubId,
bool drop_subobjects,
Oid refclassId, Oid refobjId,
SharedDependencyType deptype);
static void storeObjectDescription(StringInfo descs, objectType type,
ObjectAddress *object,
SharedDependencyType deptype,
int count);
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel);


/*
* recordSharedDependencyOn
*
* Record a dependency between 2 objects via their respective ObjectAddresses.
* The first argument is the dependent object, the second the one it
* references (which must be a shared object).
*
* This locks the referenced object and makes sure it still exists.
* Then it creates an entry in pg_shdepend. The lock is kept until
* the end of the transaction.
*
* Dependencies on pinned objects are not recorded.
*/
void
recordSharedDependencyOn(ObjectAddress *depender,
ObjectAddress *referenced,
SharedDependencyType deptype)
{
Relation sdepRel;

/*
* Objects in pg_shdepend can't have SubIds.
*/
Assert(depender->objectSubId == 0);
Assert(referenced->objectSubId == 0);

/*
* During bootstrap, do nothing since pg_shdepend may not exist yet.
* initdb will fill in appropriate pg_shdepend entries after bootstrap.
*/
if (IsBootstrapProcessingMode())
return;

sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

/* If the referenced object is pinned, do nothing. */
if (!isSharedObjectPinned(referenced->classId, referenced->objectId,
sdepRel))
{
shdepAddDependency(sdepRel, depender->classId, depender->objectId,
depender->objectSubId,
referenced->classId, referenced->objectId,
deptype);
}

heap_close(sdepRel, RowExclusiveLock);
}

/*
* recordDependencyOnOwner
*
* A convenient wrapper of recordSharedDependencyOn -- register the specified
* user as owner of the given object.
*
* Note: it's the caller's responsibility to ensure that there isn't an owner
* entry for the object already.
*/
void
recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
{
ObjectAddress myself,
referenced;

myself.classId = classId;
myself.objectId = objectId;
myself.objectSubId = 0;

referenced.classId = AuthIdRelationId;
referenced.objectId = owner;
referenced.objectSubId = 0;

recordSharedDependencyOn(&myself, &referenced, SHARED_DEPENDENCY_OWNER);
}

/*
* shdepChangeDep
*
* Update shared dependency records to account for an updated referenced
* object. This is an internal workhorse for operations such as changing
* an object's owner.
*
* There must be no more than one existing entry for the given dependent
* object and dependency type! So in practice this can only be used for
* updating SHARED_DEPENDENCY_OWNER entries, which should have that property.
*
* If there is no previous entry, we assume it was referencing a PINned
* object, so we create a new entry. If the new referenced object is
* PINned, we don't create an entry (and drop the old one, if any).
*
* sdepRel must be the pg_shdepend relation, already opened and suitably
* locked.
*/
static void
shdepChangeDep(Relation sdepRel,
Oid classid, Oid objid, int32 objsubid,
Oid refclassid, Oid refobjid,
SharedDependencyType deptype)
{
Oid dbid = classIdGetDbId(classid);
HeapTuple oldtup = NULL;
HeapTuple scantup;
ScanKeyData key[4];
SysScanDesc scan;

/*
* Make sure the new referenced object doesn't go away while we record the
* dependency.
*/
shdepLockAndCheckObject(refclassid, refobjid);

/*
* Look for a previous entry
*/
ScanKeyInit(&key[0],
Anum_pg_shdepend_dbid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(dbid));
ScanKeyInit(&key[1],
Anum_pg_shdepend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classid));
ScanKeyInit(&key[2],
Anum_pg_shdepend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objid));
ScanKeyInit(&key[3],
Anum_pg_shdepend_objsubid,
BTEqualStrategyNumber, F_INT4EQ,
Int32GetDatum(objsubid));

scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
SnapshotNow, 4, key);

while ((scantup = systable_getnext(scan)) != NULL)
{
/* Ignore if not of the target dependency type */
if (((Form_pg_shdepend) GETSTRUCT(scantup))->deptype != deptype)
continue;
/* Caller screwed up if multiple matches */
if (oldtup)
elog(ERROR,
"multiple pg_shdepend entries for object %u/%u/%d deptype %c",
classid, objid, objsubid, deptype);
oldtup = heap_copytuple(scantup);
}

systable_endscan(scan);

if (isSharedObjectPinned(refclassid, refobjid, sdepRel))
{
/* No new entry needed, so just delete existing entry if any */
if (oldtup)
simple_heap_delete(sdepRel, &oldtup->t_self);
}
else if (oldtup)
{
/* Need to update existing entry */
Form_pg_shdepend shForm = (Form_pg_shdepend) GETSTRUCT(oldtup);

/* Since oldtup is a copy, we can just modify it in-memory */
shForm->refclassid = refclassid;
shForm->refobjid = refobjid;

simple_heap_update(sdepRel, &oldtup->t_self, oldtup);

/* keep indexes current */
CatalogUpdateIndexes(sdepRel, oldtup);
}
else
{
/* Need to insert new entry */
Datum values[Natts_pg_shdepend];
bool nulls[Natts_pg_shdepend];

memset(nulls, false, sizeof(nulls));

values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(dbid);
values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classid);
values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objid);
values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(objsubid);

values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassid);
values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjid);
values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);

/*
* we are reusing oldtup just to avoid declaring a new variable, but
* it's certainly a new tuple
*/
oldtup = heap_form_tuple(RelationGetDescr(sdepRel), values, nulls);
simple_heap_insert(sdepRel, oldtup);

/* keep indexes current */
CatalogUpdateIndexes(sdepRel, oldtup);
}

if (oldtup)
heap_freetuple(oldtup);
}

/*
* changeDependencyOnOwner
*
* Update the shared dependencies to account for the new owner.
*
* Note: we don't need an objsubid argument because only whole objects
* have owners.
*/
void
changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
{
Relation sdepRel;

sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

/* Adjust the SHARED_DEPENDENCY_OWNER entry */
shdepChangeDep(sdepRel,
classId, objectId, 0,
AuthIdRelationId, newOwnerId,
SHARED_DEPENDENCY_OWNER);

/*----------
* There should never be a SHARED_DEPENDENCY_ACL entry for the owner,
* so get rid of it if there is one. This can happen if the new owner
* was previously granted some rights to the object.
*
* This step is analogous to aclnewowner's removal of duplicate entries
* in the ACL. We have to do it to handle this scenario:
* A grants some rights on an object to B
* ALTER OWNER changes the object's owner to B
* ALTER OWNER changes the object's owner to C
* The third step would remove all mention of B from the object's ACL,
* but we'd still have a SHARED_DEPENDENCY_ACL for B if we did not do
* things this way.
*
* The rule against having a SHARED_DEPENDENCY_ACL entry for the owner
* allows us to fix things up in just this one place, without having
* to make the various ALTER OWNER routines each know about it.
*----------
*/
shdepDropDependency(sdepRel, classId, objectId, 0, true,
AuthIdRelationId, newOwnerId,
SHARED_DEPENDENCY_ACL);

heap_close(sdepRel, RowExclusiveLock);
}

/*
* getOidListDiff
* Helper for updateAclDependencies.
*
* Takes two Oid arrays and removes elements that are common to both arrays,
* leaving just those that are in one input but not the other.
* We assume both arrays have been sorted and de-duped.
*/
static void
getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2)
{
int in1,
in2,
out1,
out2;

in1 = in2 = out1 = out2 = 0;
while (in1 < *nlist1 && in2 < *nlist2)
{
if (list1[in1] == list2[in2])
{
/* skip over duplicates */
in1++;
in2++;
}
else if (list1[in1] < list2[in2])
{
/* list1[in1] is not in list2 */
list1[out1++] = list1[in1++];
}
else
{
/* list2[in2] is not in list1 */
list2[out2++] = list2[in2++];
}
}

/* any remaining list1 entries are not in list2 */
while (in1 < *nlist1)
{
list1[out1++] = list1[in1++];
}

/* any remaining list2 entries are not in list1 */
while (in2 < *nlist2)
{
list2[out2++] = list2[in2++];
}

*nlist1 = out1;
*nlist2 = out2;
}

/*
* updateAclDependencies
* Update the pg_shdepend info for an object's ACL during GRANT/REVOKE.
*
* classId, objectId, objsubId: identify the object whose ACL this is
* ownerId: role owning the object
* noldmembers, oldmembers: array of roleids appearing in old ACL
* nnewmembers, newmembers: array of roleids appearing in new ACL
*
* We calculate the differences between the new and old lists of roles,
* and then insert or delete from pg_shdepend as appropriate.
*
* Note that we can't just insert all referenced roles blindly during GRANT,
* because we would end up with duplicate registered dependencies. We could
* check for existence of the tuples before inserting, but that seems to be
* more expensive than what we are doing here. Likewise we can't just delete
* blindly during REVOKE, because the user may still have other privileges.
* It is also possible that REVOKE actually adds dependencies, due to
* instantiation of a formerly implicit default ACL (although at present,
* all such dependencies should be for the owning role, which we ignore here).
*
* NOTE: Both input arrays must be sorted and de-duped. (Typically they
* are extracted from an ACL array by aclmembers(), which takes care of
* both requirements.) The arrays are pfreed before return.
*/
void
updateAclDependencies(Oid classId, Oid objectId, int32 objsubId,
Oid ownerId,
int noldmembers, Oid *oldmembers,
int nnewmembers, Oid *newmembers)
{
Relation sdepRel;
int i;

/*
* Remove entries that are common to both lists; those represent existing
* dependencies we don't need to change.
*
* OK to overwrite the inputs since we'll pfree them anyway.
*/
getOidListDiff(oldmembers, &noldmembers, newmembers, &nnewmembers);

if (noldmembers > 0 || nnewmembers > 0)
{
sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

/* Add new dependencies that weren't already present */
for (i = 0; i < nnewmembers; i++)
{
Oid roleid = newmembers[i];

/*
* Skip the owner: he has an OWNER shdep entry instead. (This is
* not just a space optimization; it makes ALTER OWNER easier. See
* notes in changeDependencyOnOwner.)
*/
if (roleid == ownerId)
continue;

/* Skip pinned roles; they don't need dependency entries */
if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
continue;

shdepAddDependency(sdepRel, classId, objectId, objsubId,
AuthIdRelationId, roleid,
SHARED_DEPENDENCY_ACL);
}

/* Drop no-longer-used old dependencies */
for (i = 0; i < noldmembers; i++)
{
Oid roleid = oldmembers[i];

/* Skip the owner, same as above */
if (roleid == ownerId)
continue;

/* Skip pinned roles */
if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
continue;

shdepDropDependency(sdepRel, classId, objectId, objsubId,
false, /* exact match on objsubId */
AuthIdRelationId, roleid,
SHARED_DEPENDENCY_ACL);
}

heap_close(sdepRel, RowExclusiveLock);
}

if (oldmembers)
pfree(oldmembers);
if (newmembers)
pfree(newmembers);
}

/*
* A struct to keep track of dependencies found in other databases.
*/
typedef struct
{
Oid dbOid;
int count;
} remoteDep;

/*
* checkSharedDependencies
*
* Check whether there are shared dependency entries for a given shared
* object; return true if so.
*
* In addition, return a string containing a newline-separated list of object
* descriptions that depend on the shared object, or NULL if none is found.
* We actually return two such strings; the "detail" result is suitable for
* returning to the client as an errdetail() string, and is limited in size.
* The "detail_log" string is potentially much longer, and should be emitted
* to the server log only.
*
* We can find three different kinds of dependencies: dependencies on objects
* of the current database; dependencies on shared objects; and dependencies
* on objects local to other databases. We can (and do) provide descriptions
* of the two former kinds of objects, but we can't do that for "remote"
* objects, so we just provide a count of them.
*
* If we find a SHARED_DEPENDENCY_PIN entry, we can error out early.
*/
bool
checkSharedDependencies(Oid classId, Oid objectId,
char **detail_msg, char **detail_log_msg)
{
Relation sdepRel;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tup;
int numReportedDeps = 0;
int numNotReportedDeps = 0;
int numNotReportedDbs = 0;
List *remDeps = NIL;
ListCell *cell;
ObjectAddress object;
StringInfoData descs;
StringInfoData alldescs;

/*
* We limit the number of dependencies reported to the client to
* MAX_REPORTED_DEPS, since client software may not deal well with
* enormous error strings. The server log always gets a full report.
*/
#define MAX_REPORTED_DEPS 100

initStringInfo(&descs);
initStringInfo(&alldescs);

sdepRel = heap_open(SharedDependRelationId, AccessShareLock);

ScanKeyInit(&key[0],
Anum_pg_shdepend_refclassid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classId));
ScanKeyInit(&key[1],
Anum_pg_shdepend_refobjid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objectId));

scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
SnapshotNow, 2, key);

while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tup);

/* This case can be dispatched quickly */
if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
{
object.classId = classId;
object.objectId = objectId;
object.objectSubId = 0;
ereport(ERROR,
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
errmsg("cannot drop %s because it is required by the database system",
getObjectDescription(&object))));
}

object.classId = sdepForm->classid;
object.objectId = sdepForm->objid;
object.objectSubId = sdepForm->objsubid;

/*
* If it's a dependency local to this database or it's a shared
* object, describe it.
*
* If it's a remote dependency, keep track of it so we can report the
* number of them later.
*/
if (sdepForm->dbid == MyDatabaseId)
{
if (numReportedDeps < MAX_REPORTED_DEPS)
{
numReportedDeps++;
storeObjectDescription(&descs, LOCAL_OBJECT, &object,
sdepForm->deptype, 0);
}
else
numNotReportedDeps++;
storeObjectDescription(&alldescs, LOCAL_OBJECT, &object,
sdepForm->deptype, 0);
}
else if (sdepForm->dbid == InvalidOid)
{
if (numReportedDeps < MAX_REPORTED_DEPS)
{
numReportedDeps++;
storeObjectDescription(&descs, SHARED_OBJECT, &object,
sdepForm->deptype, 0);
}
else
numNotReportedDeps++;
storeObjectDescription(&alldescs, SHARED_OBJECT, &object,
sdepForm->deptype, 0);
}
else
{
/* It's not local nor shared, so it must be remote. */
remoteDep *dep;
bool stored = false;

/*
* XXX this info is kept on a simple List. Maybe it's not good
* for performance, but using a hash table seems needlessly
* complex. The expected number of databases is not high anyway,
* I suppose.
*/
foreach(cell, remDeps)
{
dep = lfirst(cell);
if (dep->dbOid == sdepForm->dbid)
{
dep->count++;
stored = true;
break;
}
}
if (!stored)
{
dep = (remoteDep *) palloc(sizeof(remoteDep));
dep->dbOid = sdepForm->dbid;
dep->count = 1;
remDeps = lappend(remDeps, dep);
}
}
}

systable_endscan(scan);

heap_close(sdepRel, AccessShareLock);

/*
* Summarize dependencies in remote databases.
*/
foreach(cell, remDeps)
{
remoteDep *dep = lfirst(cell);

object.classId = DatabaseRelationId;
object.objectId = dep->dbOid;
object.objectSubId = 0;

if (numReportedDeps < MAX_REPORTED_DEPS)
{
numReportedDeps++;
storeObjectDescription(&descs, REMOTE_OBJECT, &object,
SHARED_DEPENDENCY_INVALID, dep->count);
}
else
numNotReportedDbs++;
storeObjectDescription(&alldescs, REMOTE_OBJECT, &object,
SHARED_DEPENDENCY_INVALID, dep->count);
}

list_free_deep(remDeps);

if (descs.len == 0)
{
pfree(descs.data);
pfree(alldescs.data);
*detail_msg = *detail_log_msg = NULL;
return false;
}

if (numNotReportedDeps > 0)
appendStringInfo(&descs, ngettext("\nand %d other object "
"(see server log for list)",
"\nand %d other objects "
"(see server log for list)",
numNotReportedDeps),
numNotReportedDeps);
if (numNotReportedDbs > 0)
appendStringInfo(&descs, ngettext("\nand objects in %d other database "
"(see server log for list)",
"\nand objects in %d other databases "
"(see server log for list)",
numNotReportedDbs),
numNotReportedDbs);

*detail_msg = descs.data;
*detail_log_msg = alldescs.data;
return true;
}

/*
* copyTemplateDependencies
*
* Routine to create the initial shared dependencies of a new database.
* We simply copy the dependencies from the template database.
*/
void
copyTemplateDependencies(Oid templateDbId, Oid newDbId)
{
Relation sdepRel;
TupleDesc sdepDesc;
ScanKeyData key[1];
SysScanDesc scan;
HeapTuple tup;
CatalogIndexState indstate;
Datum values[Natts_pg_shdepend];
bool nulls[Natts_pg_shdepend];
bool replace[Natts_pg_shdepend];

sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
sdepDesc = RelationGetDescr(sdepRel);

indstate = CatalogOpenIndexes(sdepRel);

/* Scan all entries with dbid = templateDbId */
ScanKeyInit(&key[0],
Anum_pg_shdepend_dbid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(templateDbId));

scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
SnapshotNow, 1, key);

/* Set up to copy the tuples except for inserting newDbId */
memset(values, 0, sizeof(values));
memset(nulls, false, sizeof(nulls));
memset(replace, false, sizeof(replace));

replace[Anum_pg_shdepend_dbid - 1] = true;
values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(newDbId);

/*
* Copy the entries of the original database, changing the database Id to
* that of the new database. Note that because we are not copying rows
* with dbId == 0 (ie, rows describing dependent shared objects) we won't
* copy the ownership dependency of the template database itself; this is
* what we want.
*/
while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
HeapTuple newtup;

newtup = heap_modify_tuple(tup, sdepDesc, values, nulls, replace);
simple_heap_insert(sdepRel, newtup);

/* Keep indexes current */
CatalogIndexInsert(indstate, newtup);

heap_freetuple(newtup);
}

systable_endscan(scan);

CatalogCloseIndexes(indstate);
heap_close(sdepRel, RowExclusiveLock);
}

/*
* dropDatabaseDependencies
*
* Delete pg_shdepend entries corresponding to a database that's being
* dropped.
*/
void
dropDatabaseDependencies(Oid databaseId)
{
Relation sdepRel;
ScanKeyData key[1];
SysScanDesc scan;
HeapTuple tup;

sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

/*
* First, delete all the entries that have the database Oid in the dbid
* field.
*/
ScanKeyInit(&key[0],
Anum_pg_shdepend_dbid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(databaseId));
/* We leave the other index fields unspecified */

scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
SnapshotNow, 1, key);

while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
simple_heap_delete(sdepRel, &tup->t_self);
}

systable_endscan(scan);

/* Now delete all entries corresponding to the database itself */
shdepDropDependency(sdepRel, DatabaseRelationId, databaseId, 0, true,
InvalidOid, InvalidOid,
SHARED_DEPENDENCY_INVALID);

heap_close(sdepRel, RowExclusiveLock);
}

/*
* deleteSharedDependencyRecordsFor
*
* Delete all pg_shdepend entries corresponding to an object that's being
* dropped or modified. The object is assumed to be either a shared object
* or local to the current database (the classId tells us which).
*
* If objectSubId is zero, we are deleting a whole object, so get rid of
* pg_shdepend entries for subobjects as well.
*/
void
deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
{
Relation sdepRel;

sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

shdepDropDependency(sdepRel, classId, objectId, objectSubId,
(objectSubId == 0),
InvalidOid, InvalidOid,
SHARED_DEPENDENCY_INVALID);

heap_close(sdepRel, RowExclusiveLock);
}

/*
* shdepAddDependency
* Internal workhorse for inserting into pg_shdepend
*
* sdepRel must be the pg_shdepend relation, already opened and suitably
* locked.
*/
static void
shdepAddDependency(Relation sdepRel,
Oid classId, Oid objectId, int32 objsubId,
Oid refclassId, Oid refobjId,
SharedDependencyType deptype)
{
HeapTuple tup;
Datum values[Natts_pg_shdepend];
bool nulls[Natts_pg_shdepend];

/*
* Make sure the object doesn't go away while we record the dependency on
* it. DROP routines should lock the object exclusively before they check
* shared dependencies.
*/
shdepLockAndCheckObject(refclassId, refobjId);

memset(nulls, false, sizeof(nulls));

/*
* Form the new tuple and record the dependency.
*/
values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(classIdGetDbId(classId));
values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classId);
values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objectId);
values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(objsubId);

values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassId);
values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjId);
values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);

tup = heap_form_tuple(sdepRel->rd_att, values, nulls);

simple_heap_insert(sdepRel, tup);

/* keep indexes current */
CatalogUpdateIndexes(sdepRel, tup);

/* clean up */
heap_freetuple(tup);
}

/*
* shdepDropDependency
* Internal workhorse for deleting entries from pg_shdepend.
*
* We drop entries having the following properties:
* dependent object is the one identified by classId/objectId/objsubId
* if refclassId isn't InvalidOid, it must match the entry's refclassid
* if refobjId isn't InvalidOid, it must match the entry's refobjid
* if deptype isn't SHARED_DEPENDENCY_INVALID, it must match entry's deptype
*
* If drop_subobjects is true, we ignore objsubId and consider all entries
* matching classId/objectId.
*
* sdepRel must be the pg_shdepend relation, already opened and suitably
* locked.
*/
static void
shdepDropDependency(Relation sdepRel,
Oid classId, Oid objectId, int32 objsubId,
bool drop_subobjects,
Oid refclassId, Oid refobjId,
SharedDependencyType deptype)
{
ScanKeyData key[4];
int nkeys;
SysScanDesc scan;
HeapTuple tup;

/* Scan for entries matching the dependent object */
ScanKeyInit(&key[0],
Anum_pg_shdepend_dbid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classIdGetDbId(classId)));
ScanKeyInit(&key[1],
Anum_pg_shdepend_classid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classId));
ScanKeyInit(&key[2],
Anum_pg_shdepend_objid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objectId));
if (drop_subobjects)
nkeys = 3;
else
{
ScanKeyInit(&key[3],
Anum_pg_shdepend_objsubid,
BTEqualStrategyNumber, F_INT4EQ,
Int32GetDatum(objsubId));
nkeys = 4;
}

scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
SnapshotNow, nkeys, key);

while (HeapTupleIsValid(tup = systable_getnext(scan)))
{
Form_pg_shdepend shdepForm = (Form_pg_shdepend) GETSTRUCT(tup);

/* Filter entries according to additional parameters */
if (OidIsValid(refclassId) && shdepForm->refclassid != refclassId)
continue;
if (OidIsValid(refobjId) && shdepForm->refobjid != refobjId)
continue;
if (deptype != SHARED_DEPENDENCY_INVALID &&
shdepForm->deptype != deptype)
continue;

/* OK, delete it */
simple_heap_delete(sdepRel, &tup->t_self);
}

systable_endscan(scan);
}

/*
* classIdGetDbId
*
* Get the database Id that should be used in pg_shdepend, given the OID
* of the catalog containing the object. For shared objects, it's 0
* (InvalidOid); for all other objects, it's the current database Id.
*/
static Oid
classIdGetDbId(Oid classId)
{
Oid dbId;

if (IsSharedRelation(classId))
dbId = InvalidOid;
else
dbId = MyDatabaseId;

return dbId;
}

/*
* shdepLockAndCheckObject
*
* Lock the object that we are about to record a dependency on.
* After it's locked, verify that it hasn't been dropped while we
* weren't looking. If the object has been dropped, this function
* does not return!
*/
void
shdepLockAndCheckObject(Oid classId, Oid objectId)
{
/* AccessShareLock should be OK, since we are not modifying the object */
LockSharedObject(classId, objectId, 0, AccessShareLock);

switch (classId)
{
case AuthIdRelationId:
if (!SearchSysCacheExists1(AUTHOID, ObjectIdGetDatum(objectId)))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("role %u was concurrently dropped",
objectId)));
break;

/*
* Currently, this routine need not support any other shared
* object types besides roles. If we wanted to record explicit
* dependencies on databases or tablespaces, we'd need code along
* these lines:
*/
#ifdef NOT_USED
case TableSpaceRelationId:
{
/* For lack of a syscache on pg_tablespace, do this: */
char *tablespace = get_tablespace_name(objectId);

if (tablespace == NULL)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("tablespace %u was concurrently dropped",
objectId)));
pfree(tablespace);
break;
}
#endif

case DatabaseRelationId:
{
/* For lack of a syscache on pg_database, do this: */
char *database = get_database_name(objectId);

if (database == NULL)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("database %u was concurrently dropped",
objectId)));
pfree(database);
break;
}


default:
elog(ERROR, "unrecognized shared classId: %u", classId);
}
}


/*
* storeObjectDescription
* Append the description of a dependent object to "descs"
*
* While searching for dependencies of a shared object, we stash the
* descriptions of dependent objects we find in a single string, which we
* later pass to ereport() in the DETAIL field when somebody attempts to
* drop a referenced shared object.
*
* When type is LOCAL_OBJECT or SHARED_OBJECT, we expect object to be the
* dependent object, deptype is the dependency type, and count is not used.
* When type is REMOTE_OBJECT, we expect object to be the database object,
* and count to be nonzero; deptype is not used in this case.
*/
static void
storeObjectDescription(StringInfo descs, objectType type,
ObjectAddress *object,
SharedDependencyType deptype,
int count)
{
char *objdesc = getObjectDescription(object);

/* separate entries with a newline */
if (descs->len != 0)
appendStringInfoChar(descs, '\n');

switch (type)
{
case LOCAL_OBJECT:
case SHARED_OBJECT:
if (deptype == SHARED_DEPENDENCY_OWNER)
appendStringInfo(descs, _("owner of %s"), objdesc);
else if (deptype == SHARED_DEPENDENCY_ACL)
appendStringInfo(descs, _("privileges for %s"), objdesc);
else
elog(ERROR, "unrecognized dependency type: %d",
(int) deptype);
break;

case REMOTE_OBJECT:
/* translator: %s will always be "database %s" */
appendStringInfo(descs, ngettext("%d object in %s",
"%d objects in %s",
count),
count, objdesc);
break;

default:
elog(ERROR, "unrecognized object type: %d", type);
}

pfree(objdesc);
}


/*
* isSharedObjectPinned
* Return whether a given shared object has a SHARED_DEPENDENCY_PIN entry.
*
* sdepRel must be the pg_shdepend relation, already opened and suitably
* locked.
*/
static bool
isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
{
bool result = false;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tup;

ScanKeyInit(&key[0],
Anum_pg_shdepend_refclassid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(classId));
ScanKeyInit(&key[1],
Anum_pg_shdepend_refobjid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(objectId));

scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
SnapshotNow, 2, key);

/*
* Since we won't generate additional pg_shdepend entries for pinned
* objects, there can be at most one entry referencing a pinned object.
* Hence, it's sufficient to look at the first returned tuple; we don't
* need to loop.
*/
tup = systable_getnext(scan);
if (HeapTupleIsValid(tup))
{
Form_pg_shdepend shdepForm = (Form_pg_shdepend) GETSTRUCT(tup);

if (shdepForm->deptype == SHARED_DEPENDENCY_PIN)
result = true;
}

systable_endscan(scan);

return result;
}

/*
* shdepDropOwned
*
* Drop the objects owned by any one of the given RoleIds. If a role has
* access to an object, the grant will be removed as well (but the object
* will not, of course).
*
* We can revoke grants immediately while doing the scan, but drops are
* saved up and done all at once with performMultipleDeletions. This
* is necessary so that we don't get failures from trying to delete
* interdependent objects in the wrong order.
*/
void
shdepDropOwned(List *roleids, DropBehavior behavior)
{
Relation sdepRel;
ListCell *cell;
ObjectAddresses *deleteobjs;

deleteobjs = new_object_addresses();

/*
* We don't need this strong a lock here, but we'll call routines that
* acquire RowExclusiveLock. Better get that right now to avoid potential
* deadlock failures.
*/
sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

/*
* For each role, find the dependent objects and drop them using the
* regular (non-shared) dependency management.
*/
foreach(cell, roleids)
{
Oid roleid = lfirst_oid(cell);
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tuple;

/* Doesn't work for pinned objects */
if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
{
ObjectAddress obj;

obj.classId = AuthIdRelationId;
obj.objectId = roleid;
obj.objectSubId = 0;

ereport(ERROR,
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
errmsg("cannot drop objects owned by %s because they are "
"required by the database system",
getObjectDescription(&obj))));
}

ScanKeyInit(&key[0],
Anum_pg_shdepend_refclassid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(AuthIdRelationId));
ScanKeyInit(&key[1],
Anum_pg_shdepend_refobjid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(roleid));

scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
SnapshotNow, 2, key);

while ((tuple = systable_getnext(scan)) != NULL)
{
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
ObjectAddress obj;

/* We only operate on objects in the current database */
if (sdepForm->dbid != MyDatabaseId)
continue;

switch (sdepForm->deptype)
{
/* Shouldn't happen */
case SHARED_DEPENDENCY_PIN:
case SHARED_DEPENDENCY_INVALID:
elog(ERROR, "unexpected dependency type");
break;
case SHARED_DEPENDENCY_ACL:
RemoveRoleFromObjectACL(roleid,
sdepForm->classid,
sdepForm->objid);
break;
case SHARED_DEPENDENCY_OWNER:
/* Save it for deletion below */
obj.classId = sdepForm->classid;
obj.objectId = sdepForm->objid;
obj.objectSubId = sdepForm->objsubid;
add_exact_object_address(&obj, deleteobjs);
break;
}
}

systable_endscan(scan);
}

/* the dependency mechanism does the actual work */
performMultipleDeletions(deleteobjs, behavior, 0);

heap_close(sdepRel, RowExclusiveLock);

free_object_addresses(deleteobjs);
}

/*
* shdepReassignOwned
*
* Change the owner of objects owned by any of the roles in roleids to
* newrole. Grants are not touched.
*/
void
shdepReassignOwned(List *roleids, Oid newrole)
{
Relation sdepRel;
ListCell *cell;

/*
* We don't need this strong a lock here, but we'll call routines that
* acquire RowExclusiveLock. Better get that right now to avoid potential
* deadlock problems.
*/
sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);

foreach(cell, roleids)
{
SysScanDesc scan;
ScanKeyData key[2];
HeapTuple tuple;
Oid roleid = lfirst_oid(cell);

/* Refuse to work on pinned roles */
if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
{
ObjectAddress obj;

obj.classId = AuthIdRelationId;
obj.objectId = roleid;
obj.objectSubId = 0;

ereport(ERROR,
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
errmsg("cannot reassign ownership of objects owned by %s because they are required by the database system",
getObjectDescription(&obj))));

/*
* There's no need to tell the whole truth, which is that we
* didn't track these dependencies at all ...
*/
}

ScanKeyInit(&key[0],
Anum_pg_shdepend_refclassid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(AuthIdRelationId));
ScanKeyInit(&key[1],
Anum_pg_shdepend_refobjid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(roleid));

scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
SnapshotNow, 2, key);

while ((tuple = systable_getnext(scan)) != NULL)
{
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);

/* We only operate on objects in the current database */
if (sdepForm->dbid != MyDatabaseId)
continue;

/* Unexpected because we checked for pins above */
if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
elog(ERROR, "unexpected shared pin");

/* We leave non-owner dependencies alone */
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
continue;

/* Issue the appropriate ALTER OWNER call */
switch (sdepForm->classid)
{
case CollationRelationId:
AlterCollationOwner_oid(sdepForm->objid, newrole);
break;

case ConversionRelationId:
AlterConversionOwner_oid(sdepForm->objid, newrole);
break;

case TypeRelationId:
AlterTypeOwnerInternal(sdepForm->objid, newrole, true);
break;

case OperatorRelationId:
AlterOperatorOwner_oid(sdepForm->objid, newrole);
break;

case NamespaceRelationId:
AlterSchemaOwner_oid(sdepForm->objid, newrole);
break;

case RelationRelationId:

/*
* Pass recursing = true so that we don't fail on indexes,
* owned sequences, etc when we happen to visit them
* before their parent table.
*/
ATExecChangeOwner(sdepForm->objid, newrole, true, AccessExclusiveLock);
break;

case ProcedureRelationId:
AlterFunctionOwner_oid(sdepForm->objid, newrole);
break;

case LanguageRelationId:
AlterLanguageOwner_oid(sdepForm->objid, newrole);
break;

case LargeObjectRelationId:
LargeObjectAlterOwner(sdepForm->objid, newrole);
break;

case DefaultAclRelationId:

/*
* Ignore default ACLs; they should be handled by DROP
* OWNED, not REASSIGN OWNED.
*/
break;

case OperatorClassRelationId:
AlterOpClassOwner_oid(sdepForm->objid, newrole);
break;

case OperatorFamilyRelationId:
AlterOpFamilyOwner_oid(sdepForm->objid, newrole);
break;

case ForeignServerRelationId:
AlterForeignServerOwner_oid(sdepForm->objid, newrole);
break;

case ForeignDataWrapperRelationId:
AlterForeignDataWrapperOwner_oid(sdepForm->objid, newrole);
break;

default:
elog(ERROR, "unexpected classid %u", sdepForm->classid);
break;
}
/* Make sure the next iteration will see my changes */
CommandCounterIncrement();
}

systable_endscan(scan);
}

heap_close(sdepRel, RowExclusiveLock);
}
Something went wrong with that request. Please try again.