Skip to content

Commit

Permalink
Fix error when trying to drop columns from temp tables. (babelfish-fo…
Browse files Browse the repository at this point in the history
…r-postgresql#410) (babelfish-for-postgresql#414)

Our original code for ENRgetSystableScan did not take into account the fact that pg_depend scans can use up to three keys. Fix the issue by making sure to use the third key if it is supplied.

Also fix the logic around performing the deletion so that we don't try to drop the entire ENR table when deleting columns.

Backport to PG15 as well.

Task: BABEL-4912

Signed-off-by: Jason Teng <[email protected]>
  • Loading branch information
Sairakan authored Jul 24, 2024
1 parent a9d512c commit 0749a9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/backend/catalog/dependency.c
Original file line number Diff line number Diff line change
Expand Up @@ -1383,8 +1383,14 @@ deleteOneObject(const ObjectAddress *object, Relation *depRel, int flags)
DeleteSecurityLabel(object);
DeleteInitPrivs(object);

// Delete from ENR - noop if not found from ENR
ENRDropEntry(object->objectId);
/*
* If objectSubId != 0, then this is a column. There are no ENR entries
* for individual columns, so skip ENRDropEntry in this case (or else we
* will delete the entire table instead of just the column). Note that this
* is a no-op if the objectId is not found from ENR.
*/
if (object->objectSubId == 0)
ENRDropEntry(object->objectId);

/*
* CommandCounterIncrement here to ensure that preceding changes are all
Expand Down
10 changes: 6 additions & 4 deletions src/backend/utils/misc/queryenvironment.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,25 +435,27 @@ bool ENRgetSystableScan(Relation rel, Oid indexId, int nkeys, ScanKey key, List
* Search through the entire ENR relation list for everything
* that has a relation (non-recursive) to this object.
* If indexId is DependDependerIndexId, we try to mimic
* SELECT * FROM pg_depend WHERE classid=v1 AND objid=v2
* SELECT * FROM pg_depend WHERE classid=v1 AND objid=v2 (AND objsubid = v3 if applicable)
* Otherwise if it is DependReferenceIndexId we try to mimic
* SELECT * FROM pg_depend WHERE refclassid=v1 AND refobjid=v2
* SELECT * FROM pg_depend WHERE refclassid=v1 AND refobjid=v2 (AND refobjsubid = v3 if applicable)
* So we cannot return right away if there is a match.
*/
ListCell *lc;
foreach(lc, enr->md.cattups[ENR_CATTUP_DEPEND]) {
Form_pg_depend tup = (Form_pg_depend) GETSTRUCT((HeapTuple) lfirst(lc));
if (indexId == DependDependerIndexId &&
tup->classid == (Oid)v1 &&
tup->objid == (Oid)v2)
tup->objid == (Oid)v2 &&
(nkeys == 2 || tup->objsubid == (int32)v3))
{
*tuplist = list_insert_nth(*tuplist, index++, lfirst(lc));
*tuplist_flags |= SYSSCAN_ENR_NEEDFREE;
found = true;
}
else if (indexId == DependReferenceIndexId &&
tup->refclassid == (Oid)v1 &&
tup->refobjid == (Oid)v2)
tup->refobjid == (Oid)v2 &&
(nkeys == 2 || tup->refobjsubid == (int32)v3))
{
*tuplist = list_insert_nth(*tuplist, index++, lfirst(lc));
*tuplist_flags |= SYSSCAN_ENR_NEEDFREE;
Expand Down

0 comments on commit 0749a9b

Please sign in to comment.