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

Commit740b204

Browse files
michaelpqpull[bot]
authored andcommitted
Fix pg_recvlogical upon signal termination
When pg_recvlogical needs to abort on a signal like SIGINT/SIGTERM, itis expected to exit cleanly as the code documents. However, the codeforgot to clean up the state of the connection before leaving. Thiswould cause the tool to emit messages like "unexpected termination ofreplication stream" error, which is meant for really unexpectedtermination or a crash.The code is refactored to apply the same termination abort operations forsignals, end LSN and keepalive cases, registering a "reason" for thetermination with a message printed under --verbose adapted to the reasonused.This is arguably a bug, but this has been this way since the tool existsand the signal termination can now become slower depending on the changebeing decoded when the signal is received.Reported-by: Andres FreundAuthor: Bharath RupireddyReviewed-by: Andres Freund, Kyotaro Horiguchi, Cary Huang, MichaelPaquierDiscussion:https://postgr.es/m/20221019213953.htdtzikf4f45ywil@awork3.anarazel.de
1 parent6c55fb5 commit740b204

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

‎src/bin/pg_basebackup/pg_recvlogical.c

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
/* Time to sleep between reconnection attempts */
3333
#defineRECONNECT_SLEEP_TIME 5
3434

35+
typedefenum
36+
{
37+
STREAM_STOP_NONE,
38+
STREAM_STOP_END_OF_WAL,
39+
STREAM_STOP_KEEPALIVE,
40+
STREAM_STOP_SIGNAL
41+
}StreamStopReason;
42+
3543
/* Global Options */
3644
staticchar*outfile=NULL;
3745
staticintverbose=0;
@@ -55,6 +63,7 @@ static const char *plugin = "test_decoding";
5563
/* Global State */
5664
staticintoutfd=-1;
5765
staticvolatilesig_atomic_ttime_to_abort= false;
66+
staticvolatilesig_atomic_tstop_reason=STREAM_STOP_NONE;
5867
staticvolatilesig_atomic_toutput_reopen= false;
5968
staticbooloutput_isfile;
6069
staticTimestampTzoutput_last_fsync=-1;
@@ -66,7 +75,8 @@ static void usage(void);
6675
staticvoidStreamLogicalLog(void);
6776
staticboolflushAndSendFeedback(PGconn*conn,TimestampTz*now);
6877
staticvoidprepareToTerminate(PGconn*conn,XLogRecPtrendpos,
69-
boolkeepalive,XLogRecPtrlsn);
78+
StreamStopReasonreason,
79+
XLogRecPtrlsn);
7080

7181
staticvoid
7282
usage(void)
@@ -207,9 +217,11 @@ StreamLogicalLog(void)
207217
TimestampTzlast_status=-1;
208218
inti;
209219
PQExpBufferquery;
220+
XLogRecPtrcur_record_lsn;
210221

211222
output_written_lsn=InvalidXLogRecPtr;
212223
output_fsync_lsn=InvalidXLogRecPtr;
224+
cur_record_lsn=InvalidXLogRecPtr;
213225

214226
/*
215227
* Connect in replication mode to the server
@@ -275,7 +287,8 @@ StreamLogicalLog(void)
275287
intbytes_written;
276288
TimestampTznow;
277289
inthdr_len;
278-
XLogRecPtrcur_record_lsn=InvalidXLogRecPtr;
290+
291+
cur_record_lsn=InvalidXLogRecPtr;
279292

280293
if (copybuf!=NULL)
281294
{
@@ -487,7 +500,7 @@ StreamLogicalLog(void)
487500

488501
if (endposReached)
489502
{
490-
prepareToTerminate(conn,endpos, true,InvalidXLogRecPtr);
503+
stop_reason=STREAM_STOP_KEEPALIVE;
491504
time_to_abort= true;
492505
break;
493506
}
@@ -527,7 +540,7 @@ StreamLogicalLog(void)
527540
*/
528541
if (!flushAndSendFeedback(conn,&now))
529542
gotoerror;
530-
prepareToTerminate(conn,endpos, false,cur_record_lsn);
543+
stop_reason=STREAM_STOP_END_OF_WAL;
531544
time_to_abort= true;
532545
break;
533546
}
@@ -572,12 +585,16 @@ StreamLogicalLog(void)
572585
/* endpos was exactly the record we just processed, we're done */
573586
if (!flushAndSendFeedback(conn,&now))
574587
gotoerror;
575-
prepareToTerminate(conn,endpos, false,cur_record_lsn);
588+
stop_reason=STREAM_STOP_END_OF_WAL;
576589
time_to_abort= true;
577590
break;
578591
}
579592
}
580593

594+
/* Clean up connection state if stream has been aborted */
595+
if (time_to_abort)
596+
prepareToTerminate(conn,endpos,stop_reason,cur_record_lsn);
597+
581598
res=PQgetResult(conn);
582599
if (PQresultStatus(res)==PGRES_COPY_OUT)
583600
{
@@ -656,6 +673,7 @@ StreamLogicalLog(void)
656673
staticvoid
657674
sigexit_handler(SIGNAL_ARGS)
658675
{
676+
stop_reason=STREAM_STOP_SIGNAL;
659677
time_to_abort= true;
660678
}
661679

@@ -1021,18 +1039,31 @@ flushAndSendFeedback(PGconn *conn, TimestampTz *now)
10211039
* retry on failure.
10221040
*/
10231041
staticvoid
1024-
prepareToTerminate(PGconn*conn,XLogRecPtrendpos,boolkeepalive,XLogRecPtrlsn)
1042+
prepareToTerminate(PGconn*conn,XLogRecPtrendpos,StreamStopReasonreason,
1043+
XLogRecPtrlsn)
10251044
{
10261045
(void)PQputCopyEnd(conn,NULL);
10271046
(void)PQflush(conn);
10281047

10291048
if (verbose)
10301049
{
1031-
if (keepalive)
1032-
pg_log_info("end position %X/%X reached by keepalive",
1033-
LSN_FORMAT_ARGS(endpos));
1034-
else
1035-
pg_log_info("end position %X/%X reached by WAL record at %X/%X",
1036-
LSN_FORMAT_ARGS(endpos),LSN_FORMAT_ARGS(lsn));
1050+
switch (reason)
1051+
{
1052+
caseSTREAM_STOP_SIGNAL:
1053+
pg_log_info("received interrupt signal, exiting");
1054+
break;
1055+
caseSTREAM_STOP_KEEPALIVE:
1056+
pg_log_info("end position %X/%X reached by keepalive",
1057+
LSN_FORMAT_ARGS(endpos));
1058+
break;
1059+
caseSTREAM_STOP_END_OF_WAL:
1060+
Assert(!XLogRecPtrIsInvalid(lsn));
1061+
pg_log_info("end position %X/%X reached by WAL record at %X/%X",
1062+
LSN_FORMAT_ARGS(endpos),LSN_FORMAT_ARGS(lsn));
1063+
break;
1064+
caseSTREAM_STOP_NONE:
1065+
Assert(false);
1066+
break;
1067+
}
10371068
}
10381069
}

‎src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,7 @@ Step
26392639
StopList
26402640
StrategyNumber
26412641
StreamCtl
2642+
StreamStopReason
26422643
String
26432644
StringInfo
26442645
StringInfoData

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp