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

Commitf13ea95

Browse files
committed
Change pg_ctl to detect server-ready by watching status in postmaster.pid.
Traditionally, "pg_ctl start -w" has waited for the server to becomeready to accept connections by attempting a connection once per second.That has the major problem that connection issues (for instance, akernel packet filter blocking traffic) can't be reliably told apartfrom server startup issues, and the minor problem that if server startupisn't quick, we accumulate "the database system is starting up" spamin the server log. We've hacked around many of the possible connectionissues, but it resulted in ugly and complicated code in pg_ctl.c.In commitc61559e, I changed the probe rate to every tenth of a second.That prompted Jeff Janes to complain that the log-spam problem had becomemuch worse. In the ensuing discussion, Andres Freund pointed out thatwe could dispense with connection attempts altogether if the postmasterwere changed to report its status in postmaster.pid, which "pg_ctl start"already relies on being able to read. This patch implements that, teachingpostmaster.c to report a status string into the pidfile at the samestate-change points already identified as being of interest for systemdstatus reporting (cf commit7d17e68). pg_ctl no longer needs to linkwith libpq at all; all its functions now depend on reading server files.In support of this, teach AddToDataDirLockFile() to allow addition ofpostmaster.pid lines in not-necessarily-sequential order. This is neededon Windows where the SHMEM_KEY line will never be written at all. We stillhave the restriction that we don't want to truncate the pidfile; documentthe reasons for that a bit better.Also, fix the pg_ctl TAP tests so they'll notice if "start -w" modeis broken --- before, they'd just wait out the sixty seconds untilthe loop gives up, and then report success anyway. (Yes, I found thatout the hard way.)While at it, arrange for pg_ctl to not need to #include miscadmin.h;as a rather low-level backend header, requiring that to be compilableclient-side is pretty dubious. This requires moving the #define'sassociated with the pidfile into a new header file, and movingPG_BACKEND_VERSIONSTR someplace else. For lack of a clearly better"someplace else", I put it into port.h, beside the declaration offind_other_exec(), since most users of that macro are passing the value tofind_other_exec(). (initdb still depends on miscadmin.h, but at leastpg_ctl and pg_upgrade no longer do.)In passing, fix main.c so that PG_BACKEND_VERSIONSTR actually defines theoutput of "postgres -V", which remarkably it had never done before.Discussion:https://postgr.es/m/CAMkU=1xJW8e+CTotojOMBd-yzUvD0e_JZu2xHo=MnuZ4__m7Pg@mail.gmail.com
1 parent8c55244 commitf13ea95

File tree

13 files changed

+203
-228
lines changed

13 files changed

+203
-228
lines changed

‎src/backend/main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ main(int argc, char *argv[])
169169
}
170170
if (strcmp(argv[1],"--version")==0||strcmp(argv[1],"-V")==0)
171171
{
172-
puts("postgres (PostgreSQL) "PG_VERSION);
172+
fputs(PG_BACKEND_VERSIONSTR,stdout);
173173
exit(0);
174174
}
175175

‎src/backend/port/sysv_shmem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include"storage/ipc.h"
3939
#include"storage/pg_shmem.h"
4040
#include"utils/guc.h"
41+
#include"utils/pidfile.h"
4142

4243

4344
/*

‎src/backend/postmaster/postmaster.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#include"utils/datetime.h"
126126
#include"utils/dynamic_loader.h"
127127
#include"utils/memutils.h"
128+
#include"utils/pidfile.h"
128129
#include"utils/ps_status.h"
129130
#include"utils/timeout.h"
130131
#include"utils/varlena.h"
@@ -1340,6 +1341,12 @@ PostmasterMain(int argc, char *argv[])
13401341
gettimeofday(&random_start_time,NULL);
13411342
#endif
13421343

1344+
/*
1345+
* Report postmaster status in the postmaster.pid file, to allow pg_ctl to
1346+
* see what's happening.
1347+
*/
1348+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_STARTING);
1349+
13431350
/*
13441351
* We're ready to rock and roll...
13451352
*/
@@ -2608,6 +2615,9 @@ pmdie(SIGNAL_ARGS)
26082615
Shutdown=SmartShutdown;
26092616
ereport(LOG,
26102617
(errmsg("received smart shutdown request")));
2618+
2619+
/* Report status */
2620+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_STOPPING);
26112621
#ifdefUSE_SYSTEMD
26122622
sd_notify(0,"STOPPING=1");
26132623
#endif
@@ -2663,6 +2673,9 @@ pmdie(SIGNAL_ARGS)
26632673
Shutdown=FastShutdown;
26642674
ereport(LOG,
26652675
(errmsg("received fast shutdown request")));
2676+
2677+
/* Report status */
2678+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_STOPPING);
26662679
#ifdefUSE_SYSTEMD
26672680
sd_notify(0,"STOPPING=1");
26682681
#endif
@@ -2727,6 +2740,9 @@ pmdie(SIGNAL_ARGS)
27272740
Shutdown=ImmediateShutdown;
27282741
ereport(LOG,
27292742
(errmsg("received immediate shutdown request")));
2743+
2744+
/* Report status */
2745+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_STOPPING);
27302746
#ifdefUSE_SYSTEMD
27312747
sd_notify(0,"STOPPING=1");
27322748
#endif
@@ -2872,6 +2888,8 @@ reaper(SIGNAL_ARGS)
28722888
ereport(LOG,
28732889
(errmsg("database system is ready to accept connections")));
28742890

