Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s5du3-0072B0-Td for pgsql-hackers@arkaria.postgresql.org; Sat, 11 May 2024 04:00:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s5du1-003EFd-My for pgsql-hackers@arkaria.postgresql.org; Sat, 11 May 2024 04:00:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s5du1-003EFV-D8 for pgsql-hackers@lists.postgresql.org; Sat, 11 May 2024 04:00:05 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s5dty-000SG6-JK for pgsql-hackers@postgresql.org; Sat, 11 May 2024 04:00:05 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-51ffff16400so4372359e87.2 for ; Fri, 10 May 2024 21:00:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715400002; x=1716004802; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=8LYaAqi7Mo/NehqvgNS0DwSGqzRg1eW3cvjgd/kX/Wc=; b=OVjY23mOhR18HMzlPS8i6xY085SaEbDw0cNI7uPkJSnaH5pds59RpHKlWA+1pVUKGx CL5yFbBFB/nm+UxldYSJfI4zgYLoJLMbrNL9IYwLT8PRxCbqC+t7OHki0yeEL8tbVeeb 2ws722Jx5u2z7BjDCVmk45pGt63ib5TSaU/fUw2ZtRYXY26kc8NAI5Tuw/SZBrtwBPLA NJTwLLAxKSP3xp+oOJ3StXBix/yR1BDrl98TYwEXrc/jKITeSFmROCLLexAvIwFsCa+t HHxol7/lmS8LAi5Ucyg+l96PVKgscC9CujhM0zaxfExF7UiICinOIIGCfwPGp+bsOaTa GZLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715400002; x=1716004802; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8LYaAqi7Mo/NehqvgNS0DwSGqzRg1eW3cvjgd/kX/Wc=; b=GHkF9LvWKp3dPUFlvf/fTLhfiG9oa/N2gtlnMhwLxqLjdYgo6QDh58tqA0WdrIHrqo l4kMFFDCJ1WRU1ppJ28FLbCzpXJMy3Q9fKgjqegzOZ7zZAjTOrjGFfBkaqSnXT8bzrf2 AT03Woh9M7N7dGpSjRps00NnezThnZB8fxrTb+PWU0Ml/VOySUKbKWSXFDVZAN2UVwY7 zVdrkvLHuFCxkEpfZiA3ooojMLv0ul2F+AsypumEXnqkeGgv2VihSn2uHoBBSt+neAuJ Zc0nmNO7B4A9Zt0hRYzy8a7dahHmdJmgnCh96Tp+bMLlzYs188OOa/EGDa2eoQi6P6AF FGQg== X-Gm-Message-State: AOJu0YzX74PssErAXPR4UCVXB5ewV9AJpAUB5U9pQgoq0U++VZvOvYQO kWQhR1ssf/zJ+KtRTN8r9VD1cdcF9OJqVCCoun96ROZL+aYj9RmZ X-Google-Smtp-Source: AGHT+IEj0ps7xCk0lUJaSC8HILaGhoEktCZJNRgUQnc9LzP+LZeWdh0QD//1Bi3ncnm3aFzV/76EnQ== X-Received: by 2002:a19:c207:0:b0:51a:f11c:81db with SMTP id 2adb3069b0e04-5220fe794b0mr3051730e87.30.1715400001634; Fri, 10 May 2024 21:00:01 -0700 (PDT) Received: from [1.0.0.7] ([178.155.25.230]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-521f38d3396sm886868e87.132.2024.05.10.21.00.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 May 2024 21:00:01 -0700 (PDT) Message-ID: <0a2e14d3-fbc8-9e86-a968-e0c4a465e5b0@gmail.com> Date: Sat, 11 May 2024 07:00:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: WAL record CRC calculated incorrectly because of underlying buffer modification Content-Language: en-US To: Thomas Munro , Andres Freund Cc: pgsql-hackers References: <055bb33c-bccc-bc1d-c2f8-8598534448ac@gmail.com> <20240510155742.sznwmnmllsewjsbc@awork3.anarazel.de> From: Alexander Lakhin In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Thomas and Andres, Thank you for looking at this! 11.05.2024 06:26, Thomas Munro wrote: > On Sat, May 11, 2024 at 3:57 AM Andres Freund wrote: >> On 2024-05-10 16:00:01 +0300, Alexander Lakhin wrote: >>> and discovered that XLogRecordAssemble() calculates CRC over a buffer, >>> that might be modified by another process. >> If, with "might", you mean that it's legitimate for that buffer to be >> modified, I don't think so. The bug is that something is modifying the buffer >> despite it being exclusively locked. >> >> I.e. what we likely have here is a bug somewhere in the hash index code. > I don't have a good grip on the higher level locking protocols of > hash.c, but one microscopic thing jumps out: > > /* > * bucket buffer was not changed, but still needs to be > * registered to ensure that we can acquire a cleanup lock on > * it during replay. > */ > if (!xlrec.is_primary_bucket_page) > { > uint8 flags = REGBUF_STANDARD | > REGBUF_NO_IMAGE | REGBUF_NO_CHANGE; > > XLogRegisterBuffer(0, bucket_buf, flags); > } > > That registers a buffer that is pinned but not content-locked, and we > tell xloginsert.c not to copy its image into the WAL, but it does it > anyway because: > > /* > * If needs_backup is true or WAL checking is enabled for current > * resource manager, log a full-page write for the current block. > */ > include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0; > > So I guess it copies the image on dodo, which has: > > 'PG_TEST_EXTRA' => 'ssl ldap > kerberos wal_consistency_checking libpq_encryption xid_wraparound' > > Perhaps a no-image, no-change registered buffer should not be > including an image, even for XLR_CHECK_CONSISTENCY? It's actually > useless for consistency checking too I guess, this issue aside, > because it doesn't change anything so there is nothing to check. Yes, I think something wrong is here. I've reduced the reproducer to: cat << 'EOF' | psql CREATE TABLE hash_cleanup_heap(keycol INT); CREATE INDEX hash_cleanup_index on hash_cleanup_heap USING HASH (keycol); BEGIN; INSERT INTO hash_cleanup_heap SELECT 1 FROM generate_series(1, 500) as i; ROLLBACK; EOF cat << 'EOF' | psql & INSERT INTO hash_cleanup_heap SELECT 1 FROM generate_series(1, 500) as i; DROP TABLE hash_cleanup_heap; EOF cat << 'EOF' | psql & SELECT pg_sleep(random() / 20); VACUUM hash_cleanup_heap; EOF wait grep 'TRAP:' server.log ("wal_consistency_checking = all" and the xloginsert patch are still required) and with additional logging I see: 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96dd|LOG: !!!hashbucketcleanup| scan page buf: 1832 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96dd|CONTEXT: while vacuuming index "hash_cleanup_index" of relation "public.hash_cleanup_heap" 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96dd|STATEMENT:  VACUUM hash_cleanup_heap; 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96de|LOG: !!!_hash_doinsert| _hash_getbucketbuf_from_hashkey: 1822 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96de|STATEMENT:  INSERT INTO hash_cleanup_heap SELECT 1 FROM generate_series(1, 500) as i; 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96dd|LOG: !!!hashbucketcleanup| xlrec.is_primary_bucket_page: 0, buf: 1832, bucket_buf: 1822 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96dd|CONTEXT: while vacuuming index "hash_cleanup_index" of relation "public.hash_cleanup_heap" 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96dd|STATEMENT:  VACUUM hash_cleanup_heap; 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96de|LOG: !!!_hash_doinsert| _hash_relbuf(rel, 1822) 2024-05-11 03:45:23.424 UTC|law|regression|663ee9d3.1f96de|STATEMENT:  INSERT INTO hash_cleanup_heap SELECT 1 FROM generate_series(1, 500) as i; TRAP: failed Assert("memcmp(block1_ptr, block1_copy, block1_len) == 0"), File: "xloginsert.c", Line: 949, PID: 2070237 Best regards, Alexander