Re: refactoring relation extension and BufferAlloc(), faster COPY
Andres Freund <andres@anarazel.de>
Hi, On 2023-02-22 11:18:57 +0200, Heikki Linnakangas wrote: > On 21/02/2023 21:22, Andres Freund wrote: > > On 2023-02-21 18:18:02 +0200, Heikki Linnakangas wrote: > > > Is it ever possible to call this without a relcache entry? WAL redo > > > functions do that with ReadBuffer, but they only extend a relation > > > implicitly, by replay a record for a particular block. > > > > I think we should use it for crash recovery as well, but the patch doesn't > > yet. We have some gnarly code there, see the loop using P_NEW in > > XLogReadBufferExtended(). Extending the file one-by-one is a lot more > > expensive than doing it in bulk. > > Hmm, XLogReadBufferExtended() could use smgrzeroextend() to fill the gap, > and then call ExtendRelationBuffered for the target page. Or the new > ExtendRelationBufferedTo() function that you mentioned. I don't think it's safe to just use smgrzeroextend(). Without the page-level interlock from the buffer entry, a concurrent reader can read/write the extended portion of the relation, while we're extending. That can lead to loosing writes. It also turns out that just doing smgrzeroextend(), without filling s_b, is often bad for performance, because it may cause reads when trying to fill the buffers. Although hopefully that's less of an issue during WAL replay, due to REGBUF_WILL_INIT. > In the common case that you load a lot of data to a relation extending it, > and then crash, the WAL replay would still extend the relation one page at a > time, which is inefficient. Changing that would need bigger changes, to > WAL-log the relation extension as a separate WAL record, for example. I > don't think we need to solve that right now, it can be addressed separately > later. Yea, that seems indeed something for later. There's several things we could do without adding WAL logging of relation extension themselves. One relatively easy thing would be to add information about the number of blocks we're extending by to XLOG_HEAP2_MULTI_INSERT records. Compared to the insertions themselves that'd barely be noticable. A slightly more complicated thing would be to peek ahead in the WAL (we have infrastructure for that now) and extend by enough for the next few relation extensions. Greetings, Andres Freund
Commits
-
lwlock: Fix quadratic behavior with very long wait lists
- 81038228582a 12.18 landed
- dc9d424cf0cd 13.14 landed
- 5f6ec27a6477 14.11 landed
- f374fb4aab3e 15.6 landed
- a4adc31f6902 16.0 landed
-
Support RBM_ZERO_AND_CLEANUP_LOCK in ExtendBufferedRelTo(), add tests
- 43a33ef54e50 16.0 landed
-
Use ExtendBufferedRelTo() in XLogReadBufferExtended()
- 26158b852d3a 16.0 landed
-
hio: Use ExtendBufferedRelBy() to extend tables more efficiently
- 00d1e02be249 16.0 landed
-
heapam: Pass number of required pages to RelationGetBufferForTuple()
- 5279e9db8e8d 16.0 landed
-
Convert many uses of ReadBuffer[Extended](P_NEW) to ExtendBufferedRel()
- acab1b0914e4 16.0 landed
-
Use ExtendBufferedRelTo() in {vm,fsm}_extend()
- fcdda1e4b502 16.0 landed
-
bufmgr: Introduce infrastructure for faster relation extension
- 31966b151e6a 16.0 landed
-
bufmgr: Support multiple in-progress IOs by using resowner
- 12f3867f5534 16.0 landed
-
bufmgr: Acquire and clean victim buffer separately
- dad50f677c42 16.0 landed
-
bufmgr: Add Pin/UnpinLocalBuffer()
- 794f25944790 16.0 landed
-
bufmgr: Add some more error checking [infrastructure] around pinning
- 819b69a81d30 16.0 landed
-
Add smgrzeroextend(), FileZero(), FileFallocate()
- 4d330a61bb19 16.0 landed
-
Don't initialize page in {vm,fsm}_extend(), not needed
- 3d6a98457d8e 16.0 landed
-
bufmgr: Remove buffer-write-dirty tracepoints
- 8a2b1b147728 16.0 landed
-
hio: Release extension lock before initializing page / pinning VM
- 14f98e0af996 16.0 landed
-
bufmgr: Fix undefined behaviour with, unrealistically, large temp_buffers
- 558cf8038768 16.0 landed
-
Fix memory leak and inefficiency in CREATE DATABASE ... STRATEGY WAL_LOG
- 5df319f3d55d 16.0 cited
-
pgstat: Track more detailed relation IO statistics
- f30d62c2fc60 16.0 cited
-
Use unnamed POSIX semaphores on Cygwin.
- f2857af485a0 16.0 cited
-
Include RelFileLocator fields individually in BufferTag.
- 82ac34db2036 16.0 cited