pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feed_mdfd_getseg can be expensive
25+ messages / 4 participants
[nested] [flat]
* _mdfd_getseg can be expensive
@ 2014-03-31 10:10 Andres Freund <andres@2ndquadrant.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2014-03-31 10:10 UTC (permalink / raw)
To: pgsql-hackers
Hi,
I recently have seen some perf profiles in which _mdfd_getseg() was in
the top #3 when VACUUMing large (~200GB) relations. Called by mdread(),
mdwrite(). Looking at it's implementation, I am not surprised. It
iterates over all segment entries a relations has; for every read or
write. That's not painful for smaller relations, but at a couple of
hundred GB it starts to be annoying. Especially if kernel readahead has
already read in all data from disk.
I don't have a good idea what to do about this yet, but it seems like
something that should be fixed mid-term.
The best I can come up is is caching the last mdvec used, but that's
fairly ugly. Alternatively it might be a good idea to not store MdfdVec
as a linked list, but as a densely allocated array.
Greetings,
Andres Freund
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2014-10-31 22:32 Andres Freund <andres@2ndquadrant.com>
parent: Andres Freund <andres@2ndquadrant.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2014-10-31 22:32 UTC (permalink / raw)
To: pgsql-hackers
Hi,
On 2014-03-31 12:10:01 +0200, Andres Freund wrote:
> I recently have seen some perf profiles in which _mdfd_getseg() was in
> the top #3 when VACUUMing large (~200GB) relations. Called by mdread(),
> mdwrite(). Looking at it's implementation, I am not surprised. It
> iterates over all segment entries a relations has; for every read or
> write. That's not painful for smaller relations, but at a couple of
> hundred GB it starts to be annoying. Especially if kernel readahead has
> already read in all data from disk.
>
> I don't have a good idea what to do about this yet, but it seems like
> something that should be fixed mid-term.
>
> The best I can come up is is caching the last mdvec used, but that's
> fairly ugly. Alternatively it might be a good idea to not store MdfdVec
> as a linked list, but as a densely allocated array.
I've seen this a couple times more since. On larger relations it gets
even more pronounced. When sequentially scanning a 2TB relation,
_mdfd_getseg() gets up to 80% proportionate CPU time towards the end of
the scan.
I wrote the attached patch that get rids of that essentially quadratic
behaviour, by replacing the mdfd chain/singly linked list with an
array. Since we seldomly grow files by a whole segment I can't see the
slightly bigger memory reallocations matter significantly. In pretty
much every other case the array is bound to be a winner.
Does anybody have fundamental arguments against that idea?
With some additional work we could save a bit more memory by getting rid
of the mdfd_segno as it's essentially redundant - but that's not
entirely trivial and I'm unsure if it's worth it.
I've also attached a second patch that makes PageIsVerified() noticeably
faster when the page is new. That's helpful and related because it makes
it easier to test the correctness of the md.c rewrite by faking full 1GB
segments. It's also pretty simple, so there's imo little reason not to
do it.
Greetings,
Andres Freund
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments: download all (2 files, .tar.gz)
0001-Faster-PageIsVerified-for-the-all-zeroes-case.patch (text/x-patch, 1.4K) download
From f7bf4813eeee7d5c3c4268e0fb6231ca6996d9fe Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 30 Oct 2014 13:24:20 +0100
Subject: [PATCH 1/2] Faster PageIsVerified() for the all zeroes case.
---
src/backend/storage/page/bufpage.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index 6351a9b..c175ed0 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -81,7 +81,7 @@ bool
PageIsVerified(Page page, BlockNumber blkno)
{
PageHeader p = (PageHeader) page;
- char *pagebytes;
+ size_t *pagebytes;
int i;
bool checksum_failure = false;
bool header_sane = false;
@@ -118,10 +118,17 @@ PageIsVerified(Page page, BlockNumber blkno)
return true;
}
- /* Check all-zeroes case */
+ /*
+ * Check all-zeroes case. Luckily BLCKSZ is guaranteed to always be a
+ * multiple of size_t - and it's much faster compare memory using the
+ * processor's native word size.
+ */
+ StaticAssertStmt(BLCKSZ == (BLCKSZ / sizeof(size_t)) * sizeof(size_t),
+ "BLCKSZ has to be a multiple of sizeof(size_t)");
+
all_zeroes = true;
- pagebytes = (char *) page;
- for (i = 0; i < BLCKSZ; i++)
+ pagebytes = (size_t *) page;
+ for (i = 0; i < (BLCKSZ / sizeof(size_t)); i++)
{
if (pagebytes[i] != 0)
{
--
2.0.0.rc2.4.g1dc51c6.dirty
0002-Much-faster-O-1-mfdvec.patch (text/x-patch, 17.1K) download
From 5bc40c11081a0d034f2857b45cb3d45c5b63fa58 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 30 Oct 2014 13:24:52 +0100
Subject: [PATCH 2/2] Much faster/O(1) mfdvec.
---
src/backend/storage/smgr/md.c | 307 +++++++++++++++++++++-------------------
src/backend/storage/smgr/smgr.c | 4 +-
src/include/storage/smgr.h | 4 +-
3 files changed, 166 insertions(+), 149 deletions(-)
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index 167d61c..6a7afa8 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -92,27 +92,22 @@
* out to an unlinked old copy of a segment file that will eventually
* disappear.
*
- * The file descriptor pointer (md_fd field) stored in the SMgrRelation
- * cache is, therefore, just the head of a list of MdfdVec objects, one
- * per segment. But note the md_fd pointer can be NULL, indicating
- * relation not open.
+ * File descriptors are stored in the md_seg_fds arrays inside
+ * SMgrRelation. The length of these arrays is stored in md_seg_no. Note
+ * that md_nfd having a specific value does not necessarily mean the relation
+ * doesn't have additional segments; we may just not have opened the next
+ * segment yet. (We could not have "all segments are in the array" as an
+ * invariant anyway, since another backend could extend the relation when we
+ * weren't looking.) We do not have entries for inactive segments, however;
+ * as soon as we find a partial segment, we assume that any subsequent
+ * segments are inactive.
*
- * Also note that mdfd_chain == NULL does not necessarily mean the relation
- * doesn't have another segment after this one; we may just not have
- * opened the next segment yet. (We could not have "all segments are
- * in the chain" as an invariant anyway, since another backend could
- * extend the relation when we weren't looking.) We do not make chain
- * entries for inactive segments, however; as soon as we find a partial
- * segment, we assume that any subsequent segments are inactive.
- *
- * All MdfdVec objects are palloc'd in the MdCxt memory context.
+ * The entire MdfdVec array is palloc'd in the MdCxt memory context.
*/
-
typedef struct _MdfdVec
{
File mdfd_vfd; /* fd number in fd.c's pool */
BlockNumber mdfd_segno; /* segment number, from 0 */
- struct _MdfdVec *mdfd_chain; /* next segment, or NULL */
} MdfdVec;
static MemoryContext MdCxt; /* context for all MdfdVec objects */
@@ -178,7 +173,9 @@ static MdfdVec *mdopen(SMgrRelation reln, ForkNumber forknum,
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
MdfdVec *seg);
static void register_unlink(RelFileNodeBackend rnode);
-static MdfdVec *_fdvec_alloc(void);
+static void _fdvec_alloc(SMgrRelation reln,
+ ForkNumber forknum,
+ size_t nseg);
static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
BlockNumber segno);
static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
@@ -288,13 +285,14 @@ mdexists(SMgrRelation reln, ForkNumber forkNum)
void
mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
{
+ MdfdVec *mdfd;
char *path;
File fd;
- if (isRedo && reln->md_fd[forkNum] != NULL)
+ if (isRedo && reln->md_seg_no[forkNum] > 0)
return; /* created and opened already... */
- Assert(reln->md_fd[forkNum] == NULL);
+ Assert(reln->md_seg_no[forkNum] == 0);
path = relpath(reln->smgr_rnode, forkNum);
@@ -324,11 +322,10 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
pfree(path);
- reln->md_fd[forkNum] = _fdvec_alloc();
-
- reln->md_fd[forkNum]->mdfd_vfd = fd;
- reln->md_fd[forkNum]->mdfd_segno = 0;
- reln->md_fd[forkNum]->mdfd_chain = NULL;
+ _fdvec_alloc(reln, forkNum, 1);
+ mdfd = &reln->md_seg_fds[forkNum][0];
+ mdfd->mdfd_vfd = fd;
+ mdfd->mdfd_segno = 0;
}
/*
@@ -573,8 +570,8 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
File fd;
/* No work if already open */
- if (reln->md_fd[forknum])
- return reln->md_fd[forknum];
+ if (reln->md_seg_no[forknum] > 0)
+ return reln->md_seg_fds[forknum];
path = relpath(reln->smgr_rnode, forknum);
@@ -606,11 +603,11 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
pfree(path);
- reln->md_fd[forknum] = mdfd = _fdvec_alloc();
-
+ _fdvec_alloc(reln, forknum, 1);
+ mdfd = &reln->md_seg_fds[forknum][0];
mdfd->mdfd_vfd = fd;
mdfd->mdfd_segno = 0;
- mdfd->mdfd_chain = NULL;
+
Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
return mdfd;
@@ -622,24 +619,21 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
void
mdclose(SMgrRelation reln, ForkNumber forknum)
{
- MdfdVec *v = reln->md_fd[forknum];
-
- /* No work if already closed */
- if (v == NULL)
- return;
+ size_t nopensegs;
- reln->md_fd[forknum] = NULL; /* prevent dangling pointer after error */
+ nopensegs = reln->md_seg_no[forknum];
- while (v != NULL)
+ /* close segments starting from the end */
+ while (nopensegs > 0)
{
- MdfdVec *ov = v;
+ MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
/* if not closed already */
if (v->mdfd_vfd >= 0)
FileClose(v->mdfd_vfd);
- /* Now free vector */
- v = v->mdfd_chain;
- pfree(ov);
+
+ nopensegs--;
+ _fdvec_alloc(reln, forknum, nopensegs);
}
}
@@ -814,6 +808,9 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
BlockNumber nblocks;
BlockNumber segno = 0;
+ /* mdopen has opened the first segment */
+ Assert(reln->md_seg_no[forknum] > 0);
+
/*
* Skip through any segments that aren't the last one, to avoid redundant
* seeks on them. We have previously verified that these segments are
@@ -827,11 +824,8 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
* segments; that's OK because the checkpointer never needs to compute
* relation size.)
*/
- while (v->mdfd_chain != NULL)
- {
- segno++;
- v = v->mdfd_chain;
- }
+ segno = reln->md_seg_no[forknum] - 1;
+ v = &reln->md_seg_fds[forknum][segno];
for (;;)
{
@@ -846,23 +840,17 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
*/
segno++;
- if (v->mdfd_chain == NULL)
- {
- /*
- * Because we pass O_CREAT, we will create the next segment (with
- * zero length) immediately, if the last segment is of length
- * RELSEG_SIZE. While perhaps not strictly necessary, this keeps
- * the logic simple.
- */
- v->mdfd_chain = _mdfd_openseg(reln, forknum, segno, O_CREAT);
- if (v->mdfd_chain == NULL)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not open file \"%s\": %m",
- _mdfd_segpath(reln, forknum, segno))));
- }
-
- v = v->mdfd_chain;
+ /*
+ * Because we pass O_CREAT, we will create the next segment (with zero
+ * length) immediately, if the last segment is of length RELSEG_SIZE.
+ * While perhaps not strictly necessary, this keeps the logic simple.
+ */
+ v = _mdfd_openseg(reln, forknum, segno, O_CREAT);
+ if (v == NULL)
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not open file \"%s\": %m",
+ _mdfd_segpath(reln, forknum, segno))));
}
}
@@ -872,9 +860,9 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
void
mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
{
- MdfdVec *v;
BlockNumber curnblk;
BlockNumber priorblocks;
+ size_t curopensegs;
/*
* NOTE: mdnblocks makes sure we have opened all active segments, so that
@@ -894,19 +882,24 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
if (nblocks == curnblk)
return; /* no work */
- v = mdopen(reln, forknum, EXTENSION_FAIL);
-
- priorblocks = 0;
- while (v != NULL)
+ /*
+ * Truncate segments, starting at the last one. Starting at the end makes
+ * managing the memory for the fd array easier should there be errors.
+ */
+ curopensegs = reln->md_seg_no[forknum];
+ while (curopensegs > 0 )
{
- MdfdVec *ov = v;
+ MdfdVec *v;
+
+ priorblocks = (curopensegs - 1) * RELSEG_SIZE;
+
+ v = &reln->md_seg_fds[forknum][curopensegs - 1];
if (priorblocks > nblocks)
{
/*
- * This segment is no longer active (and has already been unlinked
- * from the mdfd_chain). We truncate the file, but do not delete
- * it, for reasons explained in the header comments.
+ * This segment is no longer active. We truncate the file, but do
+ * not delete it, for reasons explained in the header comments.
*/
if (FileTruncate(v->mdfd_vfd, 0) < 0)
ereport(ERROR,
@@ -916,20 +909,20 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forknum, v);
- v = v->mdfd_chain;
- Assert(ov != reln->md_fd[forknum]); /* we never drop the 1st
- * segment */
- pfree(ov);
+
+ /* we never drop the 1st segment */
+ Assert(v != &reln->md_seg_fds[forknum][0]);
+
+ _fdvec_alloc(reln, forknum, curopensegs - 1);
}
else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
{
/*
* This is the last segment we want to keep. Truncate the file to
- * the right length, and clear chain link that points to any
- * remaining segments (which we shall zap). NOTE: if nblocks is
- * exactly a multiple K of RELSEG_SIZE, we will truncate the K+1st
- * segment to 0 length but keep it. This adheres to the invariant
- * given in the header comments.
+ * the right length. NOTE: if nblocks is exactly a multiple K of
+ * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
+ * keep it. This adheres to the invariant given in the header
+ * comments.
*/
BlockNumber lastsegblocks = nblocks - priorblocks;
@@ -941,18 +934,16 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
nblocks)));
if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forknum, v);
- v = v->mdfd_chain;
- ov->mdfd_chain = NULL;
}
else
{
/*
- * We still need this segment and 0 or more blocks beyond it, so
- * nothing to do here.
+ * We still need this segment, so nothing to do for this and any
+ * earlier segment.
*/
- v = v->mdfd_chain;
+ break;
}
- priorblocks += RELSEG_SIZE;
+ curopensegs--;
}
}
@@ -965,7 +956,7 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
void
mdimmedsync(SMgrRelation reln, ForkNumber forknum)
{
- MdfdVec *v;
+ size_t i;
/*
* NOTE: mdnblocks makes sure we have opened all active segments, so that
@@ -973,16 +964,18 @@ mdimmedsync(SMgrRelation reln, ForkNumber forknum)
*/
mdnblocks(reln, forknum);
- v = mdopen(reln, forknum, EXTENSION_FAIL);
+ i = reln->md_seg_no[forknum];
- while (v != NULL)
+ while (i > 0)
{
+ MdfdVec *v = &reln->md_seg_fds[forknum][i];
+
if (FileSync(v->mdfd_vfd) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m",
FilePathName(v->mdfd_vfd))));
- v = v->mdfd_chain;
+ i--;
}
}
@@ -1645,10 +1638,29 @@ ForgetDatabaseFsyncRequests(Oid dbid)
/*
* _fdvec_alloc() -- Make a MdfdVec object.
*/
-static MdfdVec *
-_fdvec_alloc(void)
+static void
+_fdvec_alloc(SMgrRelation reln,
+ ForkNumber forknum,
+ size_t nseg)
{
- return (MdfdVec *) MemoryContextAlloc(MdCxt, sizeof(MdfdVec));
+ if (nseg == 0)
+ {
+ if (reln->md_seg_no[forknum] > 0)
+ pfree(reln->md_seg_fds[forknum]);
+ }
+ else if (reln->md_seg_no[forknum] == 0)
+ {
+ reln->md_seg_fds[forknum] =
+ MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
+ }
+ else
+ {
+ reln->md_seg_fds[forknum] =
+ repalloc(reln->md_seg_fds[forknum],
+ sizeof(MdfdVec) * nseg);
+ }
+
+ reln->md_seg_no[forknum] = nseg;
}
/*
@@ -1696,13 +1708,14 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
if (fd < 0)
return NULL;
- /* allocate an mdfdvec entry for it */
- v = _fdvec_alloc();
+ if (reln->md_seg_no[forknum] < segno + 1)
+ _fdvec_alloc(reln, forknum, segno + 1);
/* fill the entry */
+ v = &reln->md_seg_fds[forknum][segno];
v->mdfd_vfd = fd;
v->mdfd_segno = segno;
- v->mdfd_chain = NULL;
+
Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
/* all done */
@@ -1721,65 +1734,69 @@ static MdfdVec *
_mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
bool skipFsync, ExtensionBehavior behavior)
{
- MdfdVec *v = mdopen(reln, forknum, behavior);
+ MdfdVec *v = NULL;
BlockNumber targetseg;
BlockNumber nextsegno;
- if (!v)
- return NULL; /* only possible if EXTENSION_RETURN_NULL */
-
targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
- for (nextsegno = 1; nextsegno <= targetseg; nextsegno++)
+ nextsegno = targetseg;
+
+ /* perhaps it's already open */
+ if (reln->md_seg_no[forknum] >= targetseg)
{
- Assert(nextsegno == v->mdfd_segno + 1);
+ v =_mdfd_openseg(reln, forknum, targetseg, 0);
+ goto done;
+ }
+
+ /* check the size of the relation, this opens all segments */
+ mdnblocks(reln, forknum);
- if (v->mdfd_chain == NULL)
+ /* check whether it's an existing segment */
+ if (reln->md_seg_no[forknum] >= targetseg)
+ {
+ v = _mdfd_openseg(reln, forknum, targetseg, 0);
+ goto done;
+ }
+
+ if (behavior != EXTENSION_CREATE && !InRecovery)
+ goto done;
+
+
+ if (reln->md_seg_no[forknum] > 0)
+ v = &reln->md_seg_fds[forknum][reln->md_seg_no[forknum] - 1];
+
+ /*
+ * We only get here if we want to extend the filenode to have one more
+ * relation. For that we first need to extend the last existing segment to
+ * be of the full size if necessary, and then add the new segment(s).
+ */
+ nextsegno = reln->md_seg_no[forknum] + 1;
+ for (nextsegno = 1; nextsegno <= targetseg; nextsegno++)
+ {
+ if (v != NULL && _mdnblocks(reln, forknum, v) < RELSEG_SIZE)
{
- /*
- * Normally we will create new segments only if authorized by the
- * caller (i.e., we are doing mdextend()). But when doing WAL
- * recovery, create segments anyway; this allows cases such as
- * replaying WAL data that has a write into a high-numbered
- * segment of a relation that was later deleted. We want to go
- * ahead and create the segments so we can finish out the replay.
- *
- * We have to maintain the invariant that segments before the last
- * active segment are of size RELSEG_SIZE; therefore, pad them out
- * with zeroes if needed. (This only matters if caller is
- * extending the relation discontiguously, but that can happen in
- * hash indexes.)
- */
- if (behavior == EXTENSION_CREATE || InRecovery)
- {
- if (_mdnblocks(reln, forknum, v) < RELSEG_SIZE)
- {
- char *zerobuf = palloc0(BLCKSZ);
+ char *zerobuf = palloc0(BLCKSZ);
- mdextend(reln, forknum,
- nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
- zerobuf, skipFsync);
- pfree(zerobuf);
- }
- v->mdfd_chain = _mdfd_openseg(reln, forknum, +nextsegno, O_CREAT);
- }
- else
- {
- /* We won't create segment if not existent */
- v->mdfd_chain = _mdfd_openseg(reln, forknum, nextsegno, 0);
- }
- if (v->mdfd_chain == NULL)
- {
- if (behavior == EXTENSION_RETURN_NULL &&
- FILE_POSSIBLY_DELETED(errno))
- return NULL;
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not open file \"%s\" (target block %u): %m",
- _mdfd_segpath(reln, forknum, nextsegno),
- blkno)));
- }
+ mdextend(reln, forknum,
+ nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
+ zerobuf, skipFsync);
+ pfree(zerobuf);
}
- v = v->mdfd_chain;
+
+ v = _mdfd_openseg(reln, forknum, nextsegno, O_CREAT);
+ }
+
+done:
+ if (v == NULL && behavior == EXTENSION_RETURN_NULL &&
+ FILE_POSSIBLY_DELETED(errno))
+ return NULL;
+ else if (v == NULL)
+ {
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not open file \"%s\" (target block %u): %m",
+ _mdfd_segpath(reln, forknum, nextsegno),
+ blkno)));
}
return v;
}
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index d16f559..43b6bae 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -173,7 +173,7 @@ smgropen(RelFileNode rnode, BackendId backend)
/* mark it not open */
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
- reln->md_fd[forknum] = NULL;
+ reln->md_seg_no[forknum] = 0;
/* it has no owner yet */
add_to_unowned_list(reln);
@@ -378,7 +378,7 @@ smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
* Exit quickly in WAL replay mode if we've already opened the file. If
* it's open, it surely must exist.
*/
- if (isRedo && reln->md_fd[forknum] != NULL)
+ if (isRedo && reln->md_seg_no[forknum] > 0)
return;
/*
diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h
index ba7c909..e6758c6 100644
--- a/src/include/storage/smgr.h
+++ b/src/include/storage/smgr.h
@@ -64,8 +64,8 @@ typedef struct SMgrRelationData
*/
int smgr_which; /* storage manager selector */
- /* for md.c; NULL for forks that are not open */
- struct _MdfdVec *md_fd[MAX_FORKNUM + 1];
+ size_t md_seg_no[MAX_FORKNUM + 1];
+ struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
/* if unowned, list link in list of all unowned SMgrRelations */
struct SMgrRelationData *next_unowned_reln;
--
2.0.0.rc2.4.g1dc51c6.dirty
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2014-10-31 22:48 Tom Lane <tgl@sss.pgh.pa.us>
parent: Andres Freund <andres@2ndquadrant.com>
0 siblings, 1 reply; 25+ messages in thread
From: Tom Lane @ 2014-10-31 22:48 UTC (permalink / raw)
To: Andres Freund <andres@2ndquadrant.com>; +Cc: pgsql-hackers
Andres Freund <andres@2ndquadrant.com> writes:
> I wrote the attached patch that get rids of that essentially quadratic
> behaviour, by replacing the mdfd chain/singly linked list with an
> array. Since we seldomly grow files by a whole segment I can't see the
> slightly bigger memory reallocations matter significantly. In pretty
> much every other case the array is bound to be a winner.
> Does anybody have fundamental arguments against that idea?
While the basic idea is sound, this particular implementation seems
pretty bizarre. What's with the "md_seg_no" stuff, and why is that
array typed size_t? IOW, why didn't you *just* replace the linked
list with an array? This patch seems to be making some other changes
that you've failed to explain.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2014-10-31 22:58 Andres Freund <andres@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2014-10-31 22:58 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers
On 2014-10-31 18:48:45 -0400, Tom Lane wrote:
> Andres Freund <andres@2ndquadrant.com> writes:
> > I wrote the attached patch that get rids of that essentially quadratic
> > behaviour, by replacing the mdfd chain/singly linked list with an
> > array. Since we seldomly grow files by a whole segment I can't see the
> > slightly bigger memory reallocations matter significantly. In pretty
> > much every other case the array is bound to be a winner.
>
> > Does anybody have fundamental arguments against that idea?
>
> While the basic idea is sound, this particular implementation seems
> pretty bizarre. What's with the "md_seg_no" stuff, and why is that
> array typed size_t?
> IOW, why didn't you *just* replace the linked list with an array?
It stores the length of the array of _MdfdVec entries. To know whether
it's safe to access some element we first need to check whether we've
loaded that many entries. It's size_t[] because that seemed to be the
most appropriate type for the lenght of an array. It's an array because
md.c had already chosen to represent relation forks via an array indexed
by the fork.
So
size_t md_seg_no[MAX_FORKNUM + 1];
contains the length of the _MdfdVec array for each fork. These arrays
are stored in:
struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
> This patch seems to be making some other changes that you've failed to
> explain.
I'm not aware of any that aren't just a consequence of not iterating
through the linked list anymore. What change are you thinking of?
Greetings,
Andres Freund
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2014-11-01 16:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Andres Freund <andres@2ndquadrant.com>
0 siblings, 1 reply; 25+ messages in thread
From: Tom Lane @ 2014-11-01 16:57 UTC (permalink / raw)
To: Andres Freund <andres@2ndquadrant.com>; +Cc: pgsql-hackers
Andres Freund <andres@2ndquadrant.com> writes:
> On 2014-10-31 18:48:45 -0400, Tom Lane wrote:
>> While the basic idea is sound, this particular implementation seems
>> pretty bizarre. What's with the "md_seg_no" stuff, and why is that
>> array typed size_t?
> It stores the length of the array of _MdfdVec entries.
Oh. "seg_no" seems like not a very good choice of name then.
Perhaps "md_seg_count" or something like that would be more intelligible.
And personally I'd have made it an int, because we are certainly not doing
segment-number arithmetic in anything wider than int anywhere else.
Introducing size_t into the mix won't do anything except create a risk of
signed-vs-unsigned logic bugs.
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2014-11-01 17:23 Andres Freund <andres@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2014-11-01 17:23 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers
On 2014-11-01 12:57:40 -0400, Tom Lane wrote:
> Andres Freund <andres@2ndquadrant.com> writes:
> > On 2014-10-31 18:48:45 -0400, Tom Lane wrote:
> >> While the basic idea is sound, this particular implementation seems
> >> pretty bizarre. What's with the "md_seg_no" stuff, and why is that
> >> array typed size_t?
>
> > It stores the length of the array of _MdfdVec entries.
>
> Oh. "seg_no" seems like not a very good choice of name then.
> Perhaps "md_seg_count" or something like that would be more intelligible.
That's fine with me.
> And personally I'd have made it an int, because we are certainly not doing
> segment-number arithmetic in anything wider than int anywhere else.
Fine with me too. I picked size_t by habit, because there's projects
that don't allow anything else to be used for lengths of memory...
I've, during testing, also noticed it has accidentally introduced a
vfd/memory leak...
So I'll repost a version with those fixes.
Greetings,
Andres Freund
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2015-12-15 18:04 Andres Freund <andres@anarazel.de>
parent: Andres Freund <andres@2ndquadrant.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2015-12-15 18:04 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-hackers
On 2014-11-01 18:23:47 +0100, Andres Freund wrote:
> On 2014-11-01 12:57:40 -0400, Tom Lane wrote:
> > Andres Freund <andres@2ndquadrant.com> writes:
> > > On 2014-10-31 18:48:45 -0400, Tom Lane wrote:
> > >> While the basic idea is sound, this particular implementation seems
> > >> pretty bizarre. What's with the "md_seg_no" stuff, and why is that
> > >> array typed size_t?
> >
> > > It stores the length of the array of _MdfdVec entries.
> >
> > Oh. "seg_no" seems like not a very good choice of name then.
> > Perhaps "md_seg_count" or something like that would be more intelligible.
>
> That's fine with me.
Went with md_num_open_segs - reading the code that was easier to
understand.
> So I'll repost a version with those fixes.
Took a while. But here we go. The attached version is a significantly
revised version of my earlier patch. Notably I've pretty much entirely
revised the code in _mdfd_getseg() to be more similar to the state in
master. Also some comment policing.
As it's more than a bit painful to test with large numbers of 1GB
segments, I've rebuilt my local postgres with 100kb segments. Running a
pgbench -s 300 with 128MB shared buffers clearly shows the efficiency
differnces: 320tps vs 4900 in an assertion enabled build. Obviously
that's kind of an artificial situation...
But I've also verified this with a) fake relations built out of sparse
files, b) actually large relations. The latter shows performance
benefits as well, but my patience limits the amount of testing I was
willing to do...
Kevin, Robert: I've CCed you because Robert commented in the recent
mdnblocks() blocks thread that Kevin lamented the O(n)'ness of md.c...
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments: download all (2 files, .tar.gz)
0001-Faster-PageIsVerified-for-the-all-zeroes-case.patch (text/x-patch, 1.4K) download
From a0b30154714df3e13bb668fa9590f7be40e4de35 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 15 Dec 2015 19:01:36 +0100
Subject: [PATCH 1/2] Faster PageIsVerified() for the all zeroes case.
---
src/backend/storage/page/bufpage.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index df77bb2..de9800f 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -81,7 +81,7 @@ bool
PageIsVerified(Page page, BlockNumber blkno)
{
PageHeader p = (PageHeader) page;
- char *pagebytes;
+ size_t *pagebytes;
int i;
bool checksum_failure = false;
bool header_sane = false;
@@ -118,10 +118,17 @@ PageIsVerified(Page page, BlockNumber blkno)
return true;
}
- /* Check all-zeroes case */
+ /*
+ * Check all-zeroes case. Luckily BLCKSZ is guaranteed to always be a
+ * multiple of size_t - and it's much faster compare memory using the
+ * processor's native word size.
+ */
+ StaticAssertStmt(BLCKSZ == (BLCKSZ / sizeof(size_t)) * sizeof(size_t),
+ "BLCKSZ has to be a multiple of sizeof(size_t)");
+
all_zeroes = true;
- pagebytes = (char *) page;
- for (i = 0; i < BLCKSZ; i++)
+ pagebytes = (size_t *) page;
+ for (i = 0; i < (BLCKSZ / sizeof(size_t)); i++)
{
if (pagebytes[i] != 0)
{
--
2.6.0.rc3
0002-Improve-scalability-of-md.c-for-large-relations.patch (text/x-patch, 19.9K) download
From 15150d1ba7f17d3a241be3073a03e6bb9a36a937 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 15 Dec 2015 19:01:36 +0100
Subject: [PATCH 2/2] Improve scalability of md.c for large relations.
Previously several routines in md.c were O(#segments) - a problem these
days, where it's not uncommon to have tables in the multi TB range.
Replace the linked list of segments hanging of SMgrRelationData with an
array of opened segments. That allows O(1) access to individual
segments, if they've previously been opened.
---
src/backend/storage/smgr/md.c | 337 +++++++++++++++++++++++-----------------
src/backend/storage/smgr/smgr.c | 4 +-
src/include/storage/smgr.h | 8 +-
3 files changed, 200 insertions(+), 149 deletions(-)
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index 42a43bb..cb163f4 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -92,27 +92,23 @@
* out to an unlinked old copy of a segment file that will eventually
* disappear.
*
- * The file descriptor pointer (md_fd field) stored in the SMgrRelation
- * cache is, therefore, just the head of a list of MdfdVec objects, one
- * per segment. But note the md_fd pointer can be NULL, indicating
- * relation not open.
+ * File descriptors are stored in md_seg_fds arrays inside SMgrRelation. The
+ * length of these arrays is stored in md_num_open_segs. Note that
+ * md_num_open_segs having a specific value does not necessarily mean the
+ * relation doesn't have additional segments; we may just not have opened the
+ * next segment yet. (We could not have "all segments are in the array" as
+ * an invariant anyway, since another backend could extend the relation while
+ * we aren't looking.) We do not have entries for inactive segments,
+ * however; as soon as we find a partial segment, we assume that any
+ * subsequent segments are inactive.
*
- * Also note that mdfd_chain == NULL does not necessarily mean the relation
- * doesn't have another segment after this one; we may just not have
- * opened the next segment yet. (We could not have "all segments are
- * in the chain" as an invariant anyway, since another backend could
- * extend the relation when we weren't looking.) We do not make chain
- * entries for inactive segments, however; as soon as we find a partial
- * segment, we assume that any subsequent segments are inactive.
- *
- * All MdfdVec objects are palloc'd in the MdCxt memory context.
+ * The entire MdfdVec array is palloc'd in the MdCxt memory context.
*/
typedef struct _MdfdVec
{
File mdfd_vfd; /* fd number in fd.c's pool */
BlockNumber mdfd_segno; /* segment number, from 0 */
- struct _MdfdVec *mdfd_chain; /* next segment, or NULL */
} MdfdVec;
static MemoryContext MdCxt; /* context for all MdfdVec objects */
@@ -178,7 +174,9 @@ static MdfdVec *mdopen(SMgrRelation reln, ForkNumber forknum,
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
MdfdVec *seg);
static void register_unlink(RelFileNodeBackend rnode);
-static MdfdVec *_fdvec_alloc(void);
+static void _fdvec_resize(SMgrRelation reln,
+ ForkNumber forknum,
+ int nseg);
static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
BlockNumber segno);
static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
@@ -287,13 +285,14 @@ mdexists(SMgrRelation reln, ForkNumber forkNum)
void
mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
{
+ MdfdVec *mdfd;
char *path;
File fd;
- if (isRedo && reln->md_fd[forkNum] != NULL)
+ if (isRedo && reln->md_num_open_segs[forkNum] > 0)
return; /* created and opened already... */
- Assert(reln->md_fd[forkNum] == NULL);
+ Assert(reln->md_num_open_segs[forkNum] == 0);
path = relpath(reln->smgr_rnode, forkNum);
@@ -323,11 +322,10 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
pfree(path);
- reln->md_fd[forkNum] = _fdvec_alloc();
-
- reln->md_fd[forkNum]->mdfd_vfd = fd;
- reln->md_fd[forkNum]->mdfd_segno = 0;
- reln->md_fd[forkNum]->mdfd_chain = NULL;
+ _fdvec_resize(reln, forkNum, 1);
+ mdfd = &reln->md_seg_fds[forkNum][0];
+ mdfd->mdfd_vfd = fd;
+ mdfd->mdfd_segno = 0;
}
/*
@@ -572,8 +570,8 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
File fd;
/* No work if already open */
- if (reln->md_fd[forknum])
- return reln->md_fd[forknum];
+ if (reln->md_num_open_segs[forknum] > 0)
+ return &reln->md_seg_fds[forknum][0];
path = relpath(reln->smgr_rnode, forknum);
@@ -605,11 +603,11 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
pfree(path);
- reln->md_fd[forknum] = mdfd = _fdvec_alloc();
-
+ _fdvec_resize(reln, forknum, 1);
+ mdfd = &reln->md_seg_fds[forknum][0];
mdfd->mdfd_vfd = fd;
mdfd->mdfd_segno = 0;
- mdfd->mdfd_chain = NULL;
+
Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
return mdfd;
@@ -621,25 +619,26 @@ mdopen(SMgrRelation reln, ForkNumber forknum, ExtensionBehavior behavior)
void
mdclose(SMgrRelation reln, ForkNumber forknum)
{
- MdfdVec *v = reln->md_fd[forknum];
+ int nopensegs = reln->md_num_open_segs[forknum];
/* No work if already closed */
- if (v == NULL)
+ if (nopensegs == 0)
return;
- reln->md_fd[forknum] = NULL; /* prevent dangling pointer after error */
-
- while (v != NULL)
+ /* close segments starting from the end */
+ while (nopensegs > 0)
{
- MdfdVec *ov = v;
+ MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
/* if not closed already */
if (v->mdfd_vfd >= 0)
FileClose(v->mdfd_vfd);
- /* Now free vector */
- v = v->mdfd_chain;
- pfree(ov);
+
+ nopensegs--;
}
+
+ /* resize just once, avoids pointless reallocations */
+ _fdvec_resize(reln, forknum, 0);
}
/*
@@ -802,9 +801,9 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
* mdnblocks() -- Get the number of blocks stored in a relation.
*
* Important side effect: all active segments of the relation are opened
- * and added to the mdfd_chain list. If this routine has not been
+ * and added to the mdfd_seg_fds array. If this routine has not been
* called, then only segments up to the last one actually touched
- * are present in the chain.
+ * are present in the array.
*/
BlockNumber
mdnblocks(SMgrRelation reln, ForkNumber forknum)
@@ -813,24 +812,25 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
BlockNumber nblocks;
BlockNumber segno = 0;
+ /* mdopen has opened the first segment */
+ Assert(reln->md_num_open_segs[forknum] > 0);
+
/*
- * Skip through any segments that aren't the last one, to avoid redundant
- * seeks on them. We have previously verified that these segments are
- * exactly RELSEG_SIZE long, and it's useless to recheck that each time.
+ * Start from the last open segments, to avoid redundant seeks. We have
+ * previously verified that these segments are exactly RELSEG_SIZE long,
+ * and it's useless to recheck that each time. XXX: No, we really
+ * haven't, _mdfd_getseg() doesn't do such checks.
*
* NOTE: this assumption could only be wrong if another backend has
* truncated the relation. We rely on higher code levels to handle that
* scenario by closing and re-opening the md fd, which is handled via
* relcache flush. (Since the checkpointer doesn't participate in
- * relcache flush, it could have segment chain entries for inactive
- * segments; that's OK because the checkpointer never needs to compute
- * relation size.)
+ * relcache flush, it could have segment entries for inactive segments;
+ * that's OK because the checkpointer never needs to compute relation
+ * size.)
*/
- while (v->mdfd_chain != NULL)
- {
- segno++;
- v = v->mdfd_chain;
- }
+ segno = reln->md_num_open_segs[forknum] - 1;
+ v = &reln->md_seg_fds[forknum][segno];
for (;;)
{
@@ -845,23 +845,17 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
*/
segno++;
- if (v->mdfd_chain == NULL)
- {
- /*
- * Because we pass O_CREAT, we will create the next segment (with
- * zero length) immediately, if the last segment is of length
- * RELSEG_SIZE. While perhaps not strictly necessary, this keeps
- * the logic simple.
- */
- v->mdfd_chain = _mdfd_openseg(reln, forknum, segno, O_CREAT);
- if (v->mdfd_chain == NULL)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not open file \"%s\": %m",
- _mdfd_segpath(reln, forknum, segno))));
- }
-
- v = v->mdfd_chain;
+ /*
+ * Because we pass O_CREAT, we will create the next segment (with zero
+ * length) immediately, if the last segment is of length RELSEG_SIZE.
+ * While perhaps not strictly necessary, this keeps the logic simple.
+ */
+ v = _mdfd_openseg(reln, forknum, segno, O_CREAT);
+ if (v == NULL)
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not open file \"%s\": %m",
+ _mdfd_segpath(reln, forknum, segno))));
}
}
@@ -871,9 +865,9 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
void
mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
{
- MdfdVec *v;
BlockNumber curnblk;
BlockNumber priorblocks;
+ int curopensegs;
/*
* NOTE: mdnblocks makes sure we have opened all active segments, so that
@@ -893,19 +887,24 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
if (nblocks == curnblk)
return; /* no work */
- v = mdopen(reln, forknum, EXTENSION_FAIL);
-
- priorblocks = 0;
- while (v != NULL)
+ /*
+ * Truncate segments, starting at the last one. Starting at the end makes
+ * managing the memory for the fd array easier should there be errors.
+ */
+ curopensegs = reln->md_num_open_segs[forknum];
+ while (curopensegs > 0)
{
- MdfdVec *ov = v;
+ MdfdVec *v;
+
+ priorblocks = (curopensegs - 1) * RELSEG_SIZE;
+
+ v = &reln->md_seg_fds[forknum][curopensegs - 1];
if (priorblocks > nblocks)
{
/*
- * This segment is no longer active (and has already been unlinked
- * from the mdfd_chain). We truncate the file, but do not delete
- * it, for reasons explained in the header comments.
+ * This segment is no longer active. We truncate the file, but do
+ * not delete it, for reasons explained in the header comments.
*/
if (FileTruncate(v->mdfd_vfd, 0) < 0)
ereport(ERROR,
@@ -915,20 +914,20 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forknum, v);
- v = v->mdfd_chain;
- Assert(ov != reln->md_fd[forknum]); /* we never drop the 1st
- * segment */
- pfree(ov);
+
+ /* we never drop the 1st segment */
+ Assert(v != &reln->md_seg_fds[forknum][0]);
+
+ _fdvec_resize(reln, forknum, curopensegs - 1);
}
else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
{
/*
* This is the last segment we want to keep. Truncate the file to
- * the right length, and clear chain link that points to any
- * remaining segments (which we shall zap). NOTE: if nblocks is
- * exactly a multiple K of RELSEG_SIZE, we will truncate the K+1st
- * segment to 0 length but keep it. This adheres to the invariant
- * given in the header comments.
+ * the right length. NOTE: if nblocks is exactly a multiple K of
+ * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
+ * keep it. This adheres to the invariant given in the header
+ * comments.
*/
BlockNumber lastsegblocks = nblocks - priorblocks;
@@ -940,18 +939,16 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
nblocks)));
if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forknum, v);
- v = v->mdfd_chain;
- ov->mdfd_chain = NULL;
}
else
{
/*
- * We still need this segment and 0 or more blocks beyond it, so
- * nothing to do here.
+ * We still need this segment, so nothing to do for this and any
+ * earlier segment.
*/
- v = v->mdfd_chain;
+ break;
}
- priorblocks += RELSEG_SIZE;
+ curopensegs--;
}
}
@@ -964,7 +961,7 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
void
mdimmedsync(SMgrRelation reln, ForkNumber forknum)
{
- MdfdVec *v;
+ int segno;
/*
* NOTE: mdnblocks makes sure we have opened all active segments, so that
@@ -972,16 +969,18 @@ mdimmedsync(SMgrRelation reln, ForkNumber forknum)
*/
mdnblocks(reln, forknum);
- v = mdopen(reln, forknum, EXTENSION_FAIL);
+ segno = reln->md_num_open_segs[forknum];
- while (v != NULL)
+ while (segno > 0)
{
+ MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
+
if (FileSync(v->mdfd_vfd) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m",
FilePathName(v->mdfd_vfd))));
- v = v->mdfd_chain;
+ segno--;
}
}
@@ -1642,12 +1641,34 @@ ForgetDatabaseFsyncRequests(Oid dbid)
/*
- * _fdvec_alloc() -- Make a MdfdVec object.
+ * _fdvec_resize() -- Resize the fork's open segments array
*/
-static MdfdVec *
-_fdvec_alloc(void)
+static void
+_fdvec_resize(SMgrRelation reln,
+ ForkNumber forknum,
+ int nseg)
{
- return (MdfdVec *) MemoryContextAlloc(MdCxt, sizeof(MdfdVec));
+ if (nseg == 0)
+ {
+ if (reln->md_num_open_segs[forknum] > 0)
+ {
+ pfree(reln->md_seg_fds[forknum]);
+ reln->md_seg_fds[forknum] = NULL;
+ }
+ }
+ else if (reln->md_num_open_segs[forknum] == 0)
+ {
+ reln->md_seg_fds[forknum] =
+ MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
+ }
+ else
+ {
+ reln->md_seg_fds[forknum] =
+ repalloc(reln->md_seg_fds[forknum],
+ sizeof(MdfdVec) * nseg);
+ }
+
+ reln->md_num_open_segs[forknum] = nseg;
}
/*
@@ -1695,13 +1716,14 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
if (fd < 0)
return NULL;
- /* allocate an mdfdvec entry for it */
- v = _fdvec_alloc();
+ if (segno <= reln->md_num_open_segs[forknum])
+ _fdvec_resize(reln, forknum, segno + 1);
/* fill the entry */
+ v = &reln->md_seg_fds[forknum][segno];
v->mdfd_vfd = fd;
v->mdfd_segno = segno;
- v->mdfd_chain = NULL;
+
Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
/* all done */
@@ -1720,66 +1742,91 @@ static MdfdVec *
_mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
bool skipFsync, ExtensionBehavior behavior)
{
- MdfdVec *v = mdopen(reln, forknum, behavior);
+ MdfdVec *v;
BlockNumber targetseg;
BlockNumber nextsegno;
- if (!v)
- return NULL; /* only possible if EXTENSION_RETURN_NULL */
-
targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
- for (nextsegno = 1; nextsegno <= targetseg; nextsegno++)
+
+ /* if an existing and opened segment, we're done */
+ if (targetseg < reln->md_num_open_segs[forknum])
+ {
+ v = &reln->md_seg_fds[forknum][targetseg];
+ return v;
+ }
+
+ /*
+ * The target segment is not yet open. Iterate over all the segments
+ * between the last opened and the target segment. This way missing
+ * segments either raise an error, or get created (according to
+ * 'behavior'). Start with either the last opened, or the first segment if
+ * none was opened before.
+ */
+ if (reln->md_num_open_segs[forknum] > 0)
+ v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
+ else
+ {
+ v = mdopen(reln, forknum, behavior);
+ if (!v)
+ return NULL; /* only possible if EXTENSION_RETURN_NULL */
+ }
+
+ for (nextsegno = reln->md_num_open_segs[forknum];
+ nextsegno <= targetseg; nextsegno++)
{
Assert(nextsegno == v->mdfd_segno + 1);
- if (v->mdfd_chain == NULL)
+ /*
+ * Normally we will create new segments only if authorized by the
+ * caller (i.e., we are doing mdextend()). But when doing WAL
+ * recovery, create segments anyway; this allows cases such as
+ * replaying WAL data that has a write into a high-numbered segment of
+ * a relation that was later deleted. We want to go ahead and create
+ * the segments so we can finish out the replay.
+ *
+ * We have to maintain the invariant that segments before the last
+ * active segment are of size RELSEG_SIZE; therefore, pad them out
+ * with zeroes if needed. (This only matters if caller is extending
+ * the relation discontiguously, but that can happen in hash indexes.)
+ */
+ if (behavior == EXTENSION_CREATE || InRecovery)
{
- /*
- * Normally we will create new segments only if authorized by the
- * caller (i.e., we are doing mdextend()). But when doing WAL
- * recovery, create segments anyway; this allows cases such as
- * replaying WAL data that has a write into a high-numbered
- * segment of a relation that was later deleted. We want to go
- * ahead and create the segments so we can finish out the replay.
- *
- * We have to maintain the invariant that segments before the last
- * active segment are of size RELSEG_SIZE; therefore, pad them out
- * with zeroes if needed. (This only matters if caller is
- * extending the relation discontiguously, but that can happen in
- * hash indexes.)
- */
- if (behavior == EXTENSION_CREATE || InRecovery)
+ if (_mdnblocks(reln, forknum, v) < RELSEG_SIZE)
{
- if (_mdnblocks(reln, forknum, v) < RELSEG_SIZE)
- {
- char *zerobuf = palloc0(BLCKSZ);
+ char *zerobuf = palloc0(BLCKSZ);
- mdextend(reln, forknum,
- nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
- zerobuf, skipFsync);
- pfree(zerobuf);
- }
- v->mdfd_chain = _mdfd_openseg(reln, forknum, +nextsegno, O_CREAT);
+ mdextend(reln, forknum,
+ nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
+ zerobuf, skipFsync);
+ pfree(zerobuf);
}
- else
- {
- /* We won't create segment if not existent */
- v->mdfd_chain = _mdfd_openseg(reln, forknum, nextsegno, 0);
- }
- if (v->mdfd_chain == NULL)
- {
- if (behavior == EXTENSION_RETURN_NULL &&
- FILE_POSSIBLY_DELETED(errno))
- return NULL;
- ereport(ERROR,
- (errcode_for_file_access(),
+
+ v = _mdfd_openseg(reln, forknum, nextsegno, O_CREAT);
+ }
+ else
+ {
+ /*
+ * XXX: we should really be checking the segment size of 'v' to be
+ * RELSEG_SIZE here, before jumping to the next segment.
+ */
+
+ /* We won't create segment if not existent */
+ v = _mdfd_openseg(reln, forknum, nextsegno, 0);
+ }
+
+ if (v == NULL)
+ {
+ if (behavior == EXTENSION_RETURN_NULL &&
+ FILE_POSSIBLY_DELETED(errno))
+ return NULL;
+ ereport(ERROR,
+ (errcode_for_file_access(),
errmsg("could not open file \"%s\" (target block %u): %m",
_mdfd_segpath(reln, forknum, nextsegno),
blkno)));
- }
}
- v = v->mdfd_chain;
}
+
return v;
}
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index 244b4ea..92964dc 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -172,7 +172,7 @@ smgropen(RelFileNode rnode, BackendId backend)
/* mark it not open */
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
- reln->md_fd[forknum] = NULL;
+ reln->md_num_open_segs[forknum] = 0;
/* it has no owner yet */
add_to_unowned_list(reln);
@@ -377,7 +377,7 @@ smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
* Exit quickly in WAL replay mode if we've already opened the file. If
* it's open, it surely must exist.
*/
- if (isRedo && reln->md_fd[forknum] != NULL)
+ if (isRedo && reln->md_num_open_segs[forknum] > 0)
return;
/*
diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h
index 69a624f..ba09aee 100644
--- a/src/include/storage/smgr.h
+++ b/src/include/storage/smgr.h
@@ -64,8 +64,12 @@ typedef struct SMgrRelationData
*/
int smgr_which; /* storage manager selector */
- /* for md.c; NULL for forks that are not open */
- struct _MdfdVec *md_fd[MAX_FORKNUM + 1];
+ /*
+ * for md.c; per-fork arrays of the number of open segments
+ * (md_num_open_segs) and the segments themselves (md_seg_fds).
+ */
+ int md_num_open_segs[MAX_FORKNUM + 1];
+ struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
/* if unowned, list link in list of all unowned SMgrRelations */
struct SMgrRelationData *next_unowned_reln;
--
2.6.0.rc3
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-07-01 01:14 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 2 replies; 25+ messages in thread
From: Peter Geoghegan @ 2016-07-01 01:14 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Tue, Dec 15, 2015 at 10:04 AM, Andres Freund <andres@anarazel.de> wrote:
> Took a while. But here we go. The attached version is a significantly
> revised version of my earlier patch. Notably I've pretty much entirely
> revised the code in _mdfd_getseg() to be more similar to the state in
> master. Also some comment policing.
Are you planning to pick this up again, Andres?
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-07-01 01:23 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
1 sibling, 1 reply; 25+ messages in thread
From: Andres Freund @ 2016-07-01 01:23 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-06-30 18:14:15 -0700, Peter Geoghegan wrote:
> On Tue, Dec 15, 2015 at 10:04 AM, Andres Freund <andres@anarazel.de> wrote:
> > Took a while. But here we go. The attached version is a significantly
> > revised version of my earlier patch. Notably I've pretty much entirely
> > revised the code in _mdfd_getseg() to be more similar to the state in
> > master. Also some comment policing.
>
> Are you planning to pick this up again, Andres?
I plan to, once the tree opens again. Likely needs some considerable
updates for recent changes.
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-07-01 01:34 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 25+ messages in thread
From: Peter Geoghegan @ 2016-07-01 01:34 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Thu, Jun 30, 2016 at 6:23 PM, Andres Freund <andres@anarazel.de> wrote:
> I plan to, once the tree opens again. Likely needs some considerable
> updates for recent changes.
Offhand, do you think that CREATE INDEX calls to smgrextend() could be
appreciably affected by this bottleneck? If that's a very involved or
difficult question, then no need to answer now.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-07-01 02:08 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2016-07-01 02:08 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-06-30 18:34:20 -0700, Peter Geoghegan wrote:
> On Thu, Jun 30, 2016 at 6:23 PM, Andres Freund <andres@anarazel.de> wrote:
> > I plan to, once the tree opens again. Likely needs some considerable
> > updates for recent changes.
>
> Offhand, do you think that CREATE INDEX calls to smgrextend() could be
> appreciably affected by this bottleneck? If that's a very involved or
> difficult question, then no need to answer now.
If you have a big enough index (maybe ~150GB+), sure. Before that,
probably not.
It's usually pretty easy to see in cpu profiles whether this issue
exists.
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-07-01 21:18 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 0 replies; 25+ messages in thread
From: Peter Geoghegan @ 2016-07-01 21:18 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers; Corey Huinker <corey.huinker@gmail.com>
On Thu, Jun 30, 2016 at 7:08 PM, Andres Freund <andres@anarazel.de> wrote:
> If you have a big enough index (maybe ~150GB+), sure. Before that,
> probably not.
>
> It's usually pretty easy to see in cpu profiles whether this issue
> exists.
I think that this is a contributing factor to why merging in parallel
CREATE INDEX becomes much more CPU bound when building such very large
indexes, which Corey Huinker has benchmarked using an advanced copy of
the patch. He has shown cases that are sped up by 3.6x when 8 parallel
workers are used (compared to a serial CREATE INDEX), but a several
hundred gigabyte index case only sees a speedup of about 1.5x. (This
bottleneck affects serial CREATE INDEX merging just as much as
parallel, since that part isn't parallelized, but it's far more
noticeable with parallel CREATE INDEX simply because merging in the
leader becomes a huge bottleneck).
Those two cases were not exactly comparable in perhaps several other
ways, but even still my sense is that that this can be at least
partially explained by md.c bottlenecks. This is something that we'll
need to confirm through profiling. Hopefully it's just this one
bottleneck.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-19 00:26 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
1 sibling, 2 replies; 25+ messages in thread
From: Andres Freund @ 2016-08-19 00:26 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-06-30 18:14:15 -0700, Peter Geoghegan wrote:
> On Tue, Dec 15, 2015 at 10:04 AM, Andres Freund <andres@anarazel.de> wrote:
> > Took a while. But here we go. The attached version is a significantly
> > revised version of my earlier patch. Notably I've pretty much entirely
> > revised the code in _mdfd_getseg() to be more similar to the state in
> > master. Also some comment policing.
>
> Are you planning to pick this up again, Andres?
Rebased version attached. A review would be welcome. Plan to push this
forward otherwise in the not too far away future.
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments: download all (2 files, .tar.gz)
0001-Improve-scalability-of-md.c-for-large-relations.patch (text/x-patch, 22.5K) download
From 62b0d36864b23b91961bb800c1b0814f20d00a8e Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 18 Aug 2016 16:04:33 -0700
Subject: [PATCH 1/2] Improve scalability of md.c for large relations.
Previously several routines in md.c were O(#segments) - a problem these
days, where it's not uncommon to have tables in the multi TB range.
Replace the linked list of segments hanging of SMgrRelationData, with an
array of opened segments. That allows O(1) access to individual
segments, if they've previously been opened.
---
src/backend/storage/smgr/md.c | 408 ++++++++++++++++++++++------------------
src/backend/storage/smgr/smgr.c | 4 +-
src/include/storage/smgr.h | 8 +-
3 files changed, 234 insertions(+), 186 deletions(-)
diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index f329d15..0992a7e 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -92,27 +92,23 @@
* out to an unlinked old copy of a segment file that will eventually
* disappear.
*
- * The file descriptor pointer (md_fd field) stored in the SMgrRelation
- * cache is, therefore, just the head of a list of MdfdVec objects, one
- * per segment. But note the md_fd pointer can be NULL, indicating
- * relation not open.
+ * File descriptors are stored in md_seg_fds arrays inside SMgrRelation. The
+ * length of these arrays is stored in md_num_open_segs. Note that
+ * md_num_open_segs having a specific value does not necessarily mean the
+ * relation doesn't have additional segments; we may just not have opened the
+ * next segment yet. (We could not have "all segments are in the array" as
+ * an invariant anyway, since another backend could extend the relation while
+ * we aren't looking.) We do not have entries for inactive segments,
+ * however; as soon as we find a partial segment, we assume that any
+ * subsequent segments are inactive.
*
- * Also note that mdfd_chain == NULL does not necessarily mean the relation
- * doesn't have another segment after this one; we may just not have
- * opened the next segment yet. (We could not have "all segments are
- * in the chain" as an invariant anyway, since another backend could
- * extend the relation when we weren't looking.) We do not make chain
- * entries for inactive segments, however; as soon as we find a partial
- * segment, we assume that any subsequent segments are inactive.
- *
- * All MdfdVec objects are palloc'd in the MdCxt memory context.
+ * The entire MdfdVec array is palloc'd in the MdCxt memory context.
*/
typedef struct _MdfdVec
{
File mdfd_vfd; /* fd number in fd.c's pool */
BlockNumber mdfd_segno; /* segment number, from 0 */
- struct _MdfdVec *mdfd_chain; /* next segment, or NULL */
} MdfdVec;
static MemoryContext MdCxt; /* context for all MdfdVec objects */
@@ -189,7 +185,9 @@ static MdfdVec *mdopen(SMgrRelation reln, ForkNumber forknum, int behavior);
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
MdfdVec *seg);
static void register_unlink(RelFileNodeBackend rnode);
-static MdfdVec *_fdvec_alloc(void);
+static void _fdvec_resize(SMgrRelation reln,
+ ForkNumber forknum,
+ int nseg);
static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
BlockNumber segno);
static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
@@ -298,13 +296,14 @@ mdexists(SMgrRelation reln, ForkNumber forkNum)
void
mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
{
+ MdfdVec *mdfd;
char *path;
File fd;
- if (isRedo && reln->md_fd[forkNum] != NULL)
+ if (isRedo && reln->md_num_open_segs[forkNum] > 0)
return; /* created and opened already... */
- Assert(reln->md_fd[forkNum] == NULL);
+ Assert(reln->md_num_open_segs[forkNum] == 0);
path = relpath(reln->smgr_rnode, forkNum);
@@ -334,11 +333,10 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
pfree(path);
- reln->md_fd[forkNum] = _fdvec_alloc();
-
- reln->md_fd[forkNum]->mdfd_vfd = fd;
- reln->md_fd[forkNum]->mdfd_segno = 0;
- reln->md_fd[forkNum]->mdfd_chain = NULL;
+ _fdvec_resize(reln, forkNum, 1);
+ mdfd = &reln->md_seg_fds[forkNum][0];
+ mdfd->mdfd_vfd = fd;
+ mdfd->mdfd_segno = 0;
}
/*
@@ -583,8 +581,8 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
File fd;
/* No work if already open */
- if (reln->md_fd[forknum])
- return reln->md_fd[forknum];
+ if (reln->md_num_open_segs[forknum] > 0)
+ return &reln->md_seg_fds[forknum][0];
path = relpath(reln->smgr_rnode, forknum);
@@ -616,11 +614,11 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
pfree(path);
- reln->md_fd[forknum] = mdfd = _fdvec_alloc();
-
+ _fdvec_resize(reln, forknum, 1);
+ mdfd = &reln->md_seg_fds[forknum][0];
mdfd->mdfd_vfd = fd;
mdfd->mdfd_segno = 0;
- mdfd->mdfd_chain = NULL;
+
Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
return mdfd;
@@ -632,25 +630,29 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
void
mdclose(SMgrRelation reln, ForkNumber forknum)
{
- MdfdVec *v = reln->md_fd[forknum];
+ int nopensegs = reln->md_num_open_segs[forknum];
/* No work if already closed */
- if (v == NULL)
+ if (nopensegs == 0)
return;
- reln->md_fd[forknum] = NULL; /* prevent dangling pointer after error */
-
- while (v != NULL)
+ /* close segments starting from the end */
+ while (nopensegs > 0)
{
- MdfdVec *ov = v;
+ MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
/* if not closed already */
if (v->mdfd_vfd >= 0)
+ {
FileClose(v->mdfd_vfd);
- /* Now free vector */
- v = v->mdfd_chain;
- pfree(ov);
+ v->mdfd_vfd = -1;
+ }
+
+ nopensegs--;
}
+
+ /* resize just once, avoids pointless reallocations */
+ _fdvec_resize(reln, forknum, 0);
}
/*
@@ -866,9 +868,9 @@ mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
* mdnblocks() -- Get the number of blocks stored in a relation.
*
* Important side effect: all active segments of the relation are opened
- * and added to the mdfd_chain list. If this routine has not been
+ * and added to the mdfd_seg_fds array. If this routine has not been
* called, then only segments up to the last one actually touched
- * are present in the chain.
+ * are present in the array.
*/
BlockNumber
mdnblocks(SMgrRelation reln, ForkNumber forknum)
@@ -877,24 +879,24 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
BlockNumber nblocks;
BlockNumber segno = 0;
+ /* mdopen has opened the first segment */
+ Assert(reln->md_num_open_segs[forknum] > 0);
+
/*
- * Skip through any segments that aren't the last one, to avoid redundant
- * seeks on them. We have previously verified that these segments are
- * exactly RELSEG_SIZE long, and it's useless to recheck that each time.
+ * Start from the last open segments, to avoid redundant seeks. We have
+ * previously verified that these segments are exactly RELSEG_SIZE long,
+ * and it's useless to recheck that each time.
*
* NOTE: this assumption could only be wrong if another backend has
* truncated the relation. We rely on higher code levels to handle that
* scenario by closing and re-opening the md fd, which is handled via
* relcache flush. (Since the checkpointer doesn't participate in
- * relcache flush, it could have segment chain entries for inactive
- * segments; that's OK because the checkpointer never needs to compute
- * relation size.)
+ * relcache flush, it could have segment entries for inactive segments;
+ * that's OK because the checkpointer never needs to compute relation
+ * size.)
*/
- while (v->mdfd_chain != NULL)
- {
- segno++;
- v = v->mdfd_chain;
- }
+ segno = reln->md_num_open_segs[forknum] - 1;
+ v = &reln->md_seg_fds[forknum][segno];
for (;;)
{
@@ -909,21 +911,16 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
*/
segno++;
- if (v->mdfd_chain == NULL)
- {
- /*
- * We used to pass O_CREAT here, but that's has the disadvantage
- * that it might create a segment which has vanished through some
- * operating system misadventure. In such a case, creating the
- * segment here undermines _mdfd_getseg's attempts to notice and
- * report an error upon access to a missing segment.
- */
- v->mdfd_chain = _mdfd_openseg(reln, forknum, segno, 0);
- if (v->mdfd_chain == NULL)
- return segno * ((BlockNumber) RELSEG_SIZE);
- }
-
- v = v->mdfd_chain;
+ /*
+ * We used to pass O_CREAT here, but that's has the disadvantage that
+ * it might create a segment which has vanished through some operating
+ * system misadventure. In such a case, creating the segment here
+ * undermines _mdfd_getseg's attempts to notice and report an error
+ * upon access to a missing segment.
+ */
+ v = _mdfd_openseg(reln, forknum, segno, 0);
+ if (v == NULL)
+ return segno * ((BlockNumber) RELSEG_SIZE);
}
}
@@ -933,9 +930,9 @@ mdnblocks(SMgrRelation reln, ForkNumber forknum)
void
mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
{
- MdfdVec *v;
BlockNumber curnblk;
BlockNumber priorblocks;
+ int curopensegs;
/*
* NOTE: mdnblocks makes sure we have opened all active segments, so that
@@ -955,19 +952,24 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
if (nblocks == curnblk)
return; /* no work */
- v = mdopen(reln, forknum, EXTENSION_FAIL);
-
- priorblocks = 0;
- while (v != NULL)
+ /*
+ * Truncate segments, starting at the last one. Starting at the end makes
+ * managing the memory for the fd array easier, should there be errors.
+ */
+ curopensegs = reln->md_num_open_segs[forknum];
+ while (curopensegs > 0)
{
- MdfdVec *ov = v;
+ MdfdVec *v;
+
+ priorblocks = (curopensegs - 1) * RELSEG_SIZE;
+
+ v = &reln->md_seg_fds[forknum][curopensegs - 1];
if (priorblocks > nblocks)
{
/*
- * This segment is no longer active (and has already been unlinked
- * from the mdfd_chain). We truncate the file, but do not delete
- * it, for reasons explained in the header comments.
+ * This segment is no longer active. We truncate the file, but do
+ * not delete it, for reasons explained in the header comments.
*/
if (FileTruncate(v->mdfd_vfd, 0) < 0)
ereport(ERROR,
@@ -977,20 +979,20 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forknum, v);
- v = v->mdfd_chain;
- Assert(ov != reln->md_fd[forknum]); /* we never drop the 1st
- * segment */
- pfree(ov);
+
+ /* we never drop the 1st segment */
+ Assert(v != &reln->md_seg_fds[forknum][0]);
+
+ _fdvec_resize(reln, forknum, curopensegs - 1);
}
else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
{
/*
* This is the last segment we want to keep. Truncate the file to
- * the right length, and clear chain link that points to any
- * remaining segments (which we shall zap). NOTE: if nblocks is
- * exactly a multiple K of RELSEG_SIZE, we will truncate the K+1st
- * segment to 0 length but keep it. This adheres to the invariant
- * given in the header comments.
+ * the right length. NOTE: if nblocks is exactly a multiple K of
+ * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
+ * keep it. This adheres to the invariant given in the header
+ * comments.
*/
BlockNumber lastsegblocks = nblocks - priorblocks;
@@ -1002,18 +1004,16 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
nblocks)));
if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forknum, v);
- v = v->mdfd_chain;
- ov->mdfd_chain = NULL;
}
else
{
/*
- * We still need this segment and 0 or more blocks beyond it, so
- * nothing to do here.
+ * We still need this segment, so nothing to do for this and any
+ * earlier segment.
*/
- v = v->mdfd_chain;
+ break;
}
- priorblocks += RELSEG_SIZE;
+ curopensegs--;
}
}
@@ -1026,7 +1026,7 @@ mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
void
mdimmedsync(SMgrRelation reln, ForkNumber forknum)
{
- MdfdVec *v;
+ int segno;
/*
* NOTE: mdnblocks makes sure we have opened all active segments, so that
@@ -1034,16 +1034,18 @@ mdimmedsync(SMgrRelation reln, ForkNumber forknum)
*/
mdnblocks(reln, forknum);
- v = mdopen(reln, forknum, EXTENSION_FAIL);
+ segno = reln->md_num_open_segs[forknum];
- while (v != NULL)
+ while (segno > 0)
{
+ MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
+
if (FileSync(v->mdfd_vfd) < 0)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m",
FilePathName(v->mdfd_vfd))));
- v = v->mdfd_chain;
+ segno--;
}
}
@@ -1706,12 +1708,34 @@ ForgetDatabaseFsyncRequests(Oid dbid)
/*
- * _fdvec_alloc() -- Make a MdfdVec object.
+ * _fdvec_resize() -- Resize the fork's open segments array
*/
-static MdfdVec *
-_fdvec_alloc(void)
+static void
+_fdvec_resize(SMgrRelation reln,
+ ForkNumber forknum,
+ int nseg)
{
- return (MdfdVec *) MemoryContextAlloc(MdCxt, sizeof(MdfdVec));
+ if (nseg == 0)
+ {
+ if (reln->md_num_open_segs[forknum] > 0)
+ {
+ pfree(reln->md_seg_fds[forknum]);
+ reln->md_seg_fds[forknum] = NULL;
+ }
+ }
+ else if (reln->md_num_open_segs[forknum] == 0)
+ {
+ reln->md_seg_fds[forknum] =
+ MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
+ }
+ else
+ {
+ reln->md_seg_fds[forknum] =
+ repalloc(reln->md_seg_fds[forknum],
+ sizeof(MdfdVec) * nseg);
+ }
+
+ reln->md_num_open_segs[forknum] = nseg;
}
/*
@@ -1759,13 +1783,14 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
if (fd < 0)
return NULL;
- /* allocate an mdfdvec entry for it */
- v = _fdvec_alloc();
+ if (segno <= reln->md_num_open_segs[forknum])
+ _fdvec_resize(reln, forknum, segno + 1);
/* fill the entry */
+ v = &reln->md_seg_fds[forknum][segno];
v->mdfd_vfd = fd;
v->mdfd_segno = segno;
- v->mdfd_chain = NULL;
+
Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
/* all done */
@@ -1784,7 +1809,7 @@ static MdfdVec *
_mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
bool skipFsync, int behavior)
{
- MdfdVec *v = mdopen(reln, forknum, behavior);
+ MdfdVec *v;
BlockNumber targetseg;
BlockNumber nextsegno;
@@ -1792,98 +1817,117 @@ _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
Assert(behavior &
(EXTENSION_FAIL | EXTENSION_CREATE | EXTENSION_RETURN_NULL));
- if (!v)
- return NULL; /* if behavior & EXTENSION_RETURN_NULL */
-
targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
- for (nextsegno = 1; nextsegno <= targetseg; nextsegno++)
+
+ /* if an existing and opened segment, we're done */
+ if (targetseg < reln->md_num_open_segs[forknum])
{
+ v = &reln->md_seg_fds[forknum][targetseg];
+ return v;
+ }
+
+ /*
+ * The target segment is not yet open. Iterate over all the segments
+ * between the last opened and the target segment. This way missing
+ * segments either raise an error, or get created (according to
+ * 'behavior'). Start with either the last opened, or the first segment if
+ * none was opened before.
+ */
+ if (reln->md_num_open_segs[forknum] > 0)
+ v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
+ else
+ {
+ v = mdopen(reln, forknum, behavior);
+ if (!v)
+ return NULL; /* if behavior & EXTENSION_RETURN_NULL */
+ }
+
+ for (nextsegno = reln->md_num_open_segs[forknum];
+ nextsegno <= targetseg; nextsegno++)
+ {
+ BlockNumber nblocks = _mdnblocks(reln, forknum, v);
+ int flags = 0;
+
Assert(nextsegno == v->mdfd_segno + 1);
- if (v->mdfd_chain == NULL)
+ if (nblocks > ((BlockNumber) RELSEG_SIZE))
+ elog(FATAL, "segment too big");
+
+ if ((behavior & EXTENSION_CREATE) ||
+ (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
{
- BlockNumber nblocks = _mdnblocks(reln, forknum, v);
- int flags = 0;
-
- if (nblocks > ((BlockNumber) RELSEG_SIZE))
- elog(FATAL, "segment too big");
-
- if ((behavior & EXTENSION_CREATE) ||
- (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
+ /*
+ * Normally we will create new segments only if authorized by
+ * the caller (i.e., we are doing mdextend()). But when doing
+ * WAL recovery, create segments anyway; this allows cases
+ * such as replaying WAL data that has a write into a
+ * high-numbered segment of a relation that was later deleted.
+ * We want to go ahead and create the segments so we can
+ * finish out the replay. However if the caller has specified
+ * EXTENSION_REALLY_RETURN_NULL, then extension is not desired
+ * even in recovery; we won't reach this point in that case.
+ *
+ * We have to maintain the invariant that segments before the
+ * last active segment are of size RELSEG_SIZE; therefore, if
+ * extending, pad them out with zeroes if needed. (This only
+ * matters if in recovery, or if the caller is extending the
+ * relation discontiguously, but that can happen in hash
+ * indexes.)
+ */
+ if (nblocks < ((BlockNumber) RELSEG_SIZE))
{
- /*
- * Normally we will create new segments only if authorized by
- * the caller (i.e., we are doing mdextend()). But when doing
- * WAL recovery, create segments anyway; this allows cases
- * such as replaying WAL data that has a write into a
- * high-numbered segment of a relation that was later deleted.
- * We want to go ahead and create the segments so we can
- * finish out the replay. However if the caller has specified
- * EXTENSION_REALLY_RETURN_NULL, then extension is not desired
- * even in recovery; we won't reach this point in that case.
- *
- * We have to maintain the invariant that segments before the
- * last active segment are of size RELSEG_SIZE; therefore, if
- * extending, pad them out with zeroes if needed. (This only
- * matters if in recovery, or if the caller is extending the
- * relation discontiguously, but that can happen in hash
- * indexes.)
- */
- if (nblocks < ((BlockNumber) RELSEG_SIZE))
- {
- char *zerobuf = palloc0(BLCKSZ);
+ char *zerobuf = palloc0(BLCKSZ);
- mdextend(reln, forknum,
- nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
- zerobuf, skipFsync);
- pfree(zerobuf);
- }
- flags = O_CREAT;
- }
- else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
- nblocks < ((BlockNumber) RELSEG_SIZE))
- {
- /*
- * When not extending (or explicitly including truncated
- * segments), only open the next segment if the current one is
- * exactly RELSEG_SIZE. If not (this branch), either return
- * NULL or fail.
- */
- if (behavior & EXTENSION_RETURN_NULL)
- {
- /*
- * Some callers discern between reasons for _mdfd_getseg()
- * returning NULL based on errno. As there's no failing
- * syscall involved in this case, explicitly set errno to
- * ENOENT, as that seems the closest interpretation.
- */
- errno = ENOENT;
- return NULL;
- }
-
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
- _mdfd_segpath(reln, forknum, nextsegno),
- blkno, nblocks)));
- }
-
- v->mdfd_chain = _mdfd_openseg(reln, forknum, nextsegno, flags);
-
- if (v->mdfd_chain == NULL)
- {
- if ((behavior & EXTENSION_RETURN_NULL) &&
- FILE_POSSIBLY_DELETED(errno))
- return NULL;
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not open file \"%s\" (target block %u): %m",
- _mdfd_segpath(reln, forknum, nextsegno),
- blkno)));
+ mdextend(reln, forknum,
+ nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
+ zerobuf, skipFsync);
+ pfree(zerobuf);
}
+ flags = O_CREAT;
+ }
+ else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
+ nblocks < ((BlockNumber) RELSEG_SIZE))
+ {
+ /*
+ * When not extending (or explicitly including truncated
+ * segments), only open the next segment if the current one is
+ * exactly RELSEG_SIZE. If not (this branch), either return
+ * NULL or fail.
+ */
+ if (behavior & EXTENSION_RETURN_NULL)
+ {
+ /*
+ * Some callers discern between reasons for _mdfd_getseg()
+ * returning NULL based on errno. As there's no failing
+ * syscall involved in this case, explicitly set errno to
+ * ENOENT, as that seems the closest interpretation.
+ */
+ errno = ENOENT;
+ return NULL;
+ }
+
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
+ _mdfd_segpath(reln, forknum, nextsegno),
+ blkno, nblocks)));
+ }
+
+ v = _mdfd_openseg(reln, forknum, nextsegno, flags);
+
+ if (v == NULL)
+ {
+ if ((behavior & EXTENSION_RETURN_NULL) &&
+ FILE_POSSIBLY_DELETED(errno))
+ return NULL;
+ ereport(ERROR,
+ (errcode_for_file_access(),
+ errmsg("could not open file \"%s\" (target block %u): %m",
+ _mdfd_segpath(reln, forknum, nextsegno),
+ blkno)));
}
- v = v->mdfd_chain;
}
+
return v;
}
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index 94aa952..93f0080 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -174,7 +174,7 @@ smgropen(RelFileNode rnode, BackendId backend)
/* mark it not open */
for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
- reln->md_fd[forknum] = NULL;
+ reln->md_num_open_segs[forknum] = 0;
/* it has no owner yet */
add_to_unowned_list(reln);
@@ -379,7 +379,7 @@ smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
* Exit quickly in WAL replay mode if we've already opened the file. If
* it's open, it surely must exist.
*/
- if (isRedo && reln->md_fd[forknum] != NULL)
+ if (isRedo && reln->md_num_open_segs[forknum] > 0)
return;
/*
diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h
index a8e7877..3430d86 100644
--- a/src/include/storage/smgr.h
+++ b/src/include/storage/smgr.h
@@ -64,8 +64,12 @@ typedef struct SMgrRelationData
*/
int smgr_which; /* storage manager selector */
- /* for md.c; NULL for forks that are not open */
- struct _MdfdVec *md_fd[MAX_FORKNUM + 1];
+ /*
+ * for md.c; per-fork arrays of the number of open segments
+ * (md_num_open_segs) and the segments themselves (md_seg_fds).
+ */
+ int md_num_open_segs[MAX_FORKNUM + 1];
+ struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
/* if unowned, list link in list of all unowned SMgrRelations */
struct SMgrRelationData *next_unowned_reln;
--
2.8.1
0002-Faster-PageIsVerified-for-the-all-zeroes-case.patch (text/x-patch, 1.5K) download
From d9321d75a48ac8b817fa469f064c76ce54b84919 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 15 Dec 2015 19:03:10 +0100
Subject: [PATCH 2/2] Faster PageIsVerified() for the all zeroes case.
That's primarily useful for testing the handling of very large
relations, using sparse files.
---
src/backend/storage/page/bufpage.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index f2a07f2..1b70bfb 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -81,7 +81,7 @@ bool
PageIsVerified(Page page, BlockNumber blkno)
{
PageHeader p = (PageHeader) page;
- char *pagebytes;
+ size_t *pagebytes;
int i;
bool checksum_failure = false;
bool header_sane = false;
@@ -118,10 +118,17 @@ PageIsVerified(Page page, BlockNumber blkno)
return true;
}
- /* Check all-zeroes case */
+ /*
+ * Check all-zeroes case. Luckily BLCKSZ is guaranteed to always be a
+ * multiple of size_t - and it's much faster to compare memory using the
+ * native word size.
+ */
+ StaticAssertStmt(BLCKSZ == (BLCKSZ / sizeof(size_t)) * sizeof(size_t),
+ "BLCKSZ has to be a multiple of sizeof(size_t)");
+
all_zeroes = true;
- pagebytes = (char *) page;
- for (i = 0; i < BLCKSZ; i++)
+ pagebytes = (size_t *) page;
+ for (i = 0; i < (BLCKSZ / sizeof(size_t)); i++)
{
if (pagebytes[i] != 0)
{
--
2.8.1
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-19 00:27 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
1 sibling, 1 reply; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-19 00:27 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Thu, Aug 18, 2016 at 5:26 PM, Andres Freund <andres@anarazel.de> wrote:
> Rebased version attached. A review would be welcome. Plan to push this
> forward otherwise in the not too far away future.
I can review this next week.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-19 00:28 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2016-08-19 00:28 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-08-18 17:27:59 -0700, Peter Geoghegan wrote:
> On Thu, Aug 18, 2016 at 5:26 PM, Andres Freund <andres@anarazel.de> wrote:
> > Rebased version attached. A review would be welcome. Plan to push this
> > forward otherwise in the not too far away future.
>
> I can review this next week.
Thanks
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-19 00:35 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-19 00:35 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Thu, Aug 18, 2016 at 5:28 PM, Andres Freund <andres@anarazel.de> wrote:
>> I can review this next week.
>
> Thanks
Given the time frame that you have in mind, I won't revisit the
question the parallel CLUSTER CPU bottleneck issue until this is
committed. The patch might change things enough that that would be a
waste of time.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-19 00:42 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2016-08-19 00:42 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-08-18 17:35:47 -0700, Peter Geoghegan wrote:
> On Thu, Aug 18, 2016 at 5:28 PM, Andres Freund <andres@anarazel.de> wrote:
> >> I can review this next week.
> >
> > Thanks
>
> Given the time frame that you have in mind, I won't revisit the
> question the parallel CLUSTER CPU bottleneck issue until this is
> committed. The patch might change things enough that that would be a
> waste of time.
How large was the index & table in question? I mean this really only
comes into effect at 100+ segments.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-19 00:43 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 0 replies; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-19 00:43 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Thu, Aug 18, 2016 at 5:42 PM, Andres Freund <andres@anarazel.de> wrote:
> How large was the index & table in question? I mean this really only
> comes into effect at 100+ segments.
Not that big, but I see no reason to take the chance, I suppose.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-31 21:09 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
1 sibling, 2 replies; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-31 21:09 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Thu, Aug 18, 2016 at 5:26 PM, Andres Freund <andres@anarazel.de> wrote:
> Rebased version attached. A review would be welcome. Plan to push this
> forward otherwise in the not too far away future.
This looks good.
The only thing that stuck out to any degree is that we don't grow the
"reln->md_seg_fds[forknum]" array within the new _fdvec_resize()
function geometrically. In other words, we don't separately keep track
of the array size and allocated size, and grow the allocated size
using the doubling strategy that you see in many places.
Now, I freely admit that that probably doesn't matter, given what that
array tracks. I'm just pointing out that that aspect did give me
pause. The struct MdfdVec is small enough that that *might* be
worthwhile.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-31 21:25 Peter Geoghegan <pg@heroku.com>
parent: Peter Geoghegan <pg@heroku.com>
1 sibling, 0 replies; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-31 21:25 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Wed, Aug 31, 2016 at 2:09 PM, Peter Geoghegan <pg@heroku.com> wrote:
> The only thing that stuck out to any degree is that we don't grow the
> "reln->md_seg_fds[forknum]" array within the new _fdvec_resize()
> function geometrically.
That new function looks like this:
> +static void
> +_fdvec_resize(SMgrRelation reln,
> + ForkNumber forknum,
> + int nseg)
> {
> - return (MdfdVec *) MemoryContextAlloc(MdCxt, sizeof(MdfdVec));
> + if (nseg == 0)
> + {
> + if (reln->md_num_open_segs[forknum] > 0)
> + {
> + pfree(reln->md_seg_fds[forknum]);
> + reln->md_seg_fds[forknum] = NULL;
> + }
> + }
> + else if (reln->md_num_open_segs[forknum] == 0)
> + {
> + reln->md_seg_fds[forknum] =
> + MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
> + }
> + else
> + {
> + reln->md_seg_fds[forknum] =
> + repalloc(reln->md_seg_fds[forknum],
> + sizeof(MdfdVec) * nseg);
> + }
> +
> + reln->md_num_open_segs[forknum] = nseg;
> }
Another tiny tweak that you might consider occurs to me here: It
couldn't hurt to "Assert(reln->md_seg_fds[forknum] == NULL)" within
the "else if (reln->md_num_open_segs[forknum] == 0)" path here, prior
to the MemoryContextAlloc(). If it's worth setting it to NULL when
there are 0 segs (i.e., "reln->md_seg_fds[forknum] = NULL"), then
perhaps it's worth checking that things are found that way later.
I guess that this is at odds with remarks in my last mail about
considering geometric allocations for the "reln->md_seg_fds[forknum]"
array. Both feedback items are more or less just things that bothered
me ever so slightly, which I don't necessarily expect you to act on,
but assume you'd like to hear.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-31 21:37 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
1 sibling, 1 reply; 25+ messages in thread
From: Andres Freund @ 2016-08-31 21:37 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-08-31 14:09:47 -0700, Peter Geoghegan wrote:
> On Thu, Aug 18, 2016 at 5:26 PM, Andres Freund <andres@anarazel.de> wrote:
> > Rebased version attached. A review would be welcome. Plan to push this
> > forward otherwise in the not too far away future.
>
> This looks good.
Thanks for looking!
> The only thing that stuck out to any degree is that we don't grow the
> "reln->md_seg_fds[forknum]" array within the new _fdvec_resize()
> function geometrically. In other words, we don't separately keep track
> of the array size and allocated size, and grow the allocated size
> using the doubling strategy that you see in many places.
I can't really see that being worth the code here. The cost of
open()/lseek()'ing etc. is going to dwarf the cost of this.
Regards,
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-31 22:06 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-31 22:06 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Wed, Aug 31, 2016 at 2:37 PM, Andres Freund <andres@anarazel.de> wrote:
>> This looks good.
>
> Thanks for looking!
No problem.
In other painfully pedantic news, I should point out that
sizeof(size_t) isn't necessarily word size (the most generic
definition of word size for the architecture), contrary to my reading
of the 0002-* patch comments. I'm mostly talking thinking about x86_64
here, of course.
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-31 22:08 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
0 siblings, 1 reply; 25+ messages in thread
From: Andres Freund @ 2016-08-31 22:08 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On August 31, 2016 3:06:23 PM PDT, Peter Geoghegan <pg@heroku.com> wrote:
>In other painfully pedantic news, I should point out that
>sizeof(size_t) isn't necessarily word size (the most generic
>definition of word size for the architecture), contrary to my reading
>of the 0002-* patch comments. I'm mostly talking thinking about x86_64
>here, of course.
Uh?
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-08-31 22:15 Peter Geoghegan <pg@heroku.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 25+ messages in thread
From: Peter Geoghegan @ 2016-08-31 22:15 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On Wed, Aug 31, 2016 at 3:08 PM, Andres Freund <andres@anarazel.de> wrote:
> On August 31, 2016 3:06:23 PM PDT, Peter Geoghegan <pg@heroku.com> wrote:
>
>>In other painfully pedantic news, I should point out that
>>sizeof(size_t) isn't necessarily word size (the most generic
>>definition of word size for the architecture), contrary to my reading
>>of the 0002-* patch comments. I'm mostly talking thinking about x86_64
>>here, of course.
>
> Uh?
Sorry, I really should have not said anything. It is true that x86_64
word size is sometimes reported as 16 and/or 32 bits [1], because of
legacy issues.
[1] https://en.wikipedia.org/wiki/Word_(computer_architecture)#Table_of_word_sizes
--
Peter Geoghegan
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
* Re: _mdfd_getseg can be expensive
@ 2016-09-09 00:43 Andres Freund <andres@anarazel.de>
parent: Peter Geoghegan <pg@heroku.com>
0 siblings, 0 replies; 25+ messages in thread
From: Andres Freund @ 2016-09-09 00:43 UTC (permalink / raw)
To: Peter Geoghegan <pg@heroku.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <kgrittn@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers
On 2016-08-31 15:15:16 -0700, Peter Geoghegan wrote:
> On Wed, Aug 31, 2016 at 3:08 PM, Andres Freund <andres@anarazel.de> wrote:
> > On August 31, 2016 3:06:23 PM PDT, Peter Geoghegan <pg@heroku.com> wrote:
> >
> >>In other painfully pedantic news, I should point out that
> >>sizeof(size_t) isn't necessarily word size (the most generic
> >>definition of word size for the architecture), contrary to my reading
> >>of the 0002-* patch comments. I'm mostly talking thinking about x86_64
> >>here, of course.
> >
> > Uh?
>
> Sorry, I really should have not said anything. It is true that x86_64
> word size is sometimes reported as 16 and/or 32 bits [1], because of
> legacy issues.
I think native word size describes the issue well enough. And, more
importantly, I can't think of an equally short but more accurate
description.
I've pushed the patches. Thanks for the review.
Andres
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 25+ messages in thread
end of thread, other threads:[~2016-09-09 00:43 UTC | newest]
Thread overview: 25+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-03-31 10:10 _mdfd_getseg can be expensive Andres Freund <andres@2ndquadrant.com>
2014-10-31 22:32 ` Andres Freund <andres@2ndquadrant.com>
2014-10-31 22:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-10-31 22:58 ` Andres Freund <andres@2ndquadrant.com>
2014-11-01 16:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-11-01 17:23 ` Andres Freund <andres@2ndquadrant.com>
2015-12-15 18:04 ` Andres Freund <andres@anarazel.de>
2016-07-01 01:14 ` Peter Geoghegan <pg@heroku.com>
2016-07-01 01:23 ` Andres Freund <andres@anarazel.de>
2016-07-01 01:34 ` Peter Geoghegan <pg@heroku.com>
2016-07-01 02:08 ` Andres Freund <andres@anarazel.de>
2016-07-01 21:18 ` Peter Geoghegan <pg@heroku.com>
2016-08-19 00:26 ` Andres Freund <andres@anarazel.de>
2016-08-19 00:27 ` Peter Geoghegan <pg@heroku.com>
2016-08-19 00:28 ` Andres Freund <andres@anarazel.de>
2016-08-19 00:35 ` Peter Geoghegan <pg@heroku.com>
2016-08-19 00:42 ` Andres Freund <andres@anarazel.de>
2016-08-19 00:43 ` Peter Geoghegan <pg@heroku.com>
2016-08-31 21:09 ` Peter Geoghegan <pg@heroku.com>
2016-08-31 21:25 ` Peter Geoghegan <pg@heroku.com>
2016-08-31 21:37 ` Andres Freund <andres@anarazel.de>
2016-08-31 22:06 ` Peter Geoghegan <pg@heroku.com>
2016-08-31 22:08 ` Andres Freund <andres@anarazel.de>
2016-08-31 22:15 ` Peter Geoghegan <pg@heroku.com>
2016-09-09 00:43 ` Andres Freund <andres@anarazel.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox