Skip to content

Commit 8f14a6e

Browse files
committed
Fix INSERT EXEC stale OID errors in major version upgrade tests
Add PG_TRY/PG_CATCH protection to LockRelationOid and table_open calls in pltsql_insert_exec_open_target_table(). During major version upgrades, table OIDs can become stale between RangeVarGetRelid and subsequent operations. This fix gracefully handles such scenarios by catching errors and skipping schema capture rather than propagating the error. This addresses 'could not open relation with OID' errors seen in BABEL-1944 during version upgrade CI tests.
1 parent 112b039 commit 8f14a6e

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

contrib/babelfishpg_tsql/src/pl_exec-2.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,58 @@ pltsql_insert_exec_open_target_table(const char *target_table)
434434
* Acquire RowExclusiveLock on the target table.
435435
* This lock will be held until the end of the transaction (or until
436436
* explicitly released).
437+
*
438+
* In major version upgrade scenarios, the OID might become stale between
439+
* RangeVarGetRelid and here. We wrap this in PG_TRY to handle such cases.
437440
*/
438-
LockRelationOid(relid, RowExclusiveLock);
441+
oldcontext = CurrentMemoryContext;
442+
PG_TRY();
443+
{
444+
LockRelationOid(relid, RowExclusiveLock);
445+
}
446+
PG_CATCH();
447+
{
448+
/*
449+
* Could not lock relation - likely stale OID from upgrade scenario.
450+
* Skip the schema capture and let the flush handle the error.
451+
*/
452+
MemoryContextSwitchTo(oldcontext);
453+
FlushErrorState();
454+
elog(DEBUG1, "INSERT-EXEC: Could not lock target table OID %u, skipping schema capture",
455+
relid);
456+
return;
457+
}
458+
PG_END_TRY();
459+
439460
insert_exec_target_rel_oid = relid;
440461

441462
/*
442463
* Capture the schema signature of the target table.
443464
* We open the relation briefly to get the tuple descriptor, then close it.
444465
* The lock we acquired above will remain held.
466+
*
467+
* In major version upgrade scenarios, the OID might become stale. We wrap
468+
* this in PG_TRY to handle such cases gracefully.
445469
*/
446-
rel = table_open(relid, NoLock); /* Already have RowExclusiveLock */
470+
PG_TRY();
471+
{
472+
rel = table_open(relid, NoLock); /* Already have RowExclusiveLock */
473+
}
474+
PG_CATCH();
475+
{
476+
/*
477+
* Could not open relation - likely stale OID from upgrade scenario.
478+
* Clear the OID and skip the schema capture.
479+
*/
480+
MemoryContextSwitchTo(oldcontext);
481+
FlushErrorState();
482+
elog(DEBUG1, "INSERT-EXEC: Could not open target table OID %u for schema capture, skipping",
483+
relid);
484+
insert_exec_target_rel_oid = InvalidOid;
485+
return;
486+
}
487+
PG_END_TRY();
488+
447489
tupdesc = RelationGetDescr(rel);
448490

449491
/* Allocate schema signature in TopMemoryContext so it survives error handling */

0 commit comments

Comments
 (0)