| From: | Nitin Motiani <nitinmotiani(at)google(dot)com> |
|---|---|
| To: | Kirill Reshke <reshkekirill(at)gmail(dot)com> |
| Cc: | Matheus Alcantara <matheusssilv97(at)gmail(dot)com>, tharakan(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org, etsuro(dot)fujita(at)gmail(dot)com, Richard Guo <guofenglinux(at)gmail(dot)com> |
| Subject: | Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual |
| Date: | 2026-11-27 12:09:52 |
| Message-ID: | CAH5HC960eKW4mTFrF6zwGc1+c3C_e7t6LJrsuN-hoXiTunAx_A@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Fri, Nov 27, 2026 at 2:02 PM Kirill Reshke <reshkekirill(at)gmail(dot)com> wrote:
>
> Looks like there is one more case in add_foreign_final_paths() with no
> needs no row locking. A generic plan for a LEFT JOIN with LIMIT 1 can
> loose a bool parameter from the WHERE clause: example:
>
> SET plan_cache_mode = force_generic_plan;
> PREPARE q(boolean) AS
> SELECT 42 FROM ft1 LEFT JOIN ft2 ON ft1.a = ft2.b
> WHERE $1 LIMIT 1;
> EXECUTE q(false);
>
Thanks for catching this, Kiril. I was able to reproduce it too. I had
originally tested limit case with inner joins which happened to be
fine because their pushed-down were sent as a part of join class to
the remote, but for outer joins postgresGetForeignPlan() omits
pseudoconstant remote_conds.
This returned one row containing 42. I expected zero rows. A NULL
> argument also returned one row. The same query over the local tables
> returned zero rows for both arguments and one row for true.
>
> Looks like this diff will do
>
> --- a/contrib/postgres_fdw/postgres_fdw.c
> +++ b/contrib/postgres_fdw/postgres_fdw.c
> @@ -1456,6 +1456,15 @@ postgresGetForeignPlan(PlannerInfo *root,
> remote_exprs = extract_actual_clauses(fpinfo->remote_conds, false);
> local_exprs = extract_actual_clauses(fpinfo->local_conds, false);
>
> + /*
> + * A pushed-down LIMIT on a join has no local gating Result. Keep
> + * shippable pseudoconstant WHERE clauses below the remote LIMIT.
> + */
> + if (has_limit && IS_JOIN_REL(foreignrel))
> + remote_exprs = list_concat(remote_exprs,
> + extract_actual_clauses(fpinfo->remote_conds,
> + true));
> +
> /*
> * We leave fdw_recheck_quals empty in this case, since we never need
> * to apply EPQ recheck clauses. In the case of a joinrel, EPQ
> diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql
> b/contrib/postgres_fdw/sql/postgres_fdw.sql
> index b71771a..76d260e 100644
>
This should work but I opted to pass the join's restriction list to
the final path, as v2 does for the FOR UPDATE case. This way we have
the same local gating for both the cases. I'm attaching v3 with this
change. Please take a look.
Regards,
Nitin Motiani
Google
| Attachment | Content-Type | Size |
|---|---|---|
| v3-0001-postgres_fdw-Fix-pseudoconstant-quals-being-ignor.patch | application/x-patch | 15.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Tender Wang | 2026-11-27 09:51:37 | Re: BUG #19942: SIGSEGV crash (and wrong results) in MERGE ... RETURNING WITH (OLD/NEW) with VIRTUAL generated colum |