postgres_fdw: don't push down non-relabeling ArrayCoerceExpr

Commit 62c3b4cd9ddc taught postgres_fdw to push down ArrayCoerceExpr, but
foreign_expr_walker() only recursed into the input array expression and
never examined elemexpr, the per-element conversion that gives the
coercion its semantics.  deparseArrayCoerceExpr() then shipped a bare
"arg::resulttype" cast, or nothing at all for an implicit-format
coercion, leaving the remote server to re-resolve the element conversion
against its own catalogs and session state.

This produced wrong results or remote errors whenever the element
conversion was not a plain relabeling, and it was inconsistent with how
postgres_fdw treats the equivalent scalar coercions.  An ArrayCoerceExpr
was shipped even when its elemexpr was a cast function (whose
shippability was never checked), a CoerceViaIO (e.g. float8out or
byteaout, which depend on extra_float_digits / bytea_output that
postgres_fdw sets differently on the remote session), or a
CoerceToDomain (which pushes domain enforcement to the remote catalog).
By contrast, a scalar CoerceViaIO is never shipped, and a scalar cast
function is shipped only when it is shippable.

Restrict pushdown to element coercions that are a plain relabeling, that
is, elemexpr is a RelabelType or a bare CaseTestExpr.  Any other element
coercion is now evaluated locally.  This keeps the common
binary-coercible case pushed down, including "col = ANY($1)" with a
varchar[]-to-text[] relabeling, which is the case 62c3b4cd9ddc set out to
optimize.

Pushing down shippable element cast functions, to reach parity with the
scalar case, is left out here for simplicity.

Reported-by: Noah Misch <[email protected]>
Discussion: https://postgr.es/m/20260711024234.43.noahmisch%40microsoft.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/e5354459383536a9644c4b0c8c26df0fcacae5f3

Modified Files
--------------
contrib/postgres_fdw/deparse.c                 | 20 +++++++
contrib/postgres_fdw/expected/postgres_fdw.out | 75 ++++++++++++++++++++++++++
contrib/postgres_fdw/sql/postgres_fdw.sql      | 35 ++++++++++++
3 files changed, 130 insertions(+)

Reply via email to