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

Commit14514a4

Browse files
committed
minor improvement: be more paranoid about system ID mismatch when streaming or reading WAL files. Replace RunIdentifySystem() with IdentifySystem() for logging purposes
1 parent69eed7a commit14514a4

File tree

3 files changed

+86
-29
lines changed

3 files changed

+86
-29
lines changed

‎src/backup.c

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static XLogRecPtr wait_wal_lsn(XLogRecPtr lsn, bool is_start_lsn,
103103
boolwait_prev_segment);
104104
staticvoidmake_pagemap_from_ptrack(parray*files,PGconn*backup_conn);
105105
staticvoid*StreamLog(void*arg);
106+
staticvoidIdentifySystem(StreamThreadArg*stream_thread_arg);
106107

107108
staticvoidcheck_external_for_tablespaces(parray*external_list,
108109
PGconn*backup_conn);
@@ -289,30 +290,10 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo)
289290
instance_config.conn_opt.pgport,
290291
instance_config.conn_opt.pgdatabase,
291292
instance_config.conn_opt.pguser);
293+
/* sanity */
294+
IdentifySystem(&stream_thread_arg);
292295

293-
if (!CheckServerVersionForStreaming(stream_thread_arg.conn))
294-
{
295-
PQfinish(stream_thread_arg.conn);
296-
/*
297-
* Error message already written in CheckServerVersionForStreaming().
298-
* There's no hope of recovering from a version mismatch, so don't
299-
* retry.
300-
*/
301-
elog(ERROR,"Cannot continue backup because stream connect has failed.");
302-
}
303-
304-
/*
305-
* Identify server, obtaining start LSN position and current timeline ID
306-
* at the same time, necessary if not valid data can be found in the
307-
* existing output directory.
308-
*/
309-
if (!RunIdentifySystem(stream_thread_arg.conn,NULL,NULL,NULL,NULL))
310-
{
311-
PQfinish(stream_thread_arg.conn);
312-
elog(ERROR,"Cannot continue backup because stream connect has failed.");
313-
}
314-
315-
/* By default there are some error */
296+
/* By default there are some error */
316297
stream_thread_arg.ret=1;
317298
/* we must use startpos as start_lsn from start_backup */
318299
stream_thread_arg.startpos=current.start_lsn;
@@ -522,7 +503,7 @@ do_backup_instance(PGconn *backup_conn, PGNodeInfo *nodeInfo)
522503
charpg_control_path[MAXPGPATH];
523504

524505
snprintf(pg_control_path,sizeof(pg_control_path),"%s/%s",
525-
instance_config.pgdata,"global/pg_control");
506+
instance_config.pgdata,XLOG_CONTROL_FILE);
526507

