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 1sgRpe-002IP3-B5 for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2024 16:35:42 +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 1sgRpc-001S3P-8O for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2024 16:35:40 +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 1sgRpb-001S3G-RG for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2024 16:35:40 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sgRpY-000dm6-FL for pgsql-general@postgresql.org; Tue, 20 Aug 2024 16:35:39 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-530d0882370so5935481e87.3 for ; Tue, 20 Aug 2024 09:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hubifi.com; s=google; t=1724171735; x=1724776535; darn=postgresql.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Q+AO9WqOTu7Wj7xf21J0EqLokRAnlZWR0rcnmNSzEB8=; b=EtznOZRA5LpFGvAF/yls31uZEe+uqY4E+ik1YR9TTxwn/W8IRWBWRM2RrU8hTeQ56C 32+9IE+TWXJhtJ+rePzl4UwpB1KykO1TJ8dtSNODc2ozj4CpXlIKDUMRNBtQRoAmcnzp +Gp4Xnrmv2c0ZSWeUcoy6hONLNMwRLXsYkVSOYIvlPFAnkazQOVdZluj3okQVChnr8py 5Y7HyMAX8sVPW76sDQCJDQn/HohRGiIP1sVVJIAA2On4G1utPadVTqOtUGBUhBMATrnA f5WKgKi/KXUk18eoKYs1s4bt8+d810Ts0omp/adqIeMF68SLKZY6Un+mbSjmeFgnFeay QrGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724171735; x=1724776535; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Q+AO9WqOTu7Wj7xf21J0EqLokRAnlZWR0rcnmNSzEB8=; b=MPbmM5ySV05mR04f3MndNeYeDaMypcUyN3cW/5JORObcEuQy4IDRkjEBcksA/ECzR1 TIiyWIO6KjdPK3IoPS2Vh4CYEX8tIPe76dUYxConmB968fLXwOFnyTAb4slIYeBcMC0n EIzGGZL4w0fqDVvsH0Is4EmTk2EUrg+HZvhNnSzkE2yoURDkiR20TII4bF0rkQDETCf1 tsl/HJQFqNIw6vH2BPeNZDeldBrkwg8LDlSJV0HVCjBpffVOKkgaRCCisSXer1pUnNhr bqX7ppbPn+i3e7+0/3anKuzYRCiCOLhZPL7e/wI0xWwKZNsyiZAVMal2LfdlX+OQZ33+ +VVQ== X-Gm-Message-State: AOJu0YznGqGCz3rV4GScUJxiiaQYfGZMGSLkw3BYj2ccq2bo29jqcmM0 pA8OxSUzSyrGecR1kKkNL6wOhcyTdEuJvRt7oANoYyDcXXqqDbu9RqH/5bCr2l8/hp87RfsPqh1 8VE8vN4IUKPIZ3eE0eCjQNOi+FoHlRFuvgE5K5BBBzzbaqu1v6jGbtQ== X-Google-Smtp-Source: AGHT+IHdWYjAOCcR3F86qIMlAM/AVm5kHk3orS5mad6v6hV/JY5QbevdX58sZiv4yKe5/KEBYR2GgJFx4YFbXhHIqVk= X-Received: by 2002:a05:6512:2509:b0:533:4656:d503 with SMTP id 2adb3069b0e04-5334656d6bfmr540872e87.37.1724171734587; Tue, 20 Aug 2024 09:35:34 -0700 (PDT) MIME-Version: 1.0 From: William Kaper Date: Tue, 20 Aug 2024 12:35:20 -0400 Message-ID: Subject: Does a partition key need to be part of a composite index for the planner to take advantage of it? (PG 16.3+) To: pgsql-general@postgresql.org Cc: Antonio Papa Content-Type: multipart/alternative; boundary="000000000000c5c6cc062020034a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c5c6cc062020034a Content-Type: text/plain; charset="UTF-8" We have a set of operational tables that are all partitioned by organization ID (customer ID) in the 100M row range. We also have 3-4 composite indexes on these tables that currently do not include the organization ID. Any queries that reference these tables always provide the organization ID as a discriminator. We recently started noticing that the query planner sequence scanning the correct partitions, but is not using the indexes. So we decided to run a test by creating a new set of composite indexes that mirror the existing ones but include organization_id as the first column in the composite index. When we create the composite index to include organization ID in the first position, then the planner both selects the correct partitions, AND index scans those partitions. Is that expected behavior and it is appropriate to include any partition keys as leading columns in any indexes on a partitioned table? One additional piece of information that may or may not be relevant: a couple weeks ago we upgraded from PG 16.1 to 16.3. In the release notes for 16.2, I did see some fixes pertaining to indexes on partitioned tables and collations. I couldn't find information on the actual fixes (my inexperience digging into PG support). I'm happy to provide some simple examples to illustrate what we are seeing if the behavior I'm describing is not expected. Thanks, Bill Kaper --000000000000c5c6cc062020034a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
We have a set of operational tables that are all partition= ed by organization ID (customer ID) in the=C2=A0100M row range. We also hav= e 3-4 composite indexes on these tables that currently do not include the o= rganization ID. Any queries that reference these tables always provide=C2= =A0the organization ID as a discriminator.=C2=A0

We recently started= noticing that the query planner sequence scanning the correct partitions, = but is not using the indexes. So we decided to run a test by creating a new= set of composite indexes that mirror the existing ones but include organiz= ation_id as the first column in the composite index. When we create the com= posite index to include organization ID in the first position, then the pla= nner both selects the correct partitions, AND index scans those partitions.= =C2=A0

Is that expected behavior and it is appropriate to include an= y partition keys as leading columns in any indexes on a partitioned table?<= br>
One additional piece of information that may or may not be relevant:= a couple weeks ago we upgraded from PG 16.1 to 16.3. In the release notes = for 16.2, I did see some fixes pertaining to indexes on partitioned tables = and collations. I couldn't find information on the actual fixes (my ine= xperience digging into PG support).=C2=A0

I'm happy to provide s= ome simple examples to illustrate what we are seeing if the behavior I'= m describing is not expected.

Thanks,
Bill Kaper
--000000000000c5c6cc062020034a--