Why is groupby doing seq scan when filtering by first column in composite primary key in Postgres 15

  Kiến thức lập trình

I have the following query:

SELECT
  "skuId",
  COALESCE(MAX("model"), $2) AS "model"
FROM
  "Forecast"
WHERE
  "orgId" = $1
GROUP BY
  "skuId"

That is quering a table

CREATE TABLE public."Forecast" (
    "orgId" text NOT NULL,
    "skuId" text NOT NULL,
    date date NOT NULL,
    percentile50 double precision,
    "extraData" jsonb NOT NULL,
    "cumulativeForecast" double precision,
    mean double precision,
    current double precision,
    model text,
    "warehouseId" text DEFAULT ''::text NOT NULL
);
ALTER TABLE ONLY public."Forecast"
    ADD CONSTRAINT "Forecast_pkey" PRIMARY KEY ("orgId", "skuId", "warehouseId", date);
CREATE INDEX "Forecast_orgId_skuId_date_idx" ON public."Forecast" USING btree ("orgId", "skuId", date);
ALTER TABLE ONLY public."Forecast"
    ADD CONSTRAINT "Forecast_orgId_skuId_fkey" FOREIGN KEY ("orgId", "skuId") REFERENCES public."Sku"("orgId", "skuId") ON UPDATE CASCADE ON DELETE CASCADE;

What we observe is that the first step is a seq scan which we don’t understand why is it not using the index on “orgId”. We are using a postgres 15.5 in Google Cloud?

enter image description here

LEAVE A COMMENT