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 1sioMn-009fxM-UP for pgsql-general@arkaria.postgresql.org; Tue, 27 Aug 2024 05:03:41 +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 1sioMk-001vGl-W0 for pgsql-general@arkaria.postgresql.org; Tue, 27 Aug 2024 05:03:39 +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 1sioMk-001vGL-Kx for pgsql-general@lists.postgresql.org; Tue, 27 Aug 2024 05:03:39 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sioMh-001dFG-Sp for pgsql-general@postgresql.org; Tue, 27 Aug 2024 05:03:37 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a86984e035aso578026966b.2 for ; Mon, 26 Aug 2024 22:03:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1724735014; x=1725339814; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=hP6JqEvFXIuwg2/otovkvMnOE9AHFZDsyr3FApGRpig=; b=gYubNUV9b/J2y33YdOw3VNkdePcYchAtpAsxiPP3wechO2wmmsmxs+eWkW7objcLNe yfYN8XQTJSvKJLESOXLxmEq4heeiN3iDOeAkKXeBM/UFt4r2IWmfpMHPEn9fQ9p3ek6q G9SruGn+NQbFWQBxxnM7RS9lcmMEH7uzexfYuEehnxl0XpCvb7oe0SyKB8dDcK6kpm+q 8XemPFC4Ud54IEg95DKivY0Ayft06pB5BZglA6gzaivBPJBHlOMf711K09XGcKJV0ty/ nzkBtN2cGqRkz8ucPeXOO3I70dW43UyfAEqG4oI33qwkBDF1AMYBQ3aaQ3lX1YZ5htge dKcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724735014; x=1725339814; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hP6JqEvFXIuwg2/otovkvMnOE9AHFZDsyr3FApGRpig=; b=mr1+wFmj0AOYkQTeRu4LOWnWpGNtIGESOdjkKDG5HlGFg4eSX97lnnJIHzQ87H5X9j nYD2RSW46w+HbTmuCU4YCaXnBf5YxtxuW/DxAKYAjS5Bx2ekHARc9hA/nuKH6uwgjm2B HZpZWnA1vWndOS0hSFuCExxFGEL+0HtsU/CsZTW/xiXwndL3eHyXjpUzTjnRXoJc1cTT HtMELVFIQQFNfCFX7PgsrcJ/719GPkDoJfsN5wyzeT7nXkrvy0vuk5PnZPuN13LELeNE It4DPlJxh3NRKrYApjmlq2bA0ugcVCbtMOQL+F5yWgjsvDRtPGn+d4Lc1y7aud79TvqR EoKg== X-Forwarded-Encrypted: i=1; AJvYcCW61TcvH0tudJMUy7NCdRWFXuw78dMEJM1397qkx1or1uzvJ5mRBYZxcIP5qlM//Eu5S4x56nljMejUl2hd@postgresql.org X-Gm-Message-State: AOJu0YwoyY73GBByBCLu+OUapiEX4r8VNqJmGguFg3LYWDsZlFgIgj9Q D9ucIx4sv/plOlQZFrehO0VC1cbOujTW3DgKC9b8IjAtL7zdIby8ztAXax6av8o= X-Google-Smtp-Source: AGHT+IE1GLv/liBBHM0A/INkebbaW3B7SywssXshu3bTSFZ292TgF+VjBxKnyckfk+05v67Mw2u87w== X-Received: by 2002:a17:906:c144:b0:a86:9487:f1d2 with SMTP id a640c23a62f3a-a86e3a75e3dmr104209366b.40.1724735013718; Mon, 26 Aug 2024 22:03:33 -0700 (PDT) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:260:52c8:9387:8d5:1d7c:eff5]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a86e5878f79sm61074966b.179.2024.08.26.22.03.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Aug 2024 22:03:33 -0700 (PDT) Message-ID: <2077e7db3cab149dfcb334dbd649d3fbe648ed98.camel@cybertec.at> Subject: Re: Does a partition key need to be part of a composite index for the planner to take advantage of it? (PG 16.3+) From: Laurenz Albe To: William Kaper , pgsql-general@postgresql.org Cc: Antonio Papa Date: Tue, 27 Aug 2024 07:03:32 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2024-08-20 at 12:35 -0400, William Kaper wrote: > We have a set of operational tables that are all partitioned by organizat= ion ID > (customer ID) in the=C2=A0100M row range. We also have 3-4 composite inde= xes on these > tables that currently do not include the organization ID. Any queries tha= t > reference these tables always provide=C2=A0the organization ID as a discr= iminator.=C2=A0 >=20 > 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 creat= e the > composite index to include organization ID in the first position, then th= e planner > both selects the correct partitions, AND index scans those partitions.=C2= =A0 >=20 > Is that expected behavior and it is appropriate to include any partition = keys > as leading columns in any indexes on a partitioned table? I think it is hard to reason about this without seeing a concrete example a= nd the EXPLAIN (ANALYZE, BUFFERS) output for it. Yours, Laurenz Albe