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 1qIFvn-00007A-IQ for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Jul 2023 21:57:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qIFvm-0002Er-9u for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Jul 2023 21:57:30 +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 1qIFvl-0002Ei-PG for pgsql-hackers@lists.postgresql.org; Sat, 08 Jul 2023 21:57:30 +0000 Received: from meesny.iki.fi ([2001:67c:2b0:1c1::201]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qIFvf-002mDa-T1 for pgsql-hackers@lists.postgresql.org; Sat, 08 Jul 2023 21:57:28 +0000 Received: from [192.168.1.115] (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 4Qz3yg0FfhzyYb; Sun, 9 Jul 2023 00:57:14 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1688853437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ak3/0DTE1fhaWo0GdhEmEZLCYvtff347KENuJSVbM5A=; b=XHZr2O07Nd5D18OkfO7ayMKUK1uSxCBaEH29Yw+YxwvdTYCBBTfzwp6ca0YkoYOErz5v19 to5dC6TzcO5EAQ1CuQPIC0dwCaVvJgmNRSd5D8Y7zSMHtKvXBeXe2KsY4srrozBwdcdJq/ CricPDq6qaDps3teXhXtYrJyZk0vqWY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1688853437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ak3/0DTE1fhaWo0GdhEmEZLCYvtff347KENuJSVbM5A=; b=PrBm8wJlPdwEVFU3cpULtKJqAISjGy5td4qhl/ifPAhm/jG9RAE7OgsTlIhmPAqJM/x9Ed 1ZFaDZhv/dmjo+kA85VhhT0UsMMXdu9QubY+OzXrZSIluvAbEJSx1lq3pPFAsCaroSjiVE 6iKK0e1d3peFJG9O9fbeS2L6PJIPE/s= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=meesny; d=iki.fi; t=1688853437; a=rsa-sha256; cv=none; b=KXFbT0AsJJ+6iRLWc6rCnjpKYRHVyZo1I/5+RXjyX6rU1D7wLEpKDgd8ctM5GpkmcxvLSw vAawruTpJWsok1dFfAu1qY3+5nWttK9x/Zn085w7e3ZysGpesGyBGREk/dYcRZn4/FGpYg 3Kus+RDC6HE8dye046l52bMwyjTgnBE= Message-ID: <2484032b-3e6d-ae6a-61e5-732e2136ad18@iki.fi> Date: Sun, 9 Jul 2023 00:57:14 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: BRIN indexes vs. SK_SEARCHARRAY (and preprocessing scan keys) Content-Language: en-US To: Tomas Vondra , PostgreSQL Hackers References: <0e1f3350-c9cf-ab62-43a5-5dae314de89c@enterprisedb.com> <57020b2e-d9c9-9bc7-4892-b36d9bb07563@enterprisedb.com> <327064f1-276e-cb45-88c2-1ff498037cf5@enterprisedb.com> <4fa33caf-b5d1-6887-65a9-82ce9d92765c@enterprisedb.com> <1701e986-fa32-f3fa-0ac8-39fae8e5db35@enterprisedb.com> <856edf2c-8e95-0a8f-5a37-c8564a770a82@enterprisedb.com> From: Heikki Linnakangas In-Reply-To: <856edf2c-8e95-0a8f-5a37-c8564a770a82@enterprisedb.com> 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 02/07/2023 19:09, Tomas Vondra wrote: > Here's an updated version of the patch series. > > I've polished and pushed the first three patches with cleanup, tests to > improve test coverage and so on. I chose not to backpatch those - I > planned to do that to make future backpatches simpler, but the changes > ended up less disruptive than expected. > > The remaining patches are just about adding SK_SEARCHARRAY to BRIN. > > 0001 - adds the optional preprocess procedure, calls it from brinrescan > > 0002 to 0005 - adds the support to the existing BRIN opclasses Could you implement this completely in the consistent-function, by caching the sorted array in fn_extra, without adding the new preprocess procedure? On first call, when fn_extra == NULL, sort the array and stash it in fn_extra. I don't think that works, because fn_extra isn't reset when the scan keys change on rescan. We could reset it, and document that you can use fn_extra for per-scankey caching. There's some precedence for not resetting it though, see commit d22a09dc70f. But we could provide an opaque per-scankey scratch space like that somewhere else. In BrinDesc, perhaps. The new preprocess support function feels a bit too inflexible to me. True, you can store whatever you want in the ScanKey that it returns, but since that's the case, why not just make it void * ?It seems that the constraint here was that you need to pass a ScanKey to the consistent function, because the consistent function's signature is what it is. But we can change the signature, if we introduce a new support amproc number for it. > The main open question I have is what exactly does it mean that the > procedure is optional. In particular, should it be supported to have a > BRIN opclass without the "preprocess" procedure but using the other > built-in support procedures? > > For example, imagine you have a custom BRIN opclass in an extension (for > a custom data type or something). This does not need to implement any > procedures, it can just call the existing built-in ones. Of course, this > won't get the "preprocess" procedure automatically. > > Should we support such opclasses or should we force the extension to be > updated by adding a preprocess procedure? I'd say "optional" means we > should support (otherwise it'd not really optional). > > The reason why this matters is that "amsearcharray" is AM-level flag, > but the support procedure is defined by the opclass. So the consistent > function needs to handle SK_SEARCHARRAY keys both with and without > preprocessing. > > That's mostly what I did for all existing BRIN opclasses (it's a bit > confusing that opclass may refer to both the "generic" minmax or the > opclass defined for a particular data type). All the opclasses now > handle three cases: > > 1) scalar keys (just like before, with amsearcharray=fase) > > 2) array keys with preprocessing (sorted array, array of hashes, ...) > > 3) array keys without preprocessing (for compatibility with old > opclasses missing the optional preprocess procedure) > > The current code is a bit ugly, because it duplicates a bunch of code, > because the option (3) mostly does (1) in a loop. I'm confident this can > be reduced by refactoring and reusing some of the "shared" code. > > The question is if my interpretation of what "optional" procedure means > is reasonable. Thoughts? > > The other thing is how to test this "compatibility" code. I assume we > want to have the procedure for all built-in opclasses, so that won't > exercise it. I did test it by temporarily removing the procedure from a > couple pg_amproc.dat entries. I guess creating a custom opclass in the > regression test is the right solution. It would be unpleasant to force all BRIN opclasses to immediately implement the searcharray-logic. If we don't want to do that, we need to implement generic SK_SEARCHARRAY handling in BRIN AM itself. That would be pretty easy, right? Just call the regular consistent function for every element in the array. If an opclass wants to provide a faster/better implementation, it can provide a new consistent support procedure that supports that. Let's assign a new amproc number for new-style consistent function, which must support SK_SEARCHARRAY, and pass it some scratch space where it can cache whatever per-scankey data. Because it gets a new amproc number, we can define the arguments as we wish. We can pass a pointer to the per-scankey scratch space as a new argument, for example. We did this backwards-compatibility dance with the 3/4-argument variants of the current consistent functions. But I think assigning a whole new procedure number is better than looking at the number of arguments. -- Heikki Linnakangas Neon (https://neon.tech)