Skip to content

Commit ef7453c

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 2433c98 commit ef7453c

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
@@ -1549,8 +1549,7 @@ ColumnarPerStripeScanCost(RelOptInfo *rel, Oid relationId, int numberOfColumnsRe
15491549
ereport(ERROR, (errmsg("could not open relation with OID %u", relationId)));
15501550
}
15511551

1552-
List *stripeList = StripesForRelfilelocator(RelationPhysicalIdentifier_compat(
1553-
relation));
1552+
List *stripeList = StripesForRelfilelocator(relation);
15541553
RelationClose(relation);
15551554

15561555
uint32 maxColumnCount = 0;
@@ -1607,8 +1606,7 @@ ColumnarTableStripeCount(Oid relationId)
16071606
ereport(ERROR, (errmsg("could not open relation with OID %u", relationId)));
16081607
}
16091608

1610-
List *stripeList = StripesForRelfilelocator(RelationPhysicalIdentifier_compat(
1611-
relation));
1609+
List *stripeList = StripesForRelfilelocator(relation);
16121610
int stripeCount = list_length(stripeList);
16131611
RelationClose(relation);
16141612

src/backend/columnar/columnar_metadata.c

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

614-
uint64 storageId = LookupStorageId(relfilelocator);
614+
uint64 storageId = LookupStorageId(relid, relfilelocator);
615615
Oid columnarChunkOid = ColumnarChunkRelationId();
616616
Relation columnarChunk = table_open(columnarChunkOid, RowExclusiveLock);
617617
ModifyState *modifyState = StartModifyRelation(columnarChunk);
@@ -672,10 +672,10 @@ SaveStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
672672
* SaveChunkGroups saves the metadata for given chunk groups in columnar.chunk_group.
673673
*/
674674
void
675-
SaveChunkGroups(RelFileLocator relfilelocator, uint64 stripe,
675+
SaveChunkGroups(Oid relid, RelFileLocator relfilelocator, uint64 stripe,
676676
List *chunkGroupRowCounts)
677677
{
678-
uint64 storageId = LookupStorageId(relfilelocator);
678+
uint64 storageId = LookupStorageId(relid, relfilelocator);
679679
Oid columnarChunkGroupOid = ColumnarChunkGroupRelationId();
680680
Relation columnarChunkGroup = table_open(columnarChunkGroupOid, RowExclusiveLock);
681681
ModifyState *modifyState = StartModifyRelation(columnarChunkGroup);
@@ -708,7 +708,7 @@ SaveChunkGroups(RelFileLocator relfilelocator, uint64 stripe,
708708
* ReadStripeSkipList fetches chunk metadata for a given stripe.
709709
*/
710710
StripeSkipList *
711-
ReadStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
711+
ReadStripeSkipList(Relation rel, uint64 stripe,
712712
TupleDesc tupleDescriptor,
713713
uint32 chunkCount, Snapshot snapshot)
714714
{
@@ -717,7 +717,8 @@ ReadStripeSkipList(RelFileLocator relfilelocator, uint64 stripe,
717717
uint32 columnCount = tupleDescriptor->natts;
718718
ScanKeyData scanKey[2];
719719

720-
uint64 storageId = LookupStorageId(relfilelocator);
720+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
721+
RelationPhysicalIdentifier_compat(rel));
721722

722723
Oid columnarChunkOid = ColumnarChunkRelationId();
723724
Relation columnarChunk = table_open(columnarChunkOid, AccessShareLock);
@@ -1255,9 +1256,10 @@ InsertEmptyStripeMetadataRow(uint64 storageId, uint64 stripeId, uint32 columnCou
12551256
* of the given relfilenode.
12561257
*/
12571258
List *
1258-
StripesForRelfilelocator(RelFileLocator relfilelocator)
1259+
StripesForRelfilelocator(Relation rel)
12591260
{
1260-
uint64 storageId = LookupStorageId(relfilelocator);
1261+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1262+
RelationPhysicalIdentifier_compat(rel));
12611263

12621264
return ReadDataFileStripeList(storageId, GetTransactionSnapshot());
12631265
}
@@ -1272,9 +1274,10 @@ StripesForRelfilelocator(RelFileLocator relfilelocator)
12721274
* returns 0.
12731275
*/
12741276
uint64
1275-
GetHighestUsedAddress(RelFileLocator relfilelocator)
1277+
GetHighestUsedAddress(Relation rel)
12761278
{
1277-
uint64 storageId = LookupStorageId(relfilelocator);
1279+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1280+
RelationPhysicalIdentifier_compat(rel));
12781281

12791282
uint64 highestUsedAddress = 0;
12801283
uint64 highestUsedId = 0;
@@ -1284,6 +1287,24 @@ GetHighestUsedAddress(RelFileLocator relfilelocator)
12841287
}
12851288

12861289

1290+
/*
1291+
* In case if relid hasn't been defined yet, we should use RelidByRelfilenumber
1292+
* to get correct relid value.
1293+
*
1294+
* Now it is basically used for temp rels, because since PG18(it was backpatched
1295+
* through PG13) RelidByRelfilenumber skip temp relations and we should use
1296+
* alternative ways to get relid value in case of temp objects.
1297+
*/
1298+
Oid
1299+
ColumnarRelationId(Oid relid, RelFileLocator relfilelocator)
1300+
{
1301+
return OidIsValid(relid) ? relid : RelidByRelfilenumber(RelationTablespace_compat(
1302+
relfilelocator),
1303+
RelationPhysicalIdentifierNumber_compat(
1304+
relfilelocator));
1305+
}
1306+
1307+
12871308
/*
12881309
* GetHighestUsedAddressAndId returns the highest used address and id for
12891310
* the given relfilenode across all active and inactive transactions.
@@ -1555,7 +1576,7 @@ BuildStripeMetadata(Relation columnarStripes, HeapTuple heapTuple)
15551576
* metadata tables.
15561577
*/
15571578
void
1558-
DeleteMetadataRows(RelFileLocator relfilelocator)
1579+
DeleteMetadataRows(Relation rel)
15591580
{
15601581
/*
15611582
* During a restore for binary upgrade, metadata tables and indexes may or
@@ -1566,7 +1587,8 @@ DeleteMetadataRows(RelFileLocator relfilelocator)
15661587
return;
15671588
}
15681589

1569-
uint64 storageId = LookupStorageId(relfilelocator);
1590+
uint64 storageId = LookupStorageId(RelationPrecomputeOid(rel),
1591+
RelationPhysicalIdentifier_compat(rel));
15701592

15711593
DeleteStorageFromColumnarMetadataTable(ColumnarStripeRelationId(),
15721594
Anum_columnar_stripe_storageid,
@@ -1940,13 +1962,11 @@ ColumnarNamespaceId(void)
19401962
* false if the relation doesn't have a meta page yet.
19411963
*/
19421964
static uint64
1943-
LookupStorageId(RelFileLocator relfilelocator)
1965+
LookupStorageId(Oid relid, RelFileLocator relfilelocator)
19441966
{
1945-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(relfilelocator),
1946-
RelationPhysicalIdentifierNumber_compat(
1947-
relfilelocator));
1967+
relid = ColumnarRelationId(relid, relfilelocator);
19481968

1949-
Relation relation = relation_open(relationId, AccessShareLock);
1969+
Relation relation = relation_open(relid, AccessShareLock);
19501970
uint64 storageId = ColumnarStorageGetStorageId(relation, false);
19511971
table_close(relation, AccessShareLock);
19521972

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;
@@ -1202,7 +1200,6 @@ static void
12021200
LogRelationStats(Relation rel, int elevel)
12031201
{
12041202
ListCell *stripeMetadataCell = NULL;
1205-
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
12061203
StringInfo infoBuf = makeStringInfo();
12071204

12081205
int compressionStats[COMPRESSION_COUNT] = { 0 };
@@ -1213,13 +1210,13 @@ LogRelationStats(Relation rel, int elevel)
12131210
uint64 droppedChunksWithData = 0;
12141211
uint64 totalDecompressedLength = 0;
12151212

1216-
List *stripeList = StripesForRelfilelocator(relfilelocator);
1213+
List *stripeList = StripesForRelfilelocator(rel);
12171214
int stripeCount = list_length(stripeList);
12181215

12191216
foreach(stripeMetadataCell, stripeList)
12201217
{
12211218
StripeMetadata *stripe = lfirst(stripeMetadataCell);
1222-
StripeSkipList *skiplist = ReadStripeSkipList(relfilelocator, stripe->id,
1219+
StripeSkipList *skiplist = ReadStripeSkipList(rel, stripe->id,
12231220
RelationGetDescr(rel),
12241221
stripe->chunkCount,
12251222
GetTransactionSnapshot());
@@ -1355,8 +1352,7 @@ TruncateColumnar(Relation rel, int elevel)
13551352
* new stripes be added beyond highestPhysicalAddress while
13561353
* we're truncating.
13571354
*/
1358-
uint64 newDataReservation = Max(GetHighestUsedAddress(
1359-
RelationPhysicalIdentifier_compat(rel)) + 1,
1355+
uint64 newDataReservation = Max(GetHighestUsedAddress(rel) + 1,
13601356
ColumnarFirstLogicalOffset);
13611357

13621358
BlockNumber old_rel_pages = smgrnblocks(RelationGetSmgr(rel), MAIN_FORKNUM);
@@ -2124,7 +2120,7 @@ ColumnarTableDropHook(Oid relid)
21242120
Relation rel = table_open(relid, AccessExclusiveLock);
21252121
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
21262122

2127-
DeleteMetadataRows(relfilelocator);
2123+
DeleteMetadataRows(rel);
21282124
DeleteColumnarTableOptions(rel->rd_id, true);
21292125

21302126
MarkRelfilenumberDropped(RelationPhysicalIdentifierNumber_compat(relfilelocator),

src/backend/columnar/columnar_writer.c

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

49+
/*
50+
* We can't rely on RelidByRelfilenumber for temp tables since
51+
* PG18(it was backpatched through PG13).
52+
*/
53+
Oid temp_relid;
54+
4955
MemoryContext stripeWriteContext;
5056
MemoryContext perTupleContext;
5157
StripeBuffers *stripeBuffers;
@@ -91,10 +97,12 @@ static StringInfo CopyStringInfo(StringInfo sourceString);
9197
* data load operation.
9298
*/
9399
ColumnarWriteState *
94-
ColumnarBeginWrite(RelFileLocator relfilelocator,
100+
ColumnarBeginWrite(Relation rel,
95101
ColumnarOptions options,
96102
TupleDesc tupleDescriptor)
97103
{
104+
RelFileLocator relfilelocator = RelationPhysicalIdentifier_compat(rel);
105+
98106
/* get comparison function pointers for each of the columns */
99107
uint32 columnCount = tupleDescriptor->natts;
100108
FmgrInfo **comparisonFunctionArray = palloc0(columnCount * sizeof(FmgrInfo *));
@@ -132,6 +140,7 @@ ColumnarBeginWrite(RelFileLocator relfilelocator,
132140

133141
ColumnarWriteState *writeState = palloc0(sizeof(ColumnarWriteState));
134142
writeState->relfilelocator = relfilelocator;
143+
writeState->temp_relid = RelationPrecomputeOid(rel);
135144
writeState->options = options;
136145
writeState->tupleDescriptor = CreateTupleDescCopy(tupleDescriptor);
137146
writeState->comparisonFunctionArray = comparisonFunctionArray;
@@ -181,10 +190,9 @@ ColumnarWriteRow(ColumnarWriteState *writeState, Datum *columnValues, bool *colu
181190
writeState->stripeSkipList = stripeSkipList;
182191
writeState->compressionBuffer = makeStringInfo();
183192

184-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(
185-
writeState->relfilelocator),
186-
RelationPhysicalIdentifierNumber_compat(
187-
writeState->relfilelocator));
193+
Oid relationId = ColumnarRelationId(writeState->temp_relid,
194+
writeState->relfilelocator);
195+
188196
Relation relation = relation_open(relationId, NoLock);
189197
writeState->emptyStripeReservation =
190198
ReserveEmptyStripe(relation, columnCount, chunkRowCount,
@@ -402,10 +410,9 @@ FlushStripe(ColumnarWriteState *writeState)
402410

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

405-
Oid relationId = RelidByRelfilenumber(RelationTablespace_compat(
406-
writeState->relfilelocator),
407-
RelationPhysicalIdentifierNumber_compat(
408-
writeState->relfilelocator));
413+
Oid relationId = ColumnarRelationId(writeState->temp_relid,
414+
writeState->relfilelocator);
415+
409416
Relation relation = relation_open(relationId, NoLock);
410417

411418
/*
@@ -497,10 +504,12 @@ FlushStripe(ColumnarWriteState *writeState)
497504
}
498505
}
499506

500-
SaveChunkGroups(writeState->relfilelocator,
507+
SaveChunkGroups(writeState->temp_relid,
508+
writeState->relfilelocator,
501509
stripeMetadata->id,
502510
writeState->chunkGroupRowCounts);
503-
SaveStripeSkipList(writeState->relfilelocator,
511+
SaveStripeSkipList(writeState->temp_relid,
512+
writeState->relfilelocator,
504513
stripeMetadata->id,
505514
stripeSkipList, tupleDescriptor);
506515

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)