aboutsummaryrefslogtreecommitdiff
path: root/contrib/postgres_fdw/postgres_fdw.c
diff options
context:
space:
mode:
authorEtsuro Fujita <efujita@postgresql.org>2024-06-07 17:45:00 +0900
committerEtsuro Fujita <efujita@postgresql.org>2024-06-07 17:45:00 +0900
commit8cfbac1492bddc6a81c1c3794355e1d7ac82e957 (patch)
tree94aca723200d9dd47b3822d636857ba151e05fe4 /contrib/postgres_fdw/postgres_fdw.c
parent3482bab5e30a0cc7fba4c904ba21b4957b2876bd (diff)
downloadpostgresql-8cfbac1492bddc6a81c1c3794355e1d7ac82e957.tar.gz
postgresql-8cfbac1492bddc6a81c1c3794355e1d7ac82e957.zip
postgres_fdw: Refuse to send FETCH FIRST WITH TIES to remote servers.
Previously, when considering LIMIT pushdown, postgres_fdw failed to check whether the query has this clause, which led to pushing false LIMIT clauses, causing incorrect results. This clause has been supported since v13, so we need to do a remote-version check before deciding that it will be safe to push such a clause, but we do not currently have a way to do the check (without accessing the remote server); disable pushing such a clause for now. Oversight in commit 357889eb1. Back-patch to v13, where that commit added the support. Per bug #18467 from Onder Kalaci. Patch by Japin Li, per a suggestion from Tom Lane, with some changes to the comments by me. Review by Onder Kalaci, Alvaro Herrera, and me. Discussion: https://postgr.es/m/18467-7bb89084ff03a08d%40postgresql.org
Diffstat (limited to 'contrib/postgres_fdw/postgres_fdw.c')
-rw-r--r--contrib/postgres_fdw/postgres_fdw.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 4053cd641c5..0bb9a5ae8f6 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -7141,6 +7141,20 @@ add_foreign_final_paths(PlannerInfo *root, RelOptInfo *input_rel,
return;
/*
+ * If the query has FETCH FIRST .. WITH TIES, 1) it must have ORDER BY as
+ * well, which is used to determine which additional rows tie for the last
+ * place in the result set, and 2) ORDER BY must already have been
+ * determined to be safe to push down before we get here. So in that case
+ * the FETCH clause is safe to push down with ORDER BY if the remote
+ * server is v13 or later, but if not, the remote query will fail entirely
+ * for lack of support for it. Since we do not currently have a way to do
+ * a remote-version check (without accessing the remote server), disable
+ * pushing the FETCH clause for now.
+ */
+ if (parse->limitOption == LIMIT_OPTION_WITH_TIES)
+ return;
+
+ /*
* Also, the LIMIT/OFFSET cannot be pushed down, if their expressions are
* not safe to remote.
*/