Skip to content

Commit 032b410

Browse files
authored
Fix: Temp table indexes with name starting without '#' do not follow rollback semantics (babelfish-for-postgresql#626) (babelfish-for-postgresql#628)
* Fix: Temp table indexes with name starting without '#' do not follow rollback semantics We use the is_bbf_temp_table flag in ENR metadata for transaction behaviour. Currently, we only mark this flag as true if the relname starts with '#', however all indexes on ENR temp tables should be have this flag set. We are refactoring the register_ENR function and marking this flag early rather than in this method Cherry pick - 6733da6 Original PR - babelfish-for-postgresql#626 Signed-off-by: Ayush Shah <ayushdsh@amazon.com>
1 parent ce7523a commit 032b410

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

src/backend/catalog/heap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ heap_create_with_catalog(const char *relname,
13851385
strncpy(enr->md.name, relname, strlen(relname) + 1);
13861386
enr->md.reliddesc = relid;
13871387
enr->md.enrtype = ENR_TSQL_TEMP;
1388+
enr->md.parent_oid = InvalidOid;
13881389
register_ENR(currentQueryEnv, enr);
13891390
MemoryContextSwitchTo(oldcontext);
13901391
}

src/backend/catalog/index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ index_create(Relation heapRelation,
10021002
strncpy(enr->md.name, indexRelationName, strlen(indexRelationName) + 1);
10031003
enr->md.reliddesc = indexRelationId;
10041004
enr->md.enrtype = ENR_TSQL_TEMP;
1005+
enr->md.parent_oid = heapRelationId;
10051006
register_ENR(currentQueryEnv, enr);
10061007
MemoryContextSwitchTo(oldcontext);
10071008
}

src/backend/executor/spi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,7 @@ SPI_register_trigger_data(TriggerData *tdata)
34263426
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
34273427
enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_newtable);
34283428
enr->reldata = tdata->tg_newtable;
3429+
enr->md.parent_oid = InvalidOid;
34293430
rc = SPI_register_relation(enr);
34303431
if (rc != SPI_OK_REL_REGISTER)
34313432
return rc;
@@ -3443,6 +3444,7 @@ SPI_register_trigger_data(TriggerData *tdata)
34433444
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
34443445
enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_oldtable);
34453446
enr->reldata = tdata->tg_oldtable;
3447+
enr->md.parent_oid = InvalidOid;
34463448
rc = SPI_register_relation(enr);
34473449
if (rc != SPI_OK_REL_REGISTER)
34483450
return rc;

src/backend/utils/misc/queryenvironment.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,30 @@ register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr)
212212
Assert(enr != NULL);
213213
Assert(get_ENR(queryEnv, enr->md.name, false) == NULL);
214214

215-
if (enr->md.name[0] == '#')
216-
enr->md.is_bbf_temp_table = true;
217-
else
218-
enr->md.is_bbf_temp_table = false;
215+
if (enr->md.parent_oid != InvalidOid)
216+
{
217+
/*
218+
* This is an index on a ENR temp object. We mark the flag by checking
219+
* the parent relations flag.
220+
* If it is an implicit index over tsql table variable, it gets marked
221+
* as false, same as parent since table variable don't follow transactional
222+
* semantics.
223+
*/
224+
EphemeralNamedRelation parent_enr = GetENRTempTableWithOid(enr->md.parent_oid);
225+
if(parent_enr)
226+
enr->md.is_bbf_temp_table = parent_enr->md.is_bbf_temp_table;
227+
else
228+
enr->md.is_bbf_temp_table = false;
229+
} else {
230+
/*
231+
* This is a table or some parent entity. If the name of this table
232+
* starts with '#', this is a tsql temp table.
233+
*/
234+
if (enr->md.name[0] == '#')
235+
enr->md.is_bbf_temp_table = true;
236+
else
237+
enr->md.is_bbf_temp_table = false;
238+
}
219239

220240
if (enr->md.is_bbf_temp_table && GetCurrentSubTransactionId() != InvalidSubTransactionId)
221241
enr->md.created_subid = GetCurrentSubTransactionId();

src/include/utils/queryenvironment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ typedef struct EphemeralNamedRelationMetadataData
100100
SubTransactionId dropped_subid;
101101
/* List of uncommitted tuples. They must be processed on ROLLBACK, or cleared on commit. */
102102
List *uncommitted_cattups[ENR_CATTUP_END];
103+
/* Maintain parent oid for tracking if child objects should follow transactional semantics based on parent nature */
104+
Oid parent_oid;
103105
} EphemeralNamedRelationMetadataData;
104106

105107
typedef EphemeralNamedRelationMetadataData *EphemeralNamedRelationMetadata;

0 commit comments

Comments
 (0)