Skip to content

Commit 308f836

Browse files
[BABEL] Fix the crash when dropping a temp table from nested QueryEnv (#706) (#715)
Fix the crash when dropping a temp table from nested QueryEnv. Extension PR : babelfish-for-postgresql/babelfish_extensions#4550 Task: [BABEL-6268] Authored by: harshdu@amazon.com
1 parent 05a299b commit 308f836

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/backend/utils/misc/queryenvironment.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,21 +1292,32 @@ void ENRDropEntry(Oid id)
12921292
{
12931293
EphemeralNamedRelation enr;
12941294
MemoryContext oldcxt;
1295+
QueryEnvironment *queryEnv;
12951296

1296-
if (sql_dialect != SQL_DIALECT_TSQL || !currentQueryEnv)
1297+
if ((sql_dialect != SQL_DIALECT_TSQL && !(is_bbf_tds_connection_hook && is_bbf_tds_connection_hook())) || !currentQueryEnv)
12971298
return;
12981299

1299-
if ((enr = GetENRTempTableWithOid(id, false)) == NULL)
1300+
/* Find the ENR and which query environment contains it */
1301+
queryEnv = currentQueryEnv;
1302+
while (queryEnv)
1303+
{
1304+
if ((enr = get_ENR_withoid(queryEnv, id, ENR_TSQL_TEMP, false)) != NULL)
1305+
break;
1306+
queryEnv = queryEnv->parentEnv;
1307+
}
1308+
1309+
/* ENR not found in any environment */
1310+
if (!enr)
13001311
return;
13011312

1302-
oldcxt = MemoryContextSwitchTo(currentQueryEnv->memctx);
1303-
currentQueryEnv->namedRelList = list_delete(currentQueryEnv->namedRelList, enr);
1313+
oldcxt = MemoryContextSwitchTo(queryEnv->memctx);
1314+
queryEnv->namedRelList = list_delete_ptr(queryEnv->namedRelList, enr);
13041315

13051316
/* If we are dropping a committed ENR, wait until COMMIT to free it. */
13061317
if (temp_table_xact_support && enr->md.is_bbf_temp_table)
13071318
{
13081319
enr->md.dropped_subid = GetCurrentSubTransactionId();
1309-
currentQueryEnv->dropped_namedRelList = lappend(currentQueryEnv->dropped_namedRelList, enr);
1320+
queryEnv->dropped_namedRelList = lappend(queryEnv->dropped_namedRelList, enr);
13101321
}
13111322
else
13121323
{

0 commit comments

Comments
 (0)