1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-07 11:02:12 +03:00

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
This commit is contained in:
Amit Kapila 2020-01-02 12:28:02 +05:30
parent ce758a3d75
commit 1ad47e8757

View File

@ -101,7 +101,7 @@ typedef struct ReorderBufferIterTXNEntry
XLogRecPtr lsn; XLogRecPtr lsn;
ReorderBufferChange *change; ReorderBufferChange *change;
ReorderBufferTXN *txn; ReorderBufferTXN *txn;
int fd; File fd;
XLogSegNo segno; XLogSegNo segno;
} ReorderBufferIterTXNEntry; } ReorderBufferIterTXNEntry;
@ -182,7 +182,8 @@ static void AssertTXNLsnOrder(ReorderBuffer *rb);
* subtransactions * subtransactions
* --------------------------------------- * ---------------------------------------
*/ */
static ReorderBufferIterTXNState *ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn); static void ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn,
ReorderBufferIterTXNState *volatile *iter_state);
static ReorderBufferChange * static ReorderBufferChange *
ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state); ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state);
static void ReorderBufferIterTXNFinish(ReorderBuffer *rb, static void ReorderBufferIterTXNFinish(ReorderBuffer *rb,
@ -199,7 +200,7 @@ static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn, static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
int fd, ReorderBufferChange *change); int fd, ReorderBufferChange *change);
static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
int *fd, XLogSegNo *segno); File *fd, XLogSegNo *segno);
static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn, static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
char *change); char *change);
static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn); static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn);
@ -942,15 +943,23 @@ ReorderBufferIterCompare(Datum a, Datum b, void *arg)
/* /*
* Allocate & initialize an iterator which iterates in lsn order over a * Allocate & initialize an iterator which iterates in lsn order over a
* transaction and all its subtransactions. * 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 * static void
ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn) ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn,
ReorderBufferIterTXNState *volatile *iter_state)
{ {
Size nr_txns = 0; Size nr_txns = 0;
ReorderBufferIterTXNState *state; ReorderBufferIterTXNState *state;
dlist_iter cur_txn_i; dlist_iter cur_txn_i;
int32 off; int32 off;
*iter_state = NULL;
/* /*
* Calculate the size of our heap: one element for every transaction that * Calculate the size of our heap: one element for every transaction that
* contains changes. (Besides the transactions already in the reorder * contains changes. (Besides the transactions already in the reorder
@ -994,6 +1003,9 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
ReorderBufferIterCompare, ReorderBufferIterCompare,
state); 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 * 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.) * will run a heap assembly step at the end; this is more efficient.)
@ -1056,8 +1068,6 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
/* assemble a valid binary heap */ /* assemble a valid binary heap */
binaryheap_build(state->heap); binaryheap_build(state->heap);
return state;
} }
/* /*
@ -1161,7 +1171,7 @@ ReorderBufferIterTXNFinish(ReorderBuffer *rb,
for (off = 0; off < state->nr_txns; off++) for (off = 0; off < state->nr_txns; off++)
{ {
if (state->entries[off].fd != -1) 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 */ /* free memory we might have "leaked" in the last *Next call */
@ -1496,7 +1506,7 @@ ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
rb->begin(rb, txn); rb->begin(rb, txn);
iterstate = ReorderBufferIterTXNInit(rb, txn); ReorderBufferIterTXNInit(rb, txn, &iterstate);
while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL) while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
{ {
Relation relation = NULL; Relation relation = NULL;
@ -2338,7 +2348,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
*/ */
static Size static Size
ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn, ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
int *fd, XLogSegNo *segno) File *fd, XLogSegNo *segno)
{ {
Size restored = 0; Size restored = 0;
XLogSegNo last_segno; XLogSegNo last_segno;
@ -2383,7 +2393,7 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid, ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
*segno); *segno);
*fd = OpenTransientFile(path, O_RDONLY | PG_BINARY, 0); *fd = PathNameOpenFile(path, O_RDONLY | PG_BINARY, 0);
if (*fd < 0 && errno == ENOENT) if (*fd < 0 && errno == ENOENT)
{ {
*fd = -1; *fd = -1;
@ -2404,12 +2414,12 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
* end of this file. * end of this file.
*/ */
ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange)); ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange));
readBytes = read(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange)); readBytes = FileRead(*fd, rb->outbuf, sizeof(ReorderBufferDiskChange));
/* eof */ /* eof */
if (readBytes == 0) if (readBytes == 0)
{ {
CloseTransientFile(*fd); FileClose(*fd);
*fd = -1; *fd = -1;
(*segno)++; (*segno)++;
continue; continue;
@ -2431,7 +2441,7 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
sizeof(ReorderBufferDiskChange) + ondisk->size); sizeof(ReorderBufferDiskChange) + ondisk->size);
ondisk = (ReorderBufferDiskChange *) rb->outbuf; ondisk = (ReorderBufferDiskChange *) rb->outbuf;
readBytes = read(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange), readBytes = FileRead(*fd, rb->outbuf + sizeof(ReorderBufferDiskChange),
ondisk->size - sizeof(ReorderBufferDiskChange)); ondisk->size - sizeof(ReorderBufferDiskChange));
if (readBytes < 0) if (readBytes < 0)