Hi,

To read or write block B of a relation it does not have open, a backend opens 
every segment up to B's, checking that each earlier one is full. 
AtEOXact_SMgr() closes the SMgrRelations that no relcache entry pins 
(ff3f9c8de5, 21d9c3ee4e), so a backend repeats those opens in every transaction 
in which it touches such a relation. The common case is a backend writing out a 
dirty victim buffer of a relation it is not otherwise using, often one in 
another database. With large relations accessed at random, much of the system 
time then goes into open() and close() of segments opened only to check their 
size.

The attached patch, for master, opens only the target segment for reads, writes 
and prefetches of existing blocks. Such "detached" segments are kept in a 
per-fork hash table (lib/simplehash.h) apart from md_seg_fds, so md_seg_fds 
stays the gap-free array that mdnblocks(), mdtruncate() and the sync code work 
on; when md_seg_fds grows to reach a detached segment, its descriptor is moved 
over. If the target segment is missing, the sequential open runs and reports 
the error as before. Extension and WAL replay still open segments in sequence, 
since they may have to create or pad the segments in between.

The sequential open checks that earlier segments are full because md_seg_fds 
must contain only active segments (72a98a6395). Detached segments are not in 
md_seg_fds, so that does not apply to them. mdsyncfiletag() has opened its 
target segment by name since 7bb3102cea, and 48c142f78d removed the 
EXTENSION_DONT_CHECK_SIZE mode it no longer needed; this does the same for 
reads, writes and prefetches.

I ran into this with Senzing, an entity resolution engine, loading a 2.8 TB 
database (4,042 segment files) on master plus the in-progress AIO write patches 
and some index prefetching changes, the same in both runs. Its backends wrote 
out about 125,000 dirty buffers per second, and the server opened about 3 
million segment files per second. With an earlier revision of this patch that 
fell to about 110,000 per second, system CPU on the database host fell from 
about 50% to 34%, and the load ran 20% faster (2,768 to 3,315 records per 
second).

Results

update_xdb from the kit below: half the clients run single-row HOT updates on a 
table in a second database, so that backends write out each other's dirty 
buffers. Transactions per second, median of five runs alternating builds (60 
seconds with 8 kB segments, 120 seconds otherwise), and the server's system CPU 
time per transaction (postmaster and its children, pgbench excluded):
segment size    clients    master   patched          sys ms/xact
8 kB (1 block)        8       129    11,182   (86x)   53    -> 0.19
8 kB (1 block)       64       191    43,839  (230x)  316    -> 0.35
8 MB                  8     2,456    11,620  (4.7x)    2.41 -> 0.16
8 MB                 64     8,557    45,213  (5.3x)    6.34 -> 0.27
1 GB                  8     8,248    10,298  (+25%)    0.40 -> 0.25
1 GB                 64    33,479    34,938   (+4%)    0.66 -> 0.43
The 8 kB case also has max_files_per_process = 1024 and is a stress test, not a 
configuration anyone runs. The same with 1 GB segments on local NVMe (ext4) 
with fsync on, two 121 GB tables, a 64 GB memory limit and the data evicted 
from the page cache before each run, median of three 300-second runs:
clients     master   patched          sys ms/xact
     8      3,222     3,572   (+11%)    0.60 -> 0.29
    64      9,492    11,075   (+17%)    1.31 -> 0.41
Single runs on disk overlap between the builds in throughput (8 clients: 
2,814-3,242 and 2,276-3,793; 64 clients: 8,729-10,877 and 10,635-11,177), but 
not in system time per transaction (0.60-0.69 and 0.28-0.47 ms; 1.09-1.53 and 
0.41-0.42 ms).

Workloads that write out no other relation's buffers do not change. Point 
lookups with 8 kB segments, slow in both builds because each lookup reads a 
block in its own file: 3,057 vs 3,086 (8 clients), 19,540 vs 19,602 (64). Stock 
pgbench with default settings, scale 1000:
                clients     master   patched
tpcb-like              8     22,186    22,764
tpcb-like             64     61,452    66,551   (runs 55.7k-71.6k
                                                and 55.8k-70.4k)
select-only            8    141,976   142,348
select-only           64    927,613   935,533
The 64-client tpcb-like difference is within the run-to-run range.

Conditions: two-socket Xeon Platinum 8280M (112 threads), Linux 7.0, data 
directory on tmpfs inside a container with a memory limit, so these measure CPU 
rather than I/O. The update_xdb runs used synchronous_commit = off, autovacuum 
= off, checkpoint_timeout = 60s and shared_buffers = 1GB (128MB for 8 kB 
segments); the stock pgbench runs used default settings. Every run restarts the 
server and starts from frozen databases. The disk runs used a two-socket Xeon 
Gold 6438Y+ and shared_buffers = 16GB. The kit is at

https://github.com/brianmacy/postgres/tree/md-segbench-kit/src/tools/aio_bench

and its md_ab.sh runs the same comparison from two install directories.

The case that puts the most entries in the hash table is one transaction 
writing out many buffers of one relation in random order, each in its own 
segment: 150,000 buffers with 8 kB segments, median of three, milliseconds:
            random order   block order
master          3,098          1,952
patched         2,470          1,959
so the hash table costs nothing measurable there.

Keeping SMgrRelations across transactions instead would undo what ff3f9c8de5 
set out to do. A backend sitting idle after writing out another database's 
buffers reads no invalidation messages until it starts a transaction or falls 
far enough behind to get a catchup interrupt, so it keeps descriptors of that 
table's files after they are dropped. With 8 kB segments, a backend built that 
way held all 11,781 segments of a dropped table for its whole 130-second idle 
period; master and this patch held none. A larger segment size also shortens 
the walk, but it is chosen when the server is built and cannot be changed for 
an existing cluster.

While testing I noticed that I/O workers keep descriptors of dropped relations: 
smgr_aio_reopen() does an unpinned smgropen(), and a worker never reaches 
AtEOXact_SMgr(), so after DROP TABLE "io worker 0" kept 5,895 truncated, 
unlinked segments open until DROP DATABASE. That is independent of this patch; 
I will start a separate thread.

The TAP test (src/test/modules/test_misc/t/017_md_detached_segments.pl) needs 
relations of many segments, so it runs only with segments of at most 32 blocks, 
as in the --with-segsize-blocks=6 build of the "Linux - Autoconf" CI job.

--
Brian Macy

Attachment: v1-0001-Open-only-the-target-segment-to-reach-existing-bl.patch
Description: Binary data

Reply via email to