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 1nSaWx-0003gv-94 for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Mar 2022 08:21:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nSaWw-0005yl-3f for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Mar 2022 08:21:46 +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 1nSaWv-0005yc-QU for pgsql-hackers@lists.postgresql.org; Fri, 11 Mar 2022 08:21:45 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nSaWt-00030G-Rk for pgsql-hackers@lists.postgresql.org; Fri, 11 Mar 2022 08:21:45 +0000 Received: by mail-pl1-x62b.google.com with SMTP id q13so7087219plk.12 for ; Fri, 11 Mar 2022 00:21:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=pqgWXpiDE9raQskM7mtgOGY7WZ1MyuDI/uOV1kK4X8I=; b=XYW/VmdlNGJUh9c56d5QNfBVDHQ+T+JHKQ9RlQEXG2C6Sx/70KHPSO9adK5BAn5bY0 7usK7wPhZwwTmvUInzsXLzhidgMEiCId3al7swUfrCrUbp+X6jyd5N4BTj9nZD9qQhPi ocJFIqvZ/pQ3EWsBhZRY4rnILr2emlI6nwVy4RwRGliT9ZeHLxiLa/5bJHvZxsB44Bpz zRPUFtcB+OCci8E3ZDI1kY1Fi9YDEFsPWm2RYs4KHDJrAVhpT77zFQnVamXE+FVFkMnn FJFZ6rVssoeIQyBfagShRiAjfRAH4RhWDGrU+EZ7Z08bAXgiSMmJI6pejirwF+QTW9vd sZLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=pqgWXpiDE9raQskM7mtgOGY7WZ1MyuDI/uOV1kK4X8I=; b=0kcgDhojd+mQHZjugmPj5+VOhNfM+UZxh65Ix6HxLlMAVDjJY+61G+e1y764iwvKVR DcW2XJIG1+BbI1vGhkeg/EHifmIDLEiyoE2RbZZhXN5mH89IOp6amx/Ilf2OzmsX2kY+ pxwnQ8tiPw8VluVu5RE8lOWe84UNQ4lGFsGXXFtpvRZwzfiAsBvwhRBoGqAgIwaDx2Oj bMUo5Oo9bkAwB2UciCWmzETTmw4WlDG4Rhc1uNyVJAj8f1Y/SO8AZMEiFz7K/c/VDi3X B9ldmvH6FLCFQsymq8J3TbHzq3OLBFQ7riSh8Z1mdet7ZF4/8XTRf1b9OV76w4/yhvE1 04Sg== X-Gm-Message-State: AOAM532jZHMoap1863QGQbl0CZMUMb9tx6FrX1rEmrvhHQ8meYa9puf1 X9fN95jf1SG6+nm8Z/qNeS4= X-Google-Smtp-Source: ABdhPJxodsVuDain2+pEEglXw84T/xQo3CcWpiwEGpbUziO4Kx96x93luVhnj0z/GtQFn6oAOIKt0Q== X-Received: by 2002:a17:902:e78f:b0:153:29c8:5395 with SMTP id cp15-20020a170902e78f00b0015329c85395mr5104716plb.55.1646986901315; Fri, 11 Mar 2022 00:21:41 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id x16-20020a637c10000000b00380b351aaacsm7512346pgc.16.2022.03.11.00.21.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 11 Mar 2022 00:21:40 -0800 (PST) Date: Fri, 11 Mar 2022 17:21:37 +0900 (JST) Message-Id: <20220311.172137.603201487885714707.horikyota.ntt@gmail.com> To: y.sokolov@postgrespro.ru Cc: pgsql-hackers@lists.postgresql.org, michail.nikolaev@gmail.com, x4mmm@yandex-team.ru, andres@anarazel.de, simon.riggs@enterprisedb.com Subject: Re: BufferAlloc: don't take two simultaneous locks From: Kyotaro Horiguchi In-Reply-To: <20220311.154949.1643256093439212518.horikyota.ntt@gmail.com> References: <20220311.153030.1881495127262295773.horikyota.ntt@gmail.com> <20220311.154949.1643256093439212518.horikyota.ntt@gmail.com> User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk At Fri, 11 Mar 2022 15:49:49 +0900 (JST), Kyotaro Horiguchi wrote in > At Fri, 11 Mar 2022 15:30:30 +0900 (JST), Kyotaro Horiguchi wrote in > > Thanks! I looked into dynahash part. Then I looked into bufmgr part. It looks fine to me but I have some comments on code comments. > * To change the association of a valid buffer, we'll need to have > * exclusive lock on both the old and new mapping partitions. > if (oldFlags & BM_TAG_VALID) We don't take lock on the new mapping partition here. + * Clear out the buffer's tag and flags. We must do this to ensure that + * linear scans of the buffer array don't think the buffer is valid. We + * also reset the usage_count since any recency of use of the old content + * is no longer relevant. + * + * We are single pinner, we hold buffer header lock and exclusive + * partition lock (if tag is valid). Given these statements it is safe to + * clear tag since no other process can inspect it to the moment. This comment is a merger of the comments from InvalidateBuffer and BufferAlloc. But I think what we need to explain here is why we invalidate the buffer here despite of we are going to reuse it soon. And I think we need to state that the old buffer is now safe to use for the new tag here. I'm not sure the statement is really correct but clearing-out actually looks like safer. > Now it is safe to use victim buffer for new tag. Invalidate the > buffer before releasing header lock to ensure that linear scans of > the buffer array don't think the buffer is valid. It is safe > because it is guaranteed that we're the single pinner of the buffer. > That pin also prevents the buffer from being stolen by others until > we reuse it or return it to freelist. So I want to revise the following comment. - * Now it is safe to use victim buffer for new tag. + * Now reuse victim buffer for new tag. > * Make sure BM_PERMANENT is set for buffers that must be written at every > * checkpoint. Unlogged buffers only need to be written at shutdown > * checkpoints, except for their "init" forks, which need to be treated > * just like permanent relations. > * > * The usage_count starts out at 1 so that the buffer can survive one > * clock-sweep pass. But if you think the current commet is fine, I don't insist on the comment chagnes. regards. -- Kyotaro Horiguchi NTT Open Source Software Center