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 1nCYjd-0001RO-W7 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jan 2022 03:12:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nCYjc-0004sw-16 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jan 2022 03:12:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nCYjb-0004sm-7y for pgsql-hackers@lists.postgresql.org; Wed, 26 Jan 2022 03:12:35 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nCYjY-0007x0-3E for pgsql-hackers@lists.postgresql.org; Wed, 26 Jan 2022 03:12:34 +0000 Received: by mail-wm1-x330.google.com with SMTP id q141-20020a1ca793000000b00347b48dfb53so3306248wme.0 for ; Tue, 25 Jan 2022 19:12:31 -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:content-language:to:from :subject; bh=Yyi02YIJHC46h1wmGn5gWBcZqK3O0M7MI4+Q3RmsfsE=; b=dM6F7XqtD3TshbEA6gUx8yo/no6G/KHFsLddhcSb28pgDqJRLFfnT8xBgcfi/A+te4 jtsAX8K7mnzWTqPakEGCPkqBg0lP5XjrDnr2Fg0MdtSEGxXx5lAnjf4uMKq33crIxwD+ NJCV+NamBLvXfPM0E52CMrEnk3EF5zv2y7ewJyNCGKVawLmGCpME7G0XoV9f1lWu2oQ9 eccGiXTVa8wDS04M7+J/FjggQugE3EciQRVN5dLGTxFyE+mGOJtcxn+KqMo614M8XfXJ RxflI4gJnw397NQzK30IApSZ+FOHAuTCQr85G74HdunMe4xsWwFP2Rh+8AyRbJE/EZhJ FkUw== 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 :content-language:to:from:subject; bh=Yyi02YIJHC46h1wmGn5gWBcZqK3O0M7MI4+Q3RmsfsE=; b=iP1Mb90ERFq+A2CgEPr6PyK5ecNOdUCjQNHJV9gZZ6Hxb0hw03MH6UbVd9JFyEDfkb 9ZJr0rrPMwnzBn4atPmVcDosAMRF/a7G5aYrPT6alyA1g/tNX8bVzCepjOS2I09i1fnq nzK/MbpQaSG2rV0YGbM2k55Jjw7uviAznRyS4ANnEngG9bX2Fqq5FuxCVUehO2x0dVCk fs0SqDj02h8GcpnjsP8ntGRx+G1V+5YgM8d0Z2oFNSawhWRhewyDSV5BWyRjUg0QEb6f cQo+q3Y58yglsZcNEhZ7Ryo/l6mCrPXOAKaeU2duHeHqHTKp40rV9a1RJ4dKocqHuqeO yvBA== X-Gm-Message-State: AOAM532YXbfv+aQYRpY703Lcdx0WLF7lGkcpvBZn+826kqRWXusXRatL fajl8OOU96j3mLl6SH3Z9Bsiu7AZ/MQrbmg3voT455U9VNexig4j1d5rvuWMRn3RIbJeT3H/0zS Vsc2fcSoHi0JpyDGzEzTFhVBhTbqEtlHIYrRZPbpV5RvdzwE8TKLqdnJ7sPMLktzCfnyIz41bIi PjVFQX7Ehw7sulyXl8oYDgiIa1Xd9u20PWpbY40IlI7AeH2LczWpbMgayj1XmEezEMX2Uicopsq 1qE8PT2TDJzIuNBHX/P4Co6DYZqfonioucFZCiYUQ460904AyTJBA== X-Google-Smtp-Source: ABdhPJwqBffxZKFnhoRKiBok8LfFPsF4tAsP9w7ucLpfL3ullGOMxv/l+IRALSZnX+ip7p/187a25w== X-Received: by 2002:a1c:720a:: with SMTP id n10mr5436348wmc.36.1643166750002; Tue, 25 Jan 2022 19:12:30 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id n14sm10602341wri.80.2022.01.25.19.12.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Jan 2022 19:12:29 -0800 (PST) Content-Type: multipart/mixed; boundary="------------abJuAEqo7f0coYUPDLcC9oUU" Message-ID: <64a5bfb0-909e-0a14-3ac9-14dc74673074@enterprisedb.com> Date: Wed, 26 Jan 2022 04:12:27 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.3.0 Content-Language: en-US To: PostgreSQL Hackers From: Tomas Vondra Subject: BRIN summarization vs. WAL logging 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. --------------abJuAEqo7f0coYUPDLcC9oUU Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, In a thread about sequences and sync replication [1], I've explained that the issue we're observing is due to not waiting for WAL at commit if the transaction only did nextval(). In which case we don't flush WAL in RecordTransactionCommit, we don't wait for sync replica, etc. The WAL may get lost in case of crash, etc. As I explained in the other thread, there are various other cases where a transaction generates WAL but does not have XID, which is sufficient for not flushing/waiting at transaction commit. Some of those cases are probably fine (e.g. there are comments explaining why this is fine for PRUNE record). But other cases (discovered by running regression tests with extra logging) looked a bit suspicious - particularly those that write multiple WAL messages, because what if we lose just some of those? So I looked at two cases related to BRIN, mostly because those were fairly simple, and I think at least the brin_summarize_range() is somewhat broken. 1) brin_desummarize_range() This is pretty simple, because this function generates a single WAL record, without waiting for it to be flushed: DESUMMARIZE pagesPerRange 1, heapBlk 0, page offset 9, blkref #0: ... But if the cluster/VM/... crashes right after you ran the function (and it completed just fine, possibly even in an explicit transaciton), that change will get lost. Not really a serious data corruption/loss, and you can simply run it again, but IMHO rather surprising. Of course, most people are unlikely to run brin_desummarize_range() very often, so maybe it's acceptable? But of course - if we expect this to be very rare operation, why skip the WAL at all? 2) brin_summarize_range() Now, the issue I think is more serious, more likely to happen, and harder to fix. When summarizing a range, we write two WAL records: INSERT heapBlk 2 pagesPerRange 2 offnum 2, blkref #0: rel 1663/63 ... SAMEPAGE_UPDATE offnum 2, blkref #0: rel 1663/63341/73957 blk 2 So, what happens if we lost the second WAL record, e.g. due to a crash? To experiment with this, I wrote a trivial patch (attached) that allows crashing on WAL message of certain type by simply setting a GUC. Now, consider this example: create table t (a int); insert into t select i from generate_series(1,5000) s(i); create index on t using brin (a); select brin_desummarize_range('t_a_idx', 1); set crash_on_wal_message = 'SAMEPAGE_UPDATE'; select brin_summarize_range('t_a_idx', 5); PANIC: crashing before 'SAMEPAGE_UPDATE' WAL message server closed the connection unexpectedly ... After recovery, this is what we have: select * from brin_page_items(get_Raw_page('t_a_idx', 2), 't_a_idx'); ... | allnulls | hasnulls | placeholder | value ... -+----------+----------+-------------+------- ... | t | f | t | (1 row) So the BRIN tuple is still marked as placeholder, which is a problem because that means we'll always consider it as matching, making the bitmap index scan less efficient. And we'll *never* fix this, because just summarizing the range does nothing: select brin_summarize_range('t_a_idx', 5); brin_summarize_range ---------------------- 0 (1 row) So it's still marked as placeholder, and to fix it you have to explicitly desummarize the range first. The reason for this seems obvious - only the process that created the placeholder tuple is expected to mark it as "placeholder=false", but this is described as two WAL records. And if we lose the update, the tuple will stay marked as a placeholder forever. Of course, this requires a crash while something is summarizing ranges. But consider the summarization is often done by autovacuum, so it's not just about hitting this from manually-executed brin_summarize_range. I'm not quite sure what to do about this. Just doing XLogFlush() does not really fix this - it makes it less likely, but the root cause is the change is described by multiple WAL messages that are not linked together in any way. We may lost the last message without noticing that, and the flush does not fix that. I didn't look at the other cases mentioned in [1], but I would't be surprised if some had a similar issue (e.g. the GIN pending list cleanup seems like another candidate). regards [1] https://www.postgresql.org/message-id/0f827a71-a01b-bcf9-fe77-3047a9d4a93c%40enterprisedb.com -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------abJuAEqo7f0coYUPDLcC9oUU Content-Type: text/x-patch; charset=UTF-8; name="trigger-wal-crash.patch" Content-Disposition: attachment; filename="trigger-wal-crash.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy9icmluL2JyaW5fcGFnZW9wcy5jIGIv c3JjL2JhY2tlbmQvYWNjZXNzL2JyaW4vYnJpbl9wYWdlb3BzLmMKaW5kZXggMTcyNTc5MTlk YmYuLmRhZWQ4YmEzYmM4IDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC9hY2Nlc3MvYnJpbi9i cmluX3BhZ2VvcHMuYworKysgYi9zcmMvYmFja2VuZC9hY2Nlc3MvYnJpbi9icmluX3BhZ2Vv cHMuYwpAQCAtMTkwLDYgKzE5MCw4IEBAIGJyaW5fZG91cGRhdGUoUmVsYXRpb24gaWR4cmVs LCBCbG9ja051bWJlciBwYWdlc1BlclJhbmdlLAogCiAJCQl4bHJlYy5vZmZudW0gPSBvbGRv ZmY7CiAKKwkJCW1heWJlX2NyYXNoX29uX3dhbCgiU0FNRVBBR0VfVVBEQVRFIik7CisKIAkJ CVhMb2dCZWdpbkluc2VydCgpOwogCQkJWExvZ1JlZ2lzdGVyRGF0YSgoY2hhciAqKSAmeGxy ZWMsIFNpemVPZkJyaW5TYW1lcGFnZVVwZGF0ZSk7CiAKZGlmZiAtLWdpdCBhL3NyYy9iYWNr ZW5kL2FjY2Vzcy90cmFuc2FtL3hhY3QuYyBiL3NyYy9iYWNrZW5kL2FjY2Vzcy90cmFuc2Ft L3hhY3QuYwppbmRleCBjOTUxNmUwM2ZhZS4uNTYyMTkyZGM1MTQgMTAw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