Skip to content

Commit 69f75bf

Browse files
author
Etsuro Fujita
committed
postgres_fdw: Fix assertion in estimate_path_cost_size().
Commit 08d2d58 added this assertion assuming that the retrieved_rows estimate for a foreign relation is set to at least one row in estimate_path_cost_size(), but the assumption isn't correct because if the relation is a foreign table with tuples=0, the estimate would be set to 0 in there when using local stats, and if the query's WHERE clause has a NULL condition, the estimate could be set to 0 in there when using remote estimates. (Note: even in the latter case the assertion could be reachable when costing foreign final paths.) Repair. Per bug #17713 from Robins Tharakan. This patch was already applied to v13 or later; apply it to v12 as well where the aforementioned commit was added. Thanks to Richard Guo for investigation. Discussion: http://postgr.es/m/17713-92cce66de7e81c04%40postgresql.org Discussion: http://postgr.es/m/CAEP4nAza%2B0fTCLkgkKYux3JDo3tUBTQORehP%2BaCxSNURpSFpHw%40mail.gmail.com
1 parent 1cca4a7 commit 69f75bf

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,24 @@ SELECT t1."C 1", t2.c1, t3.c1 FROM "S 1"."T 1" t1 full join ft1 t2 full join ft2
590590

591591
RESET enable_hashjoin;
592592
RESET enable_nestloop;
593+
-- Test executing assertion in estimate_path_cost_size() that makes sure that
594+
-- retrieved_rows for foreign rel re-used to cost pre-sorted foreign paths is
595+
-- a sensible value even when the rel has tuples=0
596+
CREATE TABLE loct_empty (c1 int NOT NULL, c2 text);
597+
CREATE FOREIGN TABLE ft_empty (c1 int NOT NULL, c2 text)
598+
SERVER loopback OPTIONS (table_name 'loct_empty');
599+
INSERT INTO loct_empty
600+
SELECT id, 'AAA' || to_char(id, 'FM000') FROM generate_series(1, 100) id;
601+
DELETE FROM loct_empty;
602+
ANALYZE ft_empty;
603+
EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft_empty ORDER BY c1;
604+
QUERY PLAN
605+
-------------------------------------------------------------------------------
606+
Foreign Scan on public.ft_empty
607+
Output: c1, c2
608+
Remote SQL: SELECT c1, c2 FROM public.loct_empty ORDER BY c1 ASC NULLS LAST
609+
(3 rows)
610+
593611
-- ===================================================================
594612
-- WHERE with remotely-executable conditions
595613
-- ===================================================================

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2749,7 +2749,7 @@ estimate_path_cost_size(PlannerInfo *root,
27492749
*/
27502750
if (fpinfo->rel_startup_cost >= 0 && fpinfo->rel_total_cost >= 0)
27512751
{
2752-
Assert(fpinfo->retrieved_rows >= 1);
2752+
Assert(fpinfo->retrieved_rows >= 0);
27532753

27542754
rows = fpinfo->rows;
27552755
retrieved_rows = fpinfo->retrieved_rows;

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,18 @@ SELECT t1."C 1", t2.c1, t3.c1 FROM "S 1"."T 1" t1 full join ft1 t2 full join ft2
285285
RESET enable_hashjoin;
286286
RESET enable_nestloop;
287287

288+
-- Test executing assertion in estimate_path_cost_size() that makes sure that
289+
-- retrieved_rows for foreign rel re-used to cost pre-sorted foreign paths is
290+
-- a sensible value even when the rel has tuples=0
291+
CREATE TABLE loct_empty (c1 int NOT NULL, c2 text);
292+
CREATE FOREIGN TABLE ft_empty (c1 int NOT NULL, c2 text)
293+
SERVER loopback OPTIONS (table_name 'loct_empty');
294+
INSERT INTO loct_empty
295+
SELECT id, 'AAA' || to_char(id, 'FM000') FROM generate_series(1, 100) id;
296+
DELETE FROM loct_empty;
297+
ANALYZE ft_empty;
298+
EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft_empty ORDER BY c1;
299+
288300
-- ===================================================================
289301
-- WHERE with remotely-executable conditions
290302
-- ===================================================================

0 commit comments

Comments
 (0)