Skip to content

Commit cfeca62

Browse files
Jan WieckJan Wieck
Jan Wieck
authored and
Jan Wieck
committed
Background writer process
This first part of the background writer does no syncing at all. It's only purpose is to keep the LRU heads clean so that regular backends seldom to never have to call write(). Jan
1 parent 5032f83 commit cfeca62

File tree

12 files changed

+271
-42
lines changed

12 files changed

+271
-42
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.166 2003/09/02 19:04:12 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.167 2003/11/19 15:55:07 wieck Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -428,8 +428,20 @@ BootstrapMain(int argc, char *argv[])
428428

429429
BaseInit();
430430

431+
/* needed to get LWLocks */
431432
if (IsUnderPostmaster)
432-
InitDummyProcess(); /* needed to get LWLocks */
433+
{
434+
switch (xlogop)
435+
{
436+
case BS_XLOG_BGWRITER:
437+
InitDummyProcess(DUMMY_PROC_BGWRITER);
438+
break;
439+
440+
default:
441+
InitDummyProcess(DUMMY_PROC_DEFAULT);
442+
break;
443+
}
444+
}
433445

434446
/*
435447
* XLOG operations
@@ -453,6 +465,11 @@ BootstrapMain(int argc, char *argv[])
453465
* postmaster */
454466
proc_exit(0); /* done */
455467

468+
case BS_XLOG_BGWRITER:
469+
CreateDummyCaches();
470+
BufferBackgroundWriter();
471+
proc_exit(0); /* done */
472+
456473
case BS_XLOG_STARTUP:
457474
StartupXLOG();
458475
LoadFreeSpaceMap();

src/backend/catalog/index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.221 2003/11/12 21:15:48 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.222 2003/11/19 15:55:07 wieck Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1043,7 +1043,7 @@ setRelhasindex(Oid relid, bool hasindex, bool isprimary, Oid reltoastidxid)
10431043
/* Send out shared cache inval if necessary */
10441044
if (!IsBootstrapProcessingMode())
10451045
CacheInvalidateHeapTuple(pg_class, tuple);
1046-
BufferSync();
1046+
BufferSync(-1, -1);
10471047
}
10481048
else if (dirty)
10491049
{

src/backend/commands/dbcommands.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.126 2003/11/12 21:15:50 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.127 2003/11/19 15:55:07 wieck Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -317,7 +317,7 @@ createdb(const CreatedbStmt *stmt)
317317
* up-to-date for the copy. (We really only need to flush buffers for
318318
* the source database...)
319319
*/
320-
BufferSync();
320+
BufferSync(-1, -1);
321321

322322
/*
323323
* Close virtual file descriptors so the kernel has more available for
@@ -454,7 +454,7 @@ createdb(const CreatedbStmt *stmt)
454454
* will see the new database in pg_database right away. (They'll see
455455
* an uncommitted tuple, but they don't care; see GetRawDatabaseInfo.)
456456
*/
457-
BufferSync();
457+
BufferSync(-1, -1);
458458
}
459459

460460

@@ -591,7 +591,7 @@ dropdb(const char *dbname)
591591
* (They'll see an uncommitted deletion, but they don't care; see
592592
* GetRawDatabaseInfo.)
593593
*/
594-
BufferSync();
594+
BufferSync(-1, -1);
595595
}
596596

597597

@@ -688,7 +688,7 @@ RenameDatabase(const char *oldname, const char *newname)
688688
* see an uncommitted tuple, but they don't care; see
689689
* GetRawDatabaseInfo.)
690690
*/
691-
BufferSync();
691+
BufferSync(-1, -1);
692692
}
693693

694694

