pgsql: Clean up error handling in pg_basebackup's walmethods.c. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Clean up error handling in pg_basebackup's walmethods.c.
Date
Msg-id E1mnQQV-0000HM-TX@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Clean up error handling in pg_basebackup's walmethods.c.

The error handling here was a mess, as a result of a fundamentally
bad design (relying on errno to keep its value much longer than is
safe to assume) as well as a lot of just plain sloppiness, both as
to noticing errors at all and as to reporting the correct errno.
Moreover, the recent addition of LZ4 compression broke things
completely, because liblz4 doesn't use errno to report errors.

To improve matters, keep the error state in the DirectoryMethodData or
TarMethodData struct, and add a string field so we can handle cases
that don't set errno.  (The tar methods already had a version of this,
but it can be done more efficiently since all these cases use a
constant error string.)  Make the dir and tar methods handle errors
in basically identical ways, which they didn't before.

This requires copying errno into the state struct in a lot of places,
which is a bit tedious, but it has the virtue that we can get rid of
ad-hoc code to save and restore errno in a number of places ... not
to mention that it fixes other places that should've saved/restored
errno but neglected to.

In passing, fix some pointlessly static buffers to be ordinary
local variables.

There remains an issue about exactly how to handle errors from
fsync(), but that seems like material for its own patch.

While the LZ4 problems are new, all the rest of this is fixes for
old bugs, so backpatch to v10 where walmethods.c was introduced.

Patch by me; thanks to Michael Paquier for review.

Discussion: https://postgr.es/m/1343113.1636489231@sss.pgh.pa.us

Branch
------
REL_12_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8378dad4c579438f40c42b8514f2837c38094ec9

Modified Files
--------------
src/bin/pg_basebackup/walmethods.c | 200 ++++++++++++++++++++++++++-----------
1 file changed, 139 insertions(+), 61 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Handle close() failures more robustly in pg_dump and pg_baseback
Next
From: Tom Lane
Date:
Subject: pgsql: Add a planner support function for starts_with().