Skip to content

Commit 31966b1

Browse files
committed
bufmgr: Introduce infrastructure for faster relation extension
The primary bottlenecks for relation extension are: 1) The extension lock is held while acquiring a victim buffer for the new page. Acquiring a victim buffer can require writing out the old page contents including possibly needing to flush WAL. 2) When extending via ReadBuffer() et al, we write a zero page during the extension, and then later write out the actual page contents. This can nearly double the write rate. 3) The existing bulk relation extension infrastructure in hio.c just amortized the cost of acquiring the relation extension lock, but none of the other costs. Unfortunately 1) cannot currently be addressed in a central manner as the callers to ReadBuffer() need to acquire the extension lock. To address that, this this commit moves the responsibility for acquiring the extension lock into bufmgr.c functions. That allows to acquire the relation extension lock for just the required time. This will also allow us to improve relation extension further, without changing callers. The reason we write all-zeroes pages during relation extension is that we hope to get ENOSPC errors earlier that way (largely works, except for CoW filesystems). It is easier to handle out-of-space errors gracefully if the page doesn't yet contain actual tuples. This commit addresses 2), by using the recently introduced smgrzeroextend(), which extends the relation, without dirtying the kernel page cache for all the extended pages. To address 3), this commit introduces a function to extend a relation by multiple blocks at a time. There are three new exposed functions: ExtendBufferedRel() for extending the relation by a single block, ExtendBufferedRelBy() to extend a relation by multiple blocks at once, and ExtendBufferedRelTo() for extending a relation up to a certain size. To avoid duplicating code between ReadBuffer(P_NEW) and the new functions, ReadBuffer(P_NEW) now implements relation extension with ExtendBufferedRel(), using a flag to tell ExtendBufferedRel() that the relation lock is already held. Note that this commit does not yet lead to a meaningful performance or scalability improvement - for that uses of ReadBuffer(P_NEW) will need to be converted to ExtendBuffered*(), which will be done in subsequent commits. Reviewed-by: Heikki Linnakangas <[email protected]> Reviewed-by: Melanie Plageman <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 8eda731 commit 31966b1

File tree

9 files changed

+901
-181
lines changed

9 files changed

+901
-181
lines changed

doc/src/sgml/monitoring.sgml

+31-12
Original file line numberDiff line numberDiff line change
@@ -7776,33 +7776,52 @@ FROM pg_stat_get_backend_idset() AS backendid;
77767776
<entry>Probe that fires when the two-phase portion of a checkpoint is
77777777
complete.</entry>
77787778
</row>
7779+
<row>
7780+
<entry><literal>buffer-extend-start</literal></entry>
7781+
<entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, unsigned int)</literal></entry>
7782+
<entry>Probe that fires when a relation extension starts.
7783+
arg0 contains the fork to be extended. arg1, arg2, and arg3 contain the
7784+
tablespace, database, and relation OIDs identifying the relation. arg4
7785+
is the ID of the backend which created the temporary relation for a
7786+
local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared
7787+
buffer. arg5 is the number of blocks the caller would like to extend
7788+
by.</entry>
7789+
</row>
7790+
<row>
7791+
<entry><literal>buffer-extend-done</literal></entry>
7792+
<entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, unsigned int, BlockNumber)</literal></entry>
7793+
<entry>Probe that fires when a relation extension is complete.
7794+
arg0 contains the fork to be extended. arg1, arg2, and arg3 contain the
7795+
tablespace, database, and relation OIDs identifying the relation. arg4
7796+
is the ID of the backend which created the temporary relation for a
7797+
local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared
7798+
buffer. arg5 is the number of blocks the relation was extended by, this
7799+
can be less than the number in the
7800+
<literal>buffer-extend-start</literal> due to resource
7801+
constraints. arg6 contains the BlockNumber of the first new
7802+
block.</entry>
7803+
</row>
77797804
<row>
77807805
<entry><literal>buffer-read-start</literal></entry>
7781-
<entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</literal></entry>
7806+
<entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</literal></entry>
77827807
<entry>Probe that fires when a buffer read is started.
7783-
arg0 and arg1 contain the fork and block numbers of the page (but
7784-
arg1 will be -1 if this is a relation extension request).
7808+
arg0 and arg1 contain the fork and block numbers of the page.
77857809
arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
77867810
identifying the relation.
77877811
arg5 is the ID of the backend which created the temporary relation for a
77887812
local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.
7789-
arg6 is true for a relation extension request, false for normal
7790-
read.</entry>
7813+
</entry>
77917814
</row>
77927815
<row>
77937816
<entry><literal>buffer-read-done</literal></entry>
7794-
<entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</literal></entry>
7817+
<entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</literal></entry>
77957818
<entry>Probe that fires when a buffer read is complete.
7796-
arg0 and arg1 contain the fork and block numbers of the page (if this
7797-
is a relation extension request, arg1 now contains the block number
7798-
of the newly added block).
7819+
arg0 and arg1 contain the fork and block numbers of the page.
77997820
arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
78007821
identifying the relation.
78017822
arg5 is the ID of the backend which created the temporary relation for a
78027823
local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.
7803-
arg6 is true for a relation extension request, false for normal
7804-
read.
7805-
arg7 is true if the buffer was found in the pool, false if not.</entry>
7824+
arg6 is true if the buffer was found in the pool, false if not.</entry>
78067825
</row>
78077826
<row>
78087827
<entry><literal>buffer-flush-start</literal></entry>

0 commit comments

Comments
 (0)