Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n06g2-0002eF-7s for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 18:49:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n06g0-0002XW-KO for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 18:49:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n06fz-0002XM-Um for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 18:49:24 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n06fv-0003sO-Sl for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 18:49:23 +0000 Received: by mail-ed1-x534.google.com with SMTP id o20so12150805eds.10 for ; Wed, 22 Dec 2021 10:49:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language :from:to:cc:references:in-reply-to; bh=YBhlGV2T6LJpHiI3DbeU2OAsq2wlrRsmFVJJRTYuBEM=; b=fkfdqwVA4jdBS55I5KQxBQkTF6TEapaMtEEZiGtV+uBt2oMPibLIxN59G2Yb+IGsKS MwBbQdvATV/OBffKUPEIVru0DfY8xqHGmeGo9S6a1ngMjpdm/Hg83wGtMfKlMzIUKXcq 4jMgznY4wgba1NnX8N88aTRmAIFRyRQi0kDNqj1vQ7jyDVfBoE1FAHrJOCvKuPLUV8T5 fgsxARm0Esf/rmUU39x7kpq40CsXquBFvOZUJ6sUV/jLPfmVvPvMj0c5SVqom1yrqqYs TyQsvyik38KR54iOrbfa908fauxs5383aMANrGaJEyeRkQNKnCUTag8plgsllhxDD0qZ PurQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:from:to:cc:references:in-reply-to; bh=YBhlGV2T6LJpHiI3DbeU2OAsq2wlrRsmFVJJRTYuBEM=; b=n0D7d7w8hHQdz2XPv38InBTw+MHuA5ctvHwX7c8ipByMb4OnugRWnQ96lvF3kwdrND M+5g/QjeCsnEht/Qmf+zVncmAa+vRVtBHYNcmrzC9t/Ns5o/smkH6k70I/gD8wO7AvTg aaCBNcFC6cu8OF/+1Za0VJoHLYeE1mCc0rQ1q89yRwON7JOGYwBq8ydrg/sOE3G8hQY8 vKAu9w098EKppEjsdPT4fm1a7BdKXeOgCxIv6Zubg0BWGW/o6hemJ4GKDAnRRjIznpKG slbQuZ4ydSBC1fWsWJrndyDyBBF7AczZb4+QMNtI9V3h0kIeC2vWWh4LucWxDQS+dupy KYsQ== X-Gm-Message-State: AOAM533ad1INLwkW7mU5Uywsw4eTeBTMlXG2TC+sL+qihJsKQXAv/Gv5 mDClDHYCHvx24zgL5t8v7EMANHa1QJSIdCNdzeWkpMChx4fVOusXFVqERQgxaUHlbckIrQwleR1 SLCGk5XKbinizPmlarq4uugV7djb+PzoGELtHX6Xv3dtvO0VYw+AzKmqb6ZRqrledRYSipRY2If 72mSBjWoW7t2VsQkBfD0p10T+DI8Oi0nQF0VhwqeJKB5kZ1wBTmTcl X-Google-Smtp-Source: ABdhPJzI5EBpKzlH0l3cK3YIKhDwjdY7d15Cg/hMfCduNSQ3SWqjn6jr2QhrKJ3uOjx2Xdnt1+6FAg== X-Received: by 2002:a17:906:340d:: with SMTP id c13mr3389045ejb.611.1640198958812; Wed, 22 Dec 2021 10:49:18 -0800 (PST) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id t20sm1168574edv.81.2021.12.22.10.49.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Dec 2021 10:49:18 -0800 (PST) Content-Type: multipart/mixed; boundary="------------ItU0fiAY5tJpPes7DHCRBCKe" Message-ID: <683c128a-5961-9b7b-ad68-0ea850e62363@enterprisedb.com> Date: Wed, 22 Dec 2021 19:49:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: sequences vs. synchronous replication Content-Language: en-US From: Tomas Vondra To: Tom Lane Cc: Peter Eisentraut , PostgreSQL Hackers References: <712cad46-a9c8-1389-aef8-faf0203c9be9@enterprisedb.com> <1452362.1639803168@sss.pgh.pa.us> <38a6a779-ee8d-de3c-fbd7-709285be88fa@enterprisedb.com> <7869761b-fdc7-6f7d-dc95-9ba8315a8ecf@enterprisedb.com> <1637476.1639862832@sss.pgh.pa.us> <8e24a470-b092-3c9f-0614-acd9df3f724f@enterprisedb.com> <8231183d-1077-5394-1ffa-0c9d8c3905b0@enterprisedb.com> <2683973.1640048519@sss.pgh.pa.us> In-Reply-To: X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ItU0fiAY5tJpPes7DHCRBCKe Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/21/21 03:49, Tomas Vondra wrote: > On 12/21/21 02:01, Tom Lane wrote: >> Tomas Vondra writes: >>> OK, I did a quick test with two very simple benchmarks - simple select >>> from a sequence, and 'pgbench -N' on scale 1. Benchmark was on current >>> master, patched means SEQ_LOG_VALS was set to 1. >> >> But ... pgbench -N doesn't use sequences at all, does it? >> >> Probably inserts into a table with a serial column would constitute a >> plausible real-world case. >> > > D'oh! For some reason I thought pgbench has a sequence on the history > table, but clearly I was mistaken. There's another thinko, because after > inspecting pg_waldump output I realized "SEQ_LOG_VALS 1" actually logs > only every 2nd increment. So it should be "SEQ_LOG_VALS 0". > > So I repeated the test fixing SEQ_LOG_VALS, and doing the pgbench with a > table like this: > >   create table test (a serial, b int); > > and a script doing > >   insert into test (b) values (1); > > The results look like this: > > 1) select nextval('s'); > >      clients          1         4 >     ------------------------------ >      master       39533    124998 >      patched       3748      9114 >     ------------------------------ >      diff          -91%      -93% > > > 2) insert into test (b) values (1); > >      clients          1         4 >     ------------------------------ >      master        3718      9188 >      patched       3698      9209 >     ------------------------------ >      diff            0%        0% > > So the nextval() results are a bit worse, due to not caching 1/2 the > nextval calls. The -90% is roughly expected, due to generating about 32x > more WAL (and having to wait for commit). > > But results for the more realistic insert workload are about the same as > before (i.e. no measurable difference). Also kinda expected, because > those transactions have to wait for WAL anyway. > Attached is a patch tweaking WAL logging - in wal_level=minimal we do the same thing as now, in higher levels we log every sequence fetch. After thinking about this a bit more, I think even the nextval workload is not such a big issue, because we can set cache for the sequences. Until now this had fairly limited impact, but it can significantly reduce the performance drop caused by WAL-logging every sequence fetch. I've repeated the nextval test on a different machine (the one I used before is busy with something else), and the results look like this: 1) 1 client cache 1 32 128 -------------------------------------- master 13975 14425 19886 patched 886 7900 18397 -------------------------------------- diff -94% -45% -7% 4) 4 clients cache 1 32 128 ----------------------------------------- master 8338 12849 18248 patched 331 8124 18983 ----------------------------------------- diff -96% -37% 4% So I think this makes it acceptable / manageable. Of course, this means the values are much less monotonous (across backends), but I don't think we really promised that. And I doubt anyone is really using sequences like this (just nextval) in performance critical use cases. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------ItU0fiAY5tJpPes7DHCRBCKe Content-Type: text/x-patch; charset=UTF-8; name="0001-WAL-log-individual-sequence-fetches-20211222.patch" Content-Disposition: attachment; filename="0001-WAL-log-individual-sequence-fetches-20211222.patch" Content-Transfer-Encoding: base64 RnJvbSBlZWFhN2NiMzZjNjlhZjA0OGYwMzIxZTQ4ODM4NjRlYmUyNTQyNDI5IE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBUb21hcyBWb25kcmEgPHRvbWFzLnZvbmRyYUBwb3N0 Z3Jlc3FsLm9yZz4KRGF0ZTogV2VkLCAyMiBEZWMgMjAyMSAwMzoxODo0NiArMDEwMApTdWJq ZWN0OiBbUEFUQ0ggMS82XSBXQUwtbG9nIGluZGl2aWR1YWwgc2VxdWVuY2UgZmV0Y2hlcwoK LS0tCiBzcmMvYmFja2VuZC9jb21tYW5kcy9zZXF1ZW5jZS5jIHwgMTggKysrKysrKysrKysr KysrKy0tCiAxIGZpbGUgY2hhbmdlZCwgMTYgaW5zZXJ0aW9ucygrKSwgMiBkZWxldGlvbnMo LSkKCmRpZmYgLS1naXQgYS9zcmMvYmFja2VuZC9jb21tYW5kcy9zZXF1ZW5jZS5jIGIvc3Jj L2JhY2tlbmQvY29tbWFuZHMvc2VxdWVuY2UuYwppbmRleCA3MmJmZGMwN2E0Li4wZjMwOWQw YTRlIDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC9jb21tYW5kcy9zZXF1ZW5jZS5jCisrKyBi L3NyYy9iYWNrZW5kL2NvbW1hbmRzL3NlcXVlbmNlLmMKQEAgLTUyLDYgKzUyLDkgQEAKICAq IFdlIGRvbid0IHdhbnQgdG8gbG9nIGVhY2ggZmV0Y2hpbmcgb2YgYSB2YWx1ZSBmcm9tIGEg c2VxdWVuY2UsCiAgKiBzbyB3ZSBwcmUtbG9nIGEgZmV3IGZldGNoZXMgaW4gYWR2YW5jZS4g SW4gdGhlIGV2ZW50IG9mCiAgKiBjcmFzaCB3ZSBjYW4gbG9zZSAoc2tpcCBvdmVyKSBhcyBt YW55IHZhbHVlcyBhcyB3ZSBwcmUtbG9nZ2VkLgorICoKKy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