Skip to content

Commit 7d5e783

Browse files
committed
Keep temp reloid for columnar cases (citusdata#8309)
Backporting citusdata#8235 PG18 and PG latest minors ignore temporary relations in `RelidByRelfilenumber` (`RelidByRelfilenode` in PG15) Relevant PG commit: postgres/postgres@86831952 Here we are keeping temp reloids instead of getting it with RelidByRelfilenumber, for example, in some cases, we can directly get reloid from relations, in other cases we keep it in some structures. Note: there is still an outstanding issue with columnar temp tables in concurrent sessions, that will be fixed in PR citusdata#8252 (cherry picked from commit daa69be)
1 parent 9aa1384 commit 7d5e783

File tree

8 files changed

+87
-58
lines changed

8 files changed

+87
-58
lines changed

src/backend/columnar/columnar_customscan.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,8 +1556,7 @@ ColumnarPerStripeScanCost(RelOptInfo *rel, Oid relationId, int numberOfColumnsRe
15561556
ereport(ERROR, (errmsg("could not open relation with OID %u", relationId)));
15571557
}
15581558

1559-
List *stripeList = StripesForRelfilelocator(RelationPhysicalIdentifier_compat(
1560-
relation));
1559+
List *stripeList = StripesForRelfilelocator(relation);
15611560
RelationClose(relation);
15621561

15631562
uint32 maxColumnCount = 0;
@@ -1614,8 +1613,7 @@ ColumnarTableStripeCount(Oid relationId)
16141613
ereport(ERROR, (errmsg("could not open relation with OID %u", relationId)));
16151614
}
16161615

1617-
List *stripeList = StripesForRelfilelocator(RelationPhysicalIdentifier_compat(
1618-
relation));
1616+
List *stripeList = StripesForRelfilelocator(relation);
16191617
int stripeCount = list_length(stripeList);
16201618
RelationClose(relation);
16211619

src/backend/columnar/columnar_metadata.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static Oid ColumnarChunkGroupRelationId(void);
125125
static Oid ColumnarChunkIndexRelationId(void);
126126
static Oid ColumnarChunkGroupIndexRelationId(void);
127127
static Oid ColumnarNamespaceId(void);
128-
static uint64 LookupStorageId(RelFileLocator relfilelocator);
128+
static uint64 LookupStorageId(Oid relationId, RelFileLocator relfilelocator);
129129
static uint64 GetHighestUsedRowNumber(uint64 storageId);
130130
static void DeleteStorageFromColumnarMetadataTable(Oid metadataTableId,
131131
AttrNumber storageIdAtrrNumber,
@@ -606,15 +606,15 @@ ReadColumnarOptions(Oid regclass, ColumnarOptions *options)
606606
* of columnar.chunk.
607607
*/
608608
void
609-
SaveStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
609+
SaveStripeSkipList(Oid relid, RelFileLocator relfilelocator, uint64 stripe,
610610
StripeSkipList *chunkList,
611611
TupleDesc tupleDescriptor)
612612
{
613613
uint32 columnIndex = 0;
614614
uint32 chunkIndex = 0;
615615
uint32 columnCount = chunkList->columnCount;
616616

617-
uint64 storageId = LookupStorageId(relfilelocator);
617+
uint64 storageId = LookupStorageId(relid, relfilelocator);
618618
Oid columnarChunkOid = ColumnarChunkRelationId();
619619
Relation columnarChunk = table_open(columnarChunkOid, RowExclusiveLock);
620620
ModifyState *modifyState = StartModifyRelation(columnarChunk);
@@ -674,10 +674,10 @@ SaveStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
674674
* SaveChunkGroups saves the metadata for given chunk groups in columnar.chunk_group.
675675
*/
676676
void
677-
SaveChunkGroups(RelFileLocator relfilelocator, uint64 stripe,
677+
SaveChunkGroups(Oid relid, RelFileLocator relfilelocator, uint64 stripe,
678678
List *chunkGroupRowCounts)
679679
{
680-
uint64 storageId = LookupStorageId(relfilelocator);
680+
uint64 storageId = LookupStorageId(relid, relfilelocator);
681681
Oid columnarChunkGroupOid = ColumnarChunkGroupRelationId();
682682
Relation columnarChunkGroup = table_open(columnarChunkGroupOid, RowExclusiveLock);
683683
ModifyState *modifyState = StartModifyRelation(columnarChunkGroup);
@@ -710,7 +710,7 @@ SaveChunkGroups(RelFileLocator relfilelocator, uint64 stripe,
710710
* ReadStripeSkipList fetches chunk metadata for a given stripe.
711711
*/
712712
StripeSkipList *
713-
ReadStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
713+
ReadStripeSkipList(Relation rel, uint64 stripe,
714714
TupleDesc tupleDescriptor,
715715
uint32 chunkCount, Snapshot snapshot)
716716
{
@@ -719,7 +719,8 @@ ReadStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
719719
uint32 columnCount = tupleDescriptor->natts;
720720
ScanKeyData scanKey[2];
721721

722-
uint64 storageId = LookupStorageId(relfilelocator);
722+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
723+
RelationPhysicalIdentifier_compat(rel));
723724

724725
Oid columnarChunkOid = ColumnarChunkRelationId();
725726
Relation columnarChunk = table_open(columnarChunkOid, AccessShareLock);
@@ -1263,9 +1264,10 @@ InsertEmptyStripeMetadataRow(uint64 storageId, uint64 stripeId, uint32 columnCou
12631264
* of the given relfilenode.
12641265
*/
12651266
List *
1266-
StripesForRelfilelocator(RelFileLocator relfilelocator)
1267+
StripesForRelfilelocator(Relation rel)
12671268
{
1268-
uint64 storageId = LookupStorageId(relfilelocator);
1269+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1270+
RelationPhysicalIdentifier_compat(rel));
12691271

12701272
return ReadDataFileStripeList(storageId, GetTransactionSnapshot());
12711273
}
@@ -1280,9 +1282,10 @@ StripesForRelfilelocator(RelFileLocator relfilelocator)
12801282
* returns 0.
12811283
*/
12821284
uint64
1283-
GetHighestUsedAddress(RelFileLocator relfilelocator)
1285+
GetHighestUsedAddress(Relation rel)
12841286
{
1285-
uint64 storageId = LookupStorageId(relfilelocator);
1287+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1288+
RelationPhysicalIdentifier_compat(rel));
12861289

12871290
uint64 highestUsedAddress = 0;
12881291
uint64 highestUsedId = 0;
@@ -1292,6 +1295,24 @@ GetHighestUsedAddress(RelFileLocator relfilelocator)
12921295
}
12931296

12941297

1298+
/*
1299+
* In case if relid hasn't been defined yet, we should use RelidByRelfilenumber
1300+
* to get correct relid value.
1301+
*
1302+
* Now it is basically used for temp rels, because since PG18(it was backpatched
1303+
* through PG13) RelidByRelfilenumber skip temp relations and we should use
1304+
* alternative ways to get relid value in case of temp objects.
1305+
*/
1306+
Oid
1307+
ColumnarRelationId(Oid relid, RelFileLocator relfilelocator)
1308+
{
1309+
return OidIsValid(relid) ? relid : RelidByRelfilenumber(RelationTablespace_compat(
1310+
relfilelocator),
1311+
RelationPhysicalIdentifierNumber_compat(
1312+
relfilelocator));
1313+
}
1314+
1315+
12951316
/*
12961317
* GetHighestUsedAddressAndId returns the highest used address and id for
12971318
* the given relfilenode across all active and inactive transactions.
@@ -1595,7 +1616,7 @@ BuildStripeMetadata(Relation columnarStripes, HeapTuple heapTuple)
15951616
* metadata tables.
15961617
*/
15971618
void
1598-
DeleteMetadataRows(RelFileLocator relfilelocator)
1619+
DeleteMetadataRows(Relation rel)
15991620
{
16001621
/*
16011622
* During a restore for binary upgrade, metadata tables and indexes may or
@@ -1606,7 +1627,8 @@ DeleteMetadataRows(RelFileLocator relfilelocator)
16061627
return;
16071628
}
16081629

1609-
uint64 storageId = LookupStorageId(relfilelocator);
1630+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1631+
RelationPhysicalIdentifier_compat(rel));
16101632

16111633
DeleteStorageFromColumnarMetadataTable(ColumnarStripeRelationId(),
16121634
Anum_columnar_stripe_storageid,
@@ -2005,13 +2027,11 @@ ColumnarNamespaceId(void)
20052027
* false if the relation doesn't have a meta page yet.
20062028
*/
20072029
static uint64
2008-
LookupStorageId(RelFileLocator relfilelocator)
2030+
LookupStorageId(Oid relid, RelFileLocator relfilelocator)
20092031
{
2010-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(relfilelocator),
2011-
RelationPhysicalIdentifierNumber_compat(
2012-
relfilelocator));
2032+
relid = ColumnarRelationId(relid, relfilelocator);
20132033

2014-
Relation relation = relation_open(relationId, AccessShareLock);
2034+
Relation relation = relation_open(relid, AccessShareLock);
20152035
uint64 storageId = ColumnarStorageGetStorageId(relation, false);
20162036
table_close(relation, AccessShareLock);
20172037

src/backend/columnar/columnar_reader.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,7 @@ ColumnarTableRowCount(Relation relation)
986986
{
987987
ListCell *stripeMetadataCell = NULL;
988988
uint64 totalRowCount = 0;
989-
List *stripeList = StripesForRelfilelocator(RelationPhysicalIdentifier_compat(
990-
relation));
989+
List *stripeList = StripesForRelfilelocator(relation);
991990

992991
foreach(stripeMetadataCell, stripeList)
993992
{
@@ -1015,8 +1014,7 @@ LoadFilteredStripeBuffers(Relation relation, StripeMetadata *stripeMetadata,
10151014

10161015
bool *projectedColumnMask = ProjectedColumnMask(columnCount, projectedColumnList);
10171016

1018-
StripeSkipList *stripeSkipList = ReadStripeSkipList(RelationPhysicalIdentifier_compat(
1019-
relation),
1017+
StripeSkipList *stripeSkipList = ReadStripeSkipList(relation,
10201018
stripeMetadata->id,
10211019
tupleDescriptor,
10221020
stripeMetadata->chunkCount,

src/backend/columnar/columnar_tableam.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ columnar_relation_set_new_filelocator(Relation rel,
872872
RelationPhysicalIdentifier_compat(rel)),
873873
GetCurrentSubTransactionId());
874874

875-
DeleteMetadataRows(RelationPhysicalIdentifier_compat(rel));
875+
DeleteMetadataRows(rel);
876876
}
877877

878878
*freezeXid = RecentXmin;
@@ -897,7 +897,7 @@ columnar_relation_nontransactional_truncate(Relation rel)
897897
NonTransactionDropWriteState(RelationPhysicalIdentifierNumber_compat(relfilelocator));
898898

899899
/* Delete old relfilenode metadata */
900-
DeleteMetadataRows(relfilelocator);
900+
DeleteMetadataRows(rel);
901901

902902
/*
903903
* No need to set new relfilenode, since the table was created in this
@@ -960,8 +960,7 @@ columnar_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap,
960960
ColumnarOptions columnarOptions = { 0 };
961961
ReadColumnarOptions(OldHeap->rd_id, &columnarOptions);
962962

963-
ColumnarWriteState *writeState = ColumnarBeginWrite(RelationPhysicalIdentifier_compat(
964-
NewHeap),
963+
ColumnarWriteState *writeState = ColumnarBeginWrite(NewHeap,
965964
columnarOptions,
966965
targetDesc);
967966

@@ -1036,8 +1035,7 @@ NeededColumnsList(TupleDesc tupdesc, Bitmapset *attr_needed)
10361035
static uint64
10371036
ColumnarTableTupleCount(Relation relation)
10381037
{
1039-
List *stripeList = StripesForRelfilelocator(RelationPhysicalIdentifier_compat(
1040-
relation));
1038+
List *stripeList = StripesForRelfilelocator(relation);
10411039
uint64 tupleCount = 0;
10421040

10431041
ListCell *lc = NULL;
@@ -1228,7 +1226,6 @@ static void
12281226
LogRelationStats(Relation rel, int elevel)
12291227
{
12301228
ListCell *stripeMetadataCell = NULL;
1231-
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
12321229
StringInfo infoBuf = makeStringInfo();
12331230

12341231
int compressionStats[COMPRESSION_COUNT] = { 0 };
@@ -1239,13 +1236,13 @@ LogRelationStats(Relation rel, int elevel)
12391236
uint64 droppedChunksWithData = 0;
12401237
uint64 totalDecompressedLength = 0;
12411238

1242-
List *stripeList = StripesForRelfilelocator(relfilelocator);
1239+
List *stripeList = StripesForRelfilelocator(rel);
12431240
int stripeCount = list_length(stripeList);
12441241

12451242
foreach(stripeMetadataCell, stripeList)
12461243
{
12471244
StripeMetadata *stripe = lfirst(stripeMetadataCell);
1248-
StripeSkipList *skiplist = ReadStripeSkipList(relfilelocator, stripe->id,
1245+
StripeSkipList *skiplist = ReadStripeSkipList(rel, stripe->id,
12491246
RelationGetDescr(rel),
12501247
stripe->chunkCount,
12511248
GetTransactionSnapshot());
@@ -1381,8 +1378,7 @@ TruncateColumnar(Relation rel, int elevel)
13811378
* new stripes be added beyond highestPhysicalAddress while
13821379
* we're truncating.
13831380
*/
1384-
uint64 newDataReservation = Max(GetHighestUsedAddress(
1385-
RelationPhysicalIdentifier_compat(rel)) + 1,
1381+
uint64 newDataReservation = Max(GetHighestUsedAddress(rel) + 1,
13861382
ColumnarFirstLogicalOffset);
13871383

13881384
BlockNumber old_rel_pages = smgrnblocks(RelationGetSmgr(rel), MAIN_FORKNUM);
@@ -2150,7 +2146,7 @@ ColumnarTableDropHook(Oid relid)
21502146
Relation rel = table_open(relid, AccessExclusiveLock);
21512147
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
21522148

2153-
DeleteMetadataRows(relfilelocator);
2149+
DeleteMetadataRows(rel);
21542150
DeleteColumnarTableOptions(rel->rd_id, true);
21552151

21562152
MarkRelfilenumberDropped(RelationPhysicalIdentifierNumber_compat(relfilelocator),

src/backend/columnar/columnar_writer.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ struct ColumnarWriteState
4848
FmgrInfo **comparisonFunctionArray;
4949
RelFileLocator relfilelocator;
5050

51+
/*
52+
* We can't rely on RelidByRelfilenumber for temp tables since
53+
* PG18(it was backpatched through PG13).
54+
*/
55+
Oid temp_relid;
56+
5157
MemoryContext stripeWriteContext;
5258
MemoryContext perTupleContext;
5359
StripeBuffers *stripeBuffers;
@@ -93,10 +99,12 @@ static StringInfo CopyStringInfo(StringInfo sourceString);
9399
* data load operation.
94100
*/
95101
ColumnarWriteState *
96-
ColumnarBeginWrite(RelFileLocator relfilelocator,
102+
ColumnarBeginWrite(Relation rel,
97103
ColumnarOptions options,
98104
TupleDesc tupleDescriptor)
99105
{
106+
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
107+
100108
/* get comparison function pointers for each of the columns */
101109
uint32 columnCount = tupleDescriptor->natts;
102110
FmgrInfo **comparisonFunctionArray = palloc0(columnCount * sizeof(FmgrInfo *));
@@ -134,6 +142,7 @@ ColumnarBeginWrite(RelFileLocator relfilelocator,
134142

135143
ColumnarWriteState *writeState = palloc0(sizeof(ColumnarWriteState));
136144
writeState->relfilelocator = relfilelocator;
145+
writeState->temp_relid = RelationPrecomputeOid(rel);
137146
writeState->options = options;
138147
writeState->tupleDescriptor = CreateTupleDescCopy(tupleDescriptor);
139148
writeState->comparisonFunctionArray = comparisonFunctionArray;
@@ -183,10 +192,9 @@ ColumnarWriteRow(ColumnarWriteState *writeState, Datum *columnValues, bool *colu
183192
writeState->stripeSkipList = stripeSkipList;
184193
writeState->compressionBuffer = makeStringInfo();
185194

186-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(
187-
writeState->relfilelocator),
188-
RelationPhysicalIdentifierNumber_compat(
189-
writeState->relfilelocator));
195+
Oid relationId = ColumnarRelationId(writeState->temp_relid,
196+
writeState->relfilelocator);
197+
190198
Relation relation = relation_open(relationId, NoLock);
191199
writeState->emptyStripeReservation =
192200
ReserveEmptyStripe(relation, columnCount, chunkRowCount,
@@ -404,10 +412,9 @@ FlushStripe(ColumnarWriteState *writeState)
404412

405413
elog(DEBUG1, "Flushing Stripe of size %d", stripeBuffers->rowCount);
406414

407-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(
408-
writeState->relfilelocator),
409-
RelationPhysicalIdentifierNumber_compat(
410-
writeState->relfilelocator));
415+
Oid relationId = ColumnarRelationId(writeState->temp_relid,
416+
writeState->relfilelocator);
417+
411418
Relation relation = relation_open(relationId, NoLock);
412419

413420
/*
@@ -499,10 +506,12 @@ FlushStripe(ColumnarWriteState *writeState)
499506
}
500507
}
501508

502-
SaveChunkGroups(writeState->relfilelocator,
509+
SaveChunkGroups(writeState->temp_relid,
510+
writeState->relfilelocator,
503511
stripeMetadata->id,
504512
writeState->chunkGroupRowCounts);
505-
SaveStripeSkipList(writeState->relfilelocator,
513+
SaveStripeSkipList(writeState->temp_relid,
514+
writeState->relfilelocator,
506515
stripeMetadata->id,
507516
stripeSkipList, tupleDescriptor);
508517

src/backend/columnar/write_state_management.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ columnar_init_write_state(Relation relation, TupleDesc tupdesc,
191191
ReadColumnarOptions(tupSlotRelationId, &columnarOptions);
192192

193193
SubXidWriteState *stackEntry = palloc0(sizeof(SubXidWriteState));
194-
stackEntry->writeState = ColumnarBeginWrite(RelationPhysicalIdentifier_compat(
195-
relation),
194+
stackEntry->writeState = ColumnarBeginWrite(relation,
196195
columnarOptions,
197196
tupdesc);
198197
stackEntry->subXid = currentSubXid;

0 commit comments

Comments
 (0)