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 1t41SV-00FzH7-1a for pgsql-admin@arkaria.postgresql.org; Thu, 24 Oct 2024 17:17:15 +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 1t41ST-005vWZ-DV for pgsql-admin@arkaria.postgresql.org; Thu, 24 Oct 2024 17:17:13 +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 1t41ST-005vWQ-26 for pgsql-admin@lists.postgresql.org; Thu, 24 Oct 2024 17:17:13 +0000 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t41SM-002esU-PR for pgsql-admin@lists.postgresql.org; Thu, 24 Oct 2024 17:17:12 +0000 Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-7180bd5b79dso601678a34.2 for ; Thu, 24 Oct 2024 10:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crunchydata.com; s=google; t=1729790226; x=1730395026; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=upfiM9VbgBepjntXAKYlOVLykUmkkhXQcZwyOpZiZTU=; b=HbIkE45w4l8n1ngYex9qvcso1qQ3aAuAIvihKZhPtN8JHEaPefLpNPxoAhufwgzVZj wAGnUUaal5YRPXwQjG923w8oO+3Sgd4R0Acm4q+N5b8Jp/it5HTOhT9HaEImm55P3+z5 CWZFt+gE/xwft2vTVPkJeMQ9hqwRHnE2dZYjT+9zcZfs2iHGMRGOiEptpZslP9SgpZ/Y Sh5yo3laUrE8IapolpyzVonAS0Dr4BBqoClV6h0uZZo1YsLzdAaHeoqUCXyyknYf2oyu bHp/bEl2pN0ApvryGzufTKhNKgU4TNRFSwFE/pNRt0hu7YWRT3gvQtlmINoNn+EjGdKM czmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729790226; x=1730395026; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=upfiM9VbgBepjntXAKYlOVLykUmkkhXQcZwyOpZiZTU=; b=h5no3t6rAjVpGDYlNvqpEXtfqsf4ldhQWwgw4DtjcnG81okcZo5LOPXTjlahctaZLy DB3oAcTRZ8Cwbk3K/JeFNAtXpjvGsPkZqd/dJai56qo2KerDi80ySDgmTwqfPNnxwIYt KPkfcZtVYfa9E306pjphZeME8mmhlYkRoBo7rAry34BqRgB/aZW9Sc/4jv+zYrfvZrbg 9VvcJCskGahgdvIKHv30kiaOicUL9NYvjrSqL9zzrhJUnUmgoOssSUs+28Gfks43BXIt ro2wY1WWJygBaoLpM9fRVeLAkAY67R2HPgiPt25XsSYAijnqJXOc2ipjfm73NCD5XvnD MMkg== X-Gm-Message-State: AOJu0YykwY+cnZqH6q6vWd6ckWkrfzmmqP2NfZQ6o2ut5WmzpinHvuTp HjnvsSTAGKzGRAylL8z+Kfyan8Y0g1C0tXgqapLNRpmDlqyOVAX65Dtz7S9t8K8snFqvnV9fEGs hEWj9gef9ED0Y3+oEolcqWxVZC9j3R/exIQDa1j9vPjUr/ulN3oY= X-Google-Smtp-Source: AGHT+IEtU9SMMufKS0moiHsrlUyri0si7LtfpcRIAVkB/f0QKplDBJsebbFBN/aP/OwvRuU60Wd/SlqjNPDGLM9KQsU= X-Received: by 2002:a05:6830:730d:b0:710:f8df:33d2 with SMTP id 46e09a7af769-7184b2b31a9mr7449504a34.0.1729790225853; Thu, 24 Oct 2024 10:17:05 -0700 (PDT) MIME-Version: 1.0 References: <31a09bff-78df-468b-8783-4bb5ebc8c0c9@quadratum-braccas.com> In-Reply-To: <31a09bff-78df-468b-8783-4bb5ebc8c0c9@quadratum-braccas.com> From: Keith Fiske Date: Thu, 24 Oct 2024 13:16:39 -0400 Message-ID: Subject: Re: Partitioned table scanning all pertitions when the where clause specifies the partition key To: Sbob Cc: "pgsql-performance@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000f2c99d06253c2b5f" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f2c99d06253c2b5f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Oct 24, 2024 at 12:53=E2=80=AFPM 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 =3D '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 =3D upper('BORRIS'); or this should give the same result EXPLAIN SELECT * FROM customers where lastname =3D upper('borris'); --=20 Keith Fiske Senior Database Engineer Crunchy Data - http://crunchydata.com --000000000000f2c99d06253c2b5f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Thu, Oct 24, 2024 at 12:53=E2=80= =AFPM 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=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bigint,
custinfo=C2=A0=C2=A0 text,
cust_dob=C2=A0=C2=A0 date,
lastname=C2=A0=C2=A0 varchar(50),
firstname varchar(50),
custaddr=C2=A0=C2=A0 varchar(200),
city=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 varc= har(100),
state=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 varchar(100),
zipcode=C2=A0=C2=A0 varchar(5)
)=C2=A0 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', 'ROBINSO= N', '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 =3D '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, i= n order to use the index, the queries must use that same function in the co= ndition. Try doing=C2=A0

EXPLAIN SELECT * FROM customers where lastn= ame =3D upper('BORRIS');

or this shoul= d give the same result

EXPLAIN SELECT * FROM custo= mers where lastname =3D upper('borris');

--
Keith Fiske
= Senior Database Engineer
Crunchy Data - http://crunchydata.com
--000000000000f2c99d06253c2b5f--