Return pg_control from pg_backup_stop().
Return pg_control from pg_backup_stop().
Hackers, This is greatly simplified implementation of the patch proposed in [1] and hopefully it addresses the concerns expressed there. Since the implementation is quite different it seemed like a new thread was appropriate, especially since the old thread title would be very misleading regarding the new functionality. The basic idea is to harden recovery by returning a copy of pg_control from pg_backup_stop() that has a flag set to prevent recovery if the backup_label file is missing. Instead of backup software copying pg_control from PGDATA, it stores an updated version that is returned from pg_backup_stop(). This is better for the following reasons: * The user can no longer remove backup_label and get what looks like a successful recovery (while almost certainly causing corruption). If backup_label is removed the cluster will not start. The user may try pg_resetwal, but that tool makes it pretty clear that corruption will result from its use. * We don't need to worry about backup software seeing a torn copy of pg_control, since Postgres can safely read it out of memory and provide a valid copy via pg_backup_stop(). This solves torn reads without needing to write pg_control via a temp file, which may affect performance on a standby. * For backup from standby, we no longer need to instruct the backup software to copy pg_control last. In fact the backup software should not copy pg_control from PGDATA at all. These changes have no impact on current backup software and they are free to use the pg_control available from pg_stop_backup() or continue to use pg_control from PGDATA. Of course they will miss the benefits of getting a consistent copy of pg_control and the backup_label checking, but will be no worse off than before. I'll register this in the July CF. Regards, -David [1] https://www.postgresql.org/message-id/2daf8adc-8db7-4204-a7f2-a7e94e2bfa4b@pgmasters.net
Re: Return pg_control from pg_backup_stop().
On 1/24/25 13:43, David Steele wrote: > > Rebased and improved a comment and an error. Rebased to fix breakage caused by the split of func.sgml in 4e23c9e. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 8/7/25 05:30, David Steele wrote: > On 1/24/25 13:43, David Steele wrote: >> >> Rebased and improved a comment and an error. > Rebased to fix breakage caused by the split of func.sgml in 4e23c9e. Rebased to implement simplification added by "Simplify creation of built-in functions with default arguments" (759b03b2). Regards, -David
Re: Return pg_control from pg_backup_stop().
On 2/20/26 10:10, David Steele wrote: > On 8/7/25 05:30, David Steele wrote: >> On 1/24/25 13:43, David Steele wrote: >>> >>> Rebased and improved a comment and an error. >> Rebased to fix breakage caused by the split of func.sgml in 4e23c9e. > > > Rebased to implement simplification added by "Simplify creation of > built-in functions with default arguments" (759b03b2). With the patches this time! Regards, -David
Re: Return pg_control from pg_backup_stop().
On 2/20/26 12:47, David Steele wrote: > On 2/20/26 10:10, David Steele wrote: >> On 8/7/25 05:30, David Steele wrote: >>> On 1/24/25 13:43, David Steele wrote: >>>> >>>> Rebased and improved a comment and an error. >>> Rebased to fix breakage caused by the split of func.sgml in 4e23c9e. >> >> >> Rebased to implement simplification added by "Simplify creation of >> built-in functions with default arguments" (759b03b2). Rebased on "Simplify creation of built-in functions with non-default ACLs." (f95d73ed). Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
Hi David
I have not read the code yet, so this may already be answered there, but I had a question about the proposal itself. This patch protects against a missing backup_label, but what about a wrong one? If a user restores a backup_label file from a different backup, the existence check alone would not detect that. Do we need some consistency check between the returned pg_control copy and the backup_label contents, or is the intended scope here limited to the “missing file” case only?
Regards
Haibo
On Mar 5, 2026, at 5:27 PM, David Steele <david@pgbackrest.org> wrote:On 2/20/26 12:47, David Steele wrote:On 2/20/26 10:10, David Steele wrote:On 8/7/25 05:30, David Steele wrote:On 1/24/25 13:43, David Steele wrote:Rebased to fix breakage caused by the split of func.sgml in 4e23c9e.
Rebased and improved a comment and an error.
Rebased to implement simplification added by "Simplify creation of built-in functions with default arguments" (759b03b2).
Rebased on "Simplify creation of built-in functions with non-default ACLs." (f95d73ed).
Regards,
-David<pgcontrol-flag-v8-01-basebackup.patch><pgcontrol-flag-v8-02-sql.patch>
Re: Return pg_control from pg_backup_stop().
On Mon, Mar 16, 2026 at 10:16:58PM -0700, Haibo Yan wrote: > I have not read the code yet, so this may already be answered there, > but I had a question about the proposal itself. This patch protects > against a missing backup_label, but what about a wrong one? If a > user restores a backup_label file from a different backup, the > existence check alone would not detect that. Do we need some > consistency check between the returned pg_control copy and the > backup_label contents, or is the intended scope here limited to the > “missing file” case only? Please note that we use bottom-posting on the lists, as of: https://en.wikipedia.org/wiki/Posting_style#Bottom-posting -- Michael
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 3/17/26 12:16, Haibo Yan wrote: > I have not read the code yet, so this may already be answered there, but > I had a question about the proposal itself. This patch protects against > a missing backup_label, but what about a wrong one? If a user restores a > backup_label file from a different backup, the existence check alone > would not detect that. Do we need some consistency check between the > returned pg_control copy and the backup_label contents, or is the > intended scope here limited to the “missing file” case only? Thank you for having a look! The goal here is only to check for a missing backup_label. The general problem is that PostgreSQL suggests that removing backup_label might be a good idea so the user does it: If you are not restoring from a backup, try removing the file \"%s/backup_label\" The user *could* copy a backup_label from another backup and there are ways we could detect that but I feel that should be material for a separate patch. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
Hi David,
Thank you for the clarification. I have now read the code, and overall it looks good to me. I only had one very small comment.
You currently have:
```
memset(controlFile + sizeof(ControlFileData), 0,
PG_CONTROL_FILE_SIZE - sizeof(ControlFileData));
memcpy(controlFile, ControlFile, sizeof(ControlFileData));
```
This is correct, since only the trailing bytes need to be zeroed before the copy.
I was just wondering whether the following might be slightly clearer:
```
memset(controlFile, 0, PG_CONTROL_FILE_SIZE);
memcpy(controlFile, ControlFile, sizeof(ControlFileData));
```
I do not think this is a real issue, though.
Thanks
Haibo
On Mar 17, 2026, at 12:05 AM, David Steele <david@pgbackrest.org> wrote:On 3/17/26 12:16, Haibo Yan wrote:I have not read the code yet, so this may already be answered there, but I had a question about the proposal itself. This patch protects against a missing backup_label, but what about a wrong one? If a user restores a backup_label file from a different backup, the existence check alone would not detect that. Do we need some consistency check between the returned pg_control copy and the backup_label contents, or is the intended scope here limited to the “missing file” case only?
Thank you for having a look!
The goal here is only to check for a missing backup_label. The general problem is that PostgreSQL suggests that removing backup_label might be a good idea so the user does it:
If you are not restoring from a backup, try removing the file \"%s/backup_label\"
The user *could* copy a backup_label from another backup and there are ways we could detect that but I feel that should be material for a separate patch.
Regards,
-David
Re: Return pg_control from pg_backup_stop().
On Tue, Mar 17, 2026 at 11:50:29AM -0700, Haibo Yan wrote:
> Thank you for the clarification. I have now read the code, and
> overall it looks good to me. I only had one very small comment.
(Bottom-posting note from above, please be careful.)
> I was just wondering whether the following might be slightly clearer:
> ```
> memset(controlFile, 0, PG_CONTROL_FILE_SIZE);
> memcpy(controlFile, ControlFile, sizeof(ControlFileData));
> ```
>
> I do not think this is a real issue, though.
{
ControlFile->backupStartPoint = checkPoint.redo;
ControlFile->backupEndRequired = backupEndRequired;
+ ControlFile->backupLabelRequired = false;
It sounds like it is going to be important to document the reason why
the flag is reset here (aka we don't need the backup_label file
anymore).
+backup_control_file(uint8_t *controlFile)
+{
+ ControlFileData *controlData = ((ControlFileData *)controlFile);
+
+ memset(controlFile + sizeof(ControlFileData), 0,
+ PG_CONTROL_FILE_SIZE - sizeof(ControlFileData));
+
+ LWLockAcquire(ControlFileLock, LW_SHARED);
+ memcpy(controlFile, ControlFile, sizeof(ControlFileData));
+ LWLockRelease(ControlFileLock);
+
+ controlData->backupLabelRequired = true;
+
+ INIT_CRC32C(controlData->crc);
+ COMP_CRC32C(controlData->crc, controlFile, offsetof(ControlFileData, crc));
+ FIN_CRC32C(controlData->crc);
I was wondering if we should have an assertion at least to cross-check
that the contents we store in shared memory never go out-of-sync with
the on-disk contents, in the shape of a USE_ASSERT_CHECKING block that
calls get_controlfile() and memcmp()'s the contents between shmem and
the on-disk file, while the LWLock is taken. We ship the control file
last on purpose, one reason being backups taken from standbys, so that
may be sensible to do.
Another property of the new control file flag that is implied in the
implementation but not documented is that we should never check for
backupLabelRequired when a backup_label is gone. Actually, the flag
is reset in InitWalRecovery() in the initial steps of recovery, and
the backup_label file is removed much later in StartupXLOG() just
*after* UpdateControlFile() to minimize the window where the contents
of the control file and the backup_label file is removed are
out-of-sync. This window means that if we crash between the
completion of UpdateControlFile() and the durable_rename() we could
have a flag reset with a backup_label still around. On restart,
recovery would fail, requiring a manual modification of the control
file, at least. It sounds to me that this implementation detail
should be documented clearly.
Finally, here is a general opinion. I like this patch, and it is
basically risk-free for base backups taken with the replication
protocol as we update the control file with the new flag set
on-the-fly.
Now, I am worried about backups that use pg_stop_backup(). Changing
backup APIs has always been a very sensitive area, and this is going
to require operators to update backup tools so as the control file
received as a result of pg_stop_backup() is copied, at the cost of
getting a failure if they don't do so. I will *not* proceed with this
change without a clear approval from some more committers or senior
hackers that they like this change (approach previously suggested by
Andres, actually, for what I can see). I am adding in CC a few
committers who have commented on this set of proposals and who have
touched the recovery code in the last few years, for awareness.
The timing is what it is, and we are at the end of a release cycle.
Let's see if we can reach a consensus of some kind.
--
Michael
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 3/18/26 08:43, Michael Paquier wrote:
> On Tue, Mar 17, 2026 at 11:50:29AM -0700, Haibo Yan wrote:
>> Thank you for the clarification. I have now read the code, and
>> overall it looks good to me. I only had one very small comment.
>
> (Bottom-posting note from above, please be careful.)
>
>> I was just wondering whether the following might be slightly clearer:
>> ```
>> memset(controlFile, 0, PG_CONTROL_FILE_SIZE);
>> memcpy(controlFile, ControlFile, sizeof(ControlFileData));
>> ```
Yeah, perhaps I am being too clever here. The reason why I like this
pattern:
memset(controlFile + sizeof(ControlFileData), 0,
PG_CONTROL_FILE_SIZE - sizeof(ControlFileData));
...is that valgrind will complain if the ControlFileData part is not
completely initialized later on.
But your version is what is generally used in the code so I'm fine with
doing it that way.
> {
> ControlFile->backupStartPoint = checkPoint.redo;
> ControlFile->backupEndRequired = backupEndRequired;
> + ControlFile->backupLabelRequired = false;
>
> It sounds like it is going to be important to document the reason why
> the flag is reset here (aka we don't need the backup_label file
> anymore).
Good point -- I'll add that in the next revision.
> +backup_control_file(uint8_t *controlFile)
> +{
> + ControlFileData *controlData = ((ControlFileData *)controlFile);
> +
> + memset(controlFile + sizeof(ControlFileData), 0,
> + PG_CONTROL_FILE_SIZE - sizeof(ControlFileData));
> +
> + LWLockAcquire(ControlFileLock, LW_SHARED);
> + memcpy(controlFile, ControlFile, sizeof(ControlFileData));
> + LWLockRelease(ControlFileLock);
> +
> + controlData->backupLabelRequired = true;
> +
> + INIT_CRC32C(controlData->crc);
> + COMP_CRC32C(controlData->crc, controlFile, offsetof(ControlFileData, crc));
> + FIN_CRC32C(controlData->crc);
>
> I was wondering if we should have an assertion at least to cross-check
> that the contents we store in shared memory never go out-of-sync with
> the on-disk contents, in the shape of a USE_ASSERT_CHECKING block that
> calls get_controlfile() and memcmp()'s the contents between shmem and
> the on-disk file, while the LWLock is taken. We ship the control file
> last on purpose, one reason being backups taken from standbys, so that
> may be sensible to do.
As far as I can see this should always be true -- I audited all the
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE)
sections and the file is always saved once if is updated. Let me see if
I can add this check without too much pain, e.g. an additional parameter.
> Another property of the new control file flag that is implied in the
> implementation but not documented is that we should never check for
> backupLabelRequired when a backup_label is gone.
I'm not sure what you mean here? That's exactly when we do want to check
as below:
/*
* No backup_label file has been found if we are here. Error if the
* control file requires backup_label.
*/
if (ControlFile->backupLabelRequired)
ereport(FATAL,
(errmsg("could not find backup_label required for recovery"),
errhint("backup_label must be present for recovery to
proceed")));
> Actually, the flag> is reset in InitWalRecovery() in the initial
steps of recovery, and
> the backup_label file is removed much later in StartupXLOG() just
> *after* UpdateControlFile() to minimize the window where the contents
> of the control file and the backup_label file is removed are
> out-of-sync. This window means that if we crash between the
> completion of UpdateControlFile() and the durable_rename() we could
> have a flag reset with a backup_label still around. On restart,
> recovery would fail, requiring a manual modification of the control
> file, at least. It sounds to me that this implementation detail
> should be documented clearly.
I'll test it but I don't think this is the case. If backup_label is
present then we'll just update pg_control again as we do now.
When backup_label is present the value of backupLabelRequired does not
matter so it just follows the prior logic.
> Finally, here is a general opinion. I like this patch, and it is
> basically risk-free for base backups taken with the replication
> protocol as we update the control file with the new flag set
> on-the-fly.
Glad to hear it!
> Now, I am worried about backups that use pg_stop_backup(). Changing
> backup APIs has always been a very sensitive area, and this is going
> to require operators to update backup tools so as the control file
> received as a result of pg_stop_backup() is copied, at the cost of
> getting a failure if they don't do so.
Using the pg_control copy from pg_backup_stop() is entirely optional and
nothing is broken if vendors decide not to use it. This can be
demonstrated by applying the 01 patch without 02. In that case the tests
in 042_low_level_backup continue to run. You can also apply 01 and 02
and revert the test changes in 042_low_level_backup and that works, too.
Vendors can use the new feature if they want the protection of
backupLabelRequired and a guaranteed non-torn copy of pg_control but if
they do nothing then nothing breaks.
> I will *not* proceed with this
> change without a clear approval from some more committers or senior
> hackers that they like this change (approach previously suggested by
> Andres, actually, for what I can see).
You are correct and this was an omission on my part. If this gets to
commit we'll definitely want to mention that this flag was Andres'
suggestion.
> I am adding in CC a few
> committers who have commented on this set of proposals and who have
> touched the recovery code in the last few years, for awareness.
> The timing is what it is, and we are at the end of a release cycle.
> Let's see if we can reach a consensus of some kind.
Perfect. I'm looking forward to their input.
I'll hold off on a new patch version until we get some feedback since I
don't think any of the requested changes are critical to the functionality.
Regards,
-David
Re: Return pg_control from pg_backup_stop().
On Wed, Mar 18, 2026 at 04:05:24AM +0000, David Steele wrote: > On 3/18/26 08:43, Michael Paquier wrote: >> I was wondering if we should have an assertion at least to cross-check >> that the contents we store in shared memory never go out-of-sync with >> the on-disk contents, in the shape of a USE_ASSERT_CHECKING block that >> calls get_controlfile() and memcmp()'s the contents between shmem and >> the on-disk file, while the LWLock is taken. We ship the control file >> last on purpose, one reason being backups taken from standbys, so that >> may be sensible to do. > > As far as I can see this should always be true -- I audited all the > > LWLockAcquire(ControlFileLock, LW_EXCLUSIVE) > > sections and the file is always saved once if is updated. Let me see if I > can add this check without too much pain, e.g. an additional parameter. This matches with my reads of the code. The attached check, that can be applied on top of your patches, passes under check-world. >> Another property of the new control file flag that is implied in the >> implementation but not documented is that we should never check for >> backupLabelRequired when a backup_label is gone. > > I'm not sure what you mean here? That's exactly when we do want to check as > below: Sorry for the confusion, I meant that "we should never check for backupLabelRequired when we have a backup_label". > Using the pg_control copy from pg_backup_stop() is entirely optional and > nothing is broken if vendors decide not to use it. This can be demonstrated > by applying the 01 patch without 02. In that case the tests in > 042_low_level_backup continue to run. You can also apply 01 and 02 and > revert the test changes in 042_low_level_backup and that works, too. FWIW, after a second look I am actually wondering if 0002 is safe at all. The contents of the control file are fetched after we are done with do_pg_backup_stop(), and there could be a bunch of activity that happens between the end of do_pg_backup_stop() and the backup_control_file() call, where the control file could be updated more and interfere with the recovery startup for some of its fields? GUC parameter updates that may touch the control file are one thing popping into mind. -- Michael
Re: Return pg_control from pg_backup_stop().
On 3/18/26 11:53, Michael Paquier wrote: > On Wed, Mar 18, 2026 at 04:05:24AM +0000, David Steele wrote: >> On 3/18/26 08:43, Michael Paquier wrote: >>> I was wondering if we should have an assertion at least to cross-check >>> that the contents we store in shared memory never go out-of-sync with >>> the on-disk contents, in the shape of a USE_ASSERT_CHECKING block that >>> calls get_controlfile() and memcmp()'s the contents between shmem and >>> the on-disk file, while the LWLock is taken. We ship the control file >>> last on purpose, one reason being backups taken from standbys, so that >>> may be sensible to do. >> >> As far as I can see this should always be true -- I audited all the >> >> LWLockAcquire(ControlFileLock, LW_EXCLUSIVE) >> >> sections and the file is always saved once if is updated. Let me see if I >> can add this check without too much pain, e.g. an additional parameter. > > This matches with my reads of the code. The attached check, that can > be applied on top of your patches, passes under check-world. Looks good to me. I have integrated it into the attached patches. I just changed it to look at our copy instead of the global ControlData since that is the one we are going to save. >> Using the pg_control copy from pg_backup_stop() is entirely optional and >> nothing is broken if vendors decide not to use it. This can be demonstrated >> by applying the 01 patch without 02. In that case the tests in >> 042_low_level_backup continue to run. You can also apply 01 and 02 and >> revert the test changes in 042_low_level_backup and that works, too. > > FWIW, after a second look I am actually wondering if 0002 is safe at > all. The contents of the control file are fetched after we are done > with do_pg_backup_stop(), and there could be a bunch of activity that > happens between the end of do_pg_backup_stop() and the > backup_control_file() call, where the control file could be updated > more and interfere with the recovery startup for some of its fields? > GUC parameter updates that may touch the control file are one thing > popping into mind. You are correct -- the copy of pg_control needs to happen before do_pg_backup_stop(). An older version of this patch saved pg_control in backup_state which made the prior location safe. However, I missed moving this code when I moved pg_control out of backup_state. Code review to the rescue. I believe I have addressed all current review comments in the attached patches. I tested Postgres crashing right after pg_control is updated but before backup_label is renamed. It worked as expected on restart. I also manually removed backup_label before restarting and that worked as well. I wrote a comment documenting all that. One thing I could do is note in the documentation that it is not strictly necessary to get pg_control from pg_backup_stop(). Right now it sounds like copying from disk is no longer an option -- but it is if you are willing to accept the possibility of pg_control being torn. But I'd hope most well-maintained backup software is taking care of that by now. The problem with caveats in the docs is it can lead to confusion and getting pg_control from pg_backup_stop() is just a better idea in general. Thoughts? Regards, -David
Re: Return pg_control from pg_backup_stop().
On Wed, Mar 18, 2026 at 07:35:47AM +0000, David Steele wrote: > You are correct -- the copy of pg_control needs to happen before > do_pg_backup_stop(). An older version of this patch saved pg_control in > backup_state which made the prior location safe. However, I missed moving > this code when I moved pg_control out of backup_state. Code review to the > rescue. Right. I am wondering also if the final result would not be better without 0002, actually, focusing only on the "simpler" base backup case through the replication protocol, and you are making a good case in mentioning it as not absolutely mandatory for base backups that are taken through the SQL functions. One could always tweak the flag manually in the control file based on the contents taken from the data folder. That's more hairy than writing the entire file, for sure, still possible. -- Michael
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 3/18/26 15:26, Michael Paquier wrote: > On Wed, Mar 18, 2026 at 07:35:47AM +0000, David Steele wrote: >> You are correct -- the copy of pg_control needs to happen before >> do_pg_backup_stop(). An older version of this patch saved pg_control in >> backup_state which made the prior location safe. However, I missed moving >> this code when I moved pg_control out of backup_state. Code review to the >> rescue. > > Right. I am wondering also if the final result would not be better > without 0002, actually, focusing only on the "simpler" base backup > case through the replication protocol, and you are making a good case > in mentioning it as not absolutely mandatory for base backups that are > taken through the SQL functions. One could always tweak the flag > manually in the control file based on the contents taken from the data > folder. That's more hairy than writing the entire file, for sure, > still possible. Getting even 01 into PG19 would be a great outcome. This would solve the problem of torn pg_control and deleted backup labels for any backups made with pg_basebackup and that's going to cover a *lot* of cases. Established third-party backup solutions that are not based on pg_basebackup are generally able to manipulate pg_control so that's not as much of a concern, perhaps. It does raise the barrier of entry for new backup software if they need to learn to read and validate pg_control to avoid a torn copy and set the flag. Patch 02 solves that problem in a general way so I still think it adds value for the ecosystem -- but we could always discuss that in the PG20 cycle. Whatever gets committed for PG19 I'll write a followup patch to describe the hazards of reading pg_control and generally how to get a good copy. However, this will be complicated enough that the best answer will likely be to use pg_basebackup or some other reputable backup software. I don't love this -- I feel like the low-level interface should be usable with such hazards. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
Whatever gets committed for PG19 I'll write a followup patch to describe
the hazards of reading pg_control and generally how to get a good copy.
However, this will be complicated enough that the best answer will
likely be to use pg_basebackup or some other reputable backup software.
I don't love this -- I feel like the low-level interface should be
usable with such hazards.
Surya Poondla and I had decided on this patchset as a pair-reviewing exercise. However, events have overtaken us, and several other people have chimed in expressing the same concerns that we had observed but hadn't yet completed our review. All of the main concerns that we had found up to this point have been addressed in the lastest patchset, except for the trivial observation that the ereport() uses the old style and doesn't need the set of parens around (errmsg(), errhint()). Patches apply clean, tests pass, test coverage seems sufficient, we're happy with the wording of the documentation, in short there really isn't a whole lot for us to add to the review, and for that reason we're removing our names from the list of reviewers in the commitfest app.
Re: Return pg_control from pg_backup_stop().
Hi Corey, On 3/19/26 04:00, Corey Huinker wrote: > Whatever gets committed for PG19 I'll write a followup patch to > describe > the hazards of reading pg_control and generally how to get a good copy. > However, this will be complicated enough that the best answer will > likely be to use pg_basebackup or some other reputable backup software. > I don't love this -- I feel like the low-level interface should be > usable with such hazards. > > Surya Poondla and I had decided on this patchset as a pair-reviewing > exercise. However, events have overtaken us, and several other people > have chimed in expressing the same concerns that we had observed but > hadn't yet completed our review. Thank you both for having a look! > All of the main concerns that we had > found up to this point have been addressed in the lastest patchset, > except for the trivial observation that the ereport() uses the old style > and doesn't need the set of parens around (errmsg(), errhint()). Grep shows there are lots of messages with the new style but many more in the old style. Presumably they are only being updated as they are modified. Do you happen to know the commit or message thread where this policy was started? I've been searching but it is such a generic search term. I've updated the message style in the new patches. > Patches > apply clean, tests pass, test coverage seems sufficient, we're happy > with the wording of the documentation, in short there really isn't a > whole lot for us to add to the review, and for that reason we're > removing our names from the list of reviewers in the commitfest app. It seems to me you've still done a review. Confirming what the other reviewers found is good info to have. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
On Wed, Mar 18, 2026 at 10:56 PM David Steele <david@pgbackrest.org> wrote:
Hi Corey,
On 3/19/26 04:00, Corey Huinker wrote:
> Whatever gets committed for PG19 I'll write a followup patch to
> describe
> the hazards of reading pg_control and generally how to get a good copy.
> However, this will be complicated enough that the best answer will
> likely be to use pg_basebackup or some other reputable backup software.
> I don't love this -- I feel like the low-level interface should be
> usable with such hazards.
>
> Surya Poondla and I had decided on this patchset as a pair-reviewing
> exercise. However, events have overtaken us, and several other people
> have chimed in expressing the same concerns that we had observed but
> hadn't yet completed our review.
Thank you both for having a look!
> All of the main concerns that we had > found up to this point have
been addressed in the lastest patchset,
> except for the trivial observation that the ereport() uses the old style
> and doesn't need the set of parens around (errmsg(), errhint()).
Grep shows there are lots of messages with the new style but many more
in the old style. Presumably they are only being updated as they are
modified.
That's always been my assumption. Not worth the churn.
Do you happen to know the commit or message thread where this
policy was started? I've been searching but it is such a generic search
term.
I limited my git log -p to elog.h, and it seems it started with e3a87b4991cc back in 2020. The only reason I knew about it was that I used to do backports from v13 to unsupported versions, and the new style would cause the build to fail on an otherwise clean cherry pick.
It seems to me you've still done a review. Confirming what the other
reviewers found is good info to have.
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 3/18/26 19:26, David Steele wrote: > On 3/18/26 15:26, Michael Paquier wrote: >> On Wed, Mar 18, 2026 at 07:35:47AM +0000, David Steele wrote: >>> You are correct -- the copy of pg_control needs to happen before >>> do_pg_backup_stop(). An older version of this patch saved pg_control in >>> backup_state which made the prior location safe. However, I missed >>> moving >>> this code when I moved pg_control out of backup_state. Code review to >>> the >>> rescue. >> >> Right. I am wondering also if the final result would not be better >> without 0002, actually, focusing only on the "simpler" base backup >> case through the replication protocol, and you are making a good case >> in mentioning it as not absolutely mandatory for base backups that are >> taken through the SQL functions. One could always tweak the flag >> manually in the control file based on the contents taken from the data >> folder. That's more hairy than writing the entire file, for sure, >> still possible. > > Getting even 01 into PG19 would be a great outcome. This would solve the > problem of torn pg_control and deleted backup labels for any backups > made with pg_basebackup and that's going to cover a *lot* of cases. > > Established third-party backup solutions that are not based on > pg_basebackup are generally able to manipulate pg_control so that's not > as much of a concern, perhaps. It does raise the barrier of entry for > new backup software if they need to learn to read and validate > pg_control to avoid a torn copy and set the flag. Patch 02 solves that > problem in a general way so I still think it adds value for the > ecosystem -- but we could always discuss that in the PG20 cycle. > > Whatever gets committed for PG19 I'll write a followup patch to describe > the hazards of reading pg_control and generally how to get a good copy. > However, this will be complicated enough that the best answer will > likely be to use pg_basebackup or some other reputable backup software. > I don't love this -- I feel like the low-level interface should be > usable with such hazards. I have withdrawn this patch. If anybody wants to pick it up in the future I'll be happy to rebase it but I think two years is long enough to maintain a patch that is not getting traction. We are left with the issue that pg_basebackup backups may contain a torn copy of pg_control. At the least this should be documented. It would also be a good idea to document that utilizing the low-level backup interface requires validating the checksum in pg_control to avoid a torn copy. This is non-trivial but certainly doable. Regards, -David
Re: Return pg_control from pg_backup_stop().
On 4/13/26 21:55, David Steele wrote: > > I have withdrawn this patch. If anybody wants to pick it up in the > future I'll be happy to rebase it but I think two years is long enough > to maintain a patch that is not getting traction. I would like to revive this but focus on the first patch for now and drop the second patch from consideration. The patch now implements only the new flag for pgcontrol and wires this logic into basebackup. It has the additional benefit of guaranteeing that the base backup contains a non-torn version of pgcontrol. I know everyone was really busy in March but now that things are a bit calmer I'd like to revisit. Heikki, Robert, Andres, Fujii -- any objections or comments? I believe Michael is on board with the feature (this part, at least) but he very sensibly would like to have some consensus. Thanks, -David
Re: Return pg_control from pg_backup_stop().
От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all, On Wed, Jul 8, 2026 at 2:49 PM David Steele wrote: > > On 4/13/26 21:55, David Steele wrote: > > > > I have withdrawn this patch. If anybody wants to pick it up in the > > future I'll be happy to rebase it but I think two years is long enough > > to maintain a patch that is not getting traction. > I would like to revive this but focus on the first patch for now and > drop the second patch from consideration. > > The patch now implements only the new flag for pgcontrol and wires this > logic into basebackup. It has the additional benefit of guaranteeing > that the base backup contains a non-torn version of pgcontrol. > > I know everyone was really busy in March but now that things are a bit > calmer I'd like to revisit. > > Heikki, Robert, Andres, Fujii -- any objections or comments? I believe > Michael is on board with the feature (this part, at least) but he very > sensibly would like to have some consensus. I reviewed and tested this patch on my PostgreSQL 20devel tree. The patch applied cleanly and built successfully without any issues. As part of the testing, I first attempted to reproduce the issue using a backup created with pg_backup_start()/pg_backup_stop() followed by a filesystem copy of the data directory. After restoring the backup and deleting backup_label, the server started successfully. While in between, I found that backupLabelRequired is set through backup_control_file(), due to which the above workflow does not exercise the new code path introduced by this patch. I then repeated the test using pg_basebackup. After creating a physical backup, I verified using pg_controldata that the backed-up pg_control contained: Backup label required: yes. I continued with the testing like restoring the backup, removing the backup_label file, and attempting to start the restored cluster. The PostgreSQL refused to start and reported the errors as expected: FATAL: could not find backup_label required for recovery HINT: restore the backup_label file that was created during the backup. This behavior is consistent with the intended purpose of the patch and discussions thereby preventing recovery from a pg_basebackup backup when the required backup_label file is missing. And also I reviewed the implementation and verified that the backupLabelRequired field is added to ControlFileData, set while preparing the copied pg_control in backup_control_file(), checked during recovery, and cleared after successful recovery. Overall, the patch behaves as intended and looks good to me. Regards, Solai
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
Hi Solai, Thank you for having a look at the patch. On 7/14/26 17:27, solai v wrote: > On Wed, Jul 8, 2026 at 2:49 PM David Steele wrote: >> >> The patch now implements only the new flag for pgcontrol and wires this >> logic into basebackup. It has the additional benefit of guaranteeing >> that the base backup contains a non-torn version of pgcontrol. > > I reviewed and tested this patch on my PostgreSQL 20devel tree. The > patch applied cleanly and built successfully without any issues. As > part of the testing, I first attempted to reproduce the issue using a > backup created with pg_backup_start()/pg_backup_stop() followed by a > filesystem copy of the data directory. After restoring the backup and > deleting backup_label, the server started successfully. > While in between, I found that backupLabelRequired is set through > backup_control_file(), due to which the above workflow does not > exercise the new code path introduced by this patch. I previously had a second patch that would also make pg_backup_start()/pg_backup_stop() work with this new feature but Michael did not seem in favor of it so I dropped it to try and get the first part committed. > I then repeated > the test using pg_basebackup. After creating a physical backup, I > verified using pg_controldata that the backed-up pg_control contained: > > Backup label required: yes. > > I continued with the testing like restoring the backup, removing the > backup_label file, and attempting to start the restored cluster. The > PostgreSQL refused to start and reported the errors as expected: > > FATAL: could not find backup_label required for recovery > HINT: restore the backup_label file that was created during the backup. > > This behavior is consistent with the intended purpose of the patch and > discussions thereby preventing recovery from a pg_basebackup backup > when the required backup_label file is missing. And also I reviewed > the implementation and verified that the backupLabelRequired field is > added to ControlFileData, set while preparing the copied pg_control in > backup_control_file(), checked during recovery, and cleared after > successful recovery. Overall, the patch behaves as intended and looks > good to me. Excellent, thank you! Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Andy Pogrebnoi <andrew.pogrebnoi@percona.com>
Дата:
On Wed, Mar 18, 2026 at 2:27 PM David Steele <david@pgbackrest.org> wrote:
On 3/18/26 15:26, Michael Paquier wrote:
> On Wed, Mar 18, 2026 at 07:35:47AM +0000, David Steele wrote:
>> You are correct -- the copy of pg_control needs to happen before
>> do_pg_backup_stop(). An older version of this patch saved pg_control in
>> backup_state which made the prior location safe. However, I missed moving
>> this code when I moved pg_control out of backup_state. Code review to the
>> rescue.
>
> Right. I am wondering also if the final result would not be better
> without 0002, actually, focusing only on the "simpler" base backup
> case through the replication protocol, and you are making a good case
> in mentioning it as not absolutely mandatory for base backups that are
> taken through the SQL functions. One could always tweak the flag
> manually in the control file based on the contents taken from the data
> folder. That's more hairy than writing the entire file, for sure,
> still possible.
Getting even 01 into PG19 would be a great outcome. This would solve the
problem of torn pg_control and deleted backup labels for any backups
made with pg_basebackup and that's going to cover a *lot* of cases.
It'd be great to have 01 committed. It seems to me like a solid and low cost solution for the torn pg_control (and missed backup lables). These are good improvements for base backups.
I also reviewed the 01 patch but have nothing to add to the previous reviewers; looks good to me.
---
Cheers,
Andy
Re: Return pg_control from pg_backup_stop().
On 6/30/26 05:15, David Steele wrote:
> The patch now implements only the new flag for pgcontrol and wires this
> logic into basebackup.
Hi David,
Thanks for working on this -- it fills a gap that I think Postgres has
needed for a long time.
I reviewed and tested v11 on master and it looks all good.
Attached is v12: your patch plus a TAP test, two one-token fixes and a
commit message tweak, and the same as a delta against v11.
One thing I think needs a decision, which I left alone. xlogrecovery.c
still suggests removing backup_label in two places. Restore a
"pg_basebackup -X none" backup without recovery.signal and you get
"could not locate required checkpoint record", hinting "try removing the
file backup_label". Do that, and you get "could not find backup_label
required for recovery" telling you to put it back.
Blocking startup is right -- that second attempt used to succeed and
silently corrupt the cluster. But the patch exists to stop people from
removing backup_label, so the hint telling them to do so probably should
not survive it. Either suppress those lines when backupLabelRequired is
set, or have the new hint mention recovery.signal, which is usually the
real problem. Say which you prefer and I will write it.
Smaller things:
1. errhint() should start with a capital letter ("Restore the ...").
Fixed in v12, along with "(char *)controlFile" -> "(char *) controlFile".
2. The commit message says "limited to pg_basebackup", but the patch also
sets the flag in pg_rewind. The logic is fine; v12 just says so, and
it probably deserves a line in pg_rewind.sgml.
3. Assert(crc_ok && memcmp(...) == 0) combines two conditions; splitting
them would say which one failed.
4. v12 moves the test out of 002_archiving.pl, which is about
archive_cleanup_command, into 057_backup_label_required.pl. Your
assertions are kept, plus checks that the flag is set in the backup,
cleared after recovery, and the same for a backup from a standby.
5. Docs are untouched beyond the pg_control_recovery() table. At least
pg_basebackup.sgml needs a mention, and something in backup.sgml about
low-level backups not getting this protection now that the SQL part is
dropped. Worth saying that pg_resetwal is the only way to clear the
flag, too.
Thanks,
Shihao
Re: Return pg_control from pg_backup_stop().
Hi Shihao,
On 8/28/26 05:34, shihao zhong wrote:
>
> Thanks for working on this -- it fills a gap that I think Postgres has
> needed for a long time.
> I reviewed and tested v11 on master and it looks all good.
Thank you for having a look at the patch.
> One thing I think needs a decision, which I left alone. xlogrecovery.c
> still suggests removing backup_label in two places. Restore a
> "pg_basebackup -X none" backup without recovery.signal and you get
> "could not locate required checkpoint record", hinting "try removing the
> file backup_label". Do that, and you get "could not find backup_label
> required for recovery" telling you to put it back.
Agreed, and I have addressed it in v13 by adding a conditional to the
relevant errors with a alternate message:
ControlFile->backupLabelRequired ?
errhint("Touch \"%s/recovery.signal\" or \"%s/standby.signal\" and add
required recovery options.\n"
"Do not remove \"%s/backup_label\"; it is required to recover this
backup.",
DataDir, DataDir, DataDir) :
> Smaller things:
> 1. errhint() should start with a capital letter ("Restore the ...").
> Fixed in v12, along with "(char *)controlFile" -> "(char *) controlFile".
pg_indent would catch this, but always better to have to source as clean
as possible. Added into v13.
> 2. The commit message says "limited to pg_basebackup", but the patch also
> sets the flag in pg_rewind. The logic is fine; v12 just says so, and
> it probably deserves a line in pg_rewind.sgml.
Changes to the commit message preserved in v13. Docs updated per below.
> 3. Assert(crc_ok && memcmp(...) == 0) combines two conditions; splitting
> them would say which one failed.
I left this as is from v11. I think it's a pretty normal thing to do. If
you are running with asserts then you are debugging anyway so resolving
the missed condition is pretty trivial, IMO.
> 4. v12 moves the test out of 002_archiving.pl, which is about
> archive_cleanup_command, into 057_backup_label_required.pl. Your
> assertions are kept, plus checks that the flag is set in the backup,
> cleared after recovery, and the same for a backup from a standby.
Yes, I think a dedicated test module makes sense here, so I kept that in
v13.
> 5. Docs are untouched beyond the pg_control_recovery() table. At least
> pg_basebackup.sgml needs a mention, and something in backup.sgml about
> low-level backups not getting this protection now that the SQL part is
> dropped. Worth saying that pg_resetwal is the only way to clear the
> flag, too.
Docs have been updated with each of these suggestions.
I also added a few additional tests to flesh out the coverage.
Regards,
-DavidRe: Return pg_control from pg_backup_stop().
От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Hi David, On 15 Sep 2026, David Steele wrote: > I left this as is from v11. I think it's a pretty normal thing to do. I found a case where the assertion itself does not hold: > +Assert(crc_ok && > + memcmp(dataDisk, &controlData, sizeof(ControlFileData)) == 0); During XLOG_CHECKPOINT_ONLINE replay, xlog_redo() updates ControlFile->checkPointCopy.nextXid under ControlFileLock without calling UpdateControlFile(). The in-memory copy can therefore differ from the on-disk file even while we hold the lock. I can trigger this with the v13 patch on current master: pause a standby's base backup at basebackup-before-send-files, allocate an XID on the primary, run CHECKPOINT and wait for replay. Resuming the backup hits this assertion in backup_control_file(). IIUC, this mismatch does not mean that pg_control is torn. Perhaps we could drop the whole-struct equality assertion? On the motivation, we have had many WAL-G support requests from users who removed backup_label to get a restored cluster to start. Here is one example [0], and a pgBackRest case prompted by the startup hint [1]. I think protecting against this mistake is worthwhile. One concern about describing pg_resetwal as "the only way to clear it otherwise", even with the corruption warning: a WAL-G user actually removed backup_label and ran pg_resetwal, thinking this only meant giving up PITR [2]. Could we frame this as bypassing the protection, not as a way to clear the flag? It cannot replace the WAL replay needed to make a backup consistent. Thank you! Best regards, Andrey Borodin. [0] https://github.com/wal-g/wal-g/issues/115 [1] https://github.com/pgbackrest/pgbackrest/issues/839#issuecomment-532253964 [2] https://github.com/wal-g/wal-g/issues/1279#issuecomment-1144554054
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
Hi Andrey, Thank you for having a look at the patch. On 9/15/26 21:13, Andrey Borodin wrote: > > On 15 Sep 2026, David Steele wrote: >> I left this as is from v11. I think it's a pretty normal thing to do. > > I found a case where the assertion itself does not hold: > >> +Assert(crc_ok && >> + memcmp(dataDisk, &controlData, sizeof(ControlFileData)) == 0); > > During XLOG_CHECKPOINT_ONLINE replay, xlog_redo() updates > ControlFile->checkPointCopy.nextXid under ControlFileLock without > calling UpdateControlFile(). The in-memory copy can therefore differ > from the on-disk file even while we hold the lock. > > I can trigger this with the v13 patch on current master: pause a > standby's base backup at basebackup-before-send-files, allocate an XID > on the primary, run CHECKPOINT and wait for replay. Resuming the backup > hits this assertion in backup_control_file(). > > IIUC, this mismatch does not mean that pg_control is torn. Perhaps we > could drop the whole-struct equality assertion? The mismatch is not an issue as far as I can see. I'll leave that determination up to Michael, though, since he added the assert. > On the motivation, we have had many WAL-G support requests from users > who removed backup_label to get a restored cluster to start. Here is > one example [0], and a pgBackRest case prompted by the startup hint [1]. > I think protecting against this mistake is worthwhile. I've seen this now more times than I can count, even from people that I would expect to know better. The hint from Postgres to delete backup_label certainly does not help the situation, though even with this patch we can't remove it completely. I'm hoping to at least deal with the common case. > One concern about describing pg_resetwal as "the only way to clear it > otherwise", even with the corruption warning: a WAL-G user actually > removed backup_label and ran pg_resetwal, thinking this only meant > giving up PITR [2]. Could we frame this as bypassing the protection, > not as a way to clear the flag? It cannot replace the WAL replay needed > to make a backup consistent. Yeah, I have also seen pg_resetwal abused in various ways. I added that language based on feedback from Shihao and I think it should be documented but perhaps we need more cautionary language there. Let's see what people think. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi David, Andrey,
1. The pg_resetwal sentence in pg_basebackup.sgml
I asked for the field to be documented and still think it should be, but the
wording offers pg_resetwal as a way out and it is not one. It clears the flag
and resets the WAL, but leaves backup_label, so the cluster still does not
start. The flag is gone by then, so the hint v13 just conditioned away is
back, and the server itself says "try removing the file backup_label". They
do, and it starts silently inconsistent. Unpatched master behaves identically,
what is new is that users get sent there. Say what clears the flag, but make
restoring backup_label the remedy. Also rename the "pg_resetwal clears the
flag" subtest in 057.
Separately, pg_resetwal never mentions backup_label in code or docs, and
always leaves an unstartable cluster when one is present. A warning there
would cut this off at the first step. Happy to post that as its own patch.
2. The assert in backup_control_file()
Michael, this one is for you since David left the call to you. Assert builds
only, so no production impact, but the invariant is false and I would drop
the block rather than narrow it. Only xlog_redo() for XLOG_CHECKPOINT_ONLINE
updates the struct without calling UpdateControlFile, so checkPointCopy.nextXid
is the only field that can differ, and InitWalRecovery replaces checkPointCopy
wholesale when backup_label is present. The copy we ship is correct either
way, and asserting it equals the file contradicts the premise that shared
memory is authoritative. One detail on Andrey's recipe: a checkpoint replayed
before the backup never trips it, since do_pg_backup_start forces a
restartpoint. Only replay during file sending matters.
Otherwise patch 01 looks ready to me.
Tested v13 on master (04c4c1c3a96). recovery, pg_rewind, pg_basebackup and
regress all pass. Two comments.
regress all pass. Two comments.
1. The pg_resetwal sentence in pg_basebackup.sgml
I asked for the field to be documented and still think it should be, but the
wording offers pg_resetwal as a way out and it is not one. It clears the flag
and resets the WAL, but leaves backup_label, so the cluster still does not
start. The flag is gone by then, so the hint v13 just conditioned away is
back, and the server itself says "try removing the file backup_label". They
do, and it starts silently inconsistent. Unpatched master behaves identically,
what is new is that users get sent there. Say what clears the flag, but make
restoring backup_label the remedy. Also rename the "pg_resetwal clears the
flag" subtest in 057.
Separately, pg_resetwal never mentions backup_label in code or docs, and
always leaves an unstartable cluster when one is present. A warning there
would cut this off at the first step. Happy to post that as its own patch.
2. The assert in backup_control_file()
Michael, this one is for you since David left the call to you. Assert builds
only, so no production impact, but the invariant is false and I would drop
the block rather than narrow it. Only xlog_redo() for XLOG_CHECKPOINT_ONLINE
updates the struct without calling UpdateControlFile, so checkPointCopy.nextXid
is the only field that can differ, and InitWalRecovery replaces checkPointCopy
wholesale when backup_label is present. The copy we ship is correct either
way, and asserting it equals the file contradicts the premise that shared
memory is authoritative. One detail on Andrey's recipe: a checkpoint replayed
before the backup never trips it, since do_pg_backup_start forces a
restartpoint. Only replay during file sending matters.
Otherwise patch 01 looks ready to me.
Thanks,
Shihao
Re: Return pg_control from pg_backup_stop().
Hi Andrew and Shihao, On 9/16/26 07:14, shihao zhong wrote: > > Tested v13 on master (04c4c1c3a96). recovery, pg_rewind, pg_basebackup and > regress all pass. Two comments. > > 1. The pg_resetwal sentence in pg_basebackup.sgml > > I asked for the field to be documented and still think it should be, but the > wording offers pg_resetwal as a way out and it is not one. Agreed, so added more cautionary language in v14. > what is new is that users get sent there. Say what clears the flag, but make > restoring backup_label the remedy. Also rename the "pg_resetwal clears the > flag" subtest in 057. I updated the comment but left the test message as is -- I think it works with the new comment. > Separately, pg_resetwal never mentions backup_label in code or docs, and > always leaves an unstartable cluster when one is present. A warning there > would cut this off at the first step. Happy to post that as its own patch. Yes, I think a separate patch would be best. Feel free to CC me so I can review. > 2. The assert in backup_control_file() > > Michael, this one is for you since David left the call to you. I went ahead and removed the assert since as Andrey showed it is demonstrably not correct. We'll see what Michael thinks. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi David,
v14 addresses both of my comments, thanks.
I'll post the pg_resetwal warning as its own thread and CC you.
Marking this Ready for Committer.
Thanks,
Shihao
v14 addresses both of my comments, thanks.
I'll post the pg_resetwal warning as its own thread and CC you.
Marking this Ready for Committer.
Thanks,
Shihao
Re: Return pg_control from pg_backup_stop().
От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi David,
On the pg_resetwal patch, one question before I write it. A restored backup
is never cleanly shut down, so pg_resetwal already needs -f there. A
backup_label check that -f overrides would only change the message. I'd
rather refuse even with -f, like the postmaster.pid check, and make the user
remove backup_label by hand. 058 would then need to remove backup_label
before its pg_resetwal step. Does that work for you? Or you would prefer
is never cleanly shut down, so pg_resetwal already needs -f there. A
backup_label check that -f overrides would only change the message. I'd
rather refuse even with -f, like the postmaster.pid check, and make the user
remove backup_label by hand. 058 would then need to remove backup_label
before its pg_resetwal step. Does that work for you? Or you would prefer
to have a warning message only.
Thanks,
Shihao
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
Hi Shihao, On 9/27/26 19:13, shihao zhong wrote: > > On the pg_resetwal patch, one question before I write it. A restored backup > is never cleanly shut down, so pg_resetwal already needs -f there. A > backup_label check that -f overrides would only change the message. I'd > rather refuse even with -f, like the postmaster.pid check, and make the user > remove backup_label by hand. 058 would then need to remove backup_label > before its pg_resetwal step. I would definitely prefer to force the user to remove backup_label before pg_resetwal will continue (even with -f). The presence of backup_label indicates they are making a mistake by using pg_resetwal, when they should probably be configuring recovery instead. Regards, -David
Re: Return pg_control from pg_backup_stop().
On 10/21/26 15:07, David Steele wrote: > Patch has been rebased. A committer pointed out to me that part of this patch fixes a bug and could be back-patched, so it should be separate. Patch 01 now fixes only the possibility of a torn pg_control file. It makes the copy out of memory to reduce the time the lock is held, but it could alternately read the pg_control file in the lock as was done by Thomas in c558e6fd92f. If the consensus is that the latter approach is preferred I'm happy to make that change. Patch 02 implements the flag to enforce the presence of backup_label and credits Andres with the original idea. This is the bulk of the prior patch. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 23/10/2026 15:42, David Steele wrote: > On 10/21/26 15:07, David Steele wrote: >> Patch has been rebased. > > A committer pointed out to me that part of this patch fixes a bug and > could be back-patched, so it should be separate. > > Patch 01 now fixes only the possibility of a torn pg_control file. Looks good to me. > It makes the copy out of memory to reduce the time the lock is held, > but it could alternately read the pg_control file in the lock as was > done by Thomas in c558e6fd92f. If the consensus is that the latter > approach is preferred I'm happy to make that change. +0.5 for following the example of c558e6fd92f. This isn't performance-sensitive so that aspect doesn't matter. But it seems a little surprising if a backup might have more up-to-date values of some fields than what the SQL functions show. - Heikki
Re: Return pg_control from pg_backup_stop().
От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 23/10/2026 15:42, David Steele wrote:
> Patch 02 implements the flag to enforce the presence of backup_label and
> credits Andres with the original idea. This is the bulk of the prior patch.
Makes sense at high level.
> --- a/doc/src/sgml/backup.sgml
> +++ b/doc/src/sgml/backup.sgml
> @@ -1031,6 +1031,19 @@ SELECT * FROM pg_backup_stop(wait_for_archive => true);
> vital to the backup working and must be written byte for byte without
> modification, which may require opening the file in binary mode.
>
> +
> + Note that a backup made with the low-level API copies
> + pg_control from the running cluster, so it does not
> + contain the flag that sets to make
> + backup_label mandatory for recovery. Removing
> + backup_label from such a backup will not prevent the
> + cluster from starting; it will instead recover from the checkpoint
> + recorded in pg_control. Unless that happens to be the
> + checkpoint the backup started from, recovery will begin too late and
> + silently produce an inconsistent cluster.
> + Backup software using this API should set the flag itself if it is able to
> + modify pg_control.
> +
>
>
>
That's good advice, but the text feels awkward. Which flag do you need
to set exactly? And maybe the text should be placed elsewhere; the
copying of the pg_control file happened earlier already and you should
probably modify it already when you copy it.
> diff --git a/doc/src/sgml/ref/pg_basebackup.sgml b/doc/src/sgml/ref/pg_basebackup.sgml
> index b4a0faccf41..93676057394 100644
> --- a/doc/src/sgml/ref/pg_basebackup.sgml
> +++ b/doc/src/sgml/ref/pg_basebackup.sgml
> @@ -60,6 +60,18 @@ PostgreSQL documentation
> must be used.
>
>
> +
> + The copy of pg_control stored in the backup is flagged
> + to require the backup_label file for recovery. If
> + backup_label is removed, recovery refuses to start
> + rather than silently producing an inconsistent cluster. Restoring
> + backup_label to the root of the backup allows
> + recovery to proceed, and the flag is cleared once recovery has read the
> + file. also clears the flag, but discarding
> + the WAL bypasses the replay that recovery needs, leaving an inconsistent
> + cluster.
> +
> +
You shouldn't try to restore a backup_label file into an existing backup
that's missing it, so that wording sounds dangerous.
>
> The backup is made over a regular PostgreSQL
> connection that uses the replication protocol. The connection must be made
> diff --git a/doc/src/sgml/ref/pg_rewind.sgml b/doc/src/sgml/ref/pg_rewind.sgml
> index ac3d0c9328f..c4888c183fb 100644
> --- a/doc/src/sgml/ref/pg_rewind.sgml
> +++ b/doc/src/sgml/ref/pg_rewind.sgml
> @@ -455,6 +455,10 @@ GRANT EXECUTE ON FUNCTION pg_catalog.pg_read_binary_file(text, bigint, bigint, b
> defined as the result of pg_current_wal_insert_lsn()
> when rewinding from a live source or the last checkpoint LSN when
> rewinding from a stopped source.
> + pg_control is also flagged to require
> + backup_label, so that removing that file does not
> + allow the target to start up in an inconsistent state. The flag is
> + cleared once the target has processed backup_label.
>
>
>
That doesn't seem worth mentioning here; too much detail.
> diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
> index 20be788da78..56368a9378f 100644
> --- a/src/backend/access/transam/xlogrecovery.c
> +++ b/src/backend/access/transam/xlogrecovery.c
> @@ -604,6 +604,10 @@ InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdown_ptr,
> ereport(FATAL,
> errmsg("could not find redo location %X/%08X referenced by checkpoint record at %X/%08X",
> LSN_FORMAT_ARGS(checkPoint.redo), LSN_FORMAT_ARGS(CheckPointLoc)),
> + ControlFile->backupLabelRequired ?
> + errhint("Touch \"%s/recovery.signal\" or \"%s/standby.signal\" and add required recovery options.\n"
> + "Do not remove \"%s/backup_label\"; it is required to recover this backup.",
> + DataDir, DataDir, DataDir) :
> errhint("If you are restoring from a backup, touch \"%s/recovery.signal\" or \"%s/standby.signal\" and add required recovery options.\n"
> "If you are not restoring from a backup, try removing the file \"%s/backup_label\".\n"
> "Be careful: removing \"%s/backup_label\" will result in a corrupt cluster if restoring from a backup.",
Umm, not new with his patch, but why do we still have that hint to
remove backup_label? Is that ever the right thing to do? Why didn't we
remove that hint when exclusive-mode backups were removed?
Idea for further hardening: If you attempt to start up postgres and the
backup_label is present, set the flag in the control file. That way,
even if the backup tool doesn't modify the control file, it will be set
on first startup attempt. If you delete the backup_label later, you
still won't be able to start up into an inconsistent state. It won't
protect you if you remove the backup_label before attempting to start
up, but it'd catch some cases.
- Heikki
Re: Return pg_control from pg_backup_stop().
On Fri, Oct 23, 2026 at 06:47:24PM +0300, Heikki Linnakangas wrote:
> Looks good to me.
Apologies for not following closely what you have been doing lately in
this area, David. I should have followed, but well..
+void
+backup_control_file(uint8 *controlFile)
+{
+ ControlFileData controlData;
+
+ LWLockAcquire(ControlFileLock, LW_SHARED);
+ memcpy(&controlData, ControlFile, sizeof(ControlFileData));
+ LWLockRelease(ControlFileLock);
[...]
/* ... and pg_control after everything else. */
- if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not stat file \"%s\": %m",
- XLOG_CONTROL_FILE)));
- sendFile(sink, XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, &statbuf,
- false, InvalidOid, InvalidOid,
- InvalidRelFileNumber, 0, &manifest, 0, NULL, 0);
+ backup_control_file(controlFile);
+ sendFileWithContent(sink, XLOG_CONTROL_FILE,
+ (char *) controlFile, PG_CONTROL_FILE_SIZE,
+ &manifest);
Yep, that's clearly something that should be backpatched, at quick
glance. Heikki, are you planning to take care of this part?
--
Michael
Re: Return pg_control from pg_backup_stop().
On 10/23/26 17:47, Heikki Linnakangas wrote: > On 23/10/2026 15:42, David Steele wrote: > >> It makes the copy out of memory to reduce the time the lock is held, >> but it could alternately read the pg_control file in the lock as was >> done by Thomas in c558e6fd92f. If the consensus is that the latter >> approach is preferred I'm happy to make that change. > +0.5 for following the example of c558e6fd92f. This isn't performance- > sensitive so that aspect doesn't matter. But it seems a little > surprising if a backup might have more up-to-date values of some fields > than what the SQL functions show. Another +0.5 from me makes it +1. We know that pg_control in memory can be ahead of pg_control on disk (for standbys) and we are quite sure that is not an issue but it seems better not to take the risk for something that we are going to back patch. Also better to use the same mechanism everywhere. I've attached a patch 01 that does that and rebased 02 on it. 02 does not make any of the changes that you requested in your later message but I'll take care of those separately. Since minor releases are coming up it seems better to prioritize 01. Note that back patching this below PG17 will require also back patching 7606175991f (see commit message for details). Regards, -David
Re: Return pg_control from pg_backup_stop().
On 10/23/26 18:17, Heikki Linnakangas wrote:
> On 23/10/2026 15:42, David Steele wrote:
>> Patch 02 implements the flag to enforce the presence of backup_label
>> and credits Andres with the original idea. This is the bulk of the
>> prior patch.
> Makes sense at high level.
>
>> --- a/doc/src/sgml/backup.sgml
>> +++ b/doc/src/sgml/backup.sgml
>> @@ -1031,6 +1031,19 @@ SELECT * FROM pg_backup_stop(wait_for_archive
>> => true);
>> vital to the backup working and must be written byte for byte
>> without
>> modification, which may require opening the file in binary mode.
>>
>> +
>> + Note that a backup made with the low-level API copies
>> + pg_control from the running cluster, so it
>> does not
>> + contain the flag that sets to
>> make
>> + backup_label mandatory for recovery. Removing
>> + backup_label from such a backup will not
>> prevent the
>> + cluster from starting; it will instead recover from the checkpoint
>> + recorded in pg_control. Unless that happens
>> to be the
>> + checkpoint the backup started from, recovery will begin too late
>> and
>> + silently produce an inconsistent cluster.
>> + Backup software using this API should set the flag itself if it
>> is able to
>> + modify pg_control.
>> +
>>
>>
>>
>
> That's good advice, but the text feels awkward. Which flag do you need
> to set exactly? And maybe the text should be placed elsewhere; the
> copying of the pg_control file happened earlier already and you should
> probably modify it already when you copy it.
On second thought I think this should just be removed rather than moved.
Modifying pg_control is not something we talk about in the docs, so
there are no good examples, and I don't think we should start here.
Removed in v18.
>> + The copy of pg_control stored in the backup
>> is flagged
>> + to require the backup_label file for
>> recovery. If
>> + backup_label is removed, recovery refuses to
>> start
>> + rather than silently producing an inconsistent cluster. Restoring
>> + backup_label to the root of the backup allows
>> + recovery to proceed, and the flag is cleared once recovery has
>> read the
>> + file. also clears the flag, but
>> discarding
>> + the WAL bypasses the replay that recovery needs, leaving an
>> inconsistent
>> + cluster.
>
> You shouldn't try to restore a backup_label file into an existing backup
> that's missing it, so that wording sounds dangerous.
Agreed, I updated this in v18 to recommend a fresh restored including
backup label. Who knows where a user might get a backup_label from
otherwise?
I also updated the related hint to suggest a new restore:
- errhint("Restore the backup_label file that was created during the
backup."));
+ errhint("Restore the backup again, including the backup_label
file."));
>> defined as the result of pg_current_wal_insert_lsn()</
>> literal>
>> when rewinding from a live source or the last checkpoint LSN when
>> rewinding from a stopped source.
>> + pg_control is also flagged to require
>> + backup_label, so that removing that file
>> does not
>> + allow the target to start up in an inconsistent state. The flag is
>> + cleared once the target has processed backup_label</
>> filename>.
>
> That doesn't seem worth mentioning here; too much detail.
Agreed. Removed in v18.
>> @@ -604,6 +604,10 @@ InitWalRecovery(ControlFileData *ControlFile,
>> bool *wasShutdown_ptr,
>> ereport(FATAL,
>> errmsg("could not find redo location %X/
>> %08X referenced by checkpoint record at %X/%08X",
>> LSN_FORMAT_ARGS(checkPoint.redo),
>> LSN_FORMAT_ARGS(CheckPointLoc)),
>> + ControlFile->backupLabelRequired ?
>> + errhint("Touch \"%s/recovery.signal\" or
>> \"%s/standby.signal\" and add required recovery options.\n"
>> + "Do not remove \"%s/
>> backup_label\"; it is required to recover this backup.",
>> + DataDir, DataDir, DataDir) :
>> errhint("If you are restoring from a
>> backup, touch \"%s/recovery.signal\" or \"%s/standby.signal\" and add
>> required recovery options.\n"
>> "If you are not restoring from a
>> backup, try removing the file \"%s/backup_label\".\n"
>> "Be careful: removing \"%s/
>> backup_label\" will result in a corrupt cluster if restoring from a
>> backup.",
>
> Umm, not new with his patch, but why do we still have that hint to
> remove backup_label? Is that ever the right thing to do? Why didn't we
> remove that hint when exclusive-mode backups were removed?
Unfortunately I believe there is still a use case for this hint. When
making a backup, e.g. via snapshot, it is still easier to write the
backup_label into PGDATA, make the snapshot, then remove it. If the
cluster crashes before the removal then the answer is to remove the
backup label, just as it was if exclusive backup crashed.
I'm not excited about the above use case but not sure we can discount it
entirely.
What we could do is document that the above use case should be
accomplished by copying backup_label to another file in PGDATA, e.g.
backup_label.snap, and then renaming it when the snapshot is restored. I
think this is reasonable given that recovery.signal/standby.signal still
needed to be created so there is some work to be done on the restore
side. Maybe then it would be OK to remove the hint?
That feels like the subject for a separate patch, though. I have added
it to my notes.
> Idea for further hardening: If you attempt to start up postgres and the
> backup_label is present, set the flag in the control file. That way,
> even if the backup tool doesn't modify the control file, it will be set
> on first startup attempt. If you delete the backup_label later, you
> still won't be able to start up into an inconsistent state. It won't
> protect you if you remove the backup_label before attempting to start
> up, but it'd catch some cases.
This would be dangerous if we accept that the above use case is valid.
In that case on restart the cluster would be bricked where in fact
removing the backup_label is the right thing to do.
Maybe we could set the flag when backup_label is present *and* either
standby.signal or recovery.signal. That works for the case above.
I originally had a patch that allowed backup software to get a non-torn
copy of pg_control with the flag set from pg_backup_stop(). If I can get
these patches committed I plan to reintroduce it. I think that is a
better answer for third-party software.
If we do decide to go with your idea instead (or in addition to), I'd
prefer to pursue it in a separate patch. I have added it to my notes.
Regards,
-DavidRe: Return pg_control from pg_backup_stop().
On Sat, Oct 24, 2026 at 02:25:30PM +0900, Michael Paquier wrote:
> /* ... and pg_control after everything else. */
> - if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
> - ereport(ERROR,
> - (errcode_for_file_access(),
> - errmsg("could not stat file \"%s\": %m",
> - XLOG_CONTROL_FILE)));
> - sendFile(sink, XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, &statbuf,
> - false, InvalidOid, InvalidOid,
> - InvalidRelFileNumber, 0, &manifest, 0, NULL, 0);
> + backup_control_file(controlFile);
> + sendFileWithContent(sink, XLOG_CONTROL_FILE,
> + (char *) controlFile, PG_CONTROL_FILE_SIZE,
> + &manifest);
>
> Yep, that's clearly something that should be backpatched, at quick
> glance. Heikki, are you planning to take care of this part?
Hearing nothing, I have added that into my own pipeline and applies
the fix down to v15. v14 has been left out, the branch going EOL soon
with a problem that is extremely going to happen in practice does not
give me a warm feeling. For v15 and v16, I have backpatched
d2c1e18de9d1 first. The fix has been applied as of 4a92c8c29bd0.
--
Michael
Re: Return pg_control from pg_backup_stop().
On Wed, Oct 28, 2026 at 09:00:44AM +0900, Michael Paquier wrote: > Hearing nothing, I have added that into my own pipeline and applies > the fix down to v15. v14 has been left out, the branch going EOL soon > with a problem that is extremely going to happen in practice does not > give me a warm feeling. For v15 and v16, I have backpatched > d2c1e18de9d1 first. The fix has been applied as of 4a92c8c29bd0. Based on a sudden report from billbug, under Solaris 11, this gets fun: https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=billbug&dt=2026-10-28%2004%3A00%3A01 And we have a crash in a BASE_BACKUP command: 2026-10-28 05:23:30.548 CET [1429:4] LOG: client backend (PID 1498) was terminated by signal 11: Segmentation Fault 2026-10-28 05:23:30.548 CET [1429:5] DETAIL: Failed process was running: BASE_BACKUP ( LABEL 'pg_basebackup base backup', PROGRESS, CHECKPOINT 'fast', WAIT 0, TABLESPACE_MAP, MANIFEST 'yes', TARGET 'client') This runs under valgrind with -DRELCACHE_FORCE_RELEASE, and of course I am not able to reproduce it with combinations of these three. The closest thing that I can see in recent history would be 040932c31146. One possible explanation is PG_CONTROL_FILE_SIZE added to the stack of perform_base_backup()? I'm still puzzled without more information from valgrind, but if that's right we could try a palloc0() instead. My blind suggestion of fix would be the attached, that I cannot really test. Thoughts or comments? -- Michael
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 10/28/26 07:38, Michael Paquier wrote: > On Wed, Oct 28, 2026 at 09:00:44AM +0900, Michael Paquier wrote: >> Hearing nothing, I have added that into my own pipeline and applies >> the fix down to v15. v14 has been left out, the branch going EOL soon >> with a problem that is extremely going to happen in practice does not >> give me a warm feeling. For v15 and v16, I have backpatched >> d2c1e18de9d1 first. The fix has been applied as of 4a92c8c29bd0. > > Based on a sudden report from billbug, under Solaris 11, this gets > fun: > https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=billbug&dt=2026-10-28%2004%3A00%3A01 > > And we have a crash in a BASE_BACKUP command: > 2026-10-28 05:23:30.548 CET [1429:4] LOG: client backend (PID 1498) > was terminated by signal 11: Segmentation Fault > 2026-10-28 05:23:30.548 CET [1429:5] DETAIL: Failed process was > running: BASE_BACKUP ( LABEL 'pg_basebackup base backup', PROGRESS, > CHECKPOINT 'fast', WAIT 0, TABLESPACE_MAP, MANIFEST 'yes', TARGET > 'client') > > This runs under valgrind with -DRELCACHE_FORCE_RELEASE, and of course > I am not able to reproduce it with combinations of these three. Are you sure valgrind is running? I'm not too familiar with build farm configuration but it set 'use_valgrind' => undef, which seems to imply it is off even if -DUSE_VALGRIND is also set. > > The closest thing that I can see in recent history would be > 040932c31146. One possible explanation is PG_CONTROL_FILE_SIZE added > to the stack of perform_base_backup()? I'm still puzzled without more > information from valgrind, but if that's right we could try a > palloc0() instead. This was my first thought, but I can't see why 8KiB on the stack would be an issue. To me this looks a lot like a latent issue being exposed because we've pushed things around on the stack a bit. > My blind suggestion of fix would be the attached, that I cannot really > test. Thoughts or comments? The patch looks good to me but I'm concerned it would just be masking the real issue even if it works. I'd be curious to see if the back branches run green when they run. It would also be helpful to get a stack trace. I've CC'd billbug's owner. The HEAD run with the 2026-10-28 04:00 UTC snapshot crashed a client backend (PID 1498) running BASE_BACKUP with SIGSEGV during src/bin/pg_basebackup/t/010_pg_basebackup.pl, but no backtrace made it into the logs. Could you check whether a core file was written and get a backtrace from it? If no core was written then the coreadm settings may be redirecting or suppressing it. Any additional info you can provide would be greatly appreciated. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 26/10/2026 13:42, David Steele wrote: > On 10/23/26 18:17, Heikki Linnakangas wrote: >> On 23/10/2026 15:42, David Steele wrote: >> Umm, not new with his patch, but why do we still have that hint to >> remove backup_label? Is that ever the right thing to do? Why didn't we >> remove that hint when exclusive-mode backups were removed? > > Unfortunately I believe there is still a use case for this hint. When > making a backup, e.g. via snapshot, it is still easier to write the > backup_label into PGDATA, make the snapshot, then remove it. If the > cluster crashes before the removal then the answer is to remove the > backup label, just as it was if exclusive backup crashed. > > I'm not excited about the above use case but not sure we can discount it > entirely. If you take an atomic filesystem-level snapshot, restoring it is equivalent to crash recovery, so you don't need a backup_label to restore it to a consistent state. There's no need for pg_start_backup() or a backup_label in that case. Admittedly, a non-atomic snapshot is a different story. But I don't see how you could do that anyway. The backup_label is returned by pg_stop_backup(). If the sequence is: 1. pg_start_backup() 2. take filesystem snapshot 3. pg_stop_backup() there's no way to include the backup_label in the snapshot, because you don't have it until you call pg_stop_backup(). - Heikki
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 10/28/26 10:48, Heikki Linnakangas wrote: > On 26/10/2026 13:42, David Steele wrote: >> On 10/23/26 18:17, Heikki Linnakangas wrote: >>> On 23/10/2026 15:42, David Steele wrote: >>> Umm, not new with his patch, but why do we still have that hint to >>> remove backup_label? Is that ever the right thing to do? Why didn't >>> we remove that hint when exclusive-mode backups were removed? >> >> Unfortunately I believe there is still a use case for this hint. When >> making a backup, e.g. via snapshot, it is still easier to write the >> backup_label into PGDATA, make the snapshot, then remove it. If the >> cluster crashes before the removal then the answer is to remove the >> backup label, just as it was if exclusive backup crashed. >> >> I'm not excited about the above use case but not sure we can discount >> it entirely. > > If you take an atomic filesystem-level snapshot, restoring it is > equivalent to crash recovery, so you don't need a backup_label to > restore it to a consistent state. There's no need for pg_start_backup() > or a backup_label in that case. Admittedly, a non-atomic snapshot is a > different story. True, but I recommend *always* using the documented backup procedure, especially if PITR will be used on restore. > But I don't see how you could do that anyway. The backup_label is > returned by pg_stop_backup(). If the sequence is: > > 1. pg_start_backup() > 2. take filesystem snapshot > 3. pg_stop_backup() > > there's no way to include the backup_label in the snapshot, because you > don't have it until you call pg_stop_backup(). Oops, yeah, that was an old pattern what was still usable up until PG14 but should no longer be possible. I searched the thread where we removed exclusive backup and I can't see any reason why we would have kept it, so it was probably an oversight. Once Michael and I have resolved the build farm issue with 01 I'll rebase and make the removal a small patch before adding the pgcontrol flag -- unless you would prefer a separate thread. Regards, -David
Re: Return pg_control from pg_backup_stop().
On Wed, Oct 28, 2026 at 08:56:25AM +0000, David Steele wrote: > I've CC'd billbug's owner. The HEAD run with the 2026-10-28 04:00 UTC > snapshot crashed a client backend (PID 1498) running BASE_BACKUP with > SIGSEGV during src/bin/pg_basebackup/t/010_pg_basebackup.pl, but no > backtrace made it into the logs. Could you check whether a core file was > written and get a backtrace from it? If no core was written then the > coreadm settings may be redirecting or suppressing it. Any additional info > you can provide would be greatly appreciated. We have what looks like a second report on HEAD and v19, different pattern: https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=thorntail&dt=2026-10-28%2006%3A31%3A23&stg=recovery-check From 001's primary logs: 2026-10-28 10:44:13.113 MSK [1706208:4] LOG: client backend (PID 1706227) was terminated by signal 10: Bus error 2026-10-28 10:44:13.113 MSK [1706208:5] DETAIL: Failed process was running: BASE_BACKUP (CHECKPOINT 'fast', MAX_RATE 32); If something else is going on, I'm not quite sure what it is yet. -- Michael
Re: Return pg_control from pg_backup_stop().
От:
David Steele <david@pgbackrest.org>
Дата:
On 10/28/26 11:56, Michael Paquier wrote: > On Wed, Oct 28, 2026 at 08:56:25AM +0000, David Steele wrote: >> I've CC'd billbug's owner. The HEAD run with the 2026-10-28 04:00 UTC >> snapshot crashed a client backend (PID 1498) running BASE_BACKUP with >> SIGSEGV during src/bin/pg_basebackup/t/010_pg_basebackup.pl, but no >> backtrace made it into the logs. Could you check whether a core file was >> written and get a backtrace from it? If no core was written then the >> coreadm settings may be redirecting or suppressing it. Any additional info >> you can provide would be greatly appreciated. > > We have what looks like a second report on HEAD and v19, different > pattern: > https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=thorntail&dt=2026-10-28%2006%3A31%3A23&stg=recovery-check > > From 001's primary logs: > 2026-10-28 10:44:13.113 MSK [1706208:4] LOG: client backend (PID > 1706227) was terminated by signal 10: Bus error > 2026-10-28 10:44:13.113 MSK [1706208:5] DETAIL: Failed process was > running: BASE_BACKUP (CHECKPOINT 'fast', MAX_RATE 32); > > If something else is going on, I'm not quite sure what it is yet. thorntail is also SPARC so this failure narrows things down quite a bit. The test that crashed is "base backup cleanly canceled". This test is designed to be cancelled in the middle so it is unlikely that it ever got to the new code, which isolates the extra 8KiB on the stack as the likely cause. Well, that already seemed clear but this is additional confirmation. On the cancel path the only runtime difference from the prior code is the size of perform_base_backup()'s frame. The new 8kB buffer is declared inside PG_ENSURE_ERROR_CLEANUP() so the error from the cancel siglongjmp()s back into the larger frame. A bus error on SPARC is usually an alignment trap, so my guess is that the larger frame exposes an alignment problem somewhere in the unwinding. That fits with my earlier suspicion that this is a latent issue exposed by moving things around on the stack. Since v19 is failing as well this is not specific to master. I think we should go ahead with your palloc0() patch to get the buildfarm green again. It takes the buffer out of the frame entirely. It may well be masking something, but that is better investigated with a backtrace than with a red buildfarm. Perhaps Noah can get one from thorntail. [This analysis was aided by Claude but I double-checked the details.] Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Noah Misch <noah@leadboat.com>
Дата:
On Wed, Oct 28, 2026 at 03:32:34PM +0000, David Steele wrote: > On 10/28/26 11:56, Michael Paquier wrote: > > https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=thorntail&dt=2026-10-28%2006%3A31%3A23&stg=recovery-check > A bus error on SPARC is usually an alignment trap I agree. SPARC Linux is one of the few platforms still doing those. > Since v19 is failing as well this is not specific to master. I think we > should go ahead with your palloc0() patch to get the buildfarm green again. > It takes the buffer out of the frame entirely. It may well be masking > something, but that is better investigated with a backtrace than with a red > buildfarm. Perhaps Noah can get one from thorntail. I won't be able to help with that directly, but: - thorntail is cfarm202.cfarm.net; accounts are available: https://portal.cfarm.net/users/new/ - If a newer buildfarm client would collect a stack trace here, I'm happy to upgrade.
Re: Return pg_control from pg_backup_stop().
On Wed, Oct 28, 2026 at 09:26:41AM -0700, Noah Misch wrote: > I won't be able to help with that directly, but: > > - thorntail is cfarm202.cfarm.net; accounts are available: > https://portal.cfarm.net/users/new/ > > - If a newer buildfarm client would collect a stack trace here, I'm happy to > upgrade. Could it be possible to send the result of an objdump -d on perform_base_backup() perhaps, before and after the fact? That should offer the glimpse of a hint, at least. -- Michael
Re: Return pg_control from pg_backup_stop().
On Wed, Oct 28, 2026 at 03:38:55PM +0900, Michael Paquier wrote: > My blind suggestion of fix would be the attached, that I cannot really > test. Thoughts or comments? I unfortunately do not have a better idea after sleeping on it, so I have applied this palloc0/pfree solution. Now I'll go light a candle to make the buildfarm gods kinder to me. -- Michael
Re: Return pg_control from pg_backup_stop().
On Thu, Oct 29, 2026 at 09:50:46AM +0900, Michael Paquier wrote: > I unfortunately do not have a better idea after sleeping on it, so I > have applied this palloc0/pfree solution. Now I'll go light a candle > to make the buildfarm gods kinder to me. billbug has just switched back to green on HEAD after two successive failures, so it looks like the stack sizing was the right guess: https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=billbug&dt=2026-10-29%2004%3A00%3A03 So something is going on here. How could we rule out if this is something on the Postgres side with SPARC? -- Michael
Re: Return pg_control from pg_backup_stop().
От:
Marcel Hofstetter <hofstetter@jomasoft.ch>
Дата:
Am 28.10.2026 um 09:56 schrieb David Steele: > On 10/28/26 07:38, Michael Paquier wrote: >> On Wed, Oct 28, 2026 at 09:00:44AM +0900, Michael Paquier wrote: >>> Hearing nothing, I have added that into my own pipeline and applies >>> the fix down to v15. v14 has been left out, the branch going EOL soon >>> with a problem that is extremely going to happen in practice does not >>> give me a warm feeling. For v15 and v16, I have backpatched >>> d2c1e18de9d1 first. The fix has been applied as of 4a92c8c29bd0. >> >> Based on a sudden report from billbug, under Solaris 11, this gets >> fun: >> https://buildfarm.postgresql.org/cgi-bin/show_log.pl? >> nm=billbug&dt=2026-10-28%2004%3A00%3A01 >> >> And we have a crash in a BASE_BACKUP command: >> 2026-10-28 05:23:30.548 CET [1429:4] LOG: client backend (PID 1498) >> was terminated by signal 11: Segmentation Fault >> 2026-10-28 05:23:30.548 CET [1429:5] DETAIL: Failed process was >> running: BASE_BACKUP ( LABEL 'pg_basebackup base backup', PROGRESS, >> CHECKPOINT 'fast', WAIT 0, TABLESPACE_MAP, MANIFEST 'yes', TARGET >> 'client') >> >> This runs under valgrind with -DRELCACHE_FORCE_RELEASE, and of course >> I am not able to reproduce it with combinations of these three. > > Are you sure valgrind is running? I'm not too familiar with build farm > configuration but it set 'use_valgrind' => undef, which seems to imply > it is off even if -DUSE_VALGRIND is also set. > >> >> The closest thing that I can see in recent history would be >> 040932c31146. One possible explanation is PG_CONTROL_FILE_SIZE added >> to the stack of perform_base_backup()? I'm still puzzled without more >> information from valgrind, but if that's right we could try a >> palloc0() instead. > > This was my first thought, but I can't see why 8KiB on the stack would > be an issue. To me this looks a lot like a latent issue being exposed > because we've pushed things around on the stack a bit. > >> My blind suggestion of fix would be the attached, that I cannot really >> test. Thoughts or comments? > > The patch looks good to me but I'm concerned it would just be masking > the real issue even if it works. > > I'd be curious to see if the back branches run green when they run. It > would also be helpful to get a stack trace. > > I've CC'd billbug's owner. The HEAD run with the 2026-10-28 04:00 UTC > snapshot crashed a client backend (PID 1498) running BASE_BACKUP with > SIGSEGV during src/bin/pg_basebackup/t/010_pg_basebackup.pl, but no > backtrace made it into the logs. Could you check whether a core file was > written and get a backtrace from it? If no core was written then the > coreadm settings may be redirecting or suppressing it. Any additional > info you can provide would be greatly appreciated. sorry. core was disabled. enabled now for the future. interesting: my second solaris11/sparc animal is fine. https://buildfarm.postgresql.org/cgi-bin/show_history.pl?nm=margay&br=master Best regards, Marcel
Re: Return pg_control from pg_backup_stop().
On Thu, 29 Oct 2026 09:50:46 +0900, Michael Paquier wrote:
> On Wed, Oct 28, 2026 at 03:38:55PM +0900, Michael Paquier wrote:
> > My blind suggestion of fix would be the attached, that I cannot really
> > test. Thoughts or comments?
>
> I unfortunately do not have a better idea after sleeping on it, so I
> have applied this palloc0/pfree solution. Now I'll go light a candle
> to make the buildfarm gods kinder to me.
> --
> Michael
Hey Michael,
As requested (back channel) I spent some quality time with my
UltraSPARC 45 this morning and dug into this.
TL;DR: it's a GCC bug, and c97931c16d fixes it for us by keeping
perform_base_backup()'s frame under 4kB.
I reproduced it on an UltraSPARC-IIIi running illumos with gcc 13.4,
-O2 and cassert, at 4a92c8c29b. Your suggested reproduction works: a
file with a 114-byte name in PGDATA, then BASE_BACKUP through psql
with "replication=1", gives the SIGSEGV every time. So does
cancelling a BASE_BACKUP (MAX_RATE 32), which is thorntail's case.
The core (mdb, full output attached):
status: process terminated by SIGSEGV, addr=fffffffe7f74733c
%pc = perform_base_backup+0x1c0: st %o0, [%g1]
%npc = perform_base_backup+0x1480
%i0 = 0x00000001008b6000
perform_base_backup+0x1c0(...)
SendBaseBackup+0x5a0(...)
exec_replication_command+0x5b4(...)
PostgresMain+0x8f0(...)
That store is in the delay slot of the branch right after
sigsetjmp() in PG_TRY(), on its second return. Here is the
disassembly you asked for, at that spot:
227de22095 (before), frame 2224 bytes:
call sigsetjmp
...
bne,pn %icc,
st %o0, [ %fp + 0x57 ]
4a92c8c29b, frame 10368 bytes:
sethi %hi(0x1c00), %i0 ! in the prologue
...
call sigsetjmp
xor %i0, -837, %g1
add %g1, %fp, %g1
bne,pn %icc,
st %o0, [ %g1 ]
c97931c16d (fix), frame 2096 bytes: same as before.
With the 8kB array, the frame slots are more than 4kB from %fp, too
far for SPARC's 13-bit immediate offset, so GCC needs a register to
hold the offset. It keeps the constant 0x1c00 in %i0 across the
sigsetjmp() call and reuses it afterwards, and later in the PG_TRY
block it loads a string address into %i0. On SPARC, siglongjmp()
doesn't restore %l and %i registers from the jmp_buf; it resumes the
register window from the frame's save area, so %i0 comes back with
the value it had at siglongjmp() time. In the core, %i0 is the
string address instead of 0x1c00, and (0x1008b6000 ^ -837) + %fp is
exactly the faulting address. On other platforms jmp_buf saves the
callee-saved registers, so the constant would come back intact. The
garbage address explains SIGSEGV on billbug and the occasional
SIGBUS on thorntail, and why the WARNING from do_pg_abort_backup()
is never logged: the backend dies before the catch block runs.
The GCC RTL dumps show where it goes wrong. LRA does reload the
constant after the call, (set (reg %i0) (const_int 7168)), but
postreload's reload_cse_move2add() turns that into a no-op move,
because it thinks %i0 survives the call. cselib resets its state at
a REG_SETJMP call, but move2add has no such check, on GCC master as
well.
To rule out Postgres entirely, the attached sj.c/sj2.c is a small
program with no Postgres code: a function with an 8kB local array and
a sigsetjmp(). With gcc 13.4 -O2 it segfaults on this machine, and
it does the same when built with Debian's sparc64-linux-gnu-gcc-13
and run under qemu. It runs fine at -O1 and -O0, and with gcc 4.4.
I'm not sure if this has been, or should be, reported to GCC as yet.
Validation of c97931c16d on the same machine: with it applied, three
runs each of both reproductions give the WARNING and ERROR and no
crash, and t/010_pg_basebackup.pl passes all 168 tests with no
backend crash in the server logs. With it reverted, the same TAP run
logs the SIGSEGV. The test still reports PASS in that case because
it only checks that pg_basebackup fails.
I don't see a better fix on our side. A helper function with
the array would also work, but palloc0() is simpler, and both rely on
the same thing: no PG_TRY() frame larger than 4kB. In a core build
on this machine, the only other function with a frame that large that
calls sigsetjmp() is be_lo_export() (char buf[BUFSIZE]). Its code
after sigsetjmp() reloads the constant, so it is fine today, but only
by luck of register allocation. I haven't checked contrib or other
GCC versions or as mentioned earlier against their bug system.
best.
-greg
Re: Return pg_control from pg_backup_stop().
On Thu, Oct 29, 2026 at 06:11:39PM +0000, Burd, Greg wrote: > Validation of c97931c16d on the same machine: with it applied, three > runs each of both reproductions give the WARNING and ERROR and no > crash, and t/010_pg_basebackup.pl passes all 168 tests with no > backend crash in the server logs. With it reverted, the same TAP run > logs the SIGSEGV. The test still reports PASS in that case because > it only checks that pg_basebackup fails. Ah, thanks for the in-depth investigation. So the previous guess was really right. Candles work. > I don't see a better fix on our side. A helper function with > the array would also work, but palloc0() is simpler, and both rely on > the same thing: no PG_TRY() frame larger than 4kB. In a core build > on this machine, the only other function with a frame that large that > calls sigsetjmp() is be_lo_export() (char buf[BUFSIZE]). Its code > after sigsetjmp() reloads the constant, so it is fine today, but only > by luck of register allocation. I haven't checked contrib or other > GCC versions or as mentioned earlier against their bug system. Ugh. Even if the buffer itself is not used within the try/catch block itself? Getting entirely rid of the TRY/CATCH would lead to a much nicer result overall: we'd be able to keep the local buffer. Or perhaps go to the simpler solution of a palloc/pfree? I cannot believe that this extra allocation would matter for lo_export() calls. If you are proved to be right, it sounds to me that we ought to fix and backpatch that as well, especially if there is nothing we can do on the Postgres side. I don't believe in luck and magic when it comes to software programming. Thoughts? -- Michael
Re: Return pg_control from pg_backup_stop().
On 10/30/26 00:08, Michael Paquier wrote: > On Thu, Oct 29, 2026 at 06:11:39PM +0000, Burd, Greg wrote: >> Validation of c97931c16d on the same machine: with it applied, three >> runs each of both reproductions give the WARNING and ERROR and no >> crash, and t/010_pg_basebackup.pl passes all 168 tests with no >> backend crash in the server logs. With it reverted, the same TAP run >> logs the SIGSEGV. The test still reports PASS in that case because >> it only checks that pg_basebackup fails. > > Ah, thanks for the in-depth investigation. So the previous guess was > really right. Candles work. Thank you, Greg! This is exactly the confirmation we needed. I would encourage you to file a bug with gcc. I've found a few bugs and the gcc folks are really easy to work with. Since you have a repro it will be easy to produce the pre-processed output they want. One bad thing -- even if they fix the bug, distros may choose not to back patch it (so it may already be fixed). I had to maintain a patch for a gcc bug on ppc64le for years before the affected distros EOL'd. That bug limited where I could use const, which was pretty annoying. >> I don't see a better fix on our side. A helper function with >> the array would also work, but palloc0() is simpler, and both rely on >> the same thing: no PG_TRY() frame larger than 4kB. In a core build >> on this machine, the only other function with a frame that large that >> calls sigsetjmp() is be_lo_export() (char buf[BUFSIZE]). Its code >> after sigsetjmp() reloads the constant, so it is fine today, but only >> by luck of register allocation. I haven't checked contrib or other >> GCC versions or as mentioned earlier against their bug system. > > Ugh. Even if the buffer itself is not used within the try/catch > block itself? Apparently so -- the failure of the base backup cancel test showed that. The buffer was never used and just the larger frame caused the problem. > Getting entirely rid of the TRY/CATCH would lead to a > much nicer result overall: we'd be able to keep the local buffer. Or > perhaps go to the simpler solution of a palloc/pfree? I cannot > believe that this extra allocation would matter for lo_export() calls. palloc/pfree seems like the simpler solution. > If you are proved to be right, it sounds to me that we ought to > fix and backpatch that as well, especially if there is nothing we can > do on the Postgres side. I don't believe in luck and magic when it > comes to software programming. I've attached a patch for be_lo_export(). Working around compiler bugs is never fun but I'm not sure what choice we have. Regards, -David
Re: Return pg_control from pg_backup_stop().
От:
Greg Burd <greg@burd.me>
Дата:
On Thursday, October 29th, 2026 at 7:08 PM, Michael Paquier wrote: > On Thu, Oct 29, 2026 at 06:11:39PM +0000, Burd, Greg wrote: > > Validation of c97931c16d on the same machine: with it applied, three > > runs each of both reproductions give the WARNING and ERROR and no > > crash, and t/010_pg_basebackup.pl passes all 168 tests with no > > backend crash in the server logs. With it reverted, the same TAP run > > logs the SIGSEGV. The test still reports PASS in that case because > > it only checks that pg_basebackup fails. > > Ah, thanks for the in-depth investigation. Hey Michael, sure thing. It's why I bought the UltraSPARC 45, oddly enough! > So the previous guess was really right. Yes, you have a talent for many things including guessing fixes to obscure issues. > Candles work. Indeed they do. :) > > I don't see a better fix on our side. A helper function with > > the array would also work, but palloc0() is simpler, and both rely on > > the same thing: no PG_TRY() frame larger than 4kB. In a core build > > on this machine, the only other function with a frame that large that > > calls sigsetjmp() is be_lo_export() (char buf[BUFSIZE]). Its code > > after sigsetjmp() reloads the constant, so it is fine today, but only > > by luck of register allocation. I haven't checked contrib or other > > GCC versions or as mentioned earlier against their bug system. > > Ugh. Even if the buffer itself is not used within the try/catch > block itself? I could test if you'd like. > Getting entirely rid of the TRY/CATCH would lead to a > much nicer result overall: we'd be able to keep the local buffer. Or > perhaps go to the simpler solution of a palloc/pfree? I cannot > believe that this extra allocation would matter for lo_export() calls. > > If you are proved to be right, it sounds to me that we ought to > fix and backpatch that as well, especially if there is nothing we can > do on the Postgres side. Better safe than sorry. Even if I submit a bug to GCC and they fix it there it will take time to get that shipped and you have people who rarely update, etc. etc. > I don't believe in luck and magic when it comes to software programming. Nope, just 1s and 0s. > Thoughts? > -- > Michael best. -greg
Re: Return pg_control from pg_backup_stop().
От:
Greg Burd <greg@burd.me>
Дата:
On Friday, October 30th, 2026 at 4:34 AM, David Steele wrote: > On 10/30/26 00:08, Michael Paquier wrote: > > On Thu, Oct 29, 2026 at 06:11:39PM +0000, Burd, Greg wrote: > >> Validation of c97931c16d on the same machine: with it applied, three > >> runs each of both reproductions give the WARNING and ERROR and no > >> crash, and t/010_pg_basebackup.pl passes all 168 tests with no > >> backend crash in the server logs. With it reverted, the same TAP run > >> logs the SIGSEGV. The test still reports PASS in that case because > >> it only checks that pg_basebackup fails. > > > > Ah, thanks for the in-depth investigation. So the previous guess was > > really right. Candles work. > > Thank you, Greg! This is exactly the confirmation we needed. Hey David, happy to help. As I said to Michael, now I can write off that UltraSPARC 45 and the power it gobbles up on my taxes with proof that it is used for work. > I would encourage you to file a bug with gcc. I've found a few bugs and > the gcc folks are really easy to work with. Since you have a repro it > will be easy to produce the pre-processed output they want. Sure, I'll carve out some time to do that. > One bad thing -- even if they fix the bug, distros may choose not to > back patch it (so it may already be fixed). I had to maintain a patch > for a gcc bug on ppc64le for years before the affected distros EOL'd. > That bug limited where I could use const, which was pretty annoying. But that's the life of an open source developer, right? I signed up for it so I can't complain. Or if I do, you should ignore me. > >> I don't see a better fix on our side. A helper function with > >> the array would also work, but palloc0() is simpler, and both rely on > >> the same thing: no PG_TRY() frame larger than 4kB. In a core build > >> on this machine, the only other function with a frame that large that > >> calls sigsetjmp() is be_lo_export() (char buf[BUFSIZE]). Its code > >> after sigsetjmp() reloads the constant, so it is fine today, but only > >> by luck of register allocation. I haven't checked contrib or other > >> GCC versions or as mentioned earlier against their bug system. > > > > Ugh. Even if the buffer itself is not used within the try/catch > > block itself? > > Apparently so -- the failure of the base backup cancel test showed that. > The buffer was never used and just the larger frame caused the problem. I agree. > > Getting entirely rid of the TRY/CATCH would lead to a > > much nicer result overall: we'd be able to keep the local buffer. Or > > perhaps go to the simpler solution of a palloc/pfree? I cannot > > believe that this extra allocation would matter for lo_export() calls. > > palloc/pfree seems like the simpler solution. +1 > > If you are proved to be right, it sounds to me that we ought to > > fix and backpatch that as well, especially if there is nothing we can > > do on the Postgres side. I don't believe in luck and magic when it > > comes to software programming. > I've attached a patch for be_lo_export(). Working around compiler bugs > is never fun but I'm not sure what choice we have. I've not reviewed the patch yet, but it is on my queue. > Regards, > -David best. -greg
Re: Return pg_control from pg_backup_stop().
On Fri, Oct 30, 2026 at 04:04:13PM +0000, Greg Burd wrote: > On Friday, October 30th, 2026 at 4:34 AM, David Steele wrote: >> I've attached a patch for be_lo_export(). Working around compiler bugs >> is never fun but I'm not sure what choice we have. > > I've not reviewed the patch yet, but it is on my queue. Thanks. Making sure that be_lo_export() can be faulty on SPARC and also making sure that the palloc/pfree fixes it would be really useful. I'm not sure that I'm going to spend cycles spawning an instance just for this purpose. That does not feel like the most useful use of time with everything that is spawning on the lists these days. -- Michael