Skip to content

Commit

Permalink
Merge pull request #7 from jfinzel/auto_restart
Browse files Browse the repository at this point in the history
Auto restart

* github.com:enova/pglogical_ticker:
  Update debian changelog
  Support auto-launching ticker and add application_name
  Bump version
  • Loading branch information
Jerry Sievers committed Sep 17, 2019
2 parents 2570779 + 4fdbb4d commit 65a04bf
Show file tree
Hide file tree
Showing 13 changed files with 932 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ EXTENSION = pglogical_ticker
DATA = pglogical_ticker--1.0.sql pglogical_ticker--1.0--1.1.sql \
pglogical_ticker--1.1.sql pglogical_ticker--1.1--1.2.sql \
pglogical_ticker--1.2.sql pglogical_ticker--1.2--1.3.sql \
pglogical_ticker--1.3.sql
pglogical_ticker--1.3.sql pglogical_ticker--1.3--1.4.sql \
pglogical_ticker--1.4.sql
PGFILEDESC = "pglogical_ticker - Have an accurate view of pglogical replication delay"

PG_CONFIG = pg_config
Expand Down
49 changes: 38 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ make install
make installcheck # run regression suite
```

Although not strictly required, to get access to the configuration settings of
`pglogical_ticker` and to auto-launch the ticker on server restart or a soft crash,
add `pglogical_ticker` to `shared_preload_libraries` in your postgresql.conf file:
```
shared_preload_libraries = 'pglogical,pglogical_ticker' #... and whatever others you already may have
```

Once installed, simply run this on the provider and all subscribers:
```sql
CREATE EXTENSION pglogical_ticker;
```

### Deploy tables and launch ticker
### Deploy ticker tables
Deploy the ticker tables. Run this command on the provider only, which
will use `pglogical.replicate_ddl_command` to send to subscriber.
```sql
Expand Down Expand Up @@ -58,22 +65,39 @@ You can manually try the tick() function if you so choose:
SELECT pglogical_ticker.tick();
```

TO LAUNCH THE BACKGROUND WORKER:
### Configuration
This is only supported if you have added `pglogical_ticker` in `shared_preload_libraries`
as noted above.

- `pglogical_ticker.database`: The database in which to launch the ticker (we currently
have no need to support multiple databases, but may add that feature at a later time).
The ticker will only auto-launch on restart if this setting is configured.
- `pglogical_ticker.naptime`: How frequently the ticker ticks - default 10 seconds
- `pglogical_ticker.restart_time`: How many seconds before the ticker auto-restarts, default 10. This
is also how long it will take to re-launch after a soft crash, for instance. Set this to
-1 to disable. **Be aware** that you cannot use this setting to prevent an already-launched
ticker from restarting. Only a server restart will take this new value into account for
the ticker backend and prevent it from ever restarting, if that is your desired behavior.

### Launching the ticker
As of version 1.4, the ticker will automatically launch upon server load
if you have `pglogical_ticker` in `shared_preload_libraries`.

Otherwise, this function will launch the ticker, only if there is not already
one running:
```sql
SELECT pglogical_ticker.launch();

/**
It is better to use this function always, which automatically checks if
It is better to use the following function instead, which automatically checks if
the system should have a ticker based on tables existing in replication.
(this assumes you don't want a replication stream open with no tables).
This function is very useful if you want to just blindly run the function
to launch the ticker if it needs to be launched, i.e. after a restart.
**/
SELECT pglogical_ticker.launch_if_repset_tables();
```

This will run the function `pglogical_ticker.tick()` every 10 seconds.
The background worker launched either by this function or upon server load will
run the function `pglogical_ticker.tick()` every n seconds according to `pglogical_ticker.naptime`.

Be sure to use caution in monitoring deployment and running of these background
worker processes.
Expand All @@ -95,10 +119,13 @@ SELECT * FROM pglogical_ticker.all_subscription_tickers();
Help is always wanted to review and improve the BackgroundWorker module.
It is directly based on `worker_spi` from Postgres' test suite.

It could be improved to take arguments for naptime and also a different
username. I would also like it to be written so as to prevent any
possibility of launching more than one worker, which currently is only
done through the exposed function in the docs `launch()`.
It could be improved to use a different username. I would also like it to
be written so as to prevent any possibility of launching more than one worker,
which currently is only done through the exposed function in the docs `launch()`.

As of 1.4, I'm also interested in allowing a clean shutdown with exit code 0,
as well as (if safe enough) searching databases for the ticker as opposed to having
to configure `pglogical_ticker.database`.

The SQL files are maintained separately to make version control much
easier to see. Make changes in these folders and then run
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pglogical-ticker (1.4.0-1) unstable; urgency=medium

* Support ticker auto-restart

-- Jeremy Finzel <[email protected]> Thu, 12 Sep 2019 10:21:00 -0500

pglogical-ticker (1.3.1-1) unstable; urgency=medium

* Fix race conditions in tests
Expand Down
2 changes: 1 addition & 1 deletion debian/control.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Package: postgresql-PGVERSION-pglogical-ticker
Architecture: any
Depends: postgresql-PGVERSION, postgresql-PGVERSION-pglogical, ${shlibs:Depends}, ${misc:Depends}
Description: Have time-based replication delay for pglogical
A pglogical extension to obtain time-based replication delay for PostgreSQL PGVERSION.
A pglogical extension to get time-based replication delay for PostgreSQL PGVERSION.
2 changes: 1 addition & 1 deletion expected/01_create_ext.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Allow running regression suite with upgrade paths
\set v `echo ${FROMVERSION:-1.3}`
\set v `echo ${FROMVERSION:-1.4}`
SET client_min_messages = warning;
CREATE EXTENSION pglogical;
CREATE EXTENSION pglogical_ticker VERSION :'v';
4 changes: 2 additions & 2 deletions functions/pglogical_ticker.launch.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ AND NOT EXISTS
(SELECT 1
FROM pg_stat_activity psa
WHERE NOT pid = pg_backend_pid()
AND query = 'SELECT pglogical_ticker.tick();')
AND application_name LIKE 'pglogical_ticker%')
AND NOT pg_is_in_recovery();
$function$
;
;
25 changes: 25 additions & 0 deletions pglogical_ticker--1.3--1.4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* pglogical_ticker--1.3--1.4.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pglogical_ticker" to load this file. \quit

CREATE OR REPLACE FUNCTION pglogical_ticker.launch()
RETURNS integer
LANGUAGE sql
STRICT
AS $function$
SELECT pglogical_ticker._launch(oid)
FROM pg_database
WHERE datname = current_database()
--This should be improved in the future but should do
--the job for now.
AND NOT EXISTS
(SELECT 1
FROM pg_stat_activity psa
WHERE NOT pid = pg_backend_pid()
AND application_name LIKE 'pglogical_ticker%')
AND NOT pg_is_in_recovery();
$function$
;


Loading

0 comments on commit 65a04bf

Please sign in to comment.