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 1o7dV1-0005Dy-1Z for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Jul 2022 13:49:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o7dUz-00072I-Vd for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Jul 2022 13:49:25 +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 1o7dUz-000728-MC for pgsql-bugs@lists.postgresql.org; Sat, 02 Jul 2022 13:49:25 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o7dUw-000252-Mk for pgsql-bugs@lists.postgresql.org; Sat, 02 Jul 2022 13:49:24 +0000 Received: by mail-pl1-x632.google.com with SMTP id r1so4767276plo.10 for ; Sat, 02 Jul 2022 06:49:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=f1u+S8joSN3WmxrFU6BSHwQjD3C/+op6JLMN/fvw+HE=; b=H/qRArMT9wRneEam1iIz3o3jHiEOnzuDujPZcxc8ztl/2n/6qgG6CAescEvGIIjggV /3Z/VdbDJde6GSh31B3cDE8+YWuRyaZHn6YtF7vwxuKPMo/bAGVYNi/LUP4JEvxljt1d 5D81evnz9xq7rcyMxsyE7hb+na8jR2bR7ea+T8C7cDRAoDP6xYFFsBqZPVv4JbpS5mVV vNiooxWWIKXi2AGxqRDjSpiPwKLCIGyI+uK9Vk6OQ4hunoFpelHRVCsEEet0EvQi6I/V I+3uqi6jpK8rfPIaUQMtcMkdoqEGyKY/fYlRExUZIt0hQQWj/rEn/JNK8XV1pO9IcYLN IPUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=f1u+S8joSN3WmxrFU6BSHwQjD3C/+op6JLMN/fvw+HE=; b=KVn7RXOMLrSq4VI92Ko6cQHkiLHgUFmhea7IrdB4DZKfJkaJrY1+si7ff7+NicJ/fN hN8rsCMpazbBufJibzGLnn/ky6s/IHr+TJah/lbqGG1CxuF5UYKZhA5uSZwz0llH9BPP /wuEseuTWoN6sq4Omu98KQWdIajMIunjNgr5e2BrCf5HFK2xrsIAJIhaZZXTSZjILqn/ BEvYCNiR8rOxIUNfgoeDW8DDzdKFVdf6jvNQyd1y10ego8FY7KYeE0KphCus53gtTQTE f0+f981ciOtSxaXrZ9+e2i2w3Q1yNATRt60VCu2N8nRW3QiuLmZc3T2FJPDoQf1cixpm YYpg== X-Gm-Message-State: AJIora+qB4i/Wa7gxbf5NVtVL2BBwe6AjIEn8r7c2fBCRZ5gdv7o0FfN SQG3C3Y1TiHZo52IOIVUmug= X-Google-Smtp-Source: AGRyM1uD7/j09LPL0KTL+V4g2GEjUT24edYvAJNtaW+WfAE5re9yu50E7ZOLNM3hMh6UtDrCYCfLEQ== X-Received: by 2002:a17:90b:1d09:b0:1ec:bb51:9396 with SMTP id on9-20020a17090b1d0900b001ecbb519396mr24791734pjb.192.1656769760579; Sat, 02 Jul 2022 06:49:20 -0700 (PDT) Received: from jrouhaud (2001-b011-1006-7aef-51d1-c191-4404-8561.dynamic-ip6.hinet.net. [2001:b011:1006:7aef:51d1:c191:4404:8561]) by smtp.gmail.com with ESMTPSA id l18-20020a635b52000000b003fba1a97c49sm17193152pgm.61.2022.07.02.06.49.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Jul 2022 06:49:19 -0700 (PDT) Date: Sat, 2 Jul 2022 21:49:16 +0800 From: Julien Rouhaud To: Hung Nguyen Cc: pgsql-bugs@lists.postgresql.org Subject: Re: your mail Message-ID: <20220702134916.bazkqsw74np4ff2i@jrouhaud> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Sat, Jul 02, 2022 at 03:15:55PM +0300, Hung Nguyen wrote: > Hellom > > > > I've just upgraded my postgres instance from v11 to v14. There was an > interesting problem because we have a trigram index on order_id column. > > This new feature makes our simple join query on that column very slow. For > example: > > SELECT count(*) from order_rows o1 join order o2 on o1.order_id = o2.order_id Oh, that's surprising. It's not clear to me why any index would be used with such a query, especially if it's not compatible with index only scans. Is that some simplification of some query or really one that exhibit the problem? > > To solve this problem the existing trigram index must be dropped and we > cannot use ILIKE queries on this column. I just wonder if there is any way > to tell postgres what index (in this case btree index) to use when doing > the join operations? There's no such capability builtin. However, trigram indexes should be way more expensive that btree indexes in general, so the planner should be able to make the correct decision, there must be something else going on. > > [Postgres 14] Allow GiST/GIN pg_trgm indexes to do equality lookups (Julien > Rouhaud) > > I'm not sure if this is really a bug, but its' super weird if the query > planner favors the trigram index over the b-tree index for joining is not > optimal to me. Thank you so much. Can you provide the full definition for both order and order_rows, and EXPLAIN (ANALYZE, TIMING, BUFFERS) for the problematic query, with and without the trgm index being used? Doing a "SET enable_bitmapscan = 0;" should be enough for that. Do you have any usual settings configured, like enable_* or others?