Skip to content

Commit a87e40b

Browse files
ayushdshYashneet Vinayak
authored andcommitted
Fix: Temp table indexes with name starting without '#' do not follow rollback semantics (#626)
* 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 Signed-off-by: Ayush Shah <[email protected]>
1 parent 4b5a02a commit a87e40b

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
@@ -1395,6 +1395,7 @@ heap_create_with_catalog(const char *relname,
13951395
strncpy(enr->md.name, relname, strlen(relname) + 1);
13961396
enr->md.reliddesc = relid;
13971397
enr->md.enrtype = ENR_TSQL_TEMP;
1398+
enr->md.parent_oid = InvalidOid;
13981399
register_ENR(currentQueryEnv, enr);
13991400
MemoryContextSwitchTo(oldcontext);
14001401
}

src/backend/catalog/index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ index_create(Relation heapRelation,
10141014
strncpy(enr->md.name, indexRelationName, strlen(indexRelationName) + 1);
10151015
enr->md.reliddesc = indexRelationId;
10161016
enr->md.enrtype = ENR_TSQL_TEMP;
1017+
enr->md.parent_oid = heapRelationId;
10171018
register_ENR(currentQueryEnv, enr);
10181019
MemoryContextSwitchTo(oldcontext);
10191020
}

src/backend/executor/spi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,6 +3430,7 @@ SPI_register_trigger_data(TriggerData *tdata)
34303430
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
34313431
enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_newtable);
34323432
enr->reldata = tdata->tg_newtable;
3433+
enr->md.parent_oid = InvalidOid;
34333434
rc = SPI_register_relation(enr);
34343435
if (rc != SPI_OK_REL_REGISTER)
34353436
return rc;
@@ -3447,6 +3448,7 @@ SPI_register_trigger_data(TriggerData *tdata)
34473448
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
34483449
enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_oldtable);
34493450
enr->reldata = tdata->tg_oldtable;
3451+
enr->md.parent_oid = InvalidOid;
34503452
rc = SPI_register_relation(enr);
34513453
if (rc != SPI_OK_REL_REGISTER)
34523454
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)