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.96) (envelope-from ) id 1w4xx6-002kha-1M for pgsql-performance@arkaria.postgresql.org; Tue, 24 Mar 2026 09:21:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w4xx4-005a9A-2i for pgsql-performance@arkaria.postgresql.org; Tue, 24 Mar 2026 09:21:31 +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.96) (envelope-from ) id 1w4xx4-005a92-1m for pgsql-performance@lists.postgresql.org; Tue, 24 Mar 2026 09:21:30 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w4xx2-00000000qsX-1r4q for pgsql-performance@lists.postgresql.org; Tue, 24 Mar 2026 09:21:30 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-48700b1ba53so34966335e9.1 for ; Tue, 24 Mar 2026 02:21:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774344087; x=1774948887; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=q5oRZmCFkmpByz8KJOox07eazE9nfmCi0eAzu7hbFPY=; b=VwCmhZcNcnT53CYAm7pKKn1qvA41ZjvL03DpP9yNSbXY3bb64rkFGY1vTsGFFtNFlT 9R7/llUwaLHhOkNs9uacKBMEWI/D+qih7DMspHhXR+vO1uq0lDlOG0LZRXj4z1QmLxlv +Bss0LY6hZJMjaMAR9Aleyi/pmKUPW9SY1hL35gG+TzX0qoJxD9KGT/gHs8I6yrnwJhp 3sIm2AMRkDQToAQ/AtdQVNOesmHYYGEe3EU68ePY7zyQssS+2gy3giAu7PMLhO8kqm3b WenQ+exoaFYWbK4XvfK5Rmfu5QTRrWE0ifOkYMNlZGXjx+US2ClCQ0eD2FPqm2svukME aMYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774344087; x=1774948887; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=q5oRZmCFkmpByz8KJOox07eazE9nfmCi0eAzu7hbFPY=; b=sjgRq2kbXRtv1WldD4CV06bfEzWOFc4ghilH2rsQ8s23LOrJ3QTVi3yLDLoKxLovvI s1Ad/wS6LmvEP1/Xw3Awy3OahNBnJim7ADr5LCbzzJLTxsRo3JUByrQfHNMk5NY7V5Li odZSt9BA9tCBrT0T8UC/BPVGaJJJblxdYhv0lmAizmkvAf0KBuUVFa2qzCKQqP1gR0Fq oB7OqfA7AGRkNt4lYFdJCm8Zc3WmdKc+vNEvZN1tagL7VqOtn9o4J35BFvKw6Ga/mqc7 th7l1Nd+tCktD6MNq+4Cx+DIEWL2fAluZFiLtdeZb1wWWPqHvB4tF37KsKohCyBgF2Vh DvJQ== X-Forwarded-Encrypted: i=1; AJvYcCWh90y5/eYo7iIbCawmLQXBoKWf11dcPtfbYTmIlJgzOY8qKb3EIuZKU3TO6TlirZxvCym0t4SIMsEJDM1O/HqYGg==@lists.postgresql.org X-Gm-Message-State: AOJu0YyGSgNMX/BRK9V3gNKXrfyDjEzkGxoCiMuI6aGeoqc+0o5IHuao bXtm02cK0Iuyg+kcykrfjoxrhcW2QLYQbR6gyL1ct6w5BsvMV1zfY2L+ X-Gm-Gg: ATEYQzwKrpI523Pra5yzJUqMkO3P9o0AMeSCYp8WSl5NR34ebq70DOjdlmTGO3FD3Ju lDgyJ4hoI5jg+bVLFWnHJ+Fb0DNozbLrVFYMczqvwz+rIqjWOM2sBsuLdOf6W5yfQNXmKxtD5Mg dxO5XcwFURqa6H+nOtdbX+S0aQnOLIAkE+tdlNEOvAvZkJMXmrgqaLNlBcojpqtmyN/+zWzDPFP NT3ic47wagLfGapJHx1fiZYYfAgLGOYxCf6ieMZLpXBKuzl/59VRA3E6+RPDbr5Q6EhGvsrxq2O AJwO5nSE18MAthTex3dcyG4Bo37EiRbC75rF4SkDTl6pXdiC8Y8HTHFOoDhbalnAAoY4rU2fVdK aS3FJNrbwpD0D0BU/aGPBfa0tj5wdFbqgN8yyqnWDJrhJ9/TU2gKZKeJhHI+JzFVczSAYJQ6yr0 Ww3gMIRVMbpOg1/X8OK4tOsm3+BYSfkH5UE7D5aW2Gu+rsRQuOxDv+Pfwz+Liw+qfdg7W+5gwp9 ehJ X-Received: by 2002:a05:600c:c48f:b0:485:b6dd:5066 with SMTP id 5b1f17b1804b1-486febb6014mr216510215e9.7.1774344087192; Tue, 24 Mar 2026 02:21:27 -0700 (PDT) Received: from smtpclient.apple (84.123.225.106.dyn.user.ono.com. [84.123.225.106]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48711764509sm37202675e9.13.2026.03.24.02.21.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Mar 2026 02:21:26 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Re: postgres chooses objectively wrong index From: Andrei Lepikhov In-Reply-To: Date: Tue, 24 Mar 2026 10:21:15 +0100 Cc: Alexey Ermakov , Tom Lane , pgsql-performance@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <217AB71B-F368-4DA1-A3B5-ABED94E1BD0A@gmail.com> References: <574349.1773786268@sss.pgh.pa.us> <381e7d47-8021-458d-9a64-0cdb80b004f0@gmail.com> <2a32c870-3f69-4b09-854d-ffea03b1e537@gmail.com> To: Merlin Moncure X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 23 Mar 2026, at 22:58, Merlin Moncure wrote: > On Thu, Mar 19, 2026 at 1:09=E2=80=AFAM Andrei Lepikhov = wrote: > Poking around more, I see that the bad plans are related to bloat. A = simple REINDEX of one of the indexes made the problem disappear; = however, what's odd is that the estimates didn't really change although = the net plan cost certainly did. It's also worth noting ANALYZE doesn't = help, only REINDEX does.=20 We already have plan-freezing and plan-hinting extensions. If I = understand you correctly, it makes sense to invent a statistics-freezing = module right now. I think such a module will be quite simple - is it a = good crutch for you? Also, we have some stuff already to work out your case someday: 1. Postgres already scans indexes during planning to improve estimations = of inequality clauses (get_actual_variable_range). Here may be a way to = estimate the bloat effect. Not sure how to do it, but allowing index AM = to read the page number of the returned tuple, you might, in principle, = detect anomalies in the index. 2. We are quite close to vacuum statistics and detailed index = statistics. This is also a way to estimate issues of stale = statistics/bloated indexes and decide on the scan type. So, keep the community posted and provide more real-life examples to = build up a proper solution. Andrei, pgEdge