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 1rnzUY-00GQJf-Oe for pgsql-bugs@arkaria.postgresql.org; Sat, 23 Mar 2024 11:24:51 +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 1rnzUW-009iHN-Cx for pgsql-bugs@arkaria.postgresql.org; Sat, 23 Mar 2024 11:24:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rnzUW-009iHE-3r for pgsql-bugs@lists.postgresql.org; Sat, 23 Mar 2024 11:24:48 +0000 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rnzUS-0064RS-Cn for pgsql-bugs@lists.postgresql.org; Sat, 23 Mar 2024 11:24:47 +0000 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-3417a3151c5so1686564f8f.1 for ; Sat, 23 Mar 2024 04:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1711193083; x=1711797883; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=loZfgCSh+Q3+ptOK8Fu3BFrXiFjEbiDoXvNT377I2Ic=; b=gwoKIN2sk6qxomw9tpZps/X5PgQQd2RPTM+Ael7/YqW1u1FWR5ekdAKgHzOIbSB5K4 dXugkLSW9n+2CRHa4HYh83EuyHErDR78XJIzT26eQ+QBp/Ll6gjzsGPnPctrVGyrJG+8 0bQDzj/ogS5x45FTBHL/lhjzDte+XmJdobxAZdlwpGBzv7+fMudH344Vras+q4ZYXv7G JrCDBmJahQ882fw+H8Hd9FP5xZt0dDSCMMyssDPzRn5NhUPkLfhw+zacZ8vo3W5b9sVu XQ11cEzKf1nRfPWcajiWZlDcov9a59tPFZbCBcM8+kJubnwB0Vb+rdf/X1YohHNqeTFz OXNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711193083; x=1711797883; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=loZfgCSh+Q3+ptOK8Fu3BFrXiFjEbiDoXvNT377I2Ic=; b=T6vU9HYUJ7L8oJgHhLLkZaq9ojuTP8tTyrfDdvkGF/40zT+L8h/pg+BqesgmsFdZQp sP2BA4eXn1ayLEBVfWjQf3hBXEetRXGQJYtcgG2catRv+11xgKesZH0kOunA+puKUPdn T3eWXqzUOUh2isrEdTEE4Gq/azg/h19IZViIfxah+6whRiUcbOkW9v5EyvUlt4PKSWZq x5uLyNMGg2MB816IW+jKxHYjO5UEDTIHq7owdh5YBZnGXT5/BamzI9vu3dVQPU7Gy2ej zxann1egBwgTHeiDbv991m7cY7rdIREwIepSr0hsgquScF+YWLmp3pV9fpF10bASiB3l myDg== X-Gm-Message-State: AOJu0YwD2kt2Hl7PO4tYd6Q59XImMwKKFHAU+GX3cu1luhFhg443N+BH ng3TaGjwHQVbEDeAyIDDj7K9LNqgHAtXbsgTGKsYhFH0QnaE+7v8l2xfskk1jg== X-Google-Smtp-Source: AGHT+IHOMwmbydRYKtEVN1ai5YraboUrKF19aSvhynwdjz/sfoqTR3isuL0FjUqAmcj0eORL9eFvoA== X-Received: by 2002:adf:e346:0:b0:341:8833:623c with SMTP id n6-20020adfe346000000b003418833623cmr1414598wrj.70.1711193082649; Sat, 23 Mar 2024 04:24:42 -0700 (PDT) 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 ck8-20020a5d5e88000000b00341ba91c1f5sm2925700wrb.102.2024.03.23.04.24.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 23 Mar 2024 04:24:42 -0700 (PDT) Message-ID: Date: Sat, 23 Mar 2024 12:24:42 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Index plan returns different results to sequential scan To: Peter Geoghegan , John Burns Cc: pgsql-bugs@lists.postgresql.org References: <3B86F188-F4BF-4331-AD79-E5FF2D0711A3@impactdatametrics.com> Content-Language: en-US From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/23/24 03:24, Peter Geoghegan wrote: > On Thu, Mar 21, 2024 at 2:03 PM John Burns wrote: >> The query is SELECT * FROM XXX where postcode % ’NW10’ >> To create a sample table — create table XXX ( udprn bigint, postcode postcode ) >> To Index it CREATE INDEX on XXX(postcode) > > The opfamily's % operator uses the B-Tree equality strategy. This > means that it works the same way as = works in most other opfamilies. > > I don't see how equality can work reliably here. A query with a > predicate "WHERE my_indexed_postcode_column % ‘NW10’" seems to work by > masking the value stored in the index, throwing away some amount of > suffix bytes in the process. But the values from the index are still > stored in their original order -- the temporarily masked suffix bytes > aren't masked in the index, of course (they're only masked > temporarily, by the cross-type equality operator %). > > Wouldn't you need something closer to "WHERE > my_indexed_postcode_column >= ‘NW10’ and my_indexed_postcode_column < > ‘NW11’" for this to work reliably? > Yeah, I was not sure how come this could be processed using equality operator, but I got distracted by bisecting this to a particular commit. I didn't realize the commit might have changed how much we rely on the opfamily to do things correctly. I do think the '%' operator is pretty close to what we do for LIKE with prefix patterns for text: explain select * from t where a like 'aa%'; QUERY PLAN ----------------------------------------------------------------------- Index Only Scan using t_a_idx on t (cost=0.29..4.31 rows=1 width=33) Index Cond: ((a ~>=~ 'aa'::text) AND (a ~<~ 'ab'::text)) Filter: (a ~~ 'aa%'::text) (3 rows) So I guess postcode would need to do something similar - treat the '%' operator as separate from opclass equality, and define a new support procedure akin to text_support, translating the '%' into the range quals that can match the index. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company