Fix flaky bgw_db_scheduler test

Change handling of background workers on rename database, the
previous implementation would restart background workers on
RENAME DATABASE unfortunately this leads to a race condition
where its possible that the background workers are restarted
while the RENAME DATABASE has not yet finished leading to the
command not completing successfully. This patch changes the
restart to a stop and generating a warning that background
jobs need to be manually restarted.
This patch also fixes 2 segfaults in the background worker
test code.
This commit is contained in:
Sven Klemm 2019-10-13 17:19:30 +02:00 committed by Sven Klemm
parent 4ad2a2f46a
commit 77ac7ccd89
5 changed files with 20 additions and 18 deletions

View File

@ -436,7 +436,12 @@ post_analyze_hook(ParseState *pstate, Query *query)
Oid db_oid = get_database_oid(stmt->subname, stmt->missing_ok);
if (OidIsValid(db_oid))
ts_bgw_message_send_and_wait(RESTART, db_oid);
{
ts_bgw_message_send_and_wait(STOP, db_oid);
ereport(WARNING,
(errmsg("You need to manually restart any running "
"background workers after this command.")));
}
}
break;
default:

View File

@ -607,12 +607,7 @@ SELECT pg_reload_conf();
t
(1 row)
SELECT pg_sleep(0.001);
pg_sleep
----------
(1 row)
\c :TEST_DBNAME :ROLE_SUPERUSER
SHOW timescaledb.shutdown_bgw_scheduler;
timescaledb.shutdown_bgw_scheduler
------------------------------------
@ -638,12 +633,7 @@ SELECT pg_reload_conf();
t
(1 row)
SELECT pg_sleep(0.001);
pg_sleep
----------
(1 row)
\c :TEST_DBNAME :ROLE_SUPERUSER
SHOW timescaledb.shutdown_bgw_scheduler;
timescaledb.shutdown_bgw_scheduler
------------------------------------

View File

@ -529,6 +529,7 @@ SELECT wait_for_bgw_scheduler('db_rename_test');
(1 row)
ALTER DATABASE db_rename_test RENAME TO db_rename_test2;
WARNING: You need to manually restart any running background workers after this command.
DROP DATABASE db_rename_test2;
-- test create database with timescaledb database as template
SELECT wait_for_bgw_scheduler(:'TEST_DBNAME');

View File

@ -270,7 +270,7 @@ SHOW timescaledb.shutdown_bgw_scheduler;
ALTER SYSTEM SET timescaledb.shutdown_bgw_scheduler TO 'on';
SELECT pg_reload_conf();
SELECT pg_sleep(0.001);
\c :TEST_DBNAME :ROLE_SUPERUSER
SHOW timescaledb.shutdown_bgw_scheduler;
SELECT ts_bgw_db_scheduler_test_wait_for_scheduler_finish();
@ -279,7 +279,7 @@ SELECT * FROM sorted_bgw_log;
ALTER SYSTEM RESET timescaledb.shutdown_bgw_scheduler;
SELECT pg_reload_conf();
SELECT pg_sleep(0.001);
\c :TEST_DBNAME :ROLE_SUPERUSER
SHOW timescaledb.shutdown_bgw_scheduler;
SELECT ts_bgw_params_mock_wait_returns_immediately(:WAIT_ON_JOB);

View File

@ -176,8 +176,11 @@ ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(PG_FUNCTION_ARGS)
worker_handle = start_test_scheduler(params);
Assert(BGWH_STARTED == WaitForBackgroundWorkerStartup(worker_handle, &pid));
Assert(BGWH_STOPPED == WaitForBackgroundWorkerShutdown(worker_handle));
if (worker_handle != NULL)
{
Assert(BGWH_STARTED == WaitForBackgroundWorkerStartup(worker_handle, &pid));
Assert(BGWH_STOPPED == WaitForBackgroundWorkerShutdown(worker_handle));
}
PG_RETURN_VOID();
}
@ -203,7 +206,10 @@ ts_bgw_db_scheduler_test_run(PG_FUNCTION_ARGS)
extern Datum
ts_bgw_db_scheduler_test_wait_for_scheduler_finish(PG_FUNCTION_ARGS)
{
Assert(BGWH_STOPPED == WaitForBackgroundWorkerShutdown(current_handle));
if (current_handle != NULL)
{
Assert(BGWH_STOPPED == WaitForBackgroundWorkerShutdown(current_handle));
}
PG_RETURN_VOID();
}