src/backend/postmaster/postmaster.c

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.348 2003/11/11 01:09:42 momjian Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.349 2003/11/19 15:55:07 wieck Exp $
4141
*
4242
* NOTES
4343
*
@@ -104,6 +104,7 @@
104104
#include "storage/pg_shmem.h"
105105
#include "storage/pmsignal.h"
106106
#include "storage/proc.h"
107+
#include "storage/bufmgr.h"
107108
#include "access/xlog.h"
108109
#include "tcop/tcopprot.h"
109110
#include "utils/guc.h"
@@ -224,7 +225,8 @@ char *preload_libraries_string = NULL;
224225
/* Startup/shutdown state */
225226
static pid_t StartupPID = 0,
226227
ShutdownPID = 0,
227-
CheckPointPID = 0;
228+
CheckPointPID = 0,
229+
BgWriterPID = 0;
228230
static time_t checkpointed = 0;
229231

230232
#define NoShutdown 0
@@ -298,6 +300,7 @@ __attribute__((format(printf, 1, 2)));
298300

299301
#define StartupDataBase() SSDataBase(BS_XLOG_STARTUP)
300302
#define CheckPointDataBase() SSDataBase(BS_XLOG_CHECKPOINT)
303+
#define StartBackgroundWriter() SSDataBase(BS_XLOG_BGWRITER)
301304
#define ShutdownDataBase() SSDataBase(BS_XLOG_SHUTDOWN)
302305

303306

@@ -1055,6 +1058,17 @@ ServerLoop(void)
10551058
}
10561059
}
10571060

1061+
/*
1062+
* If no background writer process is running and we should
1063+
* do background writing, start one. It doesn't matter if
1064+
* this fails, we'll just try again later.
1065+
*/
1066+
if (BgWriterPID == 0 && BgWriterPercent > 0 &&
1067+
Shutdown == NoShutdown && !FatalError && random_seed != 0)
1068+
{
1069+
BgWriterPID = StartBackgroundWriter();
1070+
}
1071+
10581072
/*
10591073
* Wait for something to happen.
10601074
*/
@@ -1478,6 +1492,13 @@ processCancelRequest(Port *port, void *pkt)
14781492
backendPID)));
14791493
return;
14801494
}
1495+
else if (backendPID == BgWriterPID)
1496+
{
1497+
ereport(DEBUG2,
1498+
(errmsg_internal("ignoring cancel request for bgwriter process %d",
1499+
backendPID)));
1500+
return;
1501+
}
14811502
else if (ExecBackend)
14821503
AttachSharedMemoryAndSemaphores();
14831504

@@ -1660,6 +1681,13 @@ SIGHUP_handler(SIGNAL_ARGS)
16601681
SignalChildren(SIGHUP);
16611682
load_hba();
16621683
load_ident();
1684+
1685+
/*
1686+
* Tell the background writer to terminate so that we
1687+
* will start a new one with a possibly changed config
1688+
*/
1689+
if (BgWriterPID != 0)
1690+
kill(BgWriterPID, SIGTERM);
16631691
}
16641692

16651693
PG_SETMASK(&UnBlockSig);
@@ -1692,6 +1720,8 @@ pmdie(SIGNAL_ARGS)
16921720
*
16931721
* Wait for children to end their work and ShutdownDataBase.
16941722
*/
1723+
if (BgWriterPID != 0)
1724+
kill(BgWriterPID, SIGTERM);
16951725
if (Shutdown >= SmartShutdown)
16961726
break;
16971727
Shutdown = SmartShutdown;
@@ -1724,6 +1754,8 @@ pmdie(SIGNAL_ARGS)
17241754
* abort all children with SIGTERM (rollback active transactions
17251755
* and exit) and ShutdownDataBase when they are gone.
17261756
*/
1757+
if (BgWriterPID != 0)
1758+
kill(BgWriterPID, SIGTERM);
17271759
if (Shutdown >= FastShutdown)
17281760
break;
17291761
ereport(LOG,
@@ -1770,6 +1802,8 @@ pmdie(SIGNAL_ARGS)
17701802
* abort all children with SIGQUIT and exit without attempt to
17711803
* properly shutdown data base system.
17721804
*/
1805+
if (BgWriterPID != 0)
1806+
kill(BgWriterPID, SIGQUIT);
17731807
ereport(LOG,
17741808
(errmsg("received immediate shutdown request")));
17751809
if (ShutdownPID > 0)
@@ -1877,6 +1911,12 @@ reaper(SIGNAL_ARGS)
18771911
CheckPointPID = 0;
18781912
checkpointed = time(NULL);
18791913

1914+
if (BgWriterPID == 0 && BgWriterPercent > 0 &&
1915+
Shutdown == NoShutdown && !FatalError && random_seed != 0)
1916+
{
1917+
BgWriterPID = StartBackgroundWriter();
1918+
}
1919+
18801920
/*
18811921
* Go to shutdown mode if a shutdown request was pending.
18821922
*/
@@ -1983,6 +2023,8 @@ CleanupProc(int pid,
19832023
GetSavedRedoRecPtr();
19842024
}
19852025
}
2026+
else if (pid == BgWriterPID)
2027+
BgWriterPID = 0;
19862028
else
19872029
pgstat_beterm(pid);
19882030

