Skip to content

Commit b0299c5

Browse files
committed
Auto checkpoint creation.
1 parent a0951ee commit b0299c5

File tree

6 files changed

+142
-55
lines changed

6 files changed

+142
-55
lines changed

src/backend/access/transam/xlog.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.24 2000/11/05 22:50:19 vadim Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.25 2000/11/09 11:25:58 vadim Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -42,13 +42,13 @@ void CreateCheckPoint(bool shutdown);
4242

4343
char XLogDir[MAXPGPATH];
4444
char ControlFilePath[MAXPGPATH];
45-
uint32 XLOGbuffers = 0;
45+
int XLOGbuffers = 0;
4646
XLogRecPtr MyLastRecPtr = {0, 0};
4747
bool StopIfError = false;
4848
bool InRecovery = false;
4949
StartUpID ThisStartUpID = 0;
5050

51-
int XLOG_DEBUG = 1;
51+
int XLOG_DEBUG = 0;
5252

5353
/* To read/update control file and create new log file */
5454
SPINLOCK ControlFileLockId;
@@ -919,7 +919,7 @@ MoveOfflineLogs(char *archdir, uint32 _logId, uint32 _logSeg)
919919
elog(LOG, "MoveOfflineLogs: %s %s", (archdir[0]) ?
920920
"archive" : "remove", xlde->d_name);
921921
sprintf(path, "%s%c%s", XLogDir, SEP_CHAR, xlde->d_name);
922-
if (archdir[0] != 0)
922+
if (archdir[0] == 0)
923923
unlink(path);
924924
errno = 0;
925925
}
@@ -1641,9 +1641,14 @@ SetThisStartUpID(void)
16411641
void
16421642
ShutdownXLOG()
16431643
{
1644-
1644+
#ifdef XLOG
1645+
extern void CreateDummyCaches(void);
1646+
#endif
16451647
elog(LOG, "Data Base System shutting down at %s", str_time(time(NULL)));
16461648

1649+
#ifdef XLOG
1650+
CreateDummyCaches();
1651+
#endif
16471652
CreateCheckPoint(true);
16481653

16491654
elog(LOG, "Data Base System shut down at %s", str_time(time(NULL)));

src/backend/bootstrap/bootstrap.c

Lines changed: 27 additions & 26 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.97 2000/11/08 22:09:56 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.98 2000/11/09 11:25:58 vadim Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -220,7 +220,7 @@ BootstrapMain(int argc, char *argv[])
220220
int i;
221221
char *dbName;
222222
int flag;
223-
bool xloginit = false;
223+
int xlogop = BS_XLOG_NOP;
224224
char *potential_DataDir = NULL;
225225

226226
extern int optind;
@@ -260,7 +260,7 @@ BootstrapMain(int argc, char *argv[])
260260
potential_DataDir = getenv("PGDATA"); /* Null if no PGDATA variable */
261261
}
262262

263-
while ((flag = getopt(argc, argv, "D:dCQxpB:F")) != EOF)
263+
while ((flag = getopt(argc, argv, "D:dCQx:pB:F")) != EOF)
264264
{
265265
switch (flag)
266266
{
@@ -281,7 +281,7 @@ BootstrapMain(int argc, char *argv[])
281281
Quiet = true;
282282
break;
283283
case 'x':
284-
xloginit = true;
284+
xlogop = atoi(optarg);
285285
break;
286286
case 'p':
287287
/* indicates fork from postmaster */
@@ -339,40 +339,41 @@ BootstrapMain(int argc, char *argv[])
339339
}
340340

341341
/*
342-
* Bootstrap under Postmaster means two things: (xloginit) ?
343-
* StartupXLOG : ShutdownXLOG
344-
*
345-
* If !under Postmaster and xloginit then BootStrapXLOG.
342+
* XLOG operations
346343
*/
347-
if (IsUnderPostmaster || xloginit)
344+
if (xlogop != BS_XLOG_NOP)
348345
{
349346
snprintf(XLogDir, MAXPGPATH, "%s/pg_xlog", DataDir);
350347
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
348+
if (xlogop == BS_XLOG_BOOTSTRAP)
349+
BootStrapXLOG();
350+
else
351+
{
352+
SetProcessingMode(NormalProcessing);
353+
if (xlogop == BS_XLOG_STARTUP)
354+
StartupXLOG();
355+
else if (xlogop == BS_XLOG_CHECKPOINT)
356+
{
357+
#ifdef XLOG
358+
extern void CreateDummyCaches(void);
359+
CreateDummyCaches();
360+
#endif
361+
CreateCheckPoint(false);
362+
}
363+
else if (xlogop == BS_XLOG_SHUTDOWN)
364+
ShutdownXLOG();
365+
else
366+
elog(STOP, "Unsupported XLOG op %d", xlogop);
367+
proc_exit(0);
368+
}
351369
}
352370

353-
if (IsUnderPostmaster && xloginit)
354-
{
355-
SetProcessingMode(NormalProcessing);
356-
StartupXLOG();
357-
proc_exit(0);
358-
}
359-
360-
if (!IsUnderPostmaster && xloginit)
361-
BootStrapXLOG();
362-
363371
/*
364372
* backend initialization
365373
*/
366374
InitPostgres(dbName, NULL);
367375
LockDisable(true);
368376

369-
if (IsUnderPostmaster && !xloginit)
370-
{
371-
SetProcessingMode(NormalProcessing);
372-
ShutdownXLOG();
373-
proc_exit(0);
374-
}
375-
376377
for (i = 0; i < MAXATTR; i++)
377378
{
378379
attrtypes[i] = (Form_pg_attribute) NULL;

src/backend/postmaster/postmaster.c

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.180 2000/11/08 17:57:46 petere Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.181 2000/11/09 11:25:59 vadim Exp $
1515
*
1616
* NOTES
1717
*
@@ -78,7 +78,7 @@
7878
#include "utils/exc.h"
7979
#include "utils/guc.h"
8080
#include "utils/memutils.h"
81-
81+
#include "bootstrap/bootstrap.h"
8282

8383
#define INVALID_SOCK (-1)
8484
#define ARGV_SIZE 64
@@ -197,8 +197,12 @@ bool NetServer = false; /* listen on TCP/IP */
197197
bool EnableSSL = false;
198198
bool SilentMode = false; /* silent mode (-S) */
199199

200-
static pid_t StartupPID = 0,
201-
ShutdownPID = 0;
200+
int CheckPointTimeout = 300;
201+
202+
static pid_t StartupPID = 0,
203+
ShutdownPID = 0,
204+
CheckPointPID = 0;
205+
static time_t checkpointed = 0;
202206

203207
#define NoShutdown 0
204208
#define SmartShutdown 1
@@ -250,11 +254,11 @@ static void SignalChildren(SIGNAL_ARGS);
250254
static int CountChildren(void);
251255
static bool CreateOptsFile(int argc, char *argv[]);
252256

253-
extern int BootstrapMain(int argc, char *argv[]);
254-
static pid_t SSDataBase(bool startup);
257+
static pid_t SSDataBase(int xlop);
255258

256-
#define StartupDataBase() SSDataBase(true)
257-
#define ShutdownDataBase() SSDataBase(false)
259+
#define StartupDataBase() SSDataBase(BS_XLOG_STARTUP)
260+
#define CheckPointDataBase() SSDataBase(BS_XLOG_CHECKPOINT)
261+
#define ShutdownDataBase() SSDataBase(BS_XLOG_SHUTDOWN)
258262

259263
#ifdef USE_SSL
260264
static void InitSSL(void);
@@ -814,13 +818,27 @@ ServerLoop(void)
814818

815819
for (;;)
816820
{
817-
Port *port;
818-
fd_set rmask,
819-
wmask;
820-
struct timeval *timeout = (struct timeval *) NULL;
821-
#ifdef USE_SSL
822-
struct timeval timeout_tv;
821+
Port *port;
822+
fd_set rmask,
823+
wmask;
824+
struct timeval *timeout = NULL;
825+
struct timeval timeout_tv;
826+
827+
if (CheckPointPID == 0 && checkpointed)
828+
{
829+
time_t now = time(NULL);
830+
831+
if (CheckPointTimeout + checkpointed > now)
832+
{
833+
timeout_tv.tv_sec = CheckPointTimeout + checkpointed - now;
834+
timeout_tv.tv_usec = 0;
835+
timeout = &timeout_tv;
836+
}
837+
else
838+
CheckPointPID = CheckPointDataBase();
839+
}
823840

841+
#ifdef USE_SSL
824842
/*
825843
* If we are using SSL, there may be input data already read and
826844
* pending in SSL's input buffers. If so, check for additional
@@ -850,6 +868,7 @@ ServerLoop(void)
850868

851869
if (select(nSockets, &rmask, &wmask, (fd_set *) NULL, timeout) < 0)
852870
{
871+
PG_SETMASK(&BlockSig);
853872
if (errno == EINTR || errno == EWOULDBLOCK)
854873
continue;
855874
fprintf(stderr, "%s: ServerLoop: select failed: %s\n",
@@ -1186,6 +1205,14 @@ processCancelRequest(Port *port, PacketLen len, void *pkt)
11861205
backendPID = (int) ntohl(canc->backendPID);
11871206
cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
11881207

1208+
if (backendPID == CheckPointPID)
1209+
{
1210+
if (DebugLvl)
1211+
fprintf(stderr, "%s: processCancelRequest: CheckPointPID in cancel request for process %d\n",
1212+
progname, backendPID);
1213+
return STATUS_ERROR;
1214+
}
1215+
11891216
/* See if we have a matching backend */
11901217

11911218
for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
@@ -1480,6 +1507,9 @@ reaper(SIGNAL_ARGS)
14801507
*/
14811508
SetThisStartUpID();
14821509

1510+
CheckPointPID = 0;
1511+
checkpointed = time(NULL);
1512+
14831513
pqsignal(SIGCHLD, reaper);
14841514
return;
14851515
}
@@ -1563,7 +1593,13 @@ CleanupProc(int pid,
15631593
curr = DLGetSucc(curr);
15641594
}
15651595

1566-
ProcRemove(pid);
1596+
if (pid == CheckPointPID)
1597+
{
1598+
CheckPointPID = 0;
1599+
checkpointed = time(NULL);
1600+
}
1601+
else
1602+
ProcRemove(pid);
15671603

15681604
return;
15691605
}
@@ -1612,7 +1648,13 @@ CleanupProc(int pid,
16121648
* only, couldn't we just sigpause?), so probably we'll remove
16131649
* this call from here someday. -- vadim 04-10-1999
16141650
*/
1615-
ProcRemove(pid);
1651+
if (pid == CheckPointPID)
1652+
{
1653+
CheckPointPID = 0;
1654+
checkpointed = 0;
1655+
}
1656+
else
1657+
ProcRemove(pid);
16161658

16171659
DLRemove(curr);
16181660
free(bp);
@@ -2090,6 +2132,8 @@ CountChildren(void)
20902132
if (bp->pid != mypid)
20912133
cnt++;
20922134
}
2135+
if (CheckPointPID != 0)
2136+
cnt--;
20932137
return cnt;
20942138
}
20952139

@@ -2132,10 +2176,11 @@ InitSSL(void)
21322176
#endif
21332177

21342178
static pid_t
2135-
SSDataBase(bool startup)
2179+
SSDataBase(int xlop)
21362180
{
21372181
pid_t pid;
21382182
int i;
2183+
Backend *bn;
21392184
static char ssEntry[4][2 * ARGV_SIZE];
21402185

21412186
for (i = 0; i < 4; ++i)
@@ -2159,6 +2204,7 @@ SSDataBase(bool startup)
21592204
int ac = 0;
21602205
char nbbuf[ARGV_SIZE];
21612206
char dbbuf[ARGV_SIZE];
2207+
char xlbuf[ARGV_SIZE];
21622208

21632209
/* Lose the postmaster's on-exit routines and port connections */
21642210
on_exit_reset();
@@ -2178,8 +2224,8 @@ SSDataBase(bool startup)
21782224
sprintf(nbbuf, "-B%u", NBuffers);
21792225
av[ac++] = nbbuf;
21802226

2181-
if (startup)
2182-
av[ac++] = "-x";
2227+
sprintf(xlbuf, "-x %d", xlop);
2228+
av[ac++] = xlbuf;
21832229

21842230
av[ac++] = "-p";
21852231

@@ -2206,12 +2252,28 @@ SSDataBase(bool startup)
22062252
if (pid < 0)
22072253
{
22082254
fprintf(stderr, "%s Data Base: fork failed: %s\n",
2209-
((startup) ? "Startup" : "Shutdown"), strerror(errno));
2255+
((xlop == BS_XLOG_STARTUP) ? "Startup" :
2256+
((xlop == BS_XLOG_CHECKPOINT) ? "CheckPoint" :
2257+
"Shutdown")), strerror(errno));
22102258
ExitPostmaster(1);
22112259
}
22122260

22132261
NextBackendTag -= 1;
22142262

2263+
if (xlop != BS_XLOG_CHECKPOINT)
2264+
return(pid);
2265+
2266+
if (!(bn = (Backend *) calloc(1, sizeof(Backend))))
2267+
{
2268+
fprintf(stderr, "%s: CheckPointDataBase: malloc failed\n",
2269+
progname);
2270+
ExitPostmaster(1);
2271+
}
2272+
2273+
bn->pid = pid;
2274+
bn->cancel_key = 0;
2275+
DLAddHead(BackendList, DLNewElem(bn));
2276+
22152277
return (pid);
22162278
}
22172279

0 commit comments

Comments
 (0)