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 1pUUNm-0006W8-4i for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Feb 2023 15:16:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUUNk-0003z9-Iw for pgsql-hackers@arkaria.postgresql.org; Tue, 21 Feb 2023 15:16:40 +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 1pUUNk-0003z0-9E for pgsql-hackers@lists.postgresql.org; Tue, 21 Feb 2023 15:16:40 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUUNh-0002vY-PH for pgsql-hackers@postgresql.org; Tue, 21 Feb 2023 15:16:39 +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)) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 010051B001C6; Tue, 21 Feb 2023 17:16:33 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1676992594; 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=NFZhptzm661Pa15t3myCWVASzXRX87DvEGVTIIcfsrA=; b=Wfwj9kQL6D5uWE21M3ohh/UH3uBzeClKCH5OwiMsfb7Jw5b6XKb6SfgClWgyH5vX2VZlCo lteQWksc6B4uzbxFgxXfFc31gDl8ddHIRY0PIW243cr6u3myL6fq3987PSvP4O2boRIZgD vAhjwp2yHixz8uaKRYLudzuzRRiW6i1k42pMmXS7e4BxYDRqxZLUszAESXN0Io2F97BmaS YSF02FA4aE4IgX0OmtX0uPfeLfllKm5hicQI+ejUgqF5Gosf0SKfW+Ksl2F23Wj/ep8hS/ ykth5eM7OXb1QLGpKIzhkPRAnopHw+iQjeVGNL9VQTLMef2lCXzXOLE0i7alkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1676992594; 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=NFZhptzm661Pa15t3myCWVASzXRX87DvEGVTIIcfsrA=; b=I7LCdxBTXe+ztq5k+RTBkN4CdHFFyuZVeoV33oUXHc/CDYXs1EgZMjwahI+LYMbM4Nt/Wy qIzCY23NvAimPzBmWjvVPnsJ7gUi+031XtGTbu2LJk43mRNrFoj3lbTZw/SZbbWUSG/qCm hxI3DTwm/Dutq6Y7x4JLYLv34Nw5/zpi98sZxx6p0/y5+ZPfEkWWETvtciHiQBfL1tusV4 IdvGfFfi0T2JXYQ9BGBYceTDO2jWT1mVjv5ckmPL56bWomJKPZQKk92Nzn5dye7zOFaMO2 8AY2O4j7rRly92sEB4VPviiyqiSIMlHFHwJz5u4+U2Lz+BmXYjrLc+gJxxYYHQ== ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1676992594; a=rsa-sha256; cv=none; b=ac396lgcbrsCBZkuLara7XIcF5utmpsuzMGDJkFz3TkuRmyUKsQASFacdo3ICT56GeqXOy z/Cc4PIcd2pT8M2eo7f2it+jQU6CeW3wks/opdlSzXrSIknAZrUiGfQJGxAOv9IUuzYZ27 355iNUGCY/I1rFcJzAaoucmuCFV9sqF/cuOE+/hs7tP35LsOfR2qNq+4mSRUHRMIuOdjPj GkZGUHOOAVYomPXsx9hNmnZv5co5Y5qzOK+dEtxkU+RcjTOaUiQ9/3xlxvDiRx4wejogM4 CY3QPoFBKmUfS0EaNHVWvFxiS/MpX3a7ALRy5VZZvgS+Jd2rAI2hJ7CXeCs78g== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: <8185d3a4-5123-10ca-db33-30b4a03fff7e@iki.fi> Date: Tue, 21 Feb 2023 17:16:33 +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 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> <20230211213651.pu4ns2764fxpyit5@awork3.anarazel.de> From: Heikki Linnakangas In-Reply-To: <20230211213651.pu4ns2764fxpyit5@awork3.anarazel.de> 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 On 11/02/2023 23:36, Andres Freund wrote: > On 2023-02-11 23:03:56 +0200, Heikki Linnakangas wrote: >> * 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? > > There's nothing wrong with clearing out the tag, but I don't think it's a hard > requirement today, and certainly not for the reason stated above. > > Validity of the buffer isn't determined by the tag, it's determined by > BM_VALID (or, if you interpret valid more widely, BM_TAG_VALID). > > Without either having pinned the buffer, or holding the buffer header > spinlock, the tag can change at any time. And code like DropDatabaseBuffers() > knows that, and re-checks the the tag after locking the buffer header > spinlock. > > Afaict, there'd be no correctness issue with removing the > ClearBufferTag(). There would be an efficiency issue though, because when > encountering an invalid buffer, we'd unnecessarily enter InvalidateBuffer(), > which'd find that BM_[TAG_]VALID isn't set, and not to anything. Okay, gotcha. > Even though it's not a correctness issue, it seems to me that > DropRelationsAllBuffers() etc ought to check if the buffer is BM_TAG_VALID, > before doing anything further. Particularly in DropRelationsAllBuffers(), the > check we do for each buffer isn't cheap. Doing it for buffers that don't even > have a tag seems .. not smart. Depends on what percentage of buffers are valid, I guess. If all buffers are valid, checking BM_TAG_VALID first would lose. In practice, I doubt it makes any measurable difference either way. Since we're micro-optimizing, I noticed that BufTagMatchesRelFileLocator() compares the fields in order "spcOid, dbOid, relNumber". Before commit 82ac34db20, we used RelFileLocatorEqual(), which has this comment: /* * Note: RelFileLocatorEquals and RelFileLocatorBackendEquals compare relNumber * first since that is most likely to be different in two unequal * RelFileLocators. It is probably redundant to compare spcOid if the other * fields are found equal, but do it anyway to be sure. Likewise for checking * the backend ID in RelFileLocatorBackendEquals. */ So we lost that micro-optimization. Should we reorder the checks in BufTagMatchesRelFileLocator()? - Heikki