Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fb0b5b0

Browse files
committed
Track unowned relations in doubly-linked list
Relations dropped in a single transaction are tracked in a list of unowned relations. With large number of dropped relations this resulted in poor performance at the end of a transaction, when the relations are removed from the singly linked list one by one. Commit b416691 attempted to address this issue (particularly when it happens during recovery) by removing the relations in a reverse order, resulting in O(1) lookups in the list of unowned relations. This did not work reliably, though, and it was possible to trigger the O(N^2) behavior in various ways. Instead of trying to remove the relations in a specific order with respect to the linked list, which seems rather fragile, switch to a regular doubly linked. That allows us to remove relations cheaply no matter where in the list they are. As b416691 was a bugfix, backpatched to all supported versions, do the same thing here. Reviewed-by: Alvaro Herrera Discussion: https://www.postgresql.org/message-id/flat/80c27103-99e4-1d0c-642c-d9f3b94aaa0a%402ndquadrant.com Backpatch-through: 9.4
1 parent 7009f1a commit fb0b5b0

File tree

3 files changed

+20
-65
lines changed

3 files changed

+20
-65
lines changed

src/backend/storage/smgr/md.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,13 +1743,7 @@ DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo)
17431743

17441744
smgrdounlinkall(srels, ndelrels, isRedo);
17451745

1746-
/*
1747-
* Call smgrclose() in reverse order as when smgropen() is called.
1748-
* This trick enables remove_from_unowned_list() in smgrclose()
1749-
* to search the SMgrRelation from the unowned list,
1750-
* with O(1) performance.
1751-
*/
1752-
for (i = ndelrels - 1; i >= 0; i--)
1746+
for (i = 0; i < ndelrels; i++)
17531747
smgrclose(srels[i]);
17541748
pfree(srels);
17551749
}

src/backend/storage/smgr/smgr.c

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "postgres.h"
1919

2020
#include "commands/tablespace.h"
21+
#include "lib/ilist.h"
2122
#include "storage/bufmgr.h"
2223
#include "storage/ipc.h"
2324
#include "storage/smgr.h"
@@ -82,12 +83,10 @@ static const int NSmgr = lengthof(smgrsw);
8283
*/
8384
static HTAB *SMgrRelationHash = NULL;
8485

85-
static SMgrRelation first_unowned_reln = NULL;
86+
static dlist_head unowned_relns;
8687

8788
/* local function prototypes */
8889
static void smgrshutdown(int code, Datum arg);
89-
static void add_to_unowned_list(SMgrRelation reln);
90-
static void remove_from_unowned_list(SMgrRelation reln);
9190

9291

9392
/*
@@ -150,7 +149,7 @@ smgropen(RelFileNode rnode, BackendId backend)
150149
ctl.entrysize = sizeof(SMgrRelationData);
151150
SMgrRelationHash = hash_create("smgr relation table", 400,
152151
&ctl, HASH_ELEM | HASH_BLOBS);
153-
first_unowned_reln = NULL;
152+
dlist_init(&unowned_relns);
154153
}
155154

156155
/* Look up or create an entry */
@@ -177,7 +176,7 @@ smgropen(RelFileNode rnode, BackendId backend)
177176
reln->md_num_open_segs[forknum] = 0;
178177

179178
/* it has no owner yet */
180-
add_to_unowned_list(reln);
179+
dlist_push_tail(&unowned_relns, &reln->node);
181180
}
182181

183182
return reln;
@@ -207,7 +206,7 @@ smgrsetowner(SMgrRelation *owner, SMgrRelation reln)
207206
if (reln->smgr_owner)
208207
*(reln->smgr_owner) = NULL;
209208
else
210-
remove_from_unowned_list(reln);
209+
dlist_delete(&reln->node);
211210

212211
/* Now establish the ownership relationship. */
213212
reln->smgr_owner = owner;
@@ -231,53 +230,8 @@ smgrclearowner(SMgrRelation *owner, SMgrRelation reln)
231230
/* unset our reference to the owner */
232231
reln->smgr_owner = NULL;
233232

234-
add_to_unowned_list(reln);
235-
}
236-
237-
/*
238-
* add_to_unowned_list -- link an SMgrRelation onto the unowned list
239-
*
240-
* Check remove_from_unowned_list()'s comments for performance
241-
* considerations.
242-
*/
243-
static void
244-
add_to_unowned_list(SMgrRelation reln)
245-
{
246-
/* place it at head of the list (to make smgrsetowner cheap) */
247-
reln->next_unowned_reln = first_unowned_reln;
248-
first_unowned_reln = reln;
249-
}
250-
251-
/*
252-
* remove_from_unowned_list -- unlink an SMgrRelation from the unowned list
253-
*
254-
* If the reln is not present in the list, nothing happens. Typically this
255-
* would be caller error, but there seems no reason to throw an error.
256-
*
257-
* In the worst case this could be rather slow; but in all the cases that seem
258-
* likely to be performance-critical, the reln being sought will actually be
259-
* first in the list. Furthermore, the number of unowned relns touched in any
260-
* one transaction shouldn't be all that high typically. So it doesn't seem
261-
* worth expending the additional space and management logic needed for a
262-
* doubly-linked list.
263-
*/
264-
static void
265-
remove_from_unowned_list(SMgrRelation reln)
266-
{
267-
SMgrRelation *link;
268-
SMgrRelation cur;
269-
270-
for (link = &first_unowned_reln, cur = *link;
271-
cur != NULL;
272-
link = &cur->next_unowned_reln, cur = *link)
273-
{
274-
if (cur == reln)
275-
{
276-
*link = cur->next_unowned_reln;
277-
cur->next_unowned_reln = NULL;
278-
break;
279-
}
280-
}
233+
/* add to list of unowned relations */
234+
dlist_push_tail(&unowned_relns, &reln->node);
281235
}
282236

283237
/*
@@ -304,7 +258,7 @@ smgrclose(SMgrRelation reln)
304258
owner = reln->smgr_owner;
305259

306260
if (!owner)
307-
remove_from_unowned_list(reln);
261+
dlist_delete(&reln->node);
308262

309263
if (hash_search(SMgrRelationHash,
310264
(void *) &(reln->smgr_rnode),
@@ -797,13 +751,19 @@ smgrpostckpt(void)
797751
void
798752
AtEOXact_SMgr(void)
799753
{
754+
dlist_mutable_iter iter;
755+
800756
/*
801757
* Zap all unowned SMgrRelations. We rely on smgrclose() to remove each
802758
* one from the list.
803759
*/
804-
while (first_unowned_reln != NULL)
760+
dlist_foreach_modify(iter, &unowned_relns)
805761
{
806-
Assert(first_unowned_reln->smgr_owner == NULL);
807-
smgrclose(first_unowned_reln);
762+
SMgrRelation rel = dlist_container(SMgrRelationData, node,
763+
iter.cur);
764+
765+
Assert(rel->smgr_owner == NULL);
766+
767+
smgrclose(rel);
808768
}
809769
}

src/include/storage/smgr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define SMGR_H
1616

1717
#include "fmgr.h"
18+
#include "lib/ilist.h"
1819
#include "storage/block.h"
1920
#include "storage/relfilenode.h"
2021

@@ -72,7 +73,7 @@ typedef struct SMgrRelationData
7273
struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
7374

7475
/* if unowned, list link in list of all unowned SMgrRelations */
75-
struct SMgrRelationData *next_unowned_reln;
76+
dlist_node node;
7677
} SMgrRelationData;
7778

7879
typedef SMgrRelationData *SMgrRelation;

0 commit comments

Comments
 (0)