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 1rfMBt-00H9ir-NU for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2024 15:49:54 +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 1rfMBr-00BlyO-U5 for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2024 15:49:52 +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 1rfMBr-00BlyG-Jf for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2024 15:49:52 +0000 Received: from vitry-1.mx.webmsg.me ([2001:bc8:394a:100::1]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rfMBl-001pzq-1f for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2024 15:49:50 +0000 Received: from leygaz.mildred.fr ([193.33.57.69]:43732 helo=[192.168.5.225]) by vitry-1.mx.webmsg.me with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1rfMBh-00DHWt-2P for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2024 16:49:41 +0100 Message-ID: Date: Wed, 28 Feb 2024 16:49:40 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Efficient rows filter for array inclusion with gin index Content-Language: en-US, fr-FR To: pgsql-general@lists.postgresql.org References: <7dec2a11-fc1f-47d1-8354-7bee04f0a835@mildred.fr> From: Shanti-Dominique In-Reply-To: <7dec2a11-fc1f-47d1-8354-7bee04f0a835@mildred.fr> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Replying to myself after more investigation. On 28/02/2024 12:05, Shanti-Dominique wrote: > > > 2) >     SELECT  * >     FROM    items i1 >             JOIN item_paths p1 ON i1.ref_id = p1.ref_id >             JOIN items i2 ON ARRAY[i2.ref_id] <@ p1.item_path >     WHERE   ... > > 2) uses the operator <@ which is supported by the gin index, the test > for inclusion is fast and the query does not run a sequential scan > over the whole "item_paths" table. However, because of the > ARRAY[i2.ref_id] construct, it performs a sequential scan on i2. I was under the assumption that the ARRAY[] construct prevented postgresql from efficiently using the index on the other side of the operator, but I think I was mistaken. On a database full of data, I tried getting around this but did not see any improvement of performance. First I tried to add an index on the single element array: CREATE FUNCTION uuidarr(ref_id uuid) RETURNS uuid[]   LANGUAGE SQL   IMMUTABLE   RETURNS NULL ON NULL INPUT   RETURN ARRAY[ref_id]; CREATE INDEX items_ref_id_arr2_idx ON items USING gin (uuidarr(ref_id)); EXPLAIN SELECT  i2.* FROM    items i1         JOIN item_paths p1 ON i1.ref_id = p1.ref_id         JOIN items i2 ON uuidarr(i2.ref_id) <@ p1.item_path WHERE   i1.name = 'a'; The performance was even worse. Then I tried with a generated column: CREATE TABLE items (     ref_id uuid DEFAULT public.gen_random_uuid() NOT NULL,     ref_id_array uuid[] GENERATED ALWAYS AS (uuidarr(ref_id)) STORED,     name character varying,     parent_ref_id uuid ); CREATE INDEX items_ref_id_array_idx ON items USING gin (ref_id_array); EXPLAIN SELECT  i2.* FROM    items i1         JOIN item_paths p1 ON i1.ref_id = p1.ref_id         JOIN items i2 ON i2.ref_id_array <@ p1.item_path WHERE   i1.name = 'a'; The performance was very similar to the query with ARRAY[...] It seems there is no good solution for the general case, apart from changing the structure of my dataset and removing the use of arrays entirely. I think I'll update my codebase and use <@ where it makes sense and = ANY in other places, but it'll be difficult to know for sure without running the query which one will be better.