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

Commit78e4fb8

Browse files
committed
Prevent running pg_resetwal/pg_resetxlog against wrong-version data dirs.
pg_resetwal (formerly pg_resetxlog) doesn't insist on finding a matchingversion number in pg_control, and that seems like an important thing topreserve since recovering from corrupt pg_control is a prime reason toneed to run it. However, that means you can try to run it against adata directory of a different major version, which is at best uselessand at worst disastrous. So as to provide some protection against thattype of pilot error, inspect PG_VERSION at startup and refuse to doanything if it doesn't match. PG_VERSION is read-only after initdb,so it's unlikely to get corrupted, and even if it were corrupted it wouldbe easy to fix by hand.This hazard has been there all along, so back-patch to all supportedbranches.Michael Paquier, with some kibitzing by meDiscussion:https://postgr.es/m/f4b8eb91-b934-8a0d-b3cc-68f06e2279d1@enterprisedb.com
1 parent7d9309f commit78e4fb8

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

‎doc/src/sgml/ref/pg_resetxlog.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ PostgreSQL documentation
203203
<command>pg_resetxlog</command> to run. But before you do
204204
so, make doubly certain that there is no server process still alive.
205205
</para>
206+
207+
<para>
208+
<command>pg_resetxlog</command> works only with servers of the same
209+
major version.
210+
</para>
206211
</refsect1>
207212

208213
</refentry>

‎src/bin/pg_resetxlog/pg_resetxlog.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static const char *progname;
6969
staticchar*restrict_env;
7070
#endif
7171

72+
staticvoidCheckDataVersion(void);
7273
staticboolReadControlFile(void);
7374
staticvoidGuessControlValues(void);
7475
staticvoidPrintControlValues(boolguessed);
@@ -278,6 +279,9 @@ main(int argc, char *argv[])
278279
exit(1);
279280
}
280281

282+
/* Check that data directory matches our server version */
283+
CheckDataVersion();
284+
281285
/*
282286
* Check for a postmaster lock file --- if there is one, refuse to
283287
* proceed, on grounds we might be interfering with a live installation.
@@ -399,6 +403,70 @@ main(int argc, char *argv[])
399403
}
400404

401405

406+
/*
407+
* Look at the version string stored in PG_VERSION and decide if this utility
408+
* can be run safely or not.
409+
*
410+
* We don't want to inject pg_control and WAL files that are for a different
411+
* major version; that can't do anything good. Note that we don't treat
412+
* mismatching version info in pg_control as a reason to bail out, because
413+
* recovering from a corrupted pg_control is one of the main reasons for this
414+
* program to exist at all. However, PG_VERSION is unlikely to get corrupted,
415+
* and if it were it would be easy to fix by hand. So let's make this check
416+
* to prevent simple user errors.
417+
*/
418+
staticvoid
419+
CheckDataVersion(void)
420+
{
421+
constchar*ver_file="PG_VERSION";
422+
FILE*ver_fd;
423+
charrawline[64];
424+
intlen;
425+
426+
if ((ver_fd=fopen(ver_file,"r"))==NULL)
427+
{
428+
fprintf(stderr,_("%s: could not open file \"%s\" for reading: %s\n"),
429+
progname,ver_file,strerror(errno));
430+
exit(1);
431+
}
432+
433+
/* version number has to be the first line read */
434+
if (!fgets(rawline,sizeof(rawline),ver_fd))
435+
{
436+
if (!ferror(ver_fd))
437+
{
438+
fprintf(stderr,_("%s: unexpected empty file \"%s\"\n"),
439+
progname,ver_file);
440+
}
441+
else
442+
{
443+
fprintf(stderr,_("%s: could not read file \"%s\": %s\n"),
444+
progname,ver_file,strerror(errno));
445+
}
446+
exit(1);
447+
}
448+
449+
/* remove trailing newline, handling Windows newlines as well */
450+
len=strlen(rawline);
451+
if (len>0&&rawline[len-1]=='\n')
452+
{
453+
rawline[--len]='\0';
454+
if (len>0&&rawline[len-1]=='\r')
455+
rawline[--len]='\0';
456+
}
457+
458+
if (strcmp(rawline,PG_MAJORVERSION)!=0)
459+
{
460+
fprintf(stderr,_("%s: data directory is of wrong version\n"
461+
"File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".\n"),
462+
progname,ver_file,rawline,PG_MAJORVERSION);
463+
exit(1);
464+
}
465+
466+
fclose(ver_fd);
467+
}
468+
469+
402470
/*
403471
* Try to read the existing pg_control file.
404472
*
@@ -468,7 +536,7 @@ ReadControlFile(void)
468536
}
469537

470538
/* Looks like it's a mess. */
471-
fprintf(stderr,_("%s: pg_control exists but is broken orunknown version; ignoring it\n"),
539+
fprintf(stderr,_("%s: pg_control exists but is broken orwrong version; ignoring it\n"),
472540
progname);
473541
return false;
474542
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp