Make postgres_fdw's query_cancel test less flaky.
authorTom Lane <[email protected]>
Fri, 30 Aug 2024 20:47:39 +0000 (16:47 -0400)
committerTom Lane <[email protected]>
Fri, 30 Aug 2024 20:47:39 +0000 (16:47 -0400)
This test occasionally shows

+WARNING:  could not get result of cancel request due to timeout

which appears to be because the cancel request is sometimes unluckily
sent to the remote session between queries, and then it's ignored.

This patch tries to make that less probable in three ways:

1. Use a test query that does not involve remote estimates, so that
no EXPLAINs are sent.
2. Make sure that the remote session is ready-to-go (transaction
started, SET commands sent) before we start the timer.
3. Increase the statement_timeout to 100ms, to give the local
session enough time to plan and issue the query.

We might have to go higher than 100ms to make this adequately
stable in the buildfarm, but let's see how it goes.

Back-patch to v17 where this test was introduced.

Jelte Fennema-Nio and Tom Lane

Discussion: https://postgr.es/m/578934.1725045685@sss.pgh.pa.us

contrib/postgres_fdw/expected/query_cancel.out
contrib/postgres_fdw/sql/query_cancel.sql

index afef67aa8deee543298c21acc27d81ff7edbb091..b091656cf3515dcaf0aff9e29dee1b46acee3d90 100644 (file)
@@ -2,19 +2,31 @@ SELECT version() ~ 'cygwin' AS skip_test \gset
 \if :skip_test
 \quit
 \endif
--- Make sure this big CROSS JOIN query is pushed down
-EXPLAIN (VERBOSE, COSTS OFF) SELECT count(*) FROM ft1 CROSS JOIN ft2 CROSS JOIN ft4 CROSS JOIN ft5;
+-- Let's test canceling a remote query.  Use a table that does not have
+-- remote_estimate enabled, else there will be multiple queries to the
+-- remote and we might unluckily send the cancel in between two of them.
+-- First let's confirm that the query is actually pushed down.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(*) FROM ft1 a CROSS JOIN ft1 b CROSS JOIN ft1 c CROSS JOIN ft1 d;
                                                                              QUERY PLAN                                                                              
 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Foreign Scan
    Output: (count(*))
-   Relations: Aggregate on ((((public.ft1) INNER JOIN (public.ft2)) INNER JOIN (public.ft4)) INNER JOIN (public.ft5))
-   Remote SQL: SELECT count(*) FROM ((("S 1"."T 1" r1 INNER JOIN "S 1"."T 1" r2 ON (TRUE)) INNER JOIN "S 1"."T 3" r4 ON (TRUE)) INNER JOIN "S 1"."T 4" r6 ON (TRUE))
+   Relations: Aggregate on ((((public.ft1 a) INNER JOIN (public.ft1 b)) INNER JOIN (public.ft1 c)) INNER JOIN (public.ft1 d))
+   Remote SQL: SELECT count(*) FROM ((("S 1"."T 1" r1 INNER JOIN "S 1"."T 1" r2 ON (TRUE)) INNER JOIN "S 1"."T 1" r4 ON (TRUE)) INNER JOIN "S 1"."T 1" r6 ON (TRUE))
 (4 rows)
 
--- Make sure query cancellation works
 BEGIN;
-SET LOCAL statement_timeout = '10ms';
-select count(*) from ft1 CROSS JOIN ft2 CROSS JOIN ft4 CROSS JOIN ft5; -- this takes very long
+-- Make sure that connection is open and set up.
+SELECT count(*) FROM ft1 a;
+ count 
+-------
+   822
+(1 row)
+
+-- Timeout needs to be long enough to be sure that we've sent the slow query.
+SET LOCAL statement_timeout = '100ms';
+-- This would take very long if not canceled:
+SELECT count(*) FROM ft1 a CROSS JOIN ft1 b CROSS JOIN ft1 c CROSS JOIN ft1 d;
 ERROR:  canceling statement due to statement timeout
 COMMIT;
index 8f11f3f9a6a2cbd5f8732db6366b2daad015db0d..270e129d77eff3cc0a60883d6120fed81ad7eaa4 100644 (file)
@@ -3,10 +3,18 @@ SELECT version() ~ 'cygwin' AS skip_test \gset
 \quit
 \endif
 
--- Make sure this big CROSS JOIN query is pushed down
-EXPLAIN (VERBOSE, COSTS OFF) SELECT count(*) FROM ft1 CROSS JOIN ft2 CROSS JOIN ft4 CROSS JOIN ft5;
--- Make sure query cancellation works
+-- Let's test canceling a remote query.  Use a table that does not have
+-- remote_estimate enabled, else there will be multiple queries to the
+-- remote and we might unluckily send the cancel in between two of them.
+-- First let's confirm that the query is actually pushed down.
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT count(*) FROM ft1 a CROSS JOIN ft1 b CROSS JOIN ft1 c CROSS JOIN ft1 d;
+
 BEGIN;
-SET LOCAL statement_timeout = '10ms';
-select count(*) from ft1 CROSS JOIN ft2 CROSS JOIN ft4 CROSS JOIN ft5; -- this takes very long
+-- Make sure that connection is open and set up.
+SELECT count(*) FROM ft1 a;
+-- Timeout needs to be long enough to be sure that we've sent the slow query.
+SET LOCAL statement_timeout = '100ms';
+-- This would take very long if not canceled:
+SELECT count(*) FROM ft1 a CROSS JOIN ft1 b CROSS JOIN ft1 c CROSS JOIN ft1 d;
 COMMIT;