Skip to content

Commit daa69be

Browse files
authored
Keep temp reloid for columnar cases (#8309)
Fixes #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 #8252
1 parent bc41e7b commit daa69be

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);
@@ -684,10 +684,10 @@ SaveStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
684684
* SaveChunkGroups saves the metadata for given chunk groups in columnar.chunk_group.
685685
*/
686686
void
687-
SaveChunkGroups(RelFileLocator relfilelocator, uint64 stripe,
687+
SaveChunkGroups(Oid relid, RelFileLocator relfilelocator, uint64 stripe,
688688
List *chunkGroupRowCounts)
689689
{
690-
uint64 storageId = LookupStorageId(relfilelocator);
690+
uint64 storageId = LookupStorageId(relid, relfilelocator);
691691
Oid columnarChunkGroupOid = ColumnarChunkGroupRelationId();
692692
Relation columnarChunkGroup = table_open(columnarChunkGroupOid, RowExclusiveLock);
693693
ModifyState *modifyState = StartModifyRelation(columnarChunkGroup);
@@ -720,7 +720,7 @@ SaveChunkGroups(RelFileLocator relfilelocator, uint64 stripe,
720720
* ReadStripeSkipList fetches chunk metadata for a given stripe.
721721
*/
722722
StripeSkipList *
723-
ReadStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
723+
ReadStripeSkipList(Relation rel, uint64 stripe,
724724
TupleDesc tupleDescriptor,
725725
uint32 chunkCount, Snapshot snapshot)
726726
{
@@ -729,7 +729,8 @@ ReadStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
729729
uint32 columnCount = tupleDescriptor->natts;
730730
ScanKeyData scanKey[2];
731731

732-
uint64 storageId = LookupStorageId(relfilelocator);
732+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
733+
RelationPhysicalIdentifier_compat(rel));
733734

734735
Oid columnarChunkOid = ColumnarChunkRelationId();
735736
Relation columnarChunk = table_open(columnarChunkOid, AccessShareLock);
@@ -1273,9 +1274,10 @@ InsertEmptyStripeMetadataRow(uint64 storageId, uint64 stripeId, uint32 columnCou
12731274
* of the given relfilenode.
12741275
*/
12751276
List *
1276-
StripesForRelfilelocator(RelFileLocator relfilelocator)
1277+
StripesForRelfilelocator(Relation rel)
12771278
{
1278-
uint64 storageId = LookupStorageId(relfilelocator);
1279+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1280+
RelationPhysicalIdentifier_compat(rel));
12791281

12801282
/*
12811283
* PG18 requires snapshot to be active or registered before it's used
@@ -1304,9 +1306,10 @@ StripesForRelfilelocator(RelFileLocator relfilelocator)
13041306
* returns 0.
13051307
*/
13061308
uint64
1307-
GetHighestUsedAddress(RelFileLocator relfilelocator)
1309+
GetHighestUsedAddress(Relation rel)
13081310
{
1309-
uint64 storageId = LookupStorageId(relfilelocator);
1311+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1312+
RelationPhysicalIdentifier_compat(rel));
13101313

13111314
uint64 highestUsedAddress = 0;
13121315
uint64 highestUsedId = 0;
@@ -1316,6 +1319,24 @@ GetHighestUsedAddress(RelFileLocator relfilelocator)
13161319
}
13171320

13181321

1322+
/*
1323+
* In case if relid hasn't been defined yet, we should use RelidByRelfilenumber
1324+
* to get correct relid value.
1325+
*
1326+
* Now it is basically used for temp rels, because since PG18(it was backpatched
1327+
* through PG13) RelidByRelfilenumber skip temp relations and we should use
1328+
* alternative ways to get relid value in case of temp objects.
1329+
*/
1330+
Oid
1331+
ColumnarRelationId(Oid relid, RelFileLocator relfilelocator)
1332+
{
1333+
return OidIsValid(relid) ? relid : RelidByRelfilenumber(RelationTablespace_compat(
1334+
relfilelocator),
1335+
RelationPhysicalIdentifierNumber_compat(
1336+
relfilelocator));
1337+
}
1338+
1339+
13191340
/*
13201341
* GetHighestUsedAddressAndId returns the highest used address and id for
13211342
* the given relfilenode across all active and inactive transactions.
@@ -1591,7 +1612,7 @@ BuildStripeMetadata(Relation columnarStripes, HeapTuple heapTuple)
15911612
* metadata tables.
15921613
*/
15931614
void
1594-
DeleteMetadataRows(RelFileLocator relfilelocator)
1615+
DeleteMetadataRows(Relation rel)
15951616
{
15961617
/*
15971618
* During a restore for binary upgrade, metadata tables and indexes may or
@@ -1602,7 +1623,8 @@ DeleteMetadataRows(RelFileLocator relfilelocator)
16021623
return;
16031624
}
16041625

1605-
uint64 storageId = LookupStorageId(relfilelocator);
1626+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1627+
RelationPhysicalIdentifier_compat(rel));
16061628

16071629
DeleteStorageFromColumnarMetadataTable(ColumnarStripeRelationId(),
16081630
Anum_columnar_stripe_storageid,
@@ -2001,13 +2023,11 @@ ColumnarNamespaceId(void)
20012023
* false if the relation doesn't have a meta page yet.
20022024
*/
20032025
static uint64
2004-
LookupStorageId(RelFileLocator relfilelocator)
2026+
LookupStorageId(Oid relid, RelFileLocator relfilelocator)
20052027
{
2006-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(relfilelocator),
2007-
RelationPhysicalIdentifierNumber_compat(
2008-
relfilelocator));
2028+
relid = ColumnarRelationId(relid, relfilelocator);
20092029

2010-
Relation relation = relation_open(relationId, AccessShareLock);
2030+
Relation relation = relation_open(relid, AccessShareLock);
20112031
uint64 storageId = ColumnarStorageGetStorageId(relation, false);
20122032
table_close(relation, AccessShareLock);
20132033

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,15 +1236,15 @@ 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);
12481245

12491246
Snapshot snapshot = RegisterSnapshot(GetTransactionSnapshot());
1250-
StripeSkipList *skiplist = ReadStripeSkipList(relfilelocator, stripe->id,
1247+
StripeSkipList *skiplist = ReadStripeSkipList(rel, stripe->id,
12511248
RelationGetDescr(rel),
12521249
stripe->chunkCount,
12531250
snapshot);
@@ -1385,8 +1382,7 @@ TruncateColumnar(Relation rel, int elevel)
13851382
* new stripes be added beyond highestPhysicalAddress while
13861383
* we're truncating.
13871384
*/
1388-
uint64 newDataReservation = Max(GetHighestUsedAddress(
1389-
RelationPhysicalIdentifier_compat(rel)) + 1,
1385+
uint64 newDataReservation = Max(GetHighestUsedAddress(rel) + 1,
13901386
ColumnarFirstLogicalOffset);
13911387

13921388
BlockNumber old_rel_pages = smgrnblocks(RelationGetSmgr(rel), MAIN_FORKNUM);
@@ -2154,7 +2150,7 @@ ColumnarTableDropHook(Oid relid)
21542150
Relation rel = table_open(relid, AccessExclusiveLock);
21552151
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
21562152

2157-
DeleteMetadataRows(relfilelocator);
2153+
DeleteMetadataRows(rel);
21582154
DeleteColumnarTableOptions(rel->rd_id, true);
21592155

21602156
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)