Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitecbff43

Browse files
committed
Change _mdfd_segpath() to return paths by value
This basically mirrors the changes done in the predecessor commit. While thereisn't currently a need to get these paths in critical sections, it seems ashame to unnecessarily allocate memory in these paths now that relpath()doesn't allocate anymore.Discussion:https://postgr.es/m/xeri5mla4b5syjd5a25nok5iez2kr3bm26j2qn4u7okzof2bmf@kwdh2vf7npra
1 parent37c87e6 commitecbff43

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

‎src/backend/storage/smgr/md.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ static MemoryContext MdCxt;/* context for all MdfdVec objects */
110110
#defineEXTENSION_DONT_OPEN(1 << 5)
111111

112112

113+
/*
114+
* Fixed-length string to represent paths to files that need to be built by
115+
* md.c.
116+
*
117+
* The maximum number of segments is MaxBlockNumber / RELSEG_SIZE, where
118+
* RELSEG_SIZE can be set to 1 (for testing only).
119+
*/
120+
#defineSEGMENT_CHARSOIDCHARS
121+
#defineMD_PATH_STR_MAXLEN \
122+
(\
123+
REL_PATH_STR_MAXLEN \
124+
+ sizeof((char)'.') \
125+
+ SEGMENT_CHARS \
126+
)
127+
typedefstructMdPathStr
128+
{
129+
charstr[MD_PATH_STR_MAXLEN+1];
130+
}MdPathStr;
131+
132+
113133
/* local routines */
114134
staticvoidmdunlinkfork(RelFileLocatorBackendrlocator,ForkNumberforknum,
115135
boolisRedo);
@@ -123,8 +143,8 @@ static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber f
123143
staticvoid_fdvec_resize(SMgrRelationreln,
124144
ForkNumberforknum,
125145
intnseg);
126-
staticchar*_mdfd_segpath(SMgrRelationreln,ForkNumberforknum,
127-
BlockNumbersegno);
146+
staticMdPathStr_mdfd_segpath(SMgrRelationreln,ForkNumberforknum,
147+
BlockNumbersegno);
128148
staticMdfdVec*_mdfd_openseg(SMgrRelationreln,ForkNumberforknum,
129149
BlockNumbersegno,intoflags);
130150
staticMdfdVec*_mdfd_getseg(SMgrRelationreln,ForkNumberforknum,
@@ -398,20 +418,20 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
398418
*/
399419
if (ret >=0||errno!=ENOENT)
400420
{
401-
char*segpath= (char*)palloc(strlen(path.str)+12);
421+
MdPathStrsegpath;
402422
BlockNumbersegno;
403423

404424
for (segno=1;;segno++)
405425
{
406-
sprintf(segpath,"%s.%u",path.str,segno);
426+
sprintf(segpath.str,"%s.%u",path.str,segno);
407427

408428
if (!RelFileLocatorBackendIsTemp(rlocator))
409429
{
410430
/*
411431
* Prevent other backends' fds from holding on to the disk
412432
* space. We're done if we see ENOENT, though.
413433
*/
414-
if (do_truncate(segpath)<0&&errno==ENOENT)
434+
if (do_truncate(segpath.str)<0&&errno==ENOENT)
415435
break;
416436

417437
/*
@@ -421,17 +441,16 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
421441
register_forget_request(rlocator,forknum,segno);
422442
}
423443

424-
if (unlink(segpath)<0)
444+
if (unlink(segpath.str)<0)
425445
{
426446
/* ENOENT is expected after the last segment... */
427447
if (errno!=ENOENT)
428448
ereport(WARNING,
429449
(errcode_for_file_access(),
430-
errmsg("could not remove file \"%s\": %m",segpath)));
450+
errmsg("could not remove file \"%s\": %m",segpath.str)));
431451
break;
432452
}
433453
}
434-
pfree(segpath);
435454
}
436455
}
437456

@@ -1528,18 +1547,18 @@ _fdvec_resize(SMgrRelation reln,
15281547
* Return the filename for the specified segment of the relation. The
15291548
* returned string is palloc'd.
15301549
*/
1531-
staticchar*
1550+
staticMdPathStr
15321551
_mdfd_segpath(SMgrRelationreln,ForkNumberforknum,BlockNumbersegno)
15331552
{
15341553
RelPathStrpath;
1535-
char*fullpath;
1554+
MdPathStrfullpath;
15361555

15371556
path=relpath(reln->smgr_rlocator,forknum);
15381557

15391558
if (segno>0)
1540-
fullpath=psprintf("%s.%u",path.str,segno);
1559+
sprintf(fullpath.str,"%s.%u",path.str,segno);
15411560
else
1542-
fullpath=pstrdup(path.str);
1561+
strcpy(fullpath.str,path.str);
15431562

15441563
returnfullpath;
15451564
}
@@ -1554,14 +1573,12 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
15541573
{
15551574
MdfdVec*v;
15561575
Filefd;
1557-
char*fullpath;
1576+
MdPathStrfullpath;
15581577

15591578
fullpath=_mdfd_segpath(reln,forknum,segno);
15601579

15611580
/* open the file */
1562-
fd=PathNameOpenFile(fullpath,_mdfd_open_flags() |oflags);
1563-
1564-
pfree(fullpath);
1581+
fd=PathNameOpenFile(fullpath.str,_mdfd_open_flags() |oflags);
15651582

15661583
if (fd<0)
15671584
returnNULL;
@@ -1697,7 +1714,7 @@ _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
16971714
ereport(ERROR,
16981715
(errcode_for_file_access(),
16991716
errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1700-
_mdfd_segpath(reln,forknum,nextsegno),
1717+
_mdfd_segpath(reln,forknum,nextsegno).str,
17011718
blkno,nblocks)));
17021719
}
17031720

@@ -1711,7 +1728,7 @@ _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
17111728
ereport(ERROR,
17121729
(errcode_for_file_access(),
17131730
errmsg("could not open file \"%s\" (target block %u): %m",
1714-
_mdfd_segpath(reln,forknum,nextsegno),
1731+
_mdfd_segpath(reln,forknum,nextsegno).str,
17151732
blkno)));
17161733
}
17171734
}
@@ -1762,11 +1779,10 @@ mdsyncfiletag(const FileTag *ftag, char *path)
17621779
}
17631780
else
17641781
{
1765-
char*p;
1782+
MdPathStrp;
17661783

17671784
p=_mdfd_segpath(reln,ftag->forknum,ftag->segno);
1768-
strlcpy(path,p,MAXPGPATH);
1769-
pfree(p);
1785+
strlcpy(path,p.str,MD_PATH_STR_MAXLEN);
17701786

17711787
file=PathNameOpenFile(path,_mdfd_open_flags());
17721788
if (file<0)

‎src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,7 @@ Material
16231623
MaterialPath
16241624
MaterialState
16251625
MdfdVec
1626+
MdPathStr
16261627
Memoize
16271628
MemoizeEntry
16281629
MemoizeInstrumentation

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp