Skip to content

Commit 887a7c6

Browse files
committed
Get rid of slru.c's hardwired insistence on a fixed number of slots per
SLRU area. The number of slots is still a compile-time constant (someday we might want to change that), but at least it's a different constant for each SLRU area. Increase number of subtrans buffers to 32 based on experimentation with a heavily subtrans-bashing test case, and increase number of multixact member buffers to 16, since it's obviously silly for it not to be at least twice the number of multixact offset buffers.
1 parent 3001b46 commit 887a7c6

File tree

9 files changed

+118
-65
lines changed

9 files changed

+118
-65
lines changed

src/backend/access/transam/clog.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
2525
* Portions Copyright (c) 1994, Regents of the University of California
2626
*
27-
* $PostgreSQL: pgsql/src/backend/access/transam/clog.c,v 1.35 2005/12/06 18:10:06 tgl Exp $
27+
* $PostgreSQL: pgsql/src/backend/access/transam/clog.c,v 1.36 2005/12/06 23:08:32 tgl Exp $
2828
*
2929
*-------------------------------------------------------------------------
3030
*/
@@ -147,14 +147,15 @@ TransactionIdGetStatus(TransactionId xid)
147147
Size
148148
CLOGShmemSize(void)
149149
{
150-
return SimpleLruShmemSize();
150+
return SimpleLruShmemSize(NUM_CLOG_BUFFERS);
151151
}
152152

153153
void
154154
CLOGShmemInit(void)
155155
{
156156
ClogCtl->PagePrecedes = CLOGPagePrecedes;
157-
SimpleLruInit(ClogCtl, "CLOG Ctl", CLogControlLock, "pg_clog");
157+
SimpleLruInit(ClogCtl, "CLOG Ctl", NUM_CLOG_BUFFERS,
158+
CLogControlLock, "pg_clog");
158159
}
159160

160161
/*

src/backend/access/transam/multixact.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
4343
* Portions Copyright (c) 1994, Regents of the University of California
4444
*
45-
* $PostgreSQL: pgsql/src/backend/access/transam/multixact.c,v 1.14 2005/12/06 18:10:06 tgl Exp $
45+
* $PostgreSQL: pgsql/src/backend/access/transam/multixact.c,v 1.15 2005/12/06 23:08:32 tgl Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -1249,8 +1249,8 @@ MultiXactShmemSize(void)
12491249
mul_size(sizeof(MultiXactId) * 2, MaxBackends))
12501250

12511251
size = SHARED_MULTIXACT_STATE_SIZE;
1252-
size = add_size(size, SimpleLruShmemSize());
1253-
size = add_size(size, SimpleLruShmemSize());
1252+
size = add_size(size, SimpleLruShmemSize(NUM_MXACTOFFSET_BUFFERS));
1253+
size = add_size(size, SimpleLruShmemSize(NUM_MXACTMEMBER_BUFFERS));
12541254

12551255
return size;
12561256
}
@@ -1265,9 +1265,11 @@ MultiXactShmemInit(void)
12651265
MultiXactOffsetCtl->PagePrecedes = MultiXactOffsetPagePrecedes;
12661266
MultiXactMemberCtl->PagePrecedes = MultiXactMemberPagePrecedes;
12671267

1268-
SimpleLruInit(MultiXactOffsetCtl, "MultiXactOffset Ctl",
1268+
SimpleLruInit(MultiXactOffsetCtl,
1269+
"MultiXactOffset Ctl", NUM_MXACTOFFSET_BUFFERS,
12691270
MultiXactOffsetControlLock, "pg_multixact/offsets");
1270-
SimpleLruInit(MultiXactMemberCtl, "MultiXactMember Ctl",
1271+
SimpleLruInit(MultiXactMemberCtl,
1272+
"MultiXactMember Ctl", NUM_MXACTMEMBER_BUFFERS,
12711273
MultiXactMemberControlLock, "pg_multixact/members");
12721274

12731275
/* Initialize our shared state struct */

src/backend/access/transam/slru.c

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
4242
* Portions Copyright (c) 1994, Regents of the University of California
4343
*
44-
* $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.32 2005/12/06 18:10:06 tgl Exp $
44+
* $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.33 2005/12/06 23:08:32 tgl Exp $
4545
*
4646
*-------------------------------------------------------------------------
4747
*/
@@ -87,11 +87,13 @@
8787
* until control returns to SimpleLruFlush(). This data structure remembers
8888
* which files are open.
8989
*/
90+
#define MAX_FLUSH_BUFFERS 16
91+
9092
typedef struct SlruFlushData
9193
{
92-
int num_files; /* # files actually open */
93-
int fd[NUM_SLRU_BUFFERS]; /* their FD's */
94-
int segno[NUM_SLRU_BUFFERS]; /* their log seg#s */
94+
int num_files; /* # files actually open */
95+
int fd[MAX_FLUSH_BUFFERS]; /* their FD's */
96+
int segno[MAX_FLUSH_BUFFERS]; /* their log seg#s */
9597
} SlruFlushData;
9698

9799
/*
@@ -150,25 +152,38 @@ static int SlruSelectLRUPage(SlruCtl ctl, int pageno);
150152
*/
151153

152154
Size
153-
SimpleLruShmemSize(void)
155+
SimpleLruShmemSize(int nslots)
154156
{
155-
/* we assume NUM_SLRU_BUFFERS isn't so large as to risk overflow */
156-
return BUFFERALIGN(sizeof(SlruSharedData)) + BLCKSZ * NUM_SLRU_BUFFERS;
157+
Size sz;
158+
159+
/* we assume nslots isn't so large as to risk overflow */
160+
sz = MAXALIGN(sizeof(SlruSharedData));
161+
sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
162+
sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
163+
sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
164+
sz += MAXALIGN(nslots * sizeof(int)); /* page_number[] */
165+
sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
166+
sz += MAXALIGN(nslots * sizeof(LWLockId)); /* buffer_locks[] */
167+
168+
return BUFFERALIGN(sz) + BLCKSZ * nslots;
157169
}
158170

