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 1nNRIM-0005JY-5o for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Feb 2022 03:29: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 1nNRIK-0003Py-Vw for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Feb 2022 03:29:24 +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 1nNRIK-0003Pl-N8 for pgsql-hackers@lists.postgresql.org; Fri, 25 Feb 2022 03:29:24 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nNRII-0006br-BB for pgsql-hackers@postgresql.org; Fri, 25 Feb 2022 03:29:23 +0000 Received: by mail-pg1-x52b.google.com with SMTP id 27so3458054pgk.10 for ; Thu, 24 Feb 2022 19:29:22 -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=O52gnQRZbiZ5xJq/fsJrndRYy4Y2qjjVUyeHeeYydfc=; b=TQuhcEvyOa+UcPn1U6KYv7D6Dzf70lawyLYTEPZGvoId8RkZd8RkUk+vYOZPmH+5rO Ev1bXFcnDgsJbREZ6DcKKZbHghGk964AUZQVSJejnQ8C8G6ovZBWVH4v6Cyt55uNKUJv pfVrrVOyBLqa5zddcMXpkIwcsBJK/pqaI/NFECgvAKQzBLzegV7fUdAJ+wsW48d4PstG cYSFR+/2DKdcXRKJYIqfZKeldBkVx5W0giVbvZnWGvSPCU6yFCYhNBXtDN1ejHdOANZB 6XSrJMyjQyQBkJGFQiaYrsDULEdKUpJAX9XhPcujWbk4tKcuMxuDrvlfkQszfDUgZGbr CtCA== 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=O52gnQRZbiZ5xJq/fsJrndRYy4Y2qjjVUyeHeeYydfc=; b=krRGFlSkY8BP5KDZ04jCHYBwoUX/tYqYY9cTSzBejlGqkKWNQuqyQ64jxEs5zUtZRO MnXU3dFReMLxZMrX8oJVT5AdUbUdzG6KF9x/Inntq9W8SLbNwIdiCS+VMrVBeMnFQZZ8 h1JYAk1sI54yzzMzDaYjSREYUXtFDp5yNC3U5ekaD32C5oLHWaJeY3wLjDgfpebw5IYn 68Wyww8Soq/4aiBSvBWlE3immaSFnVHDIo5/caFA4LA4qKLFPdB9JdZTnyx8zeGQIJpG 4heVsK+5KHw9Di+f9mP6pms7vlt1J8b8mfudP2YEXZwnb2/V0MebCLgyKKy5j1+mbEl7 /SmQ== X-Gm-Message-State: AOAM532OIbZ20kIjFM+LxvS4NPS9P/Az52F7J3YR8+m77W4EycvRNAAZ NIoES4apzyvkYRmpGEnYz0U= X-Google-Smtp-Source: ABdhPJyWzvgyYDqCkgpKFQ0w0gO28SzfGAQNNjVLknIyKJWL7moRa7vzcGL75bYqvu7WY+UhBYAqNA== X-Received: by 2002:a62:1881:0:b0:4e0:1b4c:36f8 with SMTP id 123-20020a621881000000b004e01b4c36f8mr5817516pfy.26.1645759760135; Thu, 24 Feb 2022 19:29:20 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id c18-20020a056a00249200b004df81f5ab5dsm953799pfv.173.2022.02.24.19.29.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 24 Feb 2022 19:29:19 -0800 (PST) Date: Fri, 25 Feb 2022 12:29:16 +0900 (JST) Message-Id: <20220225.122916.2047611000642246768.horikyota.ntt@gmail.com> To: simon.riggs@enterprisedb.com Cc: y.sokolov@postgrespro.ru, pgsql-hackers@postgresql.org Subject: Re: Buffer Manager and Contention From: Kyotaro Horiguchi In-Reply-To: <20220225.102025.173579582113640633.horikyota.ntt@gmail.com> References: <20220225.102025.173579582113640633.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, 25 Feb 2022 10:20:25 +0900 (JST), Kyotaro Horiguchi wrote in > (I added Yura, as the author of a related patch) > > At Thu, 24 Feb 2022 12:58:23 +0000, Simon Riggs wrote in > > Thinking about poor performance in the case where the data fits in > > RAM, but the working set is too big for shared_buffers, I notice a > > couple of things that seem bad in BufMgr, but don't understand why > > they are like that. > > > > 1. If we need to allocate a buffer to a new block we do this in one > > step, while holding both partition locks for the old and the new tag. > > Surely it would cause less contention to make the old block/tag > > invalid (after flushing), drop the old partition lock and then switch > > to the new one? i.e. just hold one mapping partition lock at a time. > > Is there a specific reason we do it this way? > > I'm not sure but I guess the developer wanted to make the operation > atomic. > > Yura Sokolov is proposing a patch to separte the two partition locks. > > https://www.postgresql.org/message-id/1edbb61981fe1d99c3f20e3d56d6c88999f4227c.camel%40postgrespro.ru > > And it seems to me viable for me and a benchmarking in the thread > showed a good result. I'd appreciate your input on that thread. > > > 2. Possibly connected to the above, we issue BufTableInsert() BEFORE > > we issue BufTableDelete(). That means we need extra entries in the > > buffer mapping hash table to allow us to hold both the old and the new > > at the same time, for a short period. The way dynahash.c works, we try > > Yes. > > > to allocate an entry from the freelist and if that doesn't work, we > > begin searching ALL the freelists for free entries to steal. So if we > > get enough people trying to do virtual I/O at the same time, then we > > will hit a "freelist storm" where everybody is chasing the last few > > entries. It would make more sense if we could do BufTableDelete() > > To reduce that overhead, Yura proposed a surgically modification on > dynahash, but I didn't like that and the latest patch doesn't contain > that part. > > > first, then hold onto the buffer mapping entry rather than add it to > > the freelist, so we can use it again when we do BufTableInsert() very > > shortly afterwards. That way we wouldn't need to search the freelist > > at all. What is the benefit or reason of doing the Delete after the > > Insert? > > Hmm. something like hash_swap_key() or hash_reinsert_entry()? That > sounds reasonable. (Yura's proposal was taking out an entry from hash > then attach it with a new key again.) > > > Put that another way, it looks like BufTable functions are used in a > > way that mismatches against the way dynahash is designed. > > > > Thoughts? > > On the first part, I think Yura's patch works. On the second point, > Yura already showed it gives a certain amount of gain if we do that. On second thought, even if we have a new dynahash API to atomically replace hash key, we need to hold two partition locks to do that. That is contradicting our objective. regards. -- Kyotaro Horiguchi NTT Open Source Software Center