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 1pg42e-0002K1-7D for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Mar 2023 13:34:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pg42b-0003cX-8m for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Mar 2023 13:34:41 +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 1pg42a-0003cN-UG for pgsql-hackers@lists.postgresql.org; Sat, 25 Mar 2023 13:34:40 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pg42T-0007xI-Uc for pgsql-hackers@postgresql.org; Sat, 25 Mar 2023 13:34:39 +0000 Received: by mail-lf1-x12c.google.com with SMTP id t11so5682471lfr.1 for ; Sat, 25 Mar 2023 06:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1679751271; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=Nyo+4D+cUOLXphkrtudPs3EmodhW76og3l/6oB8VLi8=; b=b+fu7CocDI0UHt83FMGh9BTmA6zHPs3MSdnT0TZ8DvWKMFGOPixp8mlc/9qYltnhfv wzEJtxo9NtlrCrwDfRZmMpaBcgjedv1MDT9ra+q7kN9QldWbr1Z4/NVANw4Ct1GzKagW 2x6KHtIwiqtdyKQJwuVTpz8C9OKHXEqmBdPGZk8TnZpHOP12cC1mkJMiNMHy6HyDlavy yWtjnZ1+oOP4DTFqicClXK0Ll/C40p2xoPYn8zrjwP/AGpZSUxCTH3UCeJ100UqaQvRq aWZ9HJEe2Iq1GjwEUNUyyKnqUrInhajt6/vhx6aHbKukTboD8bP2j2OGKfEv38opiZ25 rbrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679751271; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Nyo+4D+cUOLXphkrtudPs3EmodhW76og3l/6oB8VLi8=; b=QtXOv3KWw3ilNduCeQN9NWbo11cQ8hbD8iHa5KUQegmI7GIOoUEB2Z64319kjiV7aY 5UV/ibGoOodiIA5FfEF2WQUVj2rk5CJ0VXJF5upekRqme+Rb1H8Ih8/RT2kK/vh4setf 2LhdzgcLc6/G8IBnV4rwYT9cDq96JDqMcLBdCowbV7WTJn6ozyjGQUgEMoqJ49Q8HF7L GUkBnZhYL7x9VY6p3d3XZfoZ6clmsldbhydS+P/Auk9TZSwHwD8gvwV7BWGB2cZ5qOen RxopVpV1vUOeAhep0ec3diKIA1jB4opmoSRH1WWao6wXY0JWvFjWyyddtibS63PT4Xtw /a7w== X-Gm-Message-State: AAQBX9cv4YHzJAGEySA1nT1MgESktCmu40Oi3xWx86O2f5tjjiZVnxVP qXu3obcIVp8DqO03n8nSQE4lWAUVOspWEDs7Uv8qlbBfoP2gX/d7Drq1tHTrcfoGhoor+Zzq0kF /HS/VqbxFXVi5Y7RkbR1CY8LegSW78xG+getIzUKvBvJ+hoI2tmlYrodoEAqp9dmwy5V4DkJFQI 59DSaUkKx+OkaZ1aWjroWQd/9qdagLLIEpEhb3 X-Google-Smtp-Source: AKy350YnziIeHDxn11Byx+gAXP14+MDp7/p8AARVoDKsWlTTnsdyHM0R22x4KNaSal8YBUWlUapPVQ== X-Received: by 2002:ac2:59d9:0:b0:4dd:cbf3:e981 with SMTP id x25-20020ac259d9000000b004ddcbf3e981mr1660802lfn.28.1679751271425; Sat, 25 Mar 2023 06:34:31 -0700 (PDT) Received: from [10.137.0.17] ([84.217.81.189]) by smtp.gmail.com with ESMTPSA id w24-20020ac24438000000b004e81edf146fsm3848791lfl.209.2023.03.25.06.34.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Mar 2023 06:34:30 -0700 (PDT) Message-ID: <0c11032a-d91b-8875-ab7b-4554776390c5@enterprisedb.com> Date: Sat, 25 Mar 2023 14:34:25 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: hio.c does visibilitymap_pin()/IO while holding buffer lock To: Andres Freund , pgsql-hackers@postgresql.org, Tomas Vondra , Anastasia Lubennikova , Pavan Deolasee , Jeff Janes References: <20230325025740.wzvchp2kromw4zqz@awork3.anarazel.de> Content-Language: en-US From: Tomas Vondra In-Reply-To: <20230325025740.wzvchp2kromw4zqz@awork3.anarazel.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 On 3/25/23 03:57, Andres Freund wrote: > Hi, > > Starting with > > commit 7db0cd2145f2bce84cac92402e205e4d2b045bf2 > Author: Tomas Vondra > Date: 2021-01-17 22:11:39 +0100 > > Set PD_ALL_VISIBLE and visibility map bits in COPY FREEZE > That's a bummer :-( > RelationGetBufferForTuple does > > /* > * The page is empty, pin vmbuffer to set all_frozen bit. > */ > if (options & HEAP_INSERT_FROZEN) > { > Assert(PageGetMaxOffsetNumber(BufferGetPage(buffer)) == 0); > visibilitymap_pin(relation, BufferGetBlockNumber(buffer), vmbuffer); > } > > while holding a buffer lock. visibilitymap_pin() reads pages, if vmbuffer > doesn't already point to the right block. > > > The lock ordering rules are to lock VM pages *before* locking heap pages. > > > I think the reason this hasn't yet bitten us badly, is that INSERT_FROZEN > effectively requires that the relation is access exclusive locked. There > shouldn't be other backends locking multiple buffers in the relation (bgwriter > / checkpointer can lock a single buffer at a time, but that's it). > Right. Still, it seems a bit fragile ... > > I see roughly two ways forward: > > 1) We add a comment explaining why it's safe to violate lock ordering rules in > this one situation > Possible, although I feel uneasy about just documenting a broken rule. Would be better to maintain the locking order. > 2) Change relevant code so that we only return a valid vmbuffer if we could do > so without blocking / IO and, obviously, skip updating the VM if we > couldn't get the buffer. > I don't recall the exact details about the vm locking/pinning, but can't we just ensure we actually follow the proper locking order? I mean, this only deals with new pages, requested at line ~624: buffer = ReadBufferBI(relation, P_NEW, RBM_ZERO_AND_LOCK, bistate); Can't we ensure we actually lock the vm buffer too in ReadBufferBI, before calling ReadBufferExtended? Or am I confused and that's not possible for some reason? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company