159171
void
160-
SimpleLruInit(SlruCtl ctl, const char *name,
172+
SimpleLruInit(SlruCtl ctl, const char *name, int nslots,
161173
LWLockId ctllock, const char *subdir)
162174
{
163175
SlruShared shared;
164176
bool found;
165177

166-
shared = (SlruShared) ShmemInitStruct(name, SimpleLruShmemSize(), &found);
178+
shared = (SlruShared) ShmemInitStruct(name,
179+
SimpleLruShmemSize(nslots),
180+
&found);
167181

168182
if (!IsUnderPostmaster)
169183
{
170184
/* Initialize locks and shared memory area */
171-
char *bufptr;
185+
char *ptr;
186+
Size offset;
172187
int slotno;
173188

174189
Assert(!found);
@@ -177,19 +192,37 @@ SimpleLruInit(SlruCtl ctl, const char *name,
177192

178193
shared->ControlLock = ctllock;
179194

180-
bufptr = (char *) shared + BUFFERALIGN(sizeof(SlruSharedData));
195+
shared->num_slots = nslots;
196+
197+
shared->cur_lru_count = 0;
198+
199+
/* shared->latest_page_number will be set later */
181200

182-
for (slotno = 0; slotno < NUM_SLRU_BUFFERS; slotno++)
201+
ptr = (char *) shared;
202+
offset = MAXALIGN(sizeof(SlruSharedData));
203+
shared->page_buffer = (char **) (ptr + offset);
204+
offset += MAXALIGN(nslots * sizeof(char *));
205+
shared->page_status = (SlruPageStatus *) (ptr + offset);
206+
offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
207+
shared->page_dirty = (bool *) (ptr + offset);
208+
offset += MAXALIGN(nslots * sizeof(bool));
209+
shared->page_number = (int *) (ptr + offset);
210+
offset += MAXALIGN(nslots * sizeof(int));
211+
shared->page_lru_count = (int *) (ptr + offset);
212+
offset += MAXALIGN(nslots * sizeof(int));
213+
shared->buffer_locks = (LWLockId *) (ptr + offset);
214+
offset += MAXALIGN(nslots * sizeof(LWLockId));
215+
ptr += BUFFERALIGN(offset);
216+
217+
for (slotno = 0; slotno < nslots; slotno++)
183218
{
184-
shared->page_buffer[slotno] = bufptr;
219+
shared->page_buffer[slotno] = ptr;
185220
shared->page_status[slotno] = SLRU_PAGE_EMPTY;
186221
shared->page_dirty[slotno] = false;
187222
shared->page_lru_count[slotno] = 0;
188223
shared->buffer_locks[slotno] = LWLockAssign();
189-
bufptr += BLCKSZ;
224+
ptr += BLCKSZ;
190225
}
191-
192-
/* shared->latest_page_number will be set later */
193226
}
194227
else
195228
Assert(found);
@@ -394,7 +427,7 @@ SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
394427
LWLockAcquire(shared->ControlLock, LW_SHARED);
395428

396429
/* See if page is already in a buffer */
397-
for (slotno = 0; slotno < NUM_SLRU_BUFFERS; slotno++)
430+
for (slotno = 0; slotno < shared->num_slots; slotno++)
398431
{
399432
if (shared->page_number[slotno] == pageno &&
400433
shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
@@ -643,9 +676,20 @@ SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruFlush fdata)
643676

644677
if (fdata)
645678
{
646-
fdata->fd[fdata->num_files] = fd;
647-
fdata->segno[fdata->num_files] = segno;
648-
fdata->num_files++;
679+
if (fdata->num_files < MAX_FLUSH_BUFFERS)
680+
{
681+
fdata->fd[fdata->num_files] = fd;
682+
fdata->segno[fdata->num_files] = segno;
683+
fdata->num_files++;
684+
}
685+
else
686+
{
687+
/*
688+
* In the unlikely event that we exceed MAX_FLUSH_BUFFERS,
689+
* fall back to treating it as a standalone write.
690+
*/
691+
fdata = NULL;
692+
}
649693
}
650694
}
651695

@@ -797,7 +841,7 @@ SlruSelectLRUPage(SlruCtl ctl, int pageno)
797841
int best_page_number;
798842

799843
/* See if page already has a buffer assigned */
800-
for (slotno = 0; slotno < NUM_SLRU_BUFFERS; slotno++)
844+
for (slotno = 0; slotno < shared->num_slots; slotno++)
801845
{
802846
if (shared->page_number[slotno] == pageno &&
803847
shared->page_status[slotno] != SLRU_PAGE_EMPTY)
@@ -830,7 +874,7 @@ SlruSelectLRUPage(SlruCtl ctl, int pageno)
830874
best_delta = -1;
831875
bestslot = 0; /* no-op, just keeps compiler quiet */
832876
best_page_number = 0; /* ditto */
833-
for (slotno = 0; slotno < NUM_SLRU_BUFFERS; slotno++)
877+
for (slotno = 0; slotno < shared->num_slots; slotno++)
834878
{
835879
int this_delta;
836880
int this_page_number;
@@ -908,7 +952,7 @@ SimpleLruFlush(SlruCtl ctl, bool checkpoint)
908952

909953
LWLockAcquire(shared->ControlLock, LW_EXCLUSIVE);
910954

911-
for (slotno = 0; slotno < NUM_SLRU_BUFFERS; slotno++)
955+
for (slotno = 0; slotno < shared->num_slots; slotno++)
912956
{
913957
SimpleLruWritePage(ctl, slotno, &fdata);
914958

@@ -990,7 +1034,7 @@ restart:;
9901034
return;
9911035
}
9921036

993-
for (slotno = 0; slotno < NUM_SLRU_BUFFERS; slotno++)
1037+
for (slotno = 0; slotno < shared->num_slots; slotno++)
9941038
{
9951039
if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
9961040
continue;

src/backend/access/transam/subtrans.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
2323
* Portions Copyright (c) 1994, Regents of the University of California
2424
*
25-
* $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.14 2005/12/06 18:10:06 tgl Exp $
25+
* $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.15 2005/12/06 23:08:32 tgl Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -164,14 +164,14 @@ SubTransGetTopmostTransaction(TransactionId xid)
164164
Size
165165
SUBTRANSShmemSize(void)
166166
{
167-
return SimpleLruShmemSize();
167+
return SimpleLruShmemSize(NUM_SUBTRANS_BUFFERS);
168168
}
169169

170170
void
171171
SUBTRANSShmemInit(void)
172172
{
173173
SubTransCtl->PagePrecedes = SubTransPagePrecedes;
174-
SimpleLruInit(SubTransCtl, "SUBTRANS Ctl",
174+
SimpleLruInit(SubTransCtl, "SUBTRANS Ctl", NUM_SUBTRANS_BUFFERS,
175175
SubtransControlLock, "pg_subtrans");
176176
/* Override default assumption that writes should be fsync'd */
177177
SubTransCtl->do_fsync = false;

src/backend/storage/lmgr/lwlock.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.34 2005/10/15 02:49:26 momjian Exp $
18+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.35 2005/12/06 23:08:33 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
2222
#include "postgres.h"
2323

24-
#include "access/slru.h"
24+
#include "access/clog.h"
25+
#include "access/multixact.h"
26+
#include "access/subtrans.h"
2527
#include "storage/lwlock.h"
2628
#include "storage/proc.h"
2729
#include "storage/spin.h"
@@ -129,16 +131,13 @@ NumLWLocks(void)
129131
numLocks += 2 * NBuffers;
130132

131133
/* clog.c needs one per CLOG buffer */
132-
numLocks += NUM_SLRU_BUFFERS;
134+
numLocks += NUM_CLOG_BUFFERS;
133135

134136
/* subtrans.c needs one per SubTrans buffer */
135-
numLocks += NUM_SLRU_BUFFERS;
137+
numLocks += NUM_SUBTRANS_BUFFERS;
136138

137-
/*
138-
* multixact.c needs one per MultiXact buffer, but there are two SLRU
139-
* areas for MultiXact
140-
*/
141-
numLocks += 2 * NUM_SLRU_BUFFERS;
139+
/* multixact.c needs two SLRU areas */
140+
numLocks += NUM_MXACTOFFSET_BUFFERS + NUM_MXACTMEMBER_BUFFERS;
142141

143142
/* Leave a few extra for use by user-defined modules. */
144143
numLocks += NUM_USER_DEFINED_LWLOCKS;

src/include/access/clog.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/access/clog.h,v 1.14 2005/08/20 23:26:29 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/access/clog.h,v 1.15 2005/12/06 23:08:34 tgl Exp $
1010
*/
1111
#ifndef CLOG_H
1212
#define CLOG_H
@@ -28,6 +28,10 @@ typedef int XidStatus;
2828
#define TRANSACTION_STATUS_SUB_COMMITTED 0x03
2929

3030

31+
/* Number of SLRU buffers to use for clog */
32+
#define NUM_CLOG_BUFFERS 8
33+
34+
3135
extern void TransactionIdSetStatus(TransactionId xid, XidStatus status);
3236
extern XidStatus TransactionIdGetStatus(TransactionId xid);
3337

src/include/access/multixact.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/access/multixact.h,v 1.7 2005/10/15 02:49:42 momjian Exp $
9+
* $PostgreSQL: pgsql/src/include/access/multixact.h,v 1.8 2005/12/06 23:08:34 tgl Exp $
1010
*/
1111
#ifndef MULTIXACT_H
1212
#define MULTIXACT_H
@@ -18,6 +18,10 @@
1818

1919
#define MultiXactIdIsValid(multi) ((multi) != InvalidMultiXactId)
2020

21+
/* Number of SLRU buffers to use for multixact */
22+
#define NUM_MXACTOFFSET_BUFFERS 8
23+
#define NUM_MXACTMEMBER_BUFFERS 16
24+
2125
/* ----------------
2226
* multixact-related XLOG entries
2327
* ----------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy