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 1pQx2R-0007Ya-IH for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2023 21:04:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pQx2Q-0006bG-FY for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2023 21:04:02 +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 1pQx2Q-0006b7-2W for pgsql-hackers@lists.postgresql.org; Sat, 11 Feb 2023 21:04:02 +0000 Received: from meesny.iki.fi ([195.140.195.201]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQx2N-0003qk-TY for pgsql-hackers@postgresql.org; Sat, 11 Feb 2023 21:04:01 +0000 Received: from [192.168.1.113] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id 159AA200BD; Sat, 11 Feb 2023 23:03:57 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1676149437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=poRRV/eCW1If44QqsGPTd94tE36s3y1Siz7GP9dqqcM=; b=v2ZHPOxGxJTMgu/eqNyiDLlaWzNSav2M6r07O0ZXjU5fhlI6/HqsjiSqHN4jm0aqTiXqk+ Bb8LINAYduUjJr4oaz3OgMl9xxp33lJB6t26dcHBdKNwSxk13wEnDJDpfONC5bIa6Pi5as PniVSkHhDvf89NKpM1MWczP1j98c3+U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1676149437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=poRRV/eCW1If44QqsGPTd94tE36s3y1Siz7GP9dqqcM=; b=UoQfHN88FXeBTNr5eHvS8ZXIe6FVgRaL/PBpryctL/dhdTRel5OyIHMNxDGQCurOEC2IEB gYptYBlJO+E3ejU/B0PPcDuVRd45pD9OQetC79egSVsErNHh436hLTXhuQW18SVogFUiee BCZRhdH6UepE5WfWsUOWrwWItn7aBjE= ARC-Seal: i=1; s=meesny; d=iki.fi; t=1676149437; a=rsa-sha256; cv=none; b=ghzOgHw05BrR3uDoCLRX6UMN4vq6q4/natlHitKC6SPdtVmz4aNUzi03V3TEjkP9UyLnHJ qwIpvnEbvit7uxwLPkwt8dht0QG5vBCou6kO4Q1QEOv/WlNLyTJ/IiRLnzAt4H/H2PvMjs ArVtvATaWXnqU9hwjAFDlBqly3aPDBc= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: Date: Sat, 11 Feb 2023 23:03:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: refactoring relation extension and BufferAlloc(), faster COPY Content-Language: en-US From: Heikki Linnakangas To: Andres Freund Cc: vignesh C , pgsql-hackers@postgresql.org, Thomas Munro , Melanie Plageman , Yura Sokolov , Robert Haas , David Rowley References: <20221029025420.eplyow6k7tgu6he3@awork3.anarazel.de> <20230110020749.uvi7dfrdvdqlhgal@awork3.anarazel.de> <419312fd-9255-078c-c3e3-f0525f911d7f@iki.fi> In-Reply-To: <419312fd-9255-078c-c3e3-f0525f911d7f@iki.fi> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > v2-0005-bufmgr-Acquire-and-clean-victim-buffer-separately.patch This can be applied separately from the rest of the patches, which is nice. Some small comments on it: * Needs a rebase, it conflicted slightly with commit f30d62c2fc. * GetVictimBuffer needs a comment to explain what it does. In particular, mention that it returns a buffer that's pinned and known !BM_TAG_VALID. * I suggest renaming 'cur_buf' and other such local variables in GetVictimBufffer to just 'buf'. 'cur' prefix suggests that there is some other buffer involved too, but there is no 'prev' or 'next' or 'other' buffer. The old code called it just 'buf' too, and before this patch it actually was a bit confusing because there were two buffers involved. But with this patch, GetVictimBuffer only deals with one buffer at a time. * This FIXME: > /* OK, do the I/O */ > /* FIXME: These used the wrong smgr before afaict? */ > { > SMgrRelation smgr = smgropen(BufTagGetRelFileLocator(&buf_hdr->tag), > InvalidBackendId); > > TRACE_POSTGRESQL_BUFFER_WRITE_DIRTY_START(buf_hdr->tag.forkNum, > buf_hdr->tag.blockNum, > smgr->smgr_rlocator.locator.spcOid, > smgr->smgr_rlocator.locator.dbOid, > smgr->smgr_rlocator.locator.relNumber); > > FlushBuffer(buf_hdr, smgr, IOOBJECT_RELATION, io_context); > LWLockRelease(content_lock); > > ScheduleBufferTagForWriteback(&BackendWritebackContext, > &buf_hdr->tag); > > TRACE_POSTGRESQL_BUFFER_WRITE_DIRTY_DONE(buf_hdr->tag.forkNum, > buf_hdr->tag.blockNum, > smgr->smgr_rlocator.locator.spcOid, > smgr->smgr_rlocator.locator.dbOid, > smgr->smgr_rlocator.locator.relNumber); > } I believe that was intentional. The probes previously reported the block and relation whose read *caused* the eviction. It was not just the smgr but also the blockNum and forkNum that referred to the block that was being read. There's another pair of probe points, TRACE_POSTGRESQL_BUFFER_FLUSH_START/DONE, inside FlushBuffer that indicate the page that is being flushed. I see that reporting the evicted page is more convenient with this patch, otherwise you'd need to pass the smgr and blocknum of the page that's being read to InvalidateVictimBuffer(). IMHO you can just remove these probe points. We don't need to bend over backwards to maintain specific probe points. * InvalidateVictimBuffer reads the buffer header with an atomic read op, just to check if BM_TAG_VALID is set. If it's not, it does nothing (except for a few Asserts). But the caller has already read the buffer header. Consider refactoring it so that the caller checks VM_TAG_VALID, and only calls InvalidateVictimBuffer if it's set, saving one atomic read in InvalidateVictimBuffer. I think it would be just as readable, so no loss there. I doubt the atomic read makes any measurable performance difference, but it looks redundant. * I don't understand this comment: > /* > * Clear out the buffer's tag and flags and usagecount. We must do > * this to ensure that linear scans of the buffer array don't think > * the buffer is valid. > * > * XXX: This is a pre-existing comment I just moved, but isn't it > * entirely bogus with regard to the tag? We can't do anything with > * the buffer without taking BM_VALID / BM_TAG_VALID into > * account. Likely doesn't matter because we're already dirtying the > * cacheline, but still. > * > */ > ClearBufferTag(&buf_hdr->tag); > buf_state &= ~(BUF_FLAG_MASK | BUF_USAGECOUNT_MASK); > UnlockBufHdr(buf_hdr, buf_state); What exactly is wrong with clearing the tag? What does dirtying the cacheline have to do with the correctness here? * pgindent - Heikki