Re: postgres_fdw: Fix costing of remote sorts without remote estimates

From: "Jelte Fennema-Nio" <postgres(at)jeltef(dot)nl>
To: "Rui Zhao" <zhaorui126(at)gmail(dot)com>
Cc: "PostgreSQL-development" <pgsql-hackers(at)postgresql(dot)org>, "Etsuro Fujita" <etsuro(dot)fujita(at)gmail(dot)com>, "Robert Haas" <robertmhaas(at)gmail(dot)com>, "Ashutosh Bapat" <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, "Narayanan Venkateswaran" <narayananvpostgres(at)gmail(dot)com>
Subject: Re: postgres_fdw: Fix costing of remote sorts without remote estimates
Date: 2026-10-04 20:51:35
Message-ID: DLWCNK7EPYMZ.2UMXT8Q36OCWL@jeltef.nl
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri Oct 2, 2026 at 6:29 PM CEST, Rui Zhao wrote:
> 1. I found a regression with a local filter and LIMIT:
> 2. Local projection costs can also prevent LIMIT pushdown. Using
> sort_ft above:

Thanks for the review and finding these plan-regressions. It turns out
that the underlying problem isn't specific to my patch. With slightly
different queries master picks the same kind of bad plans. I did not use
your suggested fixes. Instead I changed estimate_path_cost_size() to
build up the costs in the order the work happens: remote work, then
transfer, then local work. That approach results in less code, and
that code is also easier to understand (imo). See the newly attached
patchset for details. It contains three preparatory patches before my
original patch:

1. Starts using run_cost instead of total_cost to make the fix easier to
understand and also fixes a small bug
2. Adds tests to show the bad plans on master
3. Fixes the bad plans in the tests introduced by 2
4. My original patch (I only improved the comments a bit)

Attachment Content-Type Size
v2-0001-postgres_fdw-Track-run-cost-instead-of-total-cost.patch text/x-patch 9.2 KB
v2-0002-postgres_fdw-Add-tests-for-costing-local-work-bel.patch text/x-patch 6.4 KB
v2-0003-postgres_fdw-Separate-remote-transfer-and-local-c.patch text/x-patch 17.5 KB
v2-0004-postgres_fdw-Fix-costing-of-remote-sorts-without-.patch text/x-patch 34.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Manu 2026-10-04 20:54:51 Re: Planning time quadratic in the IN-list length for "c = X AND (a, b) IN (...)" with BitmapOr
Previous Message Manu 2026-10-04 20:04:18 Re: BUG #19686: Rolling back SET TABLESPACE