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 1utIpf-004VN3-A6 for pgsql-performance@arkaria.postgresql.org; Tue, 02 Sep 2025 04:41:24 +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 1utIpe-001z4C-FQ for pgsql-performance@arkaria.postgresql.org; Tue, 02 Sep 2025 04:41:22 +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 1utIpe-001z44-5T for pgsql-performance@lists.postgresql.org; Tue, 02 Sep 2025 04:41:22 +0000 Received: from mail-lj1-x22c.google.com ([2a00:1450:4864:20::22c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1utIpc-0006U2-0E for pgsql-performance@lists.postgresql.org; Tue, 02 Sep 2025 04:41:21 +0000 Received: by mail-lj1-x22c.google.com with SMTP id 38308e7fff4ca-336b0b3d753so33851691fa.1 for ; Mon, 01 Sep 2025 21:41:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756788078; x=1757392878; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XB/2/ERn4kqUPtnafd2ZH89JMXzzf360GPRgmLizDcg=; b=KuSEa+C8vhW4gA5uHE+kkAEK5UO+0dnIiJDaxs6+bZY28UZIIYxrvmU6znH3zw6wBz 56Usz9XE3DHmn9jJ2l4fy5A5364oTFj/su+tPjMRhSPKoNTDxGH8oXWmvIDHDOxz8rJa QS0lL7SFwyEAx8TO5RvHcnxPZbDZyrMPIYZbgQvYMQfmsq3mtt4CkoBL8VhIBMG+36YN 4SPaaMyLdI9vR6EojFsoY/RQtEuxuwEwavjTLSVhiCAWo2CLqxrXRjt+j6E+m2cyEQ4e BkRgEiwc3QZS4XvwUnxgwuXg+AP2/FNXkUxCA5fr2e9wD/0sQl5U0puR+qfAkYm8seEs 2EtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756788078; x=1757392878; h=content-transfer-encoding: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=XB/2/ERn4kqUPtnafd2ZH89JMXzzf360GPRgmLizDcg=; b=OGcBvYnA/4HgJeyDQyV9adgIrmZL8LDGXtQvKTGWGCbAQ8NbwyFhGWNVDUYQD6vEzL UdhbyU1/JHIarrKTsOiuM+FbPtDQT2vCG18MFainCFuSA+9rccAeekrRm8tQt2H+R1Ib h5KgNSjUDkWoNkfAoh8xtzHosilj+bLjGOFdluYOFN7eBbzYvcIVf9k6jw0jfCl409yA IcxsoKeAgff6hOoCbyFFFxeftyNhNT1j8wSks7MMF0hRmw7sxA3O+HfvS+9MSehNNe67 t7g+cHehIgmzPj+rCqVnU4v0B0AuiD5ZrUjUVBZtJy3QirQp/aWqB5Nl7bjCoklked2s u5Jg== X-Forwarded-Encrypted: i=1; AJvYcCX3TZzLf8geEJ8ipKzw8u1KA5uTTsMFEP+6MkUMlx5WW8oZt8UJ00MXdylGdNZhtAlhvMrxIWwreBNUM4wLlQpX8w==@lists.postgresql.org X-Gm-Message-State: AOJu0YwE0gclFIgjNp8mgs2inqKHfK3mcAxfi7Co3mnPYdCqqB61WXQZ zw+pFUFnrp5rfynlHS+mTr0KEyeixTP3MwCi4nyj+aL/UFeBJVhiOylZ4srOrY2ne1bL5uO/+/R qGevm/bJh9Jw6SZPYKwR1ZWekqi5R01M= X-Gm-Gg: ASbGncv6NI3B4T/TsUYEPT+B6enHxPUv9kS/LXQ6fBp9Lvom6kvXmpk7YZxeh9qoLZV jj9XZY70zJ4wdhCXpqfjx3surKyrPVzLJc1uETJyqsrMrNYWIjsOB/YEInJRqpBwIvkJat7+Ojg DIuLvUtmIR7et93s3ZxLXmV7eFAhGAtXp0TCWPJYtrYE5qVNHilA3n3jLrK5/MlT5BMoyTDjklx eey/XwzyxCOY/UHa3mCNXXBOdiVAxoA16u09iGtLg== X-Google-Smtp-Source: AGHT+IF22nWpLIxXXo7Z7Jdi+dtqQnP9ThYBpDebbyD+SAXFz9sPT3iKqYAWafgYPQBnkQJ+cpskmUyVBB+30kuU+t8= X-Received: by 2002:a05:651c:2210:b0:336:cd36:36c with SMTP id 38308e7fff4ca-336cd3607aemr29327191fa.11.1756788077980; Mon, 01 Sep 2025 21:41:17 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 2 Sep 2025 16:41:06 +1200 X-Gm-Features: Ac12FXzfMXGVstGj5wrnIM81GCga-_fevz5Eyz4hJz4wXPxZ5ApNRDhrteU6pL0 Message-ID: Subject: Re: Planner makes sub-optimal execution plan To: Alena Rybakina Cc: =?UTF-8?B?0JDQu9C10LrRgdC10Lkg0JHQvtGA0YnRkdCy?= , pgsql-performance@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2 Sept 2025 at 00:41, Alena Rybakina wr= ote: > After disabling MIN/MAX optimization in the grouping_planner function: > /* > * Preprocess MIN/MAX aggregates, if any. Note: be careful abou= t > * adding logic between here and the query_planner() call. > Anything > * that is needed in MIN/MAX-optimizable cases will have to be > * duplicated in planagg.c. > */ > //if (parse->hasAggs) > // preprocess_minmax_aggregates(root); > > I got a better query plan, but I=E2=80=99m still investigating what went = wrong. This is basically just the standard issue people have with how we cost LIMI= T. If you look at this part: -> Limit (cost=3D0.43..7.91 rows=3D1 width=3D8) Output: docum.dt -> Index Scan using docum_dt_7ee1d676 on public.docum (cost=3D0.43..420487.43 rows=3D56222 width=3D8) you can see that the Limit total cost is startup (0.43) plus 420487.43 / 56222 * 1. The problem is that the planner assumes there's no correlation between dt and dt_real. It thinks because a decent number of rows have dt_real >=3D '2025-08-14 09:44:09.033592' that it'll find the LIMIT 1 row fairly quickly by scanning the docum_dt_7ee1d676 index. Unfortunately, it's not quick because the rows matching dt_real >=3D '2025-08-14 09:44:09.033592' are nearly at the end of the index. In v16+, using something like MIN(docum.dt ORDER BY dt) AS "dt__min" should force the planner into giving a better plan. ORDER BY / DISTINCT aggregates are a bit less efficient in versions earlier than that, so doing that would require a bit of additional work if using v14. It might not be too bad, though. David