Skip to content

Commit af2d94b

Browse files
committed
Use interface for storage interactions in Appendonly TAM
fix test
1 parent 5f180c5 commit af2d94b

25 files changed

Lines changed: 244 additions & 63 deletions

src/backend/access/aocs/aocs_compaction.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ AOCSCompaction_DropSegmentFile(Relation aorel, int segno)
8484
if (fd >= 0)
8585
{
8686
TruncateAOSegmentFile(fd, aorel, pseudoSegNo, 0);
87-
CloseAOSegmentFile(fd);
87+
CloseAOSegmentFile(fd, aorel);
8888
}
8989
else
9090
{
@@ -147,7 +147,7 @@ AOCSSegmentFileTruncateToEOF(Relation aorel, int segno, AOCSVPInfo *vpinfo)
147147
if (fd >= 0)
148148
{
149149
TruncateAOSegmentFile(fd, aorel, fileSegNo, segeof);
150-
CloseAOSegmentFile(fd);
150+
CloseAOSegmentFile(fd, aorel);
151151

152152
elogif(Debug_appendonly_print_compaction, LOG,
153153
"Successfully truncated AO COL relation \"%s.%s\", relation id %u, relfilenode %lu column #%d, logical segment #%d (physical segment file #%d, logical EOF " INT64_FORMAT ")",

src/backend/access/aocs/aocsam.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ aocs_delete_hook_type aocs_delete_hook = NULL;
7575
*/
7676
static void
7777
open_datumstreamread_segfile(
78-
char *basepath, RelFileNode node,
78+
char *basepath,
79+
const struct f_smgr_ao *smgrAO,
80+
RelFileNode node,
7981
AOCSFileSegInfo *segInfo,
8082
DatumStreamRead *ds,
8183
int colNo)
@@ -118,7 +120,9 @@ open_all_datumstreamread_segfiles(Relation rel,
118120
{
119121
AttrNumber attno = proj_atts[i];
120122

121-
open_datumstreamread_segfile(basepath, rel->rd_node, segInfo, ds[attno], attno);
123+
RelationOpenSmgr(rel);
124+
125+
open_datumstreamread_segfile(basepath, rel->rd_smgr->smgr_ao, rel->rd_node, segInfo, ds[attno], attno);
122126
datumstreamread_block(ds[attno], blockDirectory, attno);
123127
}
124128

@@ -139,6 +143,8 @@ open_ds_write(Relation rel, DatumStreamWrite **ds, TupleDesc relationTupleDesc,
139143
rnode.node = rel->rd_node;
140144
rnode.backend = rel->rd_backend;
141145

146+
RelationOpenSmgr(rel);
147+
142148
/* open datum streams. It will open segment file underneath */
143149
for (int i = 0; i < nvp; ++i)
144150
{
@@ -179,7 +185,8 @@ open_ds_write(Relation rel, DatumStreamWrite **ds, TupleDesc relationTupleDesc,
179185
RelationGetRelationName(rel),
180186
/* title */ titleBuf.data,
181187
XLogIsNeeded() && RelationNeedsWAL(rel),
182-
&rnode);
188+
&rnode,
189+
rel->rd_smgr->smgr_ao);
183190

184191
}
185192
}
@@ -229,6 +236,8 @@ open_ds_read(Relation rel, DatumStreamRead **ds, TupleDesc relationTupleDesc,
229236
for (AttrNumber attno = 0; attno < relationTupleDesc->natts; attno++)
230237
ds[attno] = NULL;
231238

239+
RelationOpenSmgr(rel);
240+
232241
/* And then initialize the data streams for those columns we need */
233242
for (AttrNumber i = 0; i < num_proj_atts; i++)
234243
{
@@ -270,7 +279,8 @@ open_ds_read(Relation rel, DatumStreamRead **ds, TupleDesc relationTupleDesc,
270279
attr,
271280
RelationGetRelationName(rel),
272281
/* title */ titleBuf.data,
273-
&rel->rd_node);
282+
&rel->rd_node,
283+
rel->rd_smgr->smgr_ao);
274284
}
275285
}
276286

@@ -1410,7 +1420,9 @@ openFetchSegmentFile(AOCSFetchDesc aocsFetchDesc,
14101420
if (logicalEof == 0)
14111421
return false;
14121422

1413-
open_datumstreamread_segfile(aocsFetchDesc->basepath, aocsFetchDesc->relation->rd_node,
1423+
RelationOpenSmgr(aocsFetchDesc->relation);
1424+
1425+
open_datumstreamread_segfile(aocsFetchDesc->basepath, aocsFetchDesc->relation->rd_smgr->smgr_ao, aocsFetchDesc->relation->rd_node,
14141426
fsInfo,
14151427
datumStreamFetchDesc->datumStream,
14161428
colNo);
@@ -1525,6 +1537,8 @@ aocs_fetch_init(Relation relation,
15251537
aocsFetchDesc->datumStreamFetchDesc = (DatumStreamFetchDesc *)
15261538
palloc0(relation->rd_att->natts * sizeof(DatumStreamFetchDesc));
15271539

1540+
RelationOpenSmgr(relation);
1541+
15281542
for (colno = 0; colno < relation->rd_att->natts; colno++)
15291543
{
15301544

@@ -1568,7 +1582,7 @@ aocs_fetch_init(Relation relation,
15681582
TupleDescAttr(tupleDesc, colno),
15691583
relation->rd_rel->relname.data,
15701584
/* title */ titleBuf.data,
1571-
&relation->rd_node);
1585+
&relation->rd_node, relation->rd_smgr->smgr_ao);
15721586

15731587
}
15741588
if (opts[colno])
@@ -1944,13 +1958,16 @@ aocs_begin_headerscan(Relation rel, int colno)
19441958
ao_attr.overflowSize = 0;
19451959
ao_attr.safeFSWriteSize = 0;
19461960
hdesc = palloc(sizeof(AOCSHeaderScanDescData));
1961+
1962+
RelationOpenSmgr(rel);
1963+
19471964
AppendOnlyStorageRead_Init(&hdesc->ao_read,
19481965
NULL, //current memory context
19491966
opts[colno]->blocksize,
19501967
RelationGetRelationName(rel),
19511968
"ALTER TABLE ADD COLUMN scan",
19521969
&ao_attr,
1953-
&rel->rd_node);
1970+
&rel->rd_node, rel->rd_smgr->smgr_ao);
19541971
hdesc->colno = colno;
19551972
return hdesc;
19561973
}
@@ -2035,6 +2052,9 @@ aocs_addcol_init(Relation rel,
20352052
NULL);
20362053

20372054
iattr = rel->rd_att->natts - num_newcols;
2055+
2056+
RelationOpenSmgr(rel);
2057+
20382058
for (i = 0; i < num_newcols; ++i, ++iattr)
20392059
{
20402060
Form_pg_attribute attr = TupleDescAttr(rel->rd_att, iattr);
@@ -2050,7 +2070,8 @@ aocs_addcol_init(Relation rel,
20502070
attr, RelationGetRelationName(rel),
20512071
titleBuf.data,
20522072
XLogIsNeeded() && RelationNeedsWAL(rel),
2053-
&rnode);
2073+
&rnode,
2074+
rel->rd_smgr->smgr_ao);
20542075
}
20552076
return desc;
20562077
}

src/backend/access/aocs/aocsam_handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ aoco_relation_copy_data(Relation rel, const RelFileNode *newrnode)
13651365
*/
13661366
RelationCreateStorage(*newrnode, rel->rd_rel->relpersistence, SMGR_AO, rel);
13671367

1368-
copy_append_only_data(rel->rd_node, *newrnode, rel->rd_backend, rel->rd_rel->relpersistence);
1368+
copy_append_only_data(rel->rd_node, *newrnode, rel->rd_smgr, dstrel, rel->rd_backend, rel->rd_rel->relpersistence);
13691369

13701370
/*
13711371
* For append-optimized tables, no forks other than the main fork should

src/backend/access/aocs/test/aocsam_test.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "postgres.h"
77
#include "utils/memutils.h"
8+
#include "storage/smgr.h"
89

910
#include "../aocsam.c"
1011

@@ -19,15 +20,23 @@ test__aocs_begin_headerscan(void **state)
1920
{
2021
AOCSHeaderScanDesc desc;
2122
RelationData reldata;
23+
SMgrRelationData smgrdata;
2224
FormData_pg_class pgclass;
2325

26+
memset(&reldata, 0, sizeof(SMgrRelationData));
27+
2428
reldata.rd_rel = &pgclass;
2529
reldata.rd_id = 12345;
2630
StdRdOptions opt;
2731

2832
opt.blocksize = 8192 * 5;
2933
StdRdOptions *opts[1];
3034

35+
smgrdata.smgr_ao = smgrAOGetDefault();
36+
reldata.rd_smgr = &smgrdata;
37+
reldata.rd_backend = InvalidBackendId;
38+
39+
3140
opts[0] = &opt;
3241

3342
strncpy(&pgclass.relname.data[0], "mock_relation", 13);
@@ -63,6 +72,7 @@ test__aocs_addcol_init(void **state)
6372
{
6473
AOCSAddColumnDesc desc;
6574
RelationData reldata;
75+
SMgrRelationData smgrdata;
6676
int nattr = 5;
6777
StdRdOptions **opts =
6878
(StdRdOptions **) malloc(sizeof(StdRdOptions *) * nattr);
@@ -98,6 +108,8 @@ test__aocs_addcol_init(void **state)
98108
expect_value(create_datumstreamwrite, needsWAL, true);
99109
expect_any(create_datumstreamwrite, rnode);
100110
expect_any(create_datumstreamwrite, rnode);
111+
expect_any(create_datumstreamwrite, smgrAO);
112+
expect_any(create_datumstreamwrite, smgrAO);
101113
expect_any_count(create_datumstreamwrite, attr, 2);
102114
expect_any_count(create_datumstreamwrite, relname, 2);
103115
expect_any_count(create_datumstreamwrite, title, 2);
@@ -112,6 +124,9 @@ test__aocs_addcol_init(void **state)
112124
memset(reldata.rd_att->attrs, 0, sizeof(Form_pg_attribute *) * nattr);
113125
reldata.rd_att->natts = nattr;
114126

127+
smgrdata.smgr_ao = smgrAOGetDefault();
128+
reldata.rd_smgr = &smgrdata;
129+
115130
expect_value(GetAppendOnlyEntryAttributes, relid, 12345);
116131
expect_any(GetAppendOnlyEntryAttributes, blocksize);
117132
expect_any(GetAppendOnlyEntryAttributes, safefswritesize);

src/backend/access/appendonly/aomd.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ OpenAOSegmentFile(Relation rel,
149149
File fd;
150150

151151
errno = 0;
152-
fd = PathNameOpenFile(filepathname, fileFlags);
152+
153+
RelationOpenSmgr(rel);
154+
155+
fd = rel->rd_smgr->smgr_ao->smgr_AORelOpenSegFile(filepathname, fileFlags);
153156
if (fd < 0)
154157
{
155158
if (logicalEof == 0 && errno == ENOENT)
@@ -168,9 +171,9 @@ OpenAOSegmentFile(Relation rel,
168171
* Close an Append Only relation file segment
169172
*/
170173
void
171-
CloseAOSegmentFile(File fd)
174+
CloseAOSegmentFile(File fd, Relation rel)
172175
{
173-
FileClose(fd);
176+
rel->rd_smgr->smgr_ao->smgr_FileClose(fd);
174177
}
175178

176179
/*
@@ -184,11 +187,13 @@ TruncateAOSegmentFile(File fd, Relation rel, int32 segFileNum, int64 offset)
184187
Assert(fd > 0);
185188
Assert(offset >= 0);
186189

190+
RelationOpenSmgr(rel);
191+
187192
/*
188193
* Call the 'fd' module with a 64-bit length since AO segment files
189194
* can be multi-gigabyte to the terabytes...
190195
*/
191-
if (FileTruncate(fd, offset, WAIT_EVENT_DATA_FILE_TRUNCATE) != 0)
196+
if (rel->rd_smgr->smgr_ao->smgr_FileTruncate(fd, offset, WAIT_EVENT_DATA_FILE_TRUNCATE) != 0)
192197
ereport(ERROR,
193198
(errmsg("\"%s\": failed to truncate data after eof: %m",
194199
relname)));
@@ -363,7 +368,8 @@ mdunlink_ao_perFile(const int segno, void *ctx)
363368

364369
static void
365370
copy_file(char *srcsegpath, char *dstsegpath,
366-
RelFileNode dst, int segfilenum, bool use_wal)
371+
RelFileNode dst, SMgrRelation srcSMGR, SMgrRelation dstSMGR,
372+
int segfilenum, bool use_wal)
367373
{
368374
File srcFile;
369375
File dstFile;
@@ -372,7 +378,7 @@ copy_file(char *srcsegpath, char *dstsegpath,
372378
char *buffer = palloc(BLCKSZ);
373379
int dstflags;
374380

375-
srcFile = PathNameOpenFile(srcsegpath, O_RDONLY | PG_BINARY);
381+
srcFile = srcSMGR->smgr_ao->smgr_AORelOpenSegFile(srcsegpath, O_RDONLY | PG_BINARY);
376382
if (srcFile < 0)
377383
ereport(ERROR,
378384
(errcode_for_file_access(),
@@ -387,13 +393,13 @@ copy_file(char *srcsegpath, char *dstsegpath,
387393
if (segfilenum)
388394
dstflags |= O_CREAT;
389395

390-
dstFile = PathNameOpenFile(dstsegpath, dstflags);
396+
dstFile = dstSMGR->smgr_ao->smgr_AORelOpenSegFile(dstsegpath, dstflags);
391397
if (dstFile < 0)
392398
ereport(ERROR,
393399
(errcode_for_file_access(),
394400
(errmsg("could not create destination file %s: %m", dstsegpath))));
395401

396-
left = FileDiskSize(srcFile);
402+
left = srcSMGR->smgr_ao->smgr_FileDiskSize(srcFile);
397403
if (left < 0)
398404
ereport(ERROR,
399405
(errcode_for_file_access(),
@@ -407,13 +413,13 @@ copy_file(char *srcsegpath, char *dstsegpath,
407413
CHECK_FOR_INTERRUPTS();
408414

409415
len = Min(left, BLCKSZ);
410-
if (FileRead(srcFile, buffer, len, offset, WAIT_EVENT_DATA_FILE_READ) != len)
416+
if (srcSMGR->smgr_ao->smgr_FileRead(srcFile, buffer, len, offset, WAIT_EVENT_DATA_FILE_READ) != len)
411417
ereport(ERROR,
412418
(errcode_for_file_access(),
413419
errmsg("could not read %d bytes from file \"%s\": %m",
414420
len, srcsegpath)));
415421

416-
if (FileWrite(dstFile, buffer, len, offset, WAIT_EVENT_DATA_FILE_WRITE) != len)
422+
if (dstSMGR->smgr_ao->smgr_FileWrite(dstFile, buffer, len, offset, WAIT_EVENT_DATA_FILE_WRITE) != len)
417423
ereport(ERROR,
418424
(errcode_for_file_access(),
419425
errmsg("could not write %d bytes to file \"%s\": %m",
@@ -425,19 +431,21 @@ copy_file(char *srcsegpath, char *dstsegpath,
425431
left -= len;
426432
}
427433

428-
if (FileSync(dstFile, WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC) != 0)
434+
if (dstSMGR->smgr_ao->smgr_FileSync(dstFile, WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC) != 0)
429435
ereport(ERROR,
430436
(errcode_for_file_access(),
431437
errmsg("could not fsync file \"%s\": %m",
432438
dstsegpath)));
433-
FileClose(srcFile);
434-
FileClose(dstFile);
439+
srcSMGR->smgr_ao->smgr_FileClose(srcFile);
440+
dstSMGR->smgr_ao->smgr_FileClose(dstFile);
435441
pfree(buffer);
436442
}
437443

438444
struct copy_append_only_data_callback_ctx {
439445
char *srcPath;
440446
char *dstPath;
447+
SMgrRelation srcSMGR;
448+
SMgrRelation dstSMGR;
441449
RelFileNode src;
442450
RelFileNode dst;
443451
bool useWal;
@@ -449,6 +457,7 @@ struct copy_append_only_data_callback_ctx {
449457
*/
450458
void
451459
copy_append_only_data(RelFileNode src, RelFileNode dst,
460+
SMgrRelation srcSMGR, SMgrRelation dstSMGR,
452461
BackendId backendid, char relpersistence)
453462
{
454463
char *srcPath;
@@ -464,10 +473,12 @@ copy_append_only_data(RelFileNode src, RelFileNode dst,
464473
srcPath = relpathbackend(src, backendid, MAIN_FORKNUM);
465474
dstPath = relpathbackend(dst, backendid, MAIN_FORKNUM);
466475

467-
copy_file(srcPath, dstPath, dst, 0, useWal);
476+
copy_file(srcPath, dstPath, dst, srcSMGR, dstSMGR, 0, useWal);
468477

469478
copyFiles.srcPath = srcPath;
470479
copyFiles.dstPath = dstPath;
480+
copyFiles.srcSMGR = srcSMGR;
481+
copyFiles.dstSMGR = dstSMGR;
471482
copyFiles.src = src;
472483
copyFiles.dst = dst;
473484
copyFiles.useWal = useWal;
@@ -502,7 +513,7 @@ copy_append_only_data_perFile(const int segno, void *ctx)
502513
return false;
503514
}
504515
sprintf(dstSegPath, "%s.%u", copyFiles->dstPath, segno);
505-
copy_file(srcSegPath, dstSegPath, copyFiles->dst, segno, copyFiles->useWal);
516+
copy_file(srcSegPath, dstSegPath, copyFiles->dst, copyFiles->srcSMGR, copyFiles->dstSMGR, segno, copyFiles->useWal);
506517

507518
return true;
508519
}
@@ -571,7 +582,7 @@ truncate_ao_perFile(const int segno, void *ctx)
571582
if (fd >= 0)
572583
{
573584
TruncateAOSegmentFile(fd, aorel, segno, 0);
574-
CloseAOSegmentFile(fd);
585+
CloseAOSegmentFile(fd, aorel);
575586
}
576587
else
577588
{

src/backend/access/appendonly/appendonly_compaction.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ AppendOnlyCompaction_DropSegmentFile(Relation aorel, int segno)
9191
if (fd >= 0)
9292
{
9393
TruncateAOSegmentFile(fd, aorel, fileSegNo, 0);
94-
CloseAOSegmentFile(fd);
94+
CloseAOSegmentFile(fd, aorel);
9595
}
9696
else
9797
{
@@ -256,7 +256,7 @@ AppendOnlySegmentFileTruncateToEOF(Relation aorel, int segno, int64 segeof)
256256
if (fd >= 0)
257257
{
258258
TruncateAOSegmentFile(fd, aorel, fileSegNo, segeof);
259-
CloseAOSegmentFile(fd);
259+
CloseAOSegmentFile(fd, aorel);
260260

261261
elogif(Debug_appendonly_print_compaction, LOG,
262262
"Successfully truncated AO ROW relation \"%s.%s\", relation id %u, relfilenode %lu (physical segment file #%d, logical EOF " INT64_FORMAT ")",

0 commit comments

Comments
 (0)