Skip to content

Commit 91d20ff

Browse files
committed
Additional mop-up for sync-to-fsync changes: avoid issuing fsyncs for
temp tables, and avoid WAL-logging truncations of temp tables. Do issue fsync on truncated files (not sure this is necessary but it seems like a good idea).
1 parent e674707 commit 91d20ff

File tree

5 files changed

+94
-52
lines changed

5 files changed

+94
-52
lines changed

src/backend/storage/buffer/bufmgr.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.168 2004/05/31 19:24:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.169 2004/05/31 20:31:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -182,7 +182,8 @@ ReadBufferInternal(Relation reln, BlockNumber blockNum,
182182
{
183183
/* new buffers are zero-filled */
184184
MemSet((char *) MAKE_PTR(bufHdr->data), 0, BLCKSZ);
185-
smgrextend(reln->rd_smgr, blockNum, (char *) MAKE_PTR(bufHdr->data));
185+
smgrextend(reln->rd_smgr, blockNum, (char *) MAKE_PTR(bufHdr->data),
186+
reln->rd_istemp);
186187
}
187188
else
188189
{
@@ -915,8 +916,8 @@ BufferGetFileNode(Buffer buffer)
915916
* NOTE: this actually just passes the buffer contents to the kernel; the
916917
* real write to disk won't happen until the kernel feels like it. This
917918
* is okay from our point of view since we can redo the changes from WAL.
918-
* However, we will need to force the changes to disk via sync/fsync
919-
* before we can checkpoint WAL.
919+
* However, we will need to force the changes to disk via fsync before
920+
* we can checkpoint WAL.
920921
*
921922
* BufMgrLock must be held at entry, and the buffer must be pinned. The
922923
* caller is also responsible for doing StartBufferIO/TerminateBufferIO.
@@ -979,7 +980,8 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln)
979980
*/
980981
smgrwrite(reln,
981982
buf->tag.blockNum,
982-
(char *) MAKE_PTR(buf->data));
983+
(char *) MAKE_PTR(buf->data),
984+
false);
983985

984986
/* Pop the error context stack */
985987
error_context_stack = errcontext.previous;
@@ -1033,7 +1035,7 @@ RelationTruncate(Relation rel, BlockNumber nblocks)
10331035
rel->rd_targblock = InvalidBlockNumber;
10341036

10351037
/* Do the real work */
1036-
smgrtruncate(rel->rd_smgr, nblocks);
1038+
smgrtruncate(rel->rd_smgr, nblocks, rel->rd_istemp);
10371039
}
10381040

10391041
/* ---------------------------------------------------------------------
@@ -1351,7 +1353,8 @@ FlushRelationBuffers(Relation rel, BlockNumber firstDelBlock)
13511353

13521354
smgrwrite(rel->rd_smgr,
13531355
bufHdr->tag.blockNum,
1354-
(char *) MAKE_PTR(bufHdr->data));
1356+
(char *) MAKE_PTR(bufHdr->data),
1357+
true);
13551358

13561359
bufHdr->flags &= ~(BM_DIRTY | BM_JUST_DIRTIED);
13571360
bufHdr->cntxDirty = false;

src/backend/storage/buffer/localbuf.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.54 2004/04/22 07:21:55 neilc Exp $
12+
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.55 2004/05/31 20:31:33 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -111,7 +111,8 @@ LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
111111
/* And write... */
112112
smgrwrite(reln,
113113
bufHdr->tag.blockNum,
114-
(char *) MAKE_PTR(bufHdr->data));
114+
(char *) MAKE_PTR(bufHdr->data),
115+
true);
115116

116117
LocalBufferFlushCount++;
117118
}

src/backend/storage/smgr/md.c

+24-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/smgr/md.c,v 1.105 2004/05/31 03:48:06 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/smgr/md.c,v 1.106 2004/05/31 20:31:33 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -270,7 +270,7 @@ mdunlink(RelFileNode rnode, bool isRedo)
270270
* already. Might as well pass in the position and save a seek.
271271
*/
272272
bool
273-
mdextend(SMgrRelation reln, BlockNumber blocknum, char *buffer)
273+
mdextend(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp)
274274
{
275275
long seekpos;
276276
int nbytes;
@@ -311,8 +311,11 @@ mdextend(SMgrRelation reln, BlockNumber blocknum, char *buffer)
311311
return false;
312312
}
313313

314-
if (!register_dirty_segment(reln, v))
315-
return false;
314+
if (!isTemp)
315+
{
316+
if (!register_dirty_segment(reln, v))
317+
return false;
318+
}
316319

317320
#ifndef LET_OS_MANAGE_FILESIZE
318321
Assert(_mdnblocks(v->mdfd_vfd, BLCKSZ) <= ((BlockNumber) RELSEG_SIZE));
@@ -465,7 +468,7 @@ mdread(SMgrRelation reln, BlockNumber blocknum, char *buffer)
465468
* mdwrite() -- Write the supplied block at the appropriate location.
466469
*/
467470
bool
468-
mdwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer)
471+
mdwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp)
469472
{
470473
long seekpos;
471474
MdfdVec *v;
@@ -485,8 +488,11 @@ mdwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer)
485488
if (FileWrite(v->mdfd_vfd, buffer, BLCKSZ) != BLCKSZ)
486489
return false;
487490

488-
if (!register_dirty_segment(reln, v))
489-
return false;
491+
if (!isTemp)
492+
{
493+
if (!register_dirty_segment(reln, v))
494+
return false;
495+
}
490496

491497
return true;
492498
}
@@ -565,7 +571,7 @@ mdnblocks(SMgrRelation reln)
565571
* Returns # of blocks or InvalidBlockNumber on error.
566572
*/
567573
BlockNumber
568-
mdtruncate(SMgrRelation reln, BlockNumber nblocks)
574+
mdtruncate(SMgrRelation reln, BlockNumber nblocks, bool isTemp)
569575
{
570576
MdfdVec *v;
571577
BlockNumber curnblk;
@@ -624,6 +630,11 @@ mdtruncate(SMgrRelation reln, BlockNumber nblocks)
624630

625631
if (FileTruncate(v->mdfd_vfd, lastsegblocks * BLCKSZ) < 0)
626632
return InvalidBlockNumber;
633+
if (!isTemp)
634+
{
635+
if (!register_dirty_segment(reln, v))
636+
return InvalidBlockNumber;
637+
}
627638
v = v->mdfd_chain;
628639
ov->mdfd_chain = NULL;
629640
}
@@ -640,6 +651,11 @@ mdtruncate(SMgrRelation reln, BlockNumber nblocks)
640651
#else
641652
if (FileTruncate(v->mdfd_vfd, nblocks * BLCKSZ) < 0)
642653
return InvalidBlockNumber;
654+
if (!isTemp)
655+
{
656+
if (!register_dirty_segment(reln, v))
657+
return InvalidBlockNumber;
658+
}
643659
#endif
644660

645661
return nblocks;

src/backend/storage/smgr/smgr.c