2891+
/* Report status */
2892+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_READY);
28752893
#ifdefUSE_SYSTEMD
28762894
sd_notify(0,"READY=1");
28772895
#endif
@@ -5005,10 +5023,18 @@ sigusr1_handler(SIGNAL_ARGS)
50055023
if (XLogArchivingAlways())
50065024
PgArchPID=pgarch_start();
50075025

5008-
#ifdefUSE_SYSTEMD
5026+
/*
5027+
* If we aren't planning to enter hot standby mode later, treat
5028+
* RECOVERY_STARTED as meaning we're out of startup, and report status
5029+
* accordingly.
5030+
*/
50095031
if (!EnableHotStandby)
5032+
{
5033+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_STANDBY);
5034+
#ifdefUSE_SYSTEMD
50105035
sd_notify(0,"READY=1");
50115036
#endif
5037+
}
50125038

50135039
pmState=PM_RECOVERY;
50145040
}
@@ -5024,6 +5050,8 @@ sigusr1_handler(SIGNAL_ARGS)
50245050
ereport(LOG,
50255051
(errmsg("database system is ready to accept read only connections")));
50265052

5053+
/* Report status */
5054+
AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS,PM_STATUS_READY);
50275055
#ifdefUSE_SYSTEMD
50285056
sd_notify(0,"READY=1");
50295057
#endif

‎src/backend/utils/init/miscinit.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include"utils/builtins.h"
4848
#include"utils/guc.h"
4949
#include"utils/memutils.h"
50+
#include"utils/pidfile.h"
5051
#include"utils/syscache.h"
5152
#include"utils/varlena.h"
5253

@@ -1149,8 +1150,9 @@ TouchSocketLockFiles(void)
11491150
*
11501151
* Note: because we don't truncate the file, if we were to rewrite a line
11511152
* with less data than it had before, there would be garbage after the last
1152-
* line. We don't ever actually do that, so not worth adding another kernel
1153-
* call to cover the possibility.
1153+
* line. While we could fix that by adding a truncate call, that would make
1154+
* the file update non-atomic, which we'd rather avoid. Therefore, callers
1155+
* should endeavor never to shorten a line once it's been written.
11541156
*/
11551157
void
11561158
AddToDataDirLockFile(inttarget_line,constchar*str)
@@ -1193,18 +1195,25 @@ AddToDataDirLockFile(int target_line, const char *str)
11931195
srcptr=srcbuffer;
11941196
for (lineno=1;lineno<target_line;lineno++)
11951197
{
1196-
if ((srcptr=strchr(srcptr,'\n'))==NULL)
1197-
{
1198-
elog(LOG,"incomplete data in \"%s\": found only %d newlines while trying to add line %d",
1199-
DIRECTORY_LOCK_FILE,lineno-1,target_line);
1200-
close(fd);
1201-
return;
1202-
}
1203-
srcptr++;
1198+
char*eol=strchr(srcptr,'\n');
1199+
1200+
if (eol==NULL)
1201+
break;/* not enough lines in file yet */
1202+
srcptr=eol+1;
12041203
}
12051204
memcpy(destbuffer,srcbuffer,srcptr-srcbuffer);
12061205
destptr=destbuffer+ (srcptr-srcbuffer);
12071206

1207+
/*
1208+
* Fill in any missing lines before the target line, in case lines are
1209+
* added to the file out of order.
1210+
*/
1211+
for (;lineno<target_line;lineno++)
1212+
{
1213+
if (destptr<destbuffer+sizeof(destbuffer))
1214+
*destptr++='\n';
1215+
}
1216+
12081217
/*
12091218
* Write or rewrite the target line.
12101219
*/

‎src/bin/pg_ctl/Makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ subdir = src/bin/pg_ctl
1616
top_builddir = ../../..
1717
include$(top_builddir)/src/Makefile.global
1818

19-
overrideCPPFLAGS := -I$(libpq_srcdir)$(CPPFLAGS)
20-
2119
OBJS=pg_ctl.o$(WIN32RES)
2220

2321
all: pg_ctl
2422

25-
pg_ctl:$(OBJS) | submake-libpq submake-libpgport
26-
$(CC)$(CFLAGS)$(OBJS)$(libpq_pgport)$(LDFLAGS)$(LDFLAGS_EX)$(LIBS) -o$@$(X)
23+
pg_ctl:$(OBJS) | submake-libpgport
24+
$(CC)$(CFLAGS)$(OBJS)$(LDFLAGS)$(LDFLAGS_EX)$(LIBS) -o$@$(X)
2725

2826
install: all installdirs
2927
$(INSTALL_PROGRAM) pg_ctl$(X)'$(DESTDIR)$(bindir)/pg_ctl$(X)'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp