diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2020-03-11 11:04:59 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2020-03-11 11:04:59 -0300 |
commit | 73b31e784d64a4f24644de11d02fa632fe97bfab (patch) | |
tree | 2257cdf020b6d6942ad6c96724a83f5775b03a49 /src/backend | |
parent | fab5456356c24fa36cab455e7d4275393d175654 (diff) |
Avoid duplicates in ALTER ... DEPENDS ON EXTENSION
If the command is attempted for an extension that the object already
depends on, silently do nothing.
In particular, this means that if a database containing multiple such
entries is dumped, the restore will silently do the right thing and
record just the first one. (At least, in a world where pg_dump does
dump such entries -- which it doesn't currently, but it will.)
Backpatch to 9.6, where this kind of dependency was introduced.
Reviewed-by: Ibrar Ahmed, Tom Lane (offlist)
Discussion: https://postgr.es/m/20200217225333.GA30974@alvherre.pgsql
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/catalog/pg_depend.c | 43 | ||||
-rw-r--r-- | src/backend/commands/alter.c | 7 |
2 files changed, 49 insertions, 1 deletions
diff --git a/src/backend/catalog/pg_depend.c b/src/backend/catalog/pg_depend.c index 6d935b4aa88..0fbac49083d 100644 --- a/src/backend/catalog/pg_depend.c +++ b/src/backend/catalog/pg_depend.c @@ -489,6 +489,49 @@ getExtensionOfObject(Oid classId, Oid objectId) } /* + * Return (possibly NIL) list of extensions that the given object depends on + * in DEPENDENCY_AUTO_EXTENSION mode. + */ +List * +getAutoExtensionsOfObject(Oid classId, Oid objectId) +{ + List *result = NIL; + Relation depRel; + ScanKeyData key[2]; + SysScanDesc scan; + HeapTuple tup; + + depRel = heap_open(DependRelationId, AccessShareLock); + + ScanKeyInit(&key[0], + Anum_pg_depend_classid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(classId)); + ScanKeyInit(&key[1], + Anum_pg_depend_objid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(objectId)); + + scan = systable_beginscan(depRel, DependDependerIndexId, true, + NULL, 2, key); + + while (HeapTupleIsValid((tup = systable_getnext(scan)))) + { + Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup); + + if (depform->refclassid == ExtensionRelationId && + depform->deptype == DEPENDENCY_AUTO_EXTENSION) + result = lappend_oid(result, depform->refobjid); + } + + systable_endscan(scan); + + heap_close(depRel, AccessShareLock); + + return result; +} + +/* * Detect whether a sequence is marked as "owned" by a column * * An ownership marker is an AUTO dependency from the sequence to the diff --git a/src/backend/commands/alter.c b/src/backend/commands/alter.c index 3ef3bd4b067..b12f8a60887 100644 --- a/src/backend/commands/alter.c +++ b/src/backend/commands/alter.c @@ -403,6 +403,7 @@ ExecAlterObjectDependsStmt(AlterObjectDependsStmt *stmt, ObjectAddress *refAddre ObjectAddress address; ObjectAddress refAddr; Relation rel; + List *currexts; address = get_object_address_rv(stmt->objectType, stmt->relation, stmt->objname, @@ -434,7 +435,11 @@ ExecAlterObjectDependsStmt(AlterObjectDependsStmt *stmt, ObjectAddress *refAddre if (refAddress) *refAddress = refAddr; - recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION); + /* Avoid duplicates */ + currexts = getAutoExtensionsOfObject(address.classId, + address.objectId); + if (!list_member_oid(currexts, refAddr.objectId)) + recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION); return address; } |