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 1t44lD-00GHRi-T8 for pgsql-admin@arkaria.postgresql.org; Thu, 24 Oct 2024 20:48:48 +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 1t44lC-007kzS-25 for pgsql-admin@arkaria.postgresql.org; Thu, 24 Oct 2024 20:48:46 +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 1t44lB-007kzJ-3v for pgsql-admin@lists.postgresql.org; Thu, 24 Oct 2024 20:48:46 +0000 Received: from fhigh-b5-smtp.messagingengine.com ([202.12.124.156]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t44l7-002nMB-Sp for pgsql-admin@lists.postgresql.org; Thu, 24 Oct 2024 20:48:44 +0000 Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfhigh.stl.internal (Postfix) with ESMTP id 0769625400D3; Thu, 24 Oct 2024 16:48:39 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-10.internal (MEProxy); Thu, 24 Oct 2024 16:48:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= quadratum-braccas.com; h=cc:cc:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1729802918; x=1729889318; bh=SF5iCfnrlUrGGUW/3oUbPgQCmREaS1zHntx7Up0Huyc=; b= D+rn9AxM8wEkmQQtic6fSO+4xpDE5Zx89gkRgpcigIcuO5fLNCWcS7OiALF4ooEP yNCOH9o+DebCcS7XjNl0R2N4zMkp/JWj0q2oS3La4gqvyGixPP9fo+E+wee6dOUw FqVBoEkBlcZSoLAKLgGpDgUgCqRXTk4oCxGs6iGH8jG1/k/JknjiATmxmgXF11KN 0IYhr2VBGZfPHNuNJFtwTlYW/GlUOJ4bEDyNlAEjUMvHt6M95Kkf/57jlxnoF76S 91XPM1AqEjeA7MGdokT7uIC1d+B6Htt1+e07FugUp6ZjnxCKyMJ/ZsDoD85D4lMG /qks/dAI6Vl83UTPioadJQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1729802918; x=1729889318; bh=SF5iCfnrlUrGGUW/3oUbPgQCmREa S1zHntx7Up0Huyc=; b=K5A2kUBKgqziyYy5oWXTYTtj45M5IDNDa06FM/3njNMv 4VFiX5LkW4KVSJfxsVu5P1XErZQ18PkiacM0NtX2uAxMS2glzLFAeQp6xxC8xvph glpDKKeJBiipMMqf7hwnGudOkFKuYz4kp1MR0FwIVcJsuLseYpi8oxfkkHVKQbV1 V1QU1xl8LVRKWskEsJhuwBEBtTy85Dmz1t/luncKh0AF18/ACkRKV2dbNRjZxhX4 kLeVuseP2fl0iyi3pDVZ5abw4bAnad4u3VxEUh6LFL9C9zkUHBz36DS/jhp28t5g a6kfUjyMyIDiRS4LQa/+JqrMGUUCdEZu8cBoN86H3g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdejtddguddvkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpegtkfffgggfuffvvehfhfgjsegrtderredtvdej necuhfhrohhmpefusghosgcuoehssghosgesqhhurggurhgrthhumhdqsghrrggttggrsh drtghomheqnecuggftrfgrthhtvghrnhepffehveekhfefledufeegfffftdejffffffev jeeuveelhfdvgeeuveekvdfhgfelnecuffhomhgrihhnpegtrhhunhgthhihuggrthgrrd gtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep shgsohgssehquhgrughrrghtuhhmqdgsrhgrtggtrghsrdgtohhmpdhnsggprhgtphhtth hopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehkvghithhhrdhfihhskhgv segtrhhunhgthhihuggrthgrrdgtohhmpdhrtghpthhtohepphhgshhqlhdqrggumhhinh eslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepshgsohgssehq uhgrughrrghtuhhmqdgsrhgrtggtrghsrdgtohhm X-ME-Proxy: Feedback-ID: i374947ac:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 24 Oct 2024 16:48:38 -0400 (EDT) Content-Type: multipart/alternative; boundary="------------7hW9JqQm38oIuDUsUzOZEE8R" Message-ID: Date: Thu, 24 Oct 2024 14:48:36 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Partitioned table scanning all pertitions when the where clause specifies the partition key To: Keith Fiske Cc: "pgsql-performance@lists.postgresql.org" References: <31a09bff-78df-468b-8783-4bb5ebc8c0c9@quadratum-braccas.com> Content-Language: en-US From: Sbob In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------7hW9JqQm38oIuDUsUzOZEE8R Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 10/24/24 11:16 AM, Keith Fiske wrote: > > > On Thu, Oct 24, 2024 at 12:53 PM Sbob wrote: > > All; > > > I created a table that includes a lastname column that is a > varchar(50) : > > > CREATE TABLE customers ( > cust_id      bigint, > custinfo   text, > cust_dob   date, > lastname   varchar(50), > firstname varchar(50), > custaddr   varchar(200), > city            varchar(100), > state        varchar(100), > zipcode   varchar(5) > )  PARTITION BY LIST (lastname) ; > > > Then we created 4,000 partitions with each partition hosting a > specific > set of lastname's (each partition has 100 lastname's) > > The partitions are all based on UPPER(lastname) like this: > > > CREATE TABLE cust_part1 > PARTITION OF customer > FOR VALUES IN ('SMITH', 'JONES','REX', 'ROBINSON', 'ROBINSON JR' ... ) > > > and all the partitions have been loaded up with all the data that > lines > up with the partition key (100 lastnames per partition) > > > Each partition has an INDEX on upper(lastname) > > > > When I run something like : > > EXPLAIN SELECT * FROM customers where lastname = 'BORRIS'; > > > The explain plan does an index scan on each partition even though > constraint_exclusion os set to "partition" > > > Thanks in advance for any advice > > > > > > > Since your index is functional, in order to use the index, the queries > must use that same function in the condition. Try doing > > EXPLAIN SELECT * FROM customers where lastname = upper('BORRIS'); > > or this should give the same result > > EXPLAIN SELECT * FROM customers where lastname = upper('borris'); > same results > -- > Keith Fiske > Senior Database Engineer > Crunchy Data - http://crunchydata.com --------------7hW9JqQm38oIuDUsUzOZEE8R Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


On 10/24/24 11:16 AM, Keith Fiske wrote:


On Thu, Oct 24, 2024 at 12:53 PM Sbob <sbob@quadratum-braccas.com> wrote:
All;


I created a table that includes a lastname column that is a varchar(50) :


CREATE TABLE customers (
cust_id      bigint,
custinfo   text,
cust_dob   date,
lastname   varchar(50),
firstname varchar(50),
custaddr   varchar(200),
city            varchar(100),
state        varchar(100),
zipcode   varchar(5)
)  PARTITION BY LIST (lastname) ;


Then we created 4,000 partitions with each partition hosting a specific
set of lastname's (each partition has 100 lastname's)

The partitions are all based on UPPER(lastname) like this:


CREATE TABLE cust_part1
PARTITION OF customer
FOR VALUES IN ('SMITH', 'JONES','REX', 'ROBINSON', 'ROBINSON JR' ... )


and all the partitions have been loaded up with all the data that lines
up with the partition key (100 lastnames per partition)


Each partition has an INDEX on upper(lastname)



When I run something like :

EXPLAIN SELECT * FROM customers where lastname = 'BORRIS';


The explain plan does an index scan on each partition even though
constraint_exclusion os set to "partition"


Thanks in advance for any advice






Since your index is functional, in order to use the index, the queries must use that same function in the condition. Try doing 

EXPLAIN SELECT * FROM customers where lastname = upper('BORRIS');

or this should give the same result

EXPLAIN SELECT * FROM customers where lastname = upper('borris');


same results



--
Keith Fiske
Senior Database Engineer
Crunchy Data - http://crunchydata.com
--------------7hW9JqQm38oIuDUsUzOZEE8R--