Crash by targetted recovery

Started by Kyotaro Horiguchiover 6 years ago18 messageshackers
Jump to latest
#1Kyotaro Horiguchi
horikyota.ntt@gmail.com

Hello.

We found that targetted promotion can cause an assertion failure. The
attached TAP test causes that.

TRAP: FailedAssertion("StandbyMode", File: "xlog.c", Line: 12078)

After recovery target is reached, StartupXLOG turns off standby mode
then refetches the last record. If the last record starts from the
previous WAL segment, the assertion failure is triggered.

The wrong point is that StartupXLOG does random access fetching while
WaitForWALToBecomeAvailable is thinking it is still in streaming. I
think if it is called with random access mode,
WaitForWALToBecomeAvailable should move to XLOG_FROM_ARCHIVE even
though it is thinking that it is still reading from stream.

regards.

-- Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

0001-TAP-test-for-a-crash-bug.patchtext/x-patch; charset=us-asciiDownload+32-1
0002-Fix-a-crash-bug-of-targetted-promotion.patchtext/x-patch; charset=us-asciiDownload+3-3
#2Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#1)
Re: Crash by targetted recovery

On 2020/02/27 12:48, Kyotaro Horiguchi wrote:

Hello.

We found that targetted promotion can cause an assertion failure. The
attached TAP test causes that.

TRAP: FailedAssertion("StandbyMode", File: "xlog.c", Line: 12078)

After recovery target is reached, StartupXLOG turns off standby mode
then refetches the last record. If the last record starts from the
previous WAL segment, the assertion failure is triggered.

Good catch!

The wrong point is that StartupXLOG does random access fetching while
WaitForWALToBecomeAvailable is thinking it is still in streaming. I
think if it is called with random access mode,
WaitForWALToBecomeAvailable should move to XLOG_FROM_ARCHIVE even
though it is thinking that it is still reading from stream.

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#3Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#2)
Re: Crash by targetted recovery

At Thu, 27 Feb 2020 14:40:55 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 12:48, Kyotaro Horiguchi wrote:

Hello.
We found that targetted promotion can cause an assertion failure. The
attached TAP test causes that.

TRAP: FailedAssertion("StandbyMode", File: "xlog.c", Line: 12078)

After recovery target is reached, StartupXLOG turns off standby mode
then refetches the last record. If the last record starts from the
previous WAL segment, the assertion failure is triggered.

Good catch!

The wrong point is that StartupXLOG does random access fetching while
WaitForWALToBecomeAvailable is thinking it is still in streaming. I
think if it is called with random access mode,
WaitForWALToBecomeAvailable should move to XLOG_FROM_ARCHIVE even
though it is thinking that it is still reading from stream.

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

It seems to me the word "streaming" suggests that WAL record should be
read sequentially. Random access, which means reading from arbitrary
location, breaks a stream. (But the patch doesn't try to stop wal
sender if randAccess.)

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

I thought that and it should work, but I hesitated to manipulate on
currentSource in StartupXLOG. currentSource is basically a private
state of WaitForWALToBecomeAvailable. ReadRecord modifies it but I
think it's not good to modify it out of the the logic in
WaitForWALToBecomeAvailable. Come to think of that I got to think the
following part in ReadRecord should use randAccess instead..

xlog.c:4384

/*

-      * Before we retry, reset lastSourceFailed and currentSource
-      * so that we will check the archive next.
+      * Streaming has broken, we retry from the same LSN.

*/
lastSourceFailed = false;

-     currentSource = 0;
+     private->randAccess = true;

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#4Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#3)
Re: Crash by targetted recovery

On 2020/02/27 15:23, Kyotaro Horiguchi wrote:

At Thu, 27 Feb 2020 14:40:55 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 12:48, Kyotaro Horiguchi wrote:

Hello.
We found that targetted promotion can cause an assertion failure. The
attached TAP test causes that.

TRAP: FailedAssertion("StandbyMode", File: "xlog.c", Line: 12078)

After recovery target is reached, StartupXLOG turns off standby mode
then refetches the last record. If the last record starts from the
previous WAL segment, the assertion failure is triggered.

Good catch!

The wrong point is that StartupXLOG does random access fetching while
WaitForWALToBecomeAvailable is thinking it is still in streaming. I
think if it is called with random access mode,
WaitForWALToBecomeAvailable should move to XLOG_FROM_ARCHIVE even
though it is thinking that it is still reading from stream.

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

It seems to me the word "streaming" suggests that WAL record should be
read sequentially. Random access, which means reading from arbitrary
location, breaks a stream. (But the patch doesn't try to stop wal
sender if randAccess.)

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

I thought that and it should work, but I hesitated to manipulate on
currentSource in StartupXLOG. currentSource is basically a private
state of WaitForWALToBecomeAvailable. ReadRecord modifies it but I
think it's not good to modify it out of the the logic in
WaitForWALToBecomeAvailable.

If so, what about adding the following at the top of
WaitForWALToBecomeAvailable()?

if (!StandbyMode && currentSource == XLOG_FROM_STREAM)
currentSource = 0;

Come to think of that I got to think the
following part in ReadRecord should use randAccess instead..

xlog.c:4384

/*

-      * Before we retry, reset lastSourceFailed and currentSource
-      * so that we will check the archive next.
+      * Streaming has broken, we retry from the same LSN.

*/
lastSourceFailed = false;

-     currentSource = 0;
+     private->randAccess = true;

Sorry, I failed to understand why this change is necessary...
At least the comment that you added seems incorrect
because WAL streaming should not have started yet when
we reach the above point.

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#5Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#4)
Re: Crash by targetted recovery

Thank you for the comment.
At Thu, 27 Feb 2020 16:23:44 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 15:23, Kyotaro Horiguchi wrote:

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

It seems to me the word "streaming" suggests that WAL record should be
read sequentially. Random access, which means reading from arbitrary
location, breaks a stream. (But the patch doesn't try to stop wal
sender if randAccess.)

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

I thought that and it should work, but I hesitated to manipulate on
currentSource in StartupXLOG. currentSource is basically a private
state of WaitForWALToBecomeAvailable. ReadRecord modifies it but I
think it's not good to modify it out of the the logic in
WaitForWALToBecomeAvailable.

If so, what about adding the following at the top of
WaitForWALToBecomeAvailable()?

if (!StandbyMode && currentSource == XLOG_FROM_STREAM)
currentSource = 0;

It works virtually the same way. I'm happy to do that if you don't
agree to using randAccess. But I'd rather do that in the 'if
(!InArchiveRecovery)' section.

Come to think of that I got to think the
following part in ReadRecord should use randAccess instead..
xlog.c:4384

/*

-      * Before we retry, reset lastSourceFailed and currentSource
-      * so that we will check the archive next.
+      * Streaming has broken, we retry from the same LSN.

*/
lastSourceFailed = false;

-     currentSource = 0;
+     private->randAccess = true;

Sorry, I failed to understand why this change is necessary...

It's not necessary, just for being tidy about the responsibility on
currentSource.

At least the comment that you added seems incorrect
because WAL streaming should not have started yet when
we reach the above point.

Oops, right.

-      * Streaming has broken, we retry from the same LSN.
+      * Restart recovery from the current LSN.

For clarity, I don't insist on the change at all. If it were
necessary, it's another topic, anyway. Please forget it.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

v2-0001-TAP-test-for-a-crash-bug.patchtext/x-patch; charset=us-asciiDownload+32-1
v2-0002-Fix-a-crash-bug-of-targetted-promotion.patchtext/x-patch; charset=us-asciiDownload+11-3
#6Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#5)
Re: Crash by targetted recovery

On 2020/02/27 17:05, Kyotaro Horiguchi wrote:

Thank you for the comment.
At Thu, 27 Feb 2020 16:23:44 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 15:23, Kyotaro Horiguchi wrote:

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

It seems to me the word "streaming" suggests that WAL record should be
read sequentially. Random access, which means reading from arbitrary
location, breaks a stream. (But the patch doesn't try to stop wal
sender if randAccess.)

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

I thought that and it should work, but I hesitated to manipulate on
currentSource in StartupXLOG. currentSource is basically a private
state of WaitForWALToBecomeAvailable. ReadRecord modifies it but I
think it's not good to modify it out of the the logic in
WaitForWALToBecomeAvailable.

If so, what about adding the following at the top of
WaitForWALToBecomeAvailable()?

if (!StandbyMode && currentSource == XLOG_FROM_STREAM)
currentSource = 0;

It works virtually the same way. I'm happy to do that if you don't
agree to using randAccess. But I'd rather do that in the 'if
(!InArchiveRecovery)' section.

The approach using randAccess seems unsafe. Please imagine
the case where currentSource is changed to XLOG_FROM_ARCHIVE
because randAccess is true, while walreceiver is still running.
For example, this case can occur when the record at REDO
starting point is fetched with randAccess = true after walreceiver
is invoked to fetch the last checkpoint record. The situation
"currentSource != XLOG_FROM_STREAM while walreceiver is
running" seems invalid. No?

So I think that the approach that I proposed is better.

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#7Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#4)
Re: Crash by targetted recovery

At Thu, 27 Feb 2020 20:04:41 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 17:05, Kyotaro Horiguchi wrote:

Thank you for the comment.
At Thu, 27 Feb 2020 16:23:44 +0900, Fujii Masao
<masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 15:23, Kyotaro Horiguchi wrote:

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

It seems to me the word "streaming" suggests that WAL record should be
read sequentially. Random access, which means reading from arbitrary
location, breaks a stream. (But the patch doesn't try to stop wal
sender if randAccess.)

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

I thought that and it should work, but I hesitated to manipulate on
currentSource in StartupXLOG. currentSource is basically a private
state of WaitForWALToBecomeAvailable. ReadRecord modifies it but I
think it's not good to modify it out of the the logic in
WaitForWALToBecomeAvailable.

If so, what about adding the following at the top of
WaitForWALToBecomeAvailable()?

if (!StandbyMode && currentSource == XLOG_FROM_STREAM)
currentSource = 0;

It works virtually the same way. I'm happy to do that if you don't
agree to using randAccess. But I'd rather do that in the 'if
(!InArchiveRecovery)' section.

The approach using randAccess seems unsafe. Please imagine
the case where currentSource is changed to XLOG_FROM_ARCHIVE
because randAccess is true, while walreceiver is still running.
For example, this case can occur when the record at REDO
starting point is fetched with randAccess = true after walreceiver
is invoked to fetch the last checkpoint record. The situation
"currentSource != XLOG_FROM_STREAM while walreceiver is
running" seems invalid. No?

When I mentioned an possibility of changing ReadRecord so that it
modifies randAccess instead of currentSource, I thought that
WaitForWALToBecomeAvailable should shutdown wal receiver as
needed.

At Thu, 27 Feb 2020 15:23:07 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in
me> location, breaks a stream. (But the patch doesn't try to stop wal
me> sender if randAccess.)