+44-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/storage/smgr/smgr.c,v 1.71 2004/05/31 03:48:06 tgl Exp $
14+
* $PostgreSQL: pgsql/src/backend/storage/smgr/smgr.c,v 1.72 2004/05/31 20:31:33 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -40,13 +40,14 @@ typedef struct f_smgr
4040
bool (*smgr_create) (SMgrRelation reln, bool isRedo);
4141
bool (*smgr_unlink) (RelFileNode rnode, bool isRedo);
4242
bool (*smgr_extend) (SMgrRelation reln, BlockNumber blocknum,
43-
char *buffer);
43+
char *buffer, bool isTemp);
4444
bool (*smgr_read) (SMgrRelation reln, BlockNumber blocknum,
45-
char *buffer);
45+
char *buffer);
4646
bool (*smgr_write) (SMgrRelation reln, BlockNumber blocknum,
47-
char *buffer);
47+
char *buffer, bool isTemp);
4848
BlockNumber (*smgr_nblocks) (SMgrRelation reln);
49-
BlockNumber (*smgr_truncate) (SMgrRelation reln, BlockNumber nblocks);
49+
BlockNumber (*smgr_truncate) (SMgrRelation reln, BlockNumber nblocks,
50+
bool isTemp);
5051
bool (*smgr_commit) (void); /* may be NULL */
5152
bool (*smgr_abort) (void); /* may be NULL */
5253
bool (*smgr_sync) (void); /* may be NULL */
@@ -438,9 +439,10 @@ smgr_internal_unlink(RelFileNode rnode, int which, bool isTemp, bool isRedo)
438439
* failure we clean up by truncating.
439440
*/
440441
void
441-
smgrextend(SMgrRelation reln, BlockNumber blocknum, char *buffer)
442+
smgrextend(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp)
442443
{
443-
if (! (*(smgrsw[reln->smgr_which].smgr_extend)) (reln, blocknum, buffer))
444+
if (! (*(smgrsw[reln->smgr_which].smgr_extend)) (reln, blocknum, buffer,
445+
isTemp))
444446
ereport(ERROR,
445447
(errcode_for_file_access(),
446448
errmsg("could not extend relation %u/%u: %m",
@@ -473,12 +475,18 @@ smgrread(SMgrRelation reln, BlockNumber blocknum, char *buffer)
473475
* smgrwrite() -- Write the supplied buffer out.
474476
*
475477
* This is not a synchronous write -- the block is not necessarily
476-
* on disk at return, only dumped out to the kernel.
478+
* on disk at return, only dumped out to the kernel. However,
479+
* provisions will be made to fsync the write before the next checkpoint.
480+
*
481+
* isTemp indicates that the relation is a temp table (ie, is managed
482+
* by the local-buffer manager). In this case no provisions need be
483+
* made to fsync the write before checkpointing.
477484
*/
478485
void
479-
smgrwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer)
486+
smgrwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp)
480487
{
481-
if (! (*(smgrsw[reln->smgr_which].smgr_write)) (reln, blocknum, buffer))
488+
if (! (*(smgrsw[reln->smgr_which].smgr_write)) (reln, blocknum, buffer,
489+
isTemp))
482490
ereport(ERROR,
483491
(errcode_for_file_access(),
484492
errmsg("could not write block %u of relation %u/%u: %m",
@@ -525,12 +533,9 @@ smgrnblocks(SMgrRelation reln)
525533
* transaction on failure.
526534
*/
527535
BlockNumber
528-
smgrtruncate(SMgrRelation reln, BlockNumber nblocks)
536+
smgrtruncate(SMgrRelation reln, BlockNumber nblocks, bool isTemp)
529537
{
530538
BlockNumber newblks;
531-
XLogRecPtr lsn;
532-
XLogRecData rdata;
533-
xl_smgr_truncate xlrec;
534539

535540
/*
536541
* Tell the free space map to forget anything it may have stored
@@ -540,7 +545,8 @@ smgrtruncate(SMgrRelation reln, BlockNumber nblocks)
540545
FreeSpaceMapTruncateRel(&reln->smgr_rnode, nblocks);
541546

542547
/* Do the truncation */
543-
newblks = (*(smgrsw[reln->smgr_which].smgr_truncate)) (reln, nblocks);
548+
newblks = (*(smgrsw[reln->smgr_which].smgr_truncate)) (reln, nblocks,
549+
isTemp);
544550
if (newblks == InvalidBlockNumber)
545551
ereport(ERROR,
546552
(errcode_for_file_access(),
@@ -549,20 +555,29 @@ smgrtruncate(SMgrRelation reln, BlockNumber nblocks)
549555
reln->smgr_rnode.relNode,
550556
nblocks)));
551557

552-
/*
553-
* Make a non-transactional XLOG entry showing the file truncation. It's
554-
* non-transactional because we should replay it whether the transaction
555-
* commits or not; the underlying file change is certainly not reversible.
556-
*/
557-
xlrec.blkno = newblks;
558-
xlrec.rnode = reln->smgr_rnode;
558+
if (!isTemp)
559+
{
560+
/*
561+
* Make a non-transactional XLOG entry showing the file truncation.
562+
* It's non-transactional because we should replay it whether the
563+
* transaction commits or not; the underlying file change is certainly
564+
* not reversible.
565+
*/
566+
XLogRecPtr lsn;
567+
XLogRecData rdata;
568+
xl_smgr_truncate xlrec;
559569

560-
rdata.buffer = InvalidBuffer;
561-
rdata.data = (char *) &xlrec;
562-
rdata.len = sizeof(xlrec);
563-
rdata.next = NULL;
570+
xlrec.blkno = newblks;
571+
xlrec.rnode = reln->smgr_rnode;
564572

565-
lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_TRUNCATE | XLOG_NO_TRAN, &rdata);
573+
rdata.buffer = InvalidBuffer;
574+
rdata.data = (char *) &xlrec;
575+
rdata.len = sizeof(xlrec);
576+
rdata.next = NULL;
577+
578+
lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_TRUNCATE | XLOG_NO_TRAN,
579+
&rdata);
580+
}
566581

567582
return newblks;
568583
}
@@ -725,7 +740,8 @@ smgr_redo(XLogRecPtr lsn, XLogRecord *record)
725740

726741
/* Do the truncation */
727742
newblks = (*(smgrsw[reln->smgr_which].smgr_truncate)) (reln,
728-
xlrec->blkno);
743+
xlrec->blkno,
744+
false);
729745
if (newblks == InvalidBlockNumber)
730746
ereport(WARNING,
731747
(errcode_for_file_access(),

src/include/storage/smgr.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/smgr.h,v 1.42 2004/05/31 03:48:10 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/smgr.h,v 1.43 2004/05/31 20:31:33 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -55,11 +55,14 @@ extern void smgrclosenode(RelFileNode rnode);
5555
extern void smgrcreate(SMgrRelation reln, bool isTemp, bool isRedo);
5656
extern void smgrscheduleunlink(SMgrRelation reln, bool isTemp);
5757
extern void smgrdounlink(SMgrRelation reln, bool isTemp, bool isRedo);
58-
extern void smgrextend(SMgrRelation reln, BlockNumber blocknum, char *buffer);
58+
extern void smgrextend(SMgrRelation reln, BlockNumber blocknum, char *buffer,
59+
bool isTemp);
5960
extern void smgrread(SMgrRelation reln, BlockNumber blocknum, char *buffer);
60-
extern void smgrwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer);
61+
extern void smgrwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer,
62+
bool isTemp);
6163
extern BlockNumber smgrnblocks(SMgrRelation reln);
62-
extern BlockNumber smgrtruncate(SMgrRelation reln, BlockNumber nblocks);
64+
extern BlockNumber smgrtruncate(SMgrRelation reln, BlockNumber nblocks,
65+
bool isTemp);
6366
extern void smgrDoPendingDeletes(bool isCommit);
6467
extern int smgrGetPendingDeletes(bool forCommit, RelFileNode **ptr);
6568
extern void smgrcommit(void);
@@ -78,11 +81,14 @@ extern bool mdinit(void);
7881
extern bool mdclose(SMgrRelation reln);
7982
extern bool mdcreate(SMgrRelation reln, bool isRedo);
8083
extern bool mdunlink(RelFileNode rnode, bool isRedo);
81-
extern bool mdextend(SMgrRelation reln, BlockNumber blocknum, char *buffer);
84+
extern bool mdextend(SMgrRelation reln, BlockNumber blocknum, char *buffer,
85+
bool isTemp);
8286
extern bool mdread(SMgrRelation reln, BlockNumber blocknum, char *buffer);
83-
extern bool mdwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer);
87+
extern bool mdwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer,
88+
bool isTemp);
8489
extern BlockNumber mdnblocks(SMgrRelation reln);
85-
extern BlockNumber mdtruncate(SMgrRelation reln, BlockNumber nblocks);
90+
extern BlockNumber mdtruncate(SMgrRelation reln, BlockNumber nblocks,
91+
bool isTemp);
8692
extern bool mdsync(void);
8793

8894
extern void RememberFsyncRequest(RelFileNode rnode, BlockNumber segno);

0 commit comments

Comments
 (0)