Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rZuOX-0074xp-8f for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Feb 2024 15:08:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rZuOW-000ZV1-7S for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Feb 2024 15:08:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rZuOV-000ZUt-TX for pgsql-hackers@lists.postgresql.org; Tue, 13 Feb 2024 15:08:23 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rZuOS-006Ycp-66 for pgsql-hackers@postgresql.org; Tue, 13 Feb 2024 15:08:22 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-410ed9f8621so14806695e9.1 for ; Tue, 13 Feb 2024 07:08:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1707836897; x=1708441697; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=N9YHoZk3gKglX7r7O1ikQ4cyq4w1UR40tWZzm/f2zjA=; b=FOZd71T3Zz4ipMGOngtpz2FZE+2Oyj62UsHzm1BihUsL5Syo1jJhC/auxBK3IApQQZ /cN2VxngLxojRd3FVBGzVVM4M1NpJox1crU+LPFFz8AKQdWiY4OQguQDIGDutV1gePKn j+OhkVy2PK80/JIZlW5YSela27fSjs/DjTrjKSsCL7bk3EiXn92PSV1FnM30H23Ap7oV kUvdKUMl5Jv8uMQr9lplCWfAJgLo0wMOJYt9BB3IvPjCfjoFjsDxYBEscPXys/cgcVaD bpnBg+w9K2iVRr3cH89FdbMfsu5tir2LFNzERCdjXelIDLXQr8mHC+HpZiYDM0RVJyPp F4aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707836897; x=1708441697; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=N9YHoZk3gKglX7r7O1ikQ4cyq4w1UR40tWZzm/f2zjA=; b=g/thvGH7C4i5j/EtXQguemQv6LuPGkvrXn3rnm8pzGYmyyVeeOPUlIDwFfvNolAXtT BcjXuUGgRaC2IyQ4HsTC1SFDC4MSJZRsEmzy67EnZgvLikktUfOwEoUA5+MtyhDBWPAW OkOjPCOE4H9CUrKsInMgJS0khmWB9PfVDgUTwR3lpP577k5BK3+eXwWKVqYJj9peBGhm js4IPLnVXhC5Gplr80ToW4aiJH0Rh66QNYUHVDUSIm1VHWwYIdWa/OAMM5dGQ1KLBwzu gLjwodl2Rhk6Rguu7wPI9z6daRR0DGS+dywOvqBQpuoeKrl8IV+Kz5mvHFB8EGpEf65X MyAg== X-Forwarded-Encrypted: i=1; AJvYcCWVdN2Sn1yT0VAhNY51YyX09GYXM00FZ1DPo8OQu4goGxggCur9Xd7uwaPd5dEVIWpXRSaJZDgvk7+wW+KutbhOEXeOfivO0XDPSb2Y X-Gm-Message-State: AOJu0YzM5OKWfFiZBmpnFZOz6tGXRqOp1PPBttS0jiju6xNHhP2r61NR yHG9BephCdPLZGy1g//KdQfAO0Pt+boQuSv0YbaKIr3YHF+9AQI9Zhm6QPLL3Q== X-Google-Smtp-Source: AGHT+IFlK1QDytq5pazdMxjRXwyG+9KZ04OGnIuWL/CchByqvE1jTLkL3INeC1RBQ3s/+sodIs47GQ== X-Received: by 2002:adf:e610:0:b0:33b:21e4:68d5 with SMTP id p16-20020adfe610000000b0033b21e468d5mr6917242wrm.34.1707836897567; Tue, 13 Feb 2024 07:08:17 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCU/BSQ4pKuVy62JiOCHqhiym4f5BOtLNPCb7VmfTxTBHbsEhx94H+vCXeQiXtdn/GYJlfGcCdw8PqAROP1mFdsMc16+OeLDcrfpek4h0OHq6Q69UI9Js47Ug33QbWiEGKqgSiGyBn9P3fNDc9IO3fDcAGgQGTTXymJvZKpJSsTX0NsenAYShcObtNQDiQdRHy1uofu3liim7s+j Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id bu3-20020a056000078300b0033b46b1b6adsm9920627wrb.21.2024.02.13.07.08.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 13 Feb 2024 07:08:17 -0800 (PST) Message-ID: Date: Tue, 13 Feb 2024 16:08:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: brininsert optimization opportunity Content-Language: en-US To: Alvaro Herrera Cc: Alexander Lakhin , Soumyadeep Chakraborty , Matthias van de Meent , pgsql-hackers , Ashwin Agrawal References: <202401081551.3olwx6pyw2mn@alvherre.pgsql> From: Tomas Vondra In-Reply-To: <202401081551.3olwx6pyw2mn@alvherre.pgsql> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/8/24 16:51, Alvaro Herrera wrote: > On 2023-Dec-12, Tomas Vondra wrote: > >> I propose we do a much simpler thing instead - allow the cache may be >> initialized / cleaned up repeatedly, and make sure it gets reset at >> convenient place (typically after index_insert calls that don't go >> through execIndexing). That'd mean the cleanup does not need to happen >> very far from the index_insert(), which makes the reasoning much easier. >> 0002 does this. > > I'm not in love with this 0002 patch; I think the layering after 0001 is > correct in that the insert_cleanup call should remain in validate_index > and called after the whole thing is done, but 0002 changes things so > that now every table AM has to worry about doing this correctly; and a > bug of omission will not be detected unless you have a BRIN index on > such a table and happen to use CREATE INDEX CONCURRENTLY. So a > developer has essentially zero chance to do things correctly, which I > think we'd rather avoid. > True. If the AM code does not need to worry about this kind of stuff, that would be good / less error prone. One thing that is not very clear to me is that I don't think there's a very good way to determine which places need the cleanup call. Because it depends on (a) what kind of index is used and (b) what happens in the code called earlier (which may easily do arbitrary stuff). Which means we have to call the cleanup whenever the code *might* have done inserts into the index. Maybe it's not such an issue in practice, though. > So I think we should do 0001 and perhaps some further tweaks to the > original brininsert optimization commit: I think the aminsertcleanup > callback should receive the indexRelation as first argument; and also I > think it's not index_insert_cleanup() job to worry about whether > ii_AmCache is NULL or not, but instead the callback should be invoked > always, and then it's aminsertcleanup job to do nothing if ii_AmCache is > NULL. That way, the index AM API doesn't have to worry about which > parts of IndexInfo (or the indexRelation) is aminsertcleanup going to > care about. If we decide to change this, then the docs also need a bit > of tweaking I think. > Yeah, passing the indexRelation to the am callback seems reasonable. It's more consistent what we do for other callbacks, and perhaps the callback might need the indexRelation. I don't quite see why we should invoke the callback with ii_AmCache=NULL though. If there's nothing cached, why bother? It just means all cleanup callbacks have to do this NULL check on their own. > Lastly, I kinda disagree with the notion that only some of the callers > of aminsert should call aminsertcleanup, even though btree doesn't have > an aminsertcleanup and thus it can't affect TOAST or catalogs. Maybe we > can turn index_insert_cleanup into an inline function, which can quickly > do nothing if aminsertcleanup isn't defined. Then we no longer have the > layering violation where we assume that btree doesn't care. But the > proposed change in this paragraph can be maybe handled separately to > avoid confusing things with the bugfix in the two paragraphs above. > After thinking about this a bit more I agree with you - we should call the cleanup from each place calling aminsert, even if it's for nbtree (or other index types that don't require cleanup at the moment). I wonder if there's a nice way to check this in assert-enabled builds? Could we tweak nbtree (or index AM in general) to check that all places that called aminsert also called aminsertcleanup? For example, I was thinking we might add a flag to IndexInfo (separate from the ii_AmCache), tracking if aminsert() was called, and then later check the aminsertcleanup() got called too. The problem however is there's no obviously convenient place for this check, because IndexInfo is not freed explicitly ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company