And random access during StandbyMode ususally (always?) lets RecPtr go
back. I'm not sure WaitForWALToBecomeAvailable works correctly if we
don't have a file in pg_wal and the REDO point is far back by more
than a segment from the initial checkpoint record. (It seems to cause
assertion failure, but I haven't checked that.)

If we go back to XLOG_FROM_ARCHIVE by random access, it correctly
re-connects to the primary for the past segment.

So I think that the approach that I proposed is better.

It depends on how far we assume RecPtr go back.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#8Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#7)
Re: Crash by targetted recovery

On 2020/02/28 12:13, Kyotaro Horiguchi wrote:

At Thu, 27 Feb 2020 20:04:41 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 17:05, Kyotaro Horiguchi wrote:

Thank you for the comment.
At Thu, 27 Feb 2020 16:23:44 +0900, Fujii Masao
<masao.fujii@oss.nttdata.com> wrote in

On 2020/02/27 15:23, Kyotaro Horiguchi wrote:

I failed to understand why random access while reading from
stream is bad idea. Could you elaborate why?

It seems to me the word "streaming" suggests that WAL record should be
read sequentially. Random access, which means reading from arbitrary
location, breaks a stream. (But the patch doesn't try to stop wal
sender if randAccess.)

Isn't it sufficient to set currentSource to 0 when disabling
StandbyMode?

I thought that and it should work, but I hesitated to manipulate on
currentSource in StartupXLOG. currentSource is basically a private
state of WaitForWALToBecomeAvailable. ReadRecord modifies it but I
think it's not good to modify it out of the the logic in
WaitForWALToBecomeAvailable.

If so, what about adding the following at the top of
WaitForWALToBecomeAvailable()?

if (!StandbyMode && currentSource == XLOG_FROM_STREAM)
currentSource = 0;

It works virtually the same way. I'm happy to do that if you don't
agree to using randAccess. But I'd rather do that in the 'if
(!InArchiveRecovery)' section.

The approach using randAccess seems unsafe. Please imagine
the case where currentSource is changed to XLOG_FROM_ARCHIVE
because randAccess is true, while walreceiver is still running.
For example, this case can occur when the record at REDO
starting point is fetched with randAccess = true after walreceiver
is invoked to fetch the last checkpoint record. The situation
"currentSource != XLOG_FROM_STREAM while walreceiver is
running" seems invalid. No?

When I mentioned an possibility of changing ReadRecord so that it
modifies randAccess instead of currentSource, I thought that
WaitForWALToBecomeAvailable should shutdown wal receiver as
needed.

At Thu, 27 Feb 2020 15:23:07 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in
me> location, breaks a stream. (But the patch doesn't try to stop wal
me> sender if randAccess.)

Sorry, I failed to notice that.

And random access during StandbyMode ususally (always?) lets RecPtr go
back. I'm not sure WaitForWALToBecomeAvailable works correctly if we
don't have a file in pg_wal and the REDO point is far back by more
than a segment from the initial checkpoint record.

It works correctly. This is why WaitForWALToBecomeAvailable() uses
fetching_ckpt argument.

If we go back to XLOG_FROM_ARCHIVE by random access, it correctly
re-connects to the primary for the past segment.

But this can lead to unnecessary restart of walreceiver. Since
fetching_ckpt ensures that the WAL file containing the REDO
starting record exists in pg_wal, there is no need to reconnect
to the primary when reading the REDO starting record.

Is there other case where we need to go back to XLOG_FROM_ARCHIVE
by random access?

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#9Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#8)
Re: Crash by targetted recovery

At Mon, 2 Mar 2020 20:54:04 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

And random access during StandbyMode ususally (always?) lets RecPtr go
back. I'm not sure WaitForWALToBecomeAvailable works correctly if we
don't have a file in pg_wal and the REDO point is far back by more
than a segment from the initial checkpoint record.

It works correctly. This is why WaitForWALToBecomeAvailable() uses
fetching_ckpt argument.

Hmm. You're right. We start streaming from RedoStartLSN when
fetching_ckpt. So that doesn't happen.

If we go back to XLOG_FROM_ARCHIVE by random access, it correctly
re-connects to the primary for the past segment.

But this can lead to unnecessary restart of walreceiver. Since
fetching_ckpt ensures that the WAL file containing the REDO
starting record exists in pg_wal, there is no need to reconnect
to the primary when reading the REDO starting record.

Is there other case where we need to go back to XLOG_FROM_ARCHIVE
by random access?

I understand that the reconnection for REDO record is useless. Ok I
take the !StandbyMode way.

The attached is the test script that is changed to count the added
test, and the slight revised main patch.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

v3-0001-TAP-test-for-a-crash-bug.patchtext/x-patch; charset=us-asciiDownload+33-2
v3-0002-Fix-a-crash-bug-of-targetted-promotion.patchtext/x-patch; charset=us-asciiDownload+9-3
#10Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#9)
Re: Crash by targetted recovery

On 2020/03/05 12:08, Kyotaro Horiguchi wrote:

At Mon, 2 Mar 2020 20:54:04 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

And random access during StandbyMode ususally (always?) lets RecPtr go
back. I'm not sure WaitForWALToBecomeAvailable works correctly if we
don't have a file in pg_wal and the REDO point is far back by more
than a segment from the initial checkpoint record.

It works correctly. This is why WaitForWALToBecomeAvailable() uses
fetching_ckpt argument.

Hmm. You're right. We start streaming from RedoStartLSN when
fetching_ckpt. So that doesn't happen.

If we go back to XLOG_FROM_ARCHIVE by random access, it correctly
re-connects to the primary for the past segment.

But this can lead to unnecessary restart of walreceiver. Since
fetching_ckpt ensures that the WAL file containing the REDO
starting record exists in pg_wal, there is no need to reconnect
to the primary when reading the REDO starting record.

Is there other case where we need to go back to XLOG_FROM_ARCHIVE
by random access?

I understand that the reconnection for REDO record is useless. Ok I
take the !StandbyMode way.

The attached is the test script that is changed to count the added
test, and the slight revised main patch.

Thanks for the patch!

+		/* Wal receiver should not active when entring XLOG_FROM_ARCHIVE */
+		Assert(!WalRcvStreaming());

+1 to add this assertion check.

Isn't it better to always check this while trying to read WAL from
archive or pg_wal? So, what about the following change?

                 {
                         case XLOG_FROM_ARCHIVE:
                         case XLOG_FROM_PG_WAL:
+                               /*
+                                * WAL receiver should not be running while trying to
+                                * read WAL from archive or pg_wal.
+                                */
+                               Assert(!WalRcvStreaming());
+
                                 /* Close any old file we might have open. */
                                 if (readFile >= 0)

+ lastSourceFailed = false; /* We haven't failed on the new source */

Is this really necessary? Since ReadRecord() always reset
lastSourceFailed to false, it seems not necessary.

-	else if (currentSource == 0)
+	else if (currentSource == 0 ||

Though this is a *separate topic*, 0 should be XLOG_FROM_ANY?
There are some places where 0 is used as the value of currentSource.
IMO they should be updated so that XLOG_FROM_ANY is used instead of 0.

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#11Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#10)
Re: Crash by targetted recovery

At Thu, 5 Mar 2020 19:51:11 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/03/05 12:08, Kyotaro Horiguchi wrote:

I understand that the reconnection for REDO record is useless. Ok I
take the !StandbyMode way.
The attached is the test script that is changed to count the added
test, and the slight revised main patch.

Thanks for the patch!

+ /* Wal receiver should not active when entring XLOG_FROM_ARCHIVE */
+		Assert(!WalRcvStreaming());

+1 to add this assertion check.

Isn't it better to always check this while trying to read WAL from
archive or pg_wal? So, what about the following change?

{
case XLOG_FROM_ARCHIVE:
case XLOG_FROM_PG_WAL:
+                               /*
+ * WAL receiver should not be running while trying to
+                                * read WAL from archive or pg_wal.
+                                */
+                               Assert(!WalRcvStreaming());
+
/* Close any old file we might have open. */
if (readFile >= 0)

(It seems retroverting to the first patch when I started this...)
The second place covers wider cases so I reverted the first place.

+ lastSourceFailed = false; /* We haven't failed on the new source */

Is this really necessary? Since ReadRecord() always reset
lastSourceFailed to false, it seems not necessary.

It's just to make sure. Actually lastSourceFailed is always false
when we get there. But when the source is switched, lastSourceFailed
should be changed to false as a matter of design. I'd like to do that
unless that harms.

-	else if (currentSource == 0)
+	else if (currentSource == 0 ||

Though this is a *separate topic*, 0 should be XLOG_FROM_ANY?
There are some places where 0 is used as the value of currentSource.
IMO they should be updated so that XLOG_FROM_ANY is used instead of 0.

Yeah, I've thought that many times but have neglected since it is not
critical and trivial as a separate patch. I'd take the chance to do
that now. Another minor glitch is "int oldSource = currentSource;" it
is not debugger-friendly so I changed it to XLogSource. It is added
as a new patch file before the main patch.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

v4-0001-TAP-test-for-a-crash-bug.patchtext/x-patch; charset=us-asciiDownload+33-2
v4-0002-Tidy-up-XLogSource-usage.patchtext/x-patch; charset=us-asciiDownload+3-4
v4-0003-Fix-a-crash-bug-of-targetted-promotion.patchtext/x-patch; charset=us-asciiDownload+10-3
#12Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#11)
Re: Crash by targetted recovery

On 2020/03/06 10:29, Kyotaro Horiguchi wrote:

At Thu, 5 Mar 2020 19:51:11 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/03/05 12:08, Kyotaro Horiguchi wrote:

I understand that the reconnection for REDO record is useless. Ok I
take the !StandbyMode way.
The attached is the test script that is changed to count the added
test, and the slight revised main patch.

Thanks for the patch!

+ /* Wal receiver should not active when entring XLOG_FROM_ARCHIVE */
+		Assert(!WalRcvStreaming());

+1 to add this assertion check.

Isn't it better to always check this while trying to read WAL from
archive or pg_wal? So, what about the following change?

{
case XLOG_FROM_ARCHIVE:
case XLOG_FROM_PG_WAL:
+                               /*
+ * WAL receiver should not be running while trying to
+                                * read WAL from archive or pg_wal.
+                                */
+                               Assert(!WalRcvStreaming());
+
/* Close any old file we might have open. */
if (readFile >= 0)

(It seems retroverting to the first patch when I started this...)
The second place covers wider cases so I reverted the first place.

Thanks for updating the patch that way.
Not sure which patch you're mentioning, though.

Regarding 0003 patch, I added a bit more detail comments into
the patch so that we can understand the code more easily.
Updated version of 0003 patch attached. Barring any objection,
at first, I plan to commit this patch.

+ lastSourceFailed = false; /* We haven't failed on the new source */

Is this really necessary? Since ReadRecord() always reset
lastSourceFailed to false, it seems not necessary.

It's just to make sure. Actually lastSourceFailed is always false
when we get there. But when the source is switched, lastSourceFailed
should be changed to false as a matter of design. I'd like to do that
unless that harms.

OK.

-	else if (currentSource == 0)
+	else if (currentSource == 0 ||

Though this is a *separate topic*, 0 should be XLOG_FROM_ANY?
There are some places where 0 is used as the value of currentSource.
IMO they should be updated so that XLOG_FROM_ANY is used instead of 0.

Yeah, I've thought that many times but have neglected since it is not
critical and trivial as a separate patch. I'd take the chance to do
that now. Another minor glitch is "int oldSource = currentSource;" it
is not debugger-friendly so I changed it to XLogSource. It is added
as a new patch file before the main patch.

There seems to be more other places where XLogSource and
XLOG_FROM_XXX are not used yet. For example, the initial values
of readSource and XLogReceiptSource, the type of argument
"source" in XLogFileReadAnyTLI() and XLogFileRead(), etc.
These also should be updated?

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

Attachments:

v4-0003-Fix-a-crash-bug-of-targetted-promotion_fujii.patchtext/plain; charset=UTF-8; name=v4-0003-Fix-a-crash-bug-of-targetted-promotion_fujii.patch; x-mac-creator=0; x-mac-type=0Download+22-1
#13Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#12)
Re: Crash by targetted recovery

At Sat, 7 Mar 2020 01:46:16 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

(It seems retroverting to the first patch when I started this...)
The second place covers wider cases so I reverted the first place.

Thanks for updating the patch that way.
Not sure which patch you're mentioning, though.

That meant 0003.

Regarding 0003 patch, I added a bit more detail comments into
the patch so that we can understand the code more easily.
Updated version of 0003 patch attached. Barring any objection,
at first, I plan to commit this patch.

Looks good to me. Thanks for writing the detailed comments.

+ lastSourceFailed = false; /* We haven't failed on the new source */

Is this really necessary? Since ReadRecord() always reset
lastSourceFailed to false, it seems not necessary.

It's just to make sure. Actually lastSourceFailed is always false
when we get there. But when the source is switched, lastSourceFailed
should be changed to false as a matter of design. I'd like to do that
unless that harms.

OK.

Thanks.

-	else if (currentSource == 0)
+	else if (currentSource == 0 ||

Though this is a *separate topic*, 0 should be XLOG_FROM_ANY?
There are some places where 0 is used as the value of currentSource.
IMO they should be updated so that XLOG_FROM_ANY is used instead of 0.

Yeah, I've thought that many times but have neglected since it is not
critical and trivial as a separate patch. I'd take the chance to do
that now. Another minor glitch is "int oldSource = currentSource;" it
is not debugger-friendly so I changed it to XLogSource. It is added
as a new patch file before the main patch.

There seems to be more other places where XLogSource and
XLOG_FROM_XXX are not used yet. For example, the initial values
of readSource and XLogReceiptSource, the type of argument
"source" in XLogFileReadAnyTLI() and XLogFileRead(), etc.
These also should be updated?

Right. I checked through the file and AFAICS that's all. The attachec
v5-0001-Tidy...patch is the fix on top of the v4-0003 on the current
master.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

v5-0001-Tidy-up-XLogSource-usage.patchtext/x-patch; charset=us-asciiDownload+13-14
#14Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Kyotaro Horiguchi (#13)
Re: Crash by targetted recovery

On 2020/03/09 13:49, Kyotaro Horiguchi wrote:

At Sat, 7 Mar 2020 01:46:16 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

(It seems retroverting to the first patch when I started this...)
The second place covers wider cases so I reverted the first place.

Thanks for updating the patch that way.
Not sure which patch you're mentioning, though.

That meant 0003.

Regarding 0003 patch, I added a bit more detail comments into
the patch so that we can understand the code more easily.
Updated version of 0003 patch attached. Barring any objection,
at first, I plan to commit this patch.

Looks good to me. Thanks for writing the detailed comments.

Thanks for the review! Pushed.

I will review other two patches later.

There seems to be more other places where XLogSource and
XLOG_FROM_XXX are not used yet. For example, the initial values
of readSource and XLogReceiptSource, the type of argument
"source" in XLogFileReadAnyTLI() and XLogFileRead(), etc.
These also should be updated?

Right. I checked through the file and AFAICS that's all. The attachec
v5-0001-Tidy...patch is the fix on top of the v4-0003 on the current
master.

Thanks for the patch!

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#15Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#14)
Re: Crash by targetted recovery

At Mon, 9 Mar 2020 15:46:49 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

On 2020/03/09 13:49, Kyotaro Horiguchi wrote:

At Sat, 7 Mar 2020 01:46:16 +0900, Fujii Masao

Regarding 0003 patch, I added a bit more detail comments into
the patch so that we can understand the code more easily.
Updated version of 0003 patch attached. Barring any objection,
at first, I plan to commit this patch.

Looks good to me. Thanks for writing the detailed comments.

Thanks for the review! Pushed.

Thanks for commiting.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#16Fujii Masao
masao.fujii@oss.nttdata.com
In reply to: Fujii Masao (#14)
Re: Crash by targetted recovery

On 2020/03/09 15:46, Fujii Masao wrote:

On 2020/03/09 13:49, Kyotaro Horiguchi wrote:

At Sat, 7 Mar 2020 01:46:16 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

(It seems retroverting to the first patch when I started this...)
The second place covers wider cases so I reverted the first place.

Thanks for updating the patch that way.
Not sure which patch you're mentioning, though.

That meant 0003.

Regarding 0003 patch, I added a bit more detail comments into
the patch so that we can understand the code more easily.
Updated version of 0003 patch attached. Barring any objection,
at first, I plan to commit this patch.

Looks good to me. Thanks for writing the detailed comments.

Thanks for the review! Pushed.

I will review other two patches later.

Pushed the v5-0001-Tidy-up-XLogSource-usage.patch!

Regarding the remaining patch adding the regression test,

+$result =
+  $node_standby->safe_psql('postgres', "SELECT pg_last_wal_replay_lsn()");
+my ($seg, $off) = split('/', $result);
+my $target = sprintf("$seg/%08X", (hex($off) / $segsize + 1) * $segsize);

What happens if "off" part gets the upper limit and "seg" part needs
to be incremented? What happens if pg_last_wal_replay_lsn() advances
very much (e.g., because of autovacuum) beyond the segment boundary
until the standby restarts? Of course, these situations very rarely happen,
but I'd like to avoid adding such not stable test if possible.

Regards,

--
Fujii Masao
NTT DATA CORPORATION
Advanced Platform Technology Group
Research and Development Headquarters

#17Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Fujii Masao (#16)
Re: Crash by targetted recovery

At Tue, 10 Mar 2020 10:50:52 +0900, Fujii Masao <masao.fujii@oss.nttdata.com> wrote in

Pushed the v5-0001-Tidy-up-XLogSource-usage.patch!

Thanks!

Regarding the remaining patch adding the regression test,

I didn't seriously inteneded it to be in the tree.

+$result =
+ $node_standby->safe_psql('postgres', "SELECT
pg_last_wal_replay_lsn()");
+my ($seg, $off) = split('/', $result);
+my $target = sprintf("$seg/%08X", (hex($off) / $segsize + 1) *
$segsize);

What happens if "off" part gets the upper limit and "seg" part needs
to be incremented? What happens if pg_last_wal_replay_lsn() advances
very much (e.g., because of autovacuum) beyond the segment boundary
until the standby restarts? Of course, these situations very rarely
happen,
but I'd like to avoid adding such not stable test if possible.

In the first place the "seg" is "fileno". Honestly I don't think the
test doesn't reach to fileno boundary but I did in the attached. Since
perl complains over-32bit integer arithmetic as incomptible, the
calculation gets a bit odd shape to avoid over-32bit arithmetic.

For the second point, which seems more likely to happen, I added the
VACUUM/pg_switch_wal() sequence then wait standby for catch up, before
doing the test.

Does it make sense?

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

t41989_17
v5-0001-TAP-test-for-a-crash-bug.patchtext/x-patch; charset=us-asciiDownload+49-2
#18Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Kyotaro Horiguchi (#17)
Re: Crash by targetted recovery

(Hmm. My sight must be as short as 2 word length..)

At Tue, 10 Mar 2020 14:59:00 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in

In the first place the "seg" is "fileno". Honestly I don't think the
test doesn't reach to fileno boundary but I did in the attached. Since

Of course it is a mistake of "Honestly I don't think the test reaches
to fileno boudary".

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center