527508
for (i=0;i<parray_num(backup_files_list);i++)
528509
{
@@ -2529,7 +2510,7 @@ StreamLog(void *arg)
25292510
/*
25302511
* Start the replication
25312512
*/
2532-
elog(LOG,_("started streaming WAL at %X/%X (timeline %u)"),
2513+
elog(LOG,"started streaming WAL at %X/%X (timeline %u)",
25332514
(uint32) (stream_arg->startpos >>32), (uint32)stream_arg->startpos,
25342515
stream_arg->starttli);
25352516

@@ -2570,13 +2551,13 @@ StreamLog(void *arg)
25702551
#endif
25712552
}
25722553
#else
2573-
if(ReceiveXlogStream(stream_arg->conn,stream_arg->startpos,stream_arg->starttli,NULL,
2574-
(char*)stream_arg->basedir,stop_streaming,
2575-
standby_message_timeout,NULL, false, false)== false)
2554+
if(ReceiveXlogStream(stream_arg->conn,stream_arg->startpos,stream_arg->starttli,
2555+
NULL, (char*)stream_arg->basedir,stop_streaming,
2556+
standby_message_timeout,NULL, false, false)== false)
25762557
elog(ERROR,"Problem in receivexlog");
25772558
#endif
25782559

2579-
elog(LOG,_("finished streaming WAL at %X/%X (timeline %u)"),
2560+
elog(LOG,"finished streaming WAL at %X/%X (timeline %u)",
25802561
(uint32) (stop_stream_lsn >>32), (uint32)stop_stream_lsn,stream_arg->starttli);
25812562
stream_arg->ret=0;
25822563

@@ -2744,3 +2725,62 @@ check_external_for_tablespaces(parray *external_list, PGconn *backup_conn)
27442725
}
27452726
}
27462727
}
2728+
2729+
/*
2730+
* Run IDENTIFY_SYSTEM through a given connection and
2731+
* check system identifier and timeline are matching
2732+
*/
2733+
void
2734+
IdentifySystem(StreamThreadArg*stream_thread_arg)
2735+
{
2736+
PGresult*res;
2737+
2738+
uint64stream_conn_sysidentifier=0;
2739+
char*stream_conn_sysidentifier_str;
2740+
TimeLineIDstream_conn_tli=0;
2741+
2742+
if (!CheckServerVersionForStreaming(stream_thread_arg->conn))
2743+
{
2744+
PQfinish(stream_thread_arg->conn);
2745+
/*
2746+
* Error message already written in CheckServerVersionForStreaming().
2747+
* There's no hope of recovering from a version mismatch, so don't
2748+
* retry.
2749+
*/
2750+
elog(ERROR,"Cannot continue backup because stream connect has failed.");
2751+
}
2752+
2753+
/*
2754+
* Identify server, obtain server system identifier and timeline
2755+
*/
2756+
res=pgut_execute(stream_thread_arg->conn,"IDENTIFY_SYSTEM",0,NULL);
2757+
2758+
if (PQresultStatus(res)!=PGRES_TUPLES_OK)
2759+
{
2760+
elog(WARNING,"Could not send replication command \"%s\": %s",
2761+
"IDENTIFY_SYSTEM",PQerrorMessage(stream_thread_arg->conn));
2762+
PQfinish(stream_thread_arg->conn);
2763+
elog(ERROR,"Cannot continue backup because stream connect has failed.");
2764+
}
2765+
2766+
stream_conn_sysidentifier_str=PQgetvalue(res,0,0);
2767+
stream_conn_tli=atoi(PQgetvalue(res,0,1));
2768+
2769+
/* Additional sanity, primary for PG 9.5,
2770+
* where system id can be obtained only via "IDENTIFY SYSTEM"
2771+
*/
2772+
if (!parse_uint64(stream_conn_sysidentifier_str,&stream_conn_sysidentifier,0))
2773+
elog(ERROR,"%s is not system_identifier",stream_conn_sysidentifier_str);
2774+
2775+
if (stream_conn_sysidentifier!=instance_config.system_identifier)
2776+
elog(ERROR,"System identifier mismatch. Connected PostgreSQL instance has system id: "
2777+
""UINT64_FORMAT". Expected: "UINT64_FORMAT".",
2778+
stream_conn_sysidentifier,instance_config.system_identifier);
2779+
2780+
if (stream_conn_tli!=current.tli)
2781+
elog(ERROR,"Timeline identifier mismatch. "
2782+
"Connected PostgreSQL instance has timeline id: %X. Expected: %X.",
2783+
stream_conn_tli,current.tli);
2784+
2785+
PQclear(res);
2786+
}

‎src/parsexlog.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,18 @@ wal_contains_lsn(const char *archivedir, XLogRecPtr target_lsn,
510510
xlogreader=InitXLogPageRead(&reader_data,archivedir,target_tli,
511511
wal_seg_size, false, false, true);
512512

513+
if (xlogreader==NULL)
514+
elog(ERROR,"Out of memory");
515+
516+
xlogreader->system_identifier=instance_config.system_identifier;
517+
513518
res=XLogReadRecord(xlogreader,target_lsn,&errormsg)!=NULL;
514519
/* Didn't find 'target_lsn' and there is no error, return false */
515520

521+
if (errormsg)
522+
elog(WARNING,"Could not read WAL record at %X/%X: %s",
523+
(uint32) (target_lsn >>32), (uint32) (target_lsn),errormsg);
524+
516525
CleanupXLogPageRead(xlogreader);
517526
XLogReaderFree(xlogreader);
518527

@@ -551,6 +560,11 @@ get_last_wal_lsn(const char *archivedir, XLogRecPtr start_lsn,
551560
xlogreader=InitXLogPageRead(&reader_data,archivedir,tli,wal_seg_size,
552561
false, false, true);
553562

563+
if (xlogreader==NULL)
564+
elog(ERROR,"Out of memory");
565+
566+
xlogreader->system_identifier=instance_config.system_identifier;
567+
554568
/*
555569
* Calculate startpoint. Decide: we should use 'start_lsn' or offset 0.
556570
*/

‎src/utils/pgut.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ pgut_get_conninfo_string(PGconn *conn)
206206
returnconnstr;
207207
}
208208

209+
/* TODO: it is better to use PQconnectdbParams like in psql
210+
* It will allow to set application_name for pg_probackup
211+
*/
209212
PGconn*
210213
pgut_connect(constchar*host,constchar*port,
211214
constchar*dbname,constchar*username)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp