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

Commit4ab8c81

Browse files
committed
Move pg_pwritev_with_retry() to src/common/file_utils.c
This commit moves pg_pwritev_with_retry(), a convenience wrapper ofpg_writev() able to handle partial writes, to common/file_utils.c sothat the frontend code is able to use it. A first use-case targettedfor this routine is pg_basebackup and pg_receivewal, for thezero-padding of a newly-initialized WAL segment. This is used currentlyin the backend when the GUC wal_init_zero is enabled (default).Author: Bharath RupireddyReviewed-by: Nathan Bossart, Thomas MunroDiscussion:https://postgr.es/m/CALj2ACUq7nAb7=bJNbK3yYmp-SZhJcXFR_pLk8un6XgDzDF3OA@mail.gmail.com
1 parent1b9cd69 commit4ab8c81

File tree

4 files changed

+74
-71
lines changed

4 files changed

+74
-71
lines changed

‎src/backend/storage/file/fd.c

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
#include"common/pg_prng.h"
9494
#include"miscadmin.h"
9595
#include"pgstat.h"
96-
#include"port/pg_iovec.h"
9796
#include"portability/mem.h"
9897
#include"postmaster/startup.h"
9998
#include"storage/fd.h"
@@ -3738,67 +3737,3 @@ data_sync_elevel(int elevel)
37383737
{
37393738
returndata_sync_retry ?elevel :PANIC;
37403739
}
3741-
3742-
/*
3743-
* A convenience wrapper for pg_pwritev() that retries on partial write. If an
3744-
* error is returned, it is unspecified how much has been written.
3745-
*/
3746-
ssize_t
3747-
pg_pwritev_with_retry(intfd,conststructiovec*iov,intiovcnt,off_toffset)
3748-
{
3749-
structioveciov_copy[PG_IOV_MAX];
3750-
ssize_tsum=0;
3751-
ssize_tpart;
3752-
3753-
/* We'd better have space to make a copy, in case we need to retry. */
3754-
if (iovcnt>PG_IOV_MAX)
3755-
{
3756-
errno=EINVAL;
3757-
return-1;
3758-
}
3759-
3760-
for (;;)
3761-
{
3762-
/* Write as much as we can. */
3763-
part=pg_pwritev(fd,iov,iovcnt,offset);
3764-
if (part<0)
3765-
return-1;
3766-
3767-
#ifdefSIMULATE_SHORT_WRITE
3768-
part=Min(part,4096);
3769-
#endif
3770-
3771-
/* Count our progress. */
3772-
sum+=part;
3773-
offset+=part;
3774-
3775-
/* Step over iovecs that are done. */
3776-
while (iovcnt>0&&iov->iov_len <=part)
3777-
{
3778-
part-=iov->iov_len;
3779-
++iov;
3780-
--iovcnt;
3781-
}
3782-
3783-
/* Are they all done? */
3784-
if (iovcnt==0)
3785-
{
3786-
/* We don't expect the kernel to write more than requested. */
3787-
Assert(part==0);
3788-
break;
3789-
}
3790-
3791-
/*
3792-
* Move whatever's left to the front of our mutable copy and adjust
3793-
* the leading iovec.
3794-
*/
3795-
Assert(iovcnt>0);
3796-
memmove(iov_copy,iov,sizeof(*iov)*iovcnt);
3797-
Assert(iov->iov_len>part);
3798-
iov_copy[0].iov_base= (char*)iov_copy[0].iov_base+part;
3799-
iov_copy[0].iov_len-=part;
3800-
iov=iov_copy;
3801-
}
3802-
3803-
returnsum;
3804-
}

‎src/common/file_utils.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifdefFRONTEND
2929
#include"common/logging.h"
3030
#endif
31+
#include"port/pg_iovec.h"
3132

3233
#ifdefFRONTEND
3334

