From 89fd977a8a7cb90b9d85f6e9386507a2f7997604 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 6 Apr 2020 21:28:55 -0700
Subject: [PATCH v7 03/11] Fix xlogreader fd leak encountered with twophase
 commit.

This perhaps is not the best fix, but it's better than the current
situation of failing after a few commits.

This issue appeared after 0dc8ead46, but only because before that
change fd leakage was limited to a single file descriptor.

Discussion: https://postgr.es/m/20200406025651.fpzdb5yyb7qyhqko@alap3.anarazel.de
---
 src/backend/access/transam/xlogreader.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c
index f3fea5132fe..79ff976474c 100644
--- a/src/backend/access/transam/xlogreader.c
+++ b/src/backend/access/transam/xlogreader.c
@@ -136,6 +136,9 @@ XLogReaderFree(XLogReaderState *state)
 {
 	int			block_id;
 
+	if (state->seg.ws_file != -1)
+		close(state->seg.ws_file);
+
 	for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
 	{
 		if (state->blocks[block_id].data)
-- 
2.25.0.114.g5b0ca878e0

