Re: pg_rewind does not rewind diverging timelines

From: Tatsuya Kawata <kawatatatsuya0913(at)gmail(dot)com>
To: Mats Kindahl <mats(dot)kindahl(at)gmail(dot)com>
Cc: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: pg_rewind does not rewind diverging timelines
Date: 2026-07-26 09:57:39
Message-ID: CAHza6qczQXOwEyqNX2rq5UNo2XQb_bWY33F=9sNXzVPr6DztMA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Mats-san, Zsolt-san,

Thanks -- I went through both v7 and the new version.

> + PG_CATCH();
> + {
> + ErrorData *edata = CopyErrorData();
> +
> + FlushErrorState();
> + ereport(FATAL,
> + errmsg("invalid UUID in
history file \"%s\"", path),
> + errdetail("%s",
edata->message));
> + }
>
> This is missing a MemoryContextSwitchTo before CopyErrorData, and
> results in an assertion with debug builds.

> Thank you for reviewing this and sorry for the delay. I have attached a
> new version with the issues you pointed to handled. See comments inline
> below.

The context-switch
fix in readTimeLineHistory() (restoring the caller's context before
CopyErrorData()) looks correct to me.

One note: the original problem was not only a debug-build assertion. On
non-assert builds CopyErrorData() allocates the ErrorData in ErrorContext,
FlushErrorState() then frees it, and the following
errdetail("%s", edata->message) reads freed memory -- a use-after-free that
can crash a production server, not just trip an Assert(). Your fix already
covers this; I'm just sharing it since it bears on the severity.

One minor point: on an invalid UUID the backend FATALs while the frontend
(pg_rewind) silently treats it as "unknown" (all-zero) -- probably
intentional, just flagging it. And should you ever want to drop the
PG_TRY/PG_CATCH here, uuid_in supports soft errors, so a
DirectInputFunctionCallSafe() call with an ErrorSaveContext would avoid
CopyErrorData()/FlushErrorState() and the context switch entirely -- i.e.
it removes the very handling that had to be fixed here, so this class of
mistake can't recur. The current fix is correct and minimal, so this is
purely optional.

Regards,
Tatsuya Kawata

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Thom Brown 2026-07-26 09:49:16 Re: remove_useless_joins vs. bug #19560