From: Alvaro Herrera Date: Fri, 19 Aug 2016 17:38:55 +0000 (-0300) Subject: reorderbuffer: preserve errno while reporting error X-Git-Tag: REL9_6_RC1~25 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0440f49523fb88a3ecbc1672d8fcee20197c5c40;p=postgresql reorderbuffer: preserve errno while reporting error Clobbering errno during cleanup after an error is an oft-repeated, easy to make mistake. Deal with it here as everywhere else, by saving it aside and restoring after cleanup, before ereport'ing. In passing, add a missing errcode declaration in another ereport() call in the same file, which I noticed while skimming the file looking for similar problems. Backpatch to 9.4, where this code was introduced. --- diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 9594b1c671..213ce34674 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -2317,7 +2317,10 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn, if (write(fd, rb->outbuf, ondisk->size) != ondisk->size) { + int save_errno = errno; + CloseTransientFile(fd); + errno = save_errno; ereport(ERROR, (errcode_for_file_access(), errmsg("could not write to data file for XID %u: %m", @@ -3070,7 +3073,8 @@ ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname) fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0); if (fd < 0) ereport(ERROR, - (errmsg("could not open file \"%s\": %m", path))); + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", path))); while (true) {