@@ -460,3 +461,69 @@ get_dirent_type(const char *path,
460461

461462
returnresult;
462463
}
464+
465+
/*
466+
* pg_pwritev_with_retry
467+
*
468+
* Convenience wrapper for pg_pwritev() that retries on partial write. If an
469+
* error is returned, it is unspecified how much has been written.
470+
*/
471+
ssize_t
472+
pg_pwritev_with_retry(intfd,conststructiovec*iov,intiovcnt,off_toffset)
473+
{
474+
structioveciov_copy[PG_IOV_MAX];
475+
ssize_tsum=0;
476+
ssize_tpart;
477+
478+
/* We'd better have space to make a copy, in case we need to retry. */
479+
if (iovcnt>PG_IOV_MAX)
480+
{
481+
errno=EINVAL;
482+
return-1;
483+
}
484+
485+
for (;;)
486+
{
487+
/* Write as much as we can. */
488+
part=pg_pwritev(fd,iov,iovcnt,offset);
489+
if (part<0)
490+
return-1;
491+
492+
#ifdefSIMULATE_SHORT_WRITE
493+
part=Min(part,4096);
494+
#endif
495+
496+
/* Count our progress. */
497+
sum+=part;
498+
offset+=part;
499+
500+
/* Step over iovecs that are done. */
501+
while (iovcnt>0&&iov->iov_len <=part)
502+
{
503+
part-=iov->iov_len;
504+
++iov;
505+
--iovcnt;
506+
}
507+
508+
/* Are they all done? */
509+
if (iovcnt==0)
510+
{
511+
/* We don't expect the kernel to write more than requested. */
512+
Assert(part==0);
513+
break;
514+
}
515+
516+
/*
517+
* Move whatever's left to the front of our mutable copy and adjust
518+
* the leading iovec.
519+
*/
520+
Assert(iovcnt>0);
521+
memmove(iov_copy,iov,sizeof(*iov)*iovcnt);
522+
Assert(iov->iov_len>part);
523+
iov_copy[0].iov_base= (char*)iov_copy[0].iov_base+part;
524+
iov_copy[0].iov_len-=part;
525+
iov=iov_copy;
526+
}
527+
528+
returnsum;
529+
}

‎src/include/common/file_utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ typedef enum PGFileType
2424
PGFILETYPE_LNK
2525
}PGFileType;
2626

27+
structiovec;/* avoid including port/pg_iovec.h here */
28+
2729
#ifdefFRONTEND
2830
externintfsync_fname(constchar*fname,boolisdir);
2931
externvoidfsync_pgdata(constchar*pg_data,intserverVersion);
@@ -37,4 +39,9 @@ extern PGFileType get_dirent_type(const char *path,
3739
boollook_through_symlinks,
3840
intelevel);
3941

42+
externssize_tpg_pwritev_with_retry(intfd,
43+
conststructiovec*iov,
44+
intiovcnt,
45+
off_toffset);
46+
4047
#endif/* FILE_UTILS_H */

‎src/include/storage/fd.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ typedef enum RecoveryInitSyncMethod
5151
RECOVERY_INIT_SYNC_METHOD_SYNCFS
5252
}RecoveryInitSyncMethod;
5353

54-
structiovec;/* avoid including port/pg_iovec.h here */
55-
5654
typedefintFile;
5755

5856

@@ -178,10 +176,6 @@ extern intpg_fsync_no_writethrough(int fd);
178176
externintpg_fsync_writethrough(intfd);
179177
externintpg_fdatasync(intfd);
180178
externvoidpg_flush_data(intfd,off_toffset,off_tnbytes);
181-
externssize_tpg_pwritev_with_retry(intfd,
182-
conststructiovec*iov,
183-
intiovcnt,
184-
off_toffset);
185179
externintpg_truncate(constchar*path,off_tlength);
186180
externvoidfsync_fname(constchar*fname,boolisdir);
187181
externintfsync_fname_ext(constchar*fname,boolisdir,boolignore_perm,intelevel);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp