Skip to content

Commit 93f4146

Browse files
committedApr 14, 2021
Simplify tests of postgres_fdw terminating connections
The tests introduced in 32a9c0b for connections broken and re-established rely on pg_terminate_backend() for their logic. When these were introduced, this function simply sent a signal to a backend without waiting for the operation to complete, and the tests repeatedly looked at pg_stat_activity to check if the operation was completed or not. Since aaf0432, it is possible to define a timeout to make pg_terminate_backend() wait for a certain duration, so make use of it, with a timeout reasonably large enough (3min) to give enough room for the tests to pass even on slow machines. Some measurements show that the tests of postgres_fdw are much faster with this change. For example, on my laptop, they now take 4s instead of 6s. Author: Bharath Rupireddy Discussion: https://postgr.es/m/CALj2ACXGY_EfGrMTjKjHy2zi-u1u9rdeioU_fro0T6Jo8t56KQ@mail.gmail.com

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed
 

‎contrib/postgres_fdw/expected/postgres_fdw.out

+16-18
Original file line numberDiff line numberDiff line change
@@ -9195,19 +9195,6 @@ WARNING: there is no transaction in progress
91959195
-- ===================================================================
91969196
-- reestablish new connection
91979197
-- ===================================================================
9198-
-- Terminate the backend having the specified application_name and wait for
9199-
-- the termination to complete.
9200-
CREATE OR REPLACE PROCEDURE terminate_backend_and_wait(appname text) AS $$
9201-
BEGIN
9202-
PERFORM pg_terminate_backend(pid) FROM pg_stat_activity
9203-
WHERE application_name = appname;
9204-
LOOP
9205-
PERFORM * FROM pg_stat_activity WHERE application_name = appname;
9206-
EXIT WHEN NOT FOUND;
9207-
PERFORM pg_sleep(1), pg_stat_clear_snapshot();
9208-
END LOOP;
9209-
END;
9210-
$$ LANGUAGE plpgsql;
92119198
-- Change application_name of remote connection to special one
92129199
-- so that we can easily terminate the connection later.
92139200
ALTER SERVER loopback OPTIONS (application_name 'fdw_retry_check');
@@ -9217,8 +9204,14 @@ SELECT 1 FROM ft1 LIMIT 1;
92179204
1
92189205
(1 row)
92199206

9220-
-- Terminate the remote connection.
9221-
CALL terminate_backend_and_wait('fdw_retry_check');
9207+
-- Terminate the remote connection and wait for the termination to complete.
9208+
SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
9209+
WHERE application_name = 'fdw_retry_check';
9210+
pg_terminate_backend
9211+
----------------------
9212+
t
9213+
(1 row)
9214+
92229215
-- This query should detect the broken connection when starting new remote
92239216
-- transaction, reestablish new connection, and then succeed.
92249217
BEGIN;
@@ -9231,15 +9224,20 @@ SELECT 1 FROM ft1 LIMIT 1;
92319224
-- If the query detects the broken connection when starting new remote
92329225
-- subtransaction, it doesn't reestablish new connection and should fail.
92339226
-- The text of the error might vary across platforms, so don't show it.
9234-
CALL terminate_backend_and_wait('fdw_retry_check');
9227+
-- Terminate the remote connection and wait for the termination to complete.
9228+
SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
9229+
WHERE application_name = 'fdw_retry_check';
9230+
pg_terminate_backend
9231+
----------------------
9232+
t
9233+
(1 row)
9234+
92359235
SAVEPOINT s;
92369236
\set VERBOSITY sqlstate
92379237
SELECT 1 FROM ft1 LIMIT 1; -- should fail
92389238
ERROR: 08006
92399239
\set VERBOSITY default
92409240
COMMIT;
9241-
-- Clean up
9242-
DROP PROCEDURE terminate_backend_and_wait(text);
92439241
-- =============================================================================
92449242
-- test connection invalidation cases and postgres_fdw_get_connections function
92459243
-- =============================================================================

‎contrib/postgres_fdw/sql/postgres_fdw.sql

+6-20
Original file line numberDiff line numberDiff line change
@@ -2786,27 +2786,14 @@ ROLLBACK;
27862786
-- reestablish new connection
27872787
-- ===================================================================
27882788

2789-
-- Terminate the backend having the specified application_name and wait for
2790-
-- the termination to complete.
2791-
CREATE OR REPLACE PROCEDURE terminate_backend_and_wait(appname text) AS $$
2792-
BEGIN
2793-
PERFORM pg_terminate_backend(pid) FROM pg_stat_activity
2794-
WHERE application_name = appname;
2795-
LOOP
2796-
PERFORM * FROM pg_stat_activity WHERE application_name = appname;
2797-
EXIT WHEN NOT FOUND;
2798-
PERFORM pg_sleep(1), pg_stat_clear_snapshot();
2799-
END LOOP;
2800-
END;
2801-
$$ LANGUAGE plpgsql;
2802-
28032789
-- Change application_name of remote connection to special one
28042790
-- so that we can easily terminate the connection later.
28052791
ALTER SERVER loopback OPTIONS (application_name 'fdw_retry_check');
28062792
SELECT 1 FROM ft1 LIMIT 1;
28072793

2808-
-- Terminate the remote connection.
2809-
CALL terminate_backend_and_wait('fdw_retry_check');
2794+
-- Terminate the remote connection and wait for the termination to complete.
2795+
SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
2796+
WHERE application_name = 'fdw_retry_check';
28102797

28112798
-- This query should detect the broken connection when starting new remote
28122799
-- transaction, reestablish new connection, and then succeed.
@@ -2816,16 +2803,15 @@ SELECT 1 FROM ft1 LIMIT 1;
28162803
-- If the query detects the broken connection when starting new remote
28172804
-- subtransaction, it doesn't reestablish new connection and should fail.
28182805
-- The text of the error might vary across platforms, so don't show it.
2819-
CALL terminate_backend_and_wait('fdw_retry_check');
2806+
-- Terminate the remote connection and wait for the termination to complete.
2807+
SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
2808+
WHERE application_name = 'fdw_retry_check';
28202809
SAVEPOINT s;
28212810
\set VERBOSITY sqlstate
28222811
SELECT 1 FROM ft1 LIMIT 1; -- should fail
28232812
\set VERBOSITY default
28242813
COMMIT;
28252814

2826-
-- Clean up
2827-
DROP PROCEDURE terminate_backend_and_wait(text);
2828-
28292815
-- =============================================================================
28302816
-- test connection invalidation cases and postgres_fdw_get_connections function
28312817
-- =============================================================================

0 commit comments

Comments
 (0)