37
37
*
38
38
*
39
39
* 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 $
41
41
*
42
42
* NOTES
43
43
*
104
104
#include "storage/pg_shmem.h"
105
105
#include "storage/pmsignal.h"
106
106
#include "storage/proc.h"
107
+ #include "storage/bufmgr.h"
107
108
#include "access/xlog.h"
108
109
#include "tcop/tcopprot.h"
109
110
#include "utils/guc.h"
@@ -224,7 +225,8 @@ char *preload_libraries_string = NULL;
224
225
/* Startup/shutdown state */
225
226
static pid_t StartupPID = 0 ,
226
227
ShutdownPID = 0 ,
227
- CheckPointPID = 0 ;
228
+ CheckPointPID = 0 ,
229
+ BgWriterPID = 0 ;
228
230
static time_t checkpointed = 0 ;
229
231
230
232
#define NoShutdown 0
@@ -298,6 +300,7 @@ __attribute__((format(printf, 1, 2)));
298
300
299
301
#define StartupDataBase () SSDataBase(BS_XLOG_STARTUP)
300
302
#define CheckPointDataBase () SSDataBase(BS_XLOG_CHECKPOINT)
303
+ #define StartBackgroundWriter () SSDataBase(BS_XLOG_BGWRITER)
301
304
#define ShutdownDataBase () SSDataBase(BS_XLOG_SHUTDOWN)
302
305
303
306
@@ -1055,6 +1058,17 @@ ServerLoop(void)
1055
1058
}
1056
1059
}
1057
1060
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
+
1058
1072
/*
1059
1073
* Wait for something to happen.
1060
1074
*/
@@ -1478,6 +1492,13 @@ processCancelRequest(Port *port, void *pkt)
1478
1492
backendPID )));
1479
1493
return ;
1480
1494
}
1495
+ else if (backendPID == BgWriterPID )
1496
+ {
1497
+ ereport (DEBUG2 ,
1498
+ (errmsg_internal ("ignoring cancel request for bgwriter process %d" ,
1499
+ backendPID )));
1500
+ return ;
1501
+ }
1481
1502
else if (ExecBackend )
1482
1503
AttachSharedMemoryAndSemaphores ();
1483
1504
@@ -1660,6 +1681,13 @@ SIGHUP_handler(SIGNAL_ARGS)
1660
1681
SignalChildren (SIGHUP );
1661
1682
load_hba ();
1662
1683
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 );
1663
1691
}
1664
1692
1665
1693
PG_SETMASK (& UnBlockSig );
@@ -1692,6 +1720,8 @@ pmdie(SIGNAL_ARGS)
1692
1720
*
1693
1721
* Wait for children to end their work and ShutdownDataBase.
1694
1722
*/
1723
+ if (BgWriterPID != 0 )
1724
+ kill (BgWriterPID , SIGTERM );
1695
1725
if (Shutdown >= SmartShutdown )
1696
1726
break ;
1697
1727
Shutdown = SmartShutdown ;
@@ -1724,6 +1754,8 @@ pmdie(SIGNAL_ARGS)
1724
1754
* abort all children with SIGTERM (rollback active transactions
1725
1755
* and exit) and ShutdownDataBase when they are gone.
1726
1756
*/
1757
+ if (BgWriterPID != 0 )
1758
+ kill (BgWriterPID , SIGTERM );
1727
1759
if (Shutdown >= FastShutdown )
1728
1760
break ;
1729
1761
ereport (LOG ,
@@ -1770,6 +1802,8 @@ pmdie(SIGNAL_ARGS)
1770
1802
* abort all children with SIGQUIT and exit without attempt to
1771
1803
* properly shutdown data base system.
1772
1804
*/
1805
+ if (BgWriterPID != 0 )
1806
+ kill (BgWriterPID , SIGQUIT );
1773
1807
ereport (LOG ,
1774
1808
(errmsg ("received immediate shutdown request" )));
1775
1809
if (ShutdownPID > 0 )
@@ -1877,6 +1911,12 @@ reaper(SIGNAL_ARGS)
1877
1911
CheckPointPID = 0 ;
1878
1912
checkpointed = time (NULL );
1879
1913
1914
+ if (BgWriterPID == 0 && BgWriterPercent > 0 &&
1915
+ Shutdown == NoShutdown && !FatalError && random_seed != 0 )
1916
+ {
1917
+ BgWriterPID = StartBackgroundWriter ();
1918
+ }
1919
+
1880
1920
/*
1881
1921
* Go to shutdown mode if a shutdown request was pending.
1882
1922
*/
@@ -1983,6 +2023,8 @@ CleanupProc(int pid,
1983
2023
GetSavedRedoRecPtr ();
1984
2024
}
1985
2025
}
2026
+ else if (pid == BgWriterPID )
2027
+ BgWriterPID = 0 ;
1986
2028
else
1987
2029
pgstat_beterm (pid );
1988
2030
@@ -1996,6 +2038,7 @@ CleanupProc(int pid,
1996
2038
{
1997
2039
LogChildExit (LOG ,
1998
2040
(pid == CheckPointPID ) ? gettext ("checkpoint process" ) :
2041
+ (pid == BgWriterPID ) ? gettext ("bgwriter process" ) :
1999
2042
gettext ("server process" ),
2000
2043
pid , exitstatus );
2001
2044
ereport (LOG ,
@@ -2044,6 +2087,10 @@ CleanupProc(int pid,
2044
2087
CheckPointPID = 0 ;
2045
2088
checkpointed = 0 ;
2046
2089
}
2090
+ else if (pid == BgWriterPID )
2091
+ {
2092
+ BgWriterPID = 0 ;
2093
+ }
2047
2094
else
2048
2095
{
2049
2096
/*
@@ -2754,6 +2801,8 @@ CountChildren(void)
2754
2801
}
2755
2802
if (CheckPointPID != 0 )
2756
2803
cnt -- ;
2804
+ if (BgWriterPID != 0 )
2805
+ cnt -- ;
2757
2806
return cnt ;
2758
2807
}
2759
2808
@@ -2827,6 +2876,9 @@ SSDataBase(int xlop)
2827
2876
case BS_XLOG_CHECKPOINT :
2828
2877
statmsg = "checkpoint subprocess" ;
2829
2878
break ;
2879
+ case BS_XLOG_BGWRITER :
2880
+ statmsg = "bgwriter subprocess" ;
2881
+ break ;
2830
2882
case BS_XLOG_SHUTDOWN :
2831
2883
statmsg = "shutdown subprocess" ;
2832
2884
break ;
@@ -2883,6 +2935,10 @@ SSDataBase(int xlop)
2883
2935
ereport (LOG ,
2884
2936
(errmsg ("could not fork checkpoint process: %m" )));
2885
2937
break ;
2938
+ case BS_XLOG_BGWRITER :
2939
+ ereport (LOG ,
2940
+ (errmsg ("could not fork bgwriter process: %m" )));
2941
+ break ;
2886
2942
case BS_XLOG_SHUTDOWN :
2887
2943
ereport (LOG ,
2888
2944
(errmsg ("could not fork shutdown process: %m" )));
@@ -2895,19 +2951,22 @@ SSDataBase(int xlop)
2895
2951
2896
2952
/*
2897
2953
* 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.
2899
2956
*/
2900
2957
if (xlop == BS_XLOG_CHECKPOINT )
2901
2958
return 0 ;
2959
+ if (xlop == BS_XLOG_BGWRITER )
2960
+ return 0 ;
2902
2961
ExitPostmaster (1 );
2903
2962
}
2904
2963
2905
2964
/*
2906
2965
* 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.
2909
2968
*/
2910
- if (xlop == BS_XLOG_CHECKPOINT )
2969
+ if (xlop == BS_XLOG_CHECKPOINT || xlop == BS_XLOG_BGWRITER )
2911
2970
{
2912
2971
if (!(bn = (Backend * ) malloc (sizeof (Backend ))))
2913
2972
{
0 commit comments