postgres.git / summary / log / commit / refs

commit    a6f4f407ada026007d37ab9d494bb6380ec12527
Author:   Amit Kapila <akapila@postgresql.org>
Date:     Thu Jan 02 06:58:02 2020 +0000
First released in: 13.0, 9.4.26, 9.5.21, 9.6.17, 10.12, 11.7, 12.2

    Fix running out of file descriptors for spill files.
    
    Currently while decoding changes, if the number of changes exceeds a
    certain threshold, we spill those to disk.  And this happens for each
    (sub)transaction.  Now, while reading all these files, we don't close them
    until we read all the files.  While reading these files, if the number of
    such files exceeds the maximum number of file descriptors, the operation
    errors out.
    
    Use PathNameOpenFile interface to open these files as that internally has
    the mechanism to release kernel FDs as needed to get us under the
    max_safe_fds limit.
    
    Reported-by: Amit Khandekar
    Author: Amit Khandekar
    Reviewed-by: Amit Kapila
    Backpatch-through: 9.4
    Discussion: https://postgr.es/m/CAJ3gD9c-sECEn79zXw4yBnBdOttacoE-6gAyP0oy60nfs_sabQ@mail.gmail.com


src/backend/replication/logical/reorderbuffer.c | 40 +++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index afe3d53d844..4bab9208965 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -102,7 +102,7 @@ typedef struct ReorderBufferIterTXNEntry XLogRecPtr lsn; ReorderBufferChange *change; ReorderBufferTXN *txn; - int fd; + File fd; XLogSegNo segno; } ReorderBufferIterTXNEntry; @@ -183,7 +183,8 @@ static void AssertTXNLsnOrder(ReorderBuffer *rb); * subtransactions * --------------------------------------- */ -static ReorderBufferIterTXNState *ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn); +static void ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn, + ReorderBufferIterTXNState *volatile *iter_state); static ReorderBufferChange * ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state); static void ReorderBufferIterTXNFinish(ReorderBuffer *rb, @@ -200,7 +201,7 @@ static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn); static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn, int fd, ReorderBufferChange *change); static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, - int *fd, XLogSegNo *segno); + File *fd, XLogSegNo *segno); static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn, char *change); static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn); @@ -944,15 +945,23 @@ ReorderBufferIterCompare(Datum a, Datum b, void *arg) /* * Allocate & initialize an iterator which iterates in lsn order over a * transaction and all its subtransactions. + * + * Note: The iterator state is returned through iter_state parameter rather + * than the function's return value. This is because the state gets cleaned up + * in a PG_CATCH block in the caller, so we want to make sure the caller gets + * back the state even if this function throws an exception. */ -static ReorderBufferIterTXNState * -ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn) +static void +ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn, + ReorderBufferIterTXNState *volatile *iter_state) { Size nr_txns = 0; ReorderBufferIterTXNState *state; dlist_iter cur_txn_i; int32 off; + *iter_state = NULL; + /* * Calculate the size of our heap: one element for every transaction that * contains changes. (Besides the transactions already in the reorder @@ -996,6 +1005,9 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn) ReorderBufferIterCompare, state); + /* Now that the state fields are initialized, it is safe to return it. */ + *iter_state = state; + /* * Now insert items into the binary heap, in an unordered fashion. (We * will run a heap assembly step at the end; this is more efficient.) @@ -1058,8 +1070,6 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn) /* assemble a valid binary heap */ binaryheap_build(state->heap); - - return state; } /* @@ -1163,7 +1173,7 @@ ReorderBufferIterTXNFinish(ReorderBuffer *rb, for (off = 0; off < state->nr_txns; off++) { if (state->entries[off].fd != -1) - CloseTransientFile(state->entries[off].fd); + FileClose(state->entries[off].fd); } /* free memory we might have "leaked" in the last *Next call */ @@ -1501,7 +1511,7 @@ ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid, rb->begin(rb, txn); - iterstate = ReorderBufferIterTXNInit(rb, txn); + ReorderBufferIterTXNInit(rb, txn, &iterstate); while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL) { Relation relation = NULL; @@ -2406,7 +2416,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn, */ static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, - int *fd, XLogSegNo *segno) + File *fd, XLogSegNo *segno) { Size restored = 0; XLogSegNo last_segno; @@ -2451,7 +2461,7 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid, *segno); - *fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0); + *fd = PathNameOpenFile(path, O_RDONLY | PG_BINARY, 0); if (*fd < 0 && errno == ENOENT) { *fd = -1; @@ -2472,12 +2482,12 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, * end of this file. */ ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange)); - readBytes = read(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange)); + readBytes = FileRead(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange)); /* eof */ if (readBytes == 0) { - CloseTransientFile(*fd); + FileClose(*fd); *fd = -1; (*segno)++; continue; @@ -2499,8 +2509,8 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, sizeof(ReorderBufferDiskChange) + ondisk->size); ondisk = (ReorderBufferDiskChange *) rb->outbuf; - readBytes = read(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange), - ondisk->size - sizeof(ReorderBufferDiskChange)); + readBytes = FileRead(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange), + ondisk->size - sizeof(ReorderBufferDiskChange)); if (readBytes < 0) ereport(ERROR, [parent: 78b381c5ddad]