@@ -1996,6 +2038,7 @@ CleanupProc(int pid,
19962038
{
19972039
LogChildExit(LOG,
19982040
(pid == CheckPointPID) ? gettext("checkpoint process") :
2041+
(pid == BgWriterPID) ? gettext("bgwriter process") :
19992042
gettext("server process"),
20002043
pid, exitstatus);
20012044
ereport(LOG,
@@ -2044,6 +2087,10 @@ CleanupProc(int pid,
20442087
CheckPointPID = 0;
20452088
checkpointed = 0;
20462089
}
2090+
else if (pid == BgWriterPID)
2091+
{
2092+
BgWriterPID = 0;
2093+
}
20472094
else
20482095
{
20492096
/*
@@ -2754,6 +2801,8 @@ CountChildren(void)
27542801
}
27552802
if (CheckPointPID != 0)
27562803
cnt--;
2804+
if (BgWriterPID != 0)
2805+
cnt--;
27572806
return cnt;
27582807
}
27592808

@@ -2827,6 +2876,9 @@ SSDataBase(int xlop)
28272876
case BS_XLOG_CHECKPOINT:
28282877
statmsg = "checkpoint subprocess";
28292878
break;
2879+
case BS_XLOG_BGWRITER:
2880+
statmsg = "bgwriter subprocess";
2881+
break;
28302882
case BS_XLOG_SHUTDOWN:
28312883
statmsg = "shutdown subprocess";
28322884
break;
@@ -2883,6 +2935,10 @@ SSDataBase(int xlop)
28832935
ereport(LOG,
28842936
(errmsg("could not fork checkpoint process: %m")));
28852937
break;
2938+
case BS_XLOG_BGWRITER:
2939+
ereport(LOG,
2940+
(errmsg("could not fork bgwriter process: %m")));
2941+
break;
28862942
case BS_XLOG_SHUTDOWN:
28872943
ereport(LOG,
28882944
(errmsg("could not fork shutdown process: %m")));
@@ -2895,19 +2951,22 @@ SSDataBase(int xlop)
28952951

28962952
/*
28972953
* fork failure is fatal during startup/shutdown, but there's no
2898-
* need to choke if a routine checkpoint fails.
2954+
* need to choke if a routine checkpoint or starting a background
2955+
* writer fails.
28992956
*/
29002957
if (xlop == BS_XLOG_CHECKPOINT)
29012958
return 0;
2959+
if (xlop == BS_XLOG_BGWRITER)
2960+
return 0;
29022961
ExitPostmaster(1);
29032962
}
29042963

29052964
/*
29062965
* The startup and shutdown processes are not considered normal
2907-
* backends, but the checkpoint process is. Checkpoint must be added
2908-
* to the list of backends.
2966+
* backends, but the checkpoint and bgwriter processes are.
2967+
* They must be added to the list of backends.
29092968
*/
2910-
if (xlop == BS_XLOG_CHECKPOINT)
2969+
if (xlop == BS_XLOG_CHECKPOINT || xlop == BS_XLOG_BGWRITER)
29112970
{
29122971
if (!(bn = (Backend *) malloc(sizeof(Backend))))
29132972
{

0 commit comments

Comments
 (0)