Skip to content

Installing and Configuring the Spock Extension

The Spock extension can be installed with pgEdge Postgres packages or built from source. pgEdge Postgres deployments provide a simplified upgrade path that allows you to take advantage of important new features quickly and easily.

Spock is open-source, with code available for review at GitHub.

Warning

Spock uses files stored in the spock schema to provide replication functionality. Do not delete, create, or modify files in the spock schema directly; use Spock functions and procedures to manage replication.

For detailed information about using Spock to create a two-node cluster, see Creating a Two-Node Cluster.

Installing Spock with pgEdge Enterprise Postgres

The latest Spock extension is automatically installed with any pgEdge Enterprise Postgres installation. A pgEdge deployment provides quick and easy access to the following features:

Building the Extension from Source

Spock is available as an open-source project that you can build from source code distributed at the GitHub project page. The Spock extension must be built on a version-specific build of Postgres that is patched to provide hooks so spock can access the Postgres engine.

To patch the Postgres source code, download the source code from the Postgres project, and move into the source directory. Then, clone the spock directory:

git clone https://github.com/pgEdge/spock.git

Then, apply the .diff files from the spock/patches/version directory that match your build version. To apply a patch, use the command:

patch -p1 < path_to_patch/patch_name

After applying version-specific patches, you can configure, make, and make install the Postgres server as described in the Postgres documentation. When the build completes, add the location of your pg_config file to your PATH variable:

export PATH=path_to_pg_config_file

Before invoking make and make install, install the jansson library to meet Spock prerequisites.

Then, move into the spock directory, and use a build process much like any other Postgres extension:

make
make install

Configuring Postgres for Spock Replication

After installing Postgres on each node that will host a spock instance, use initdb to initialize a Postgres cluster. After initializing the cluster, you can connect with psql, and query the server for file locations and cluster details:

[sdouglas@lima-pg3 pg18]$ sudo -u postgres psql -U postgres -p 5432
psql (18.1)
Type "help" for help.

postgres=# SHOW data_directory;
SHOW config_file;
SHOW hba_file;

     data_directory     
------------------------
 /var/lib/pgsql/18/data
(1 row)

              config_file               
----------------------------------------
 /var/lib/pgsql/18/data/postgresql.conf
(1 row)

              hba_file              
------------------------------------
 /var/lib/pgsql/18/data/pg_hba.conf
(1 row)

Then, use your choice of editor to update the postgresql.conf file, adding required Postgres parameters to the end of the file:

wal_level = logical
max_worker_processes = 10
max_replication_slots = 10
max_wal_senders = 10
shared_preload_libraries = 'spock'
track_commit_timestamp = on

Note on replication origin states:

  • PostgreSQL 15-17: The max_replication_slots parameter controls both the number of replication slots and the number of replication origin states; when sizing this parameter, account for both slots and origins (typically one origin per subscription).
  • PostgreSQL 18+: A new parameter max_active_replication_origins was introduced to separately control the number of replication origin states; the default value is 10, which may be insufficient for clusters with many subscriptions; set this to at least the number of subscriptions plus some headroom (similar sizing to max_replication_slots).

After modifying the Postgres parameters, use your OS-specific command to restart the Postgres server:

[sdouglas@lima-pg3 pg18]$ sudo systemctl restart postgresql-18
[sdouglas@lima-pg3 pg18]$ sudo systemctl status postgresql-18
● postgresql-18.service - PostgreSQL 18 database server
     Loaded: loaded (/usr/lib/systemd/system/postgresql-18.service; enabled; preset: disabled)
     Active: active (running) since Wed 2026-02-11 11:44:11 EST; 7s ago

Then, connect to the psql command line, and create the spock extension:

postgres=# CREATE EXTENSION spock;
CREATE EXTENSION
postgres=#

On each node that will host spock, modify the pg_hba.conf file and allow connections between n1 and n2. The following snippet is provided as an example only, and is not recommended for a production system as it will open your system for connection from any client:

host    all          all          <node_1_IP_address>/32    trust
host    all          all          <node_2_IP_address>/32    trust

host    replication  all          <node_1_IP_address>/32    trust
host    replication  all          <node_2_IP_address>/32    trust

Creating a Replication Scenario

After configuring the nodes, connect to the psql command line of the first provider node, and use the spock.node_create command to create the provider node:

SELECT spock.node_create(
    node_name := 'provider1',
    dsn := 'host=providerhost port=5432 dbname=db'
);

Then, add the tables in the public schema to the default replication set:

SELECT spock.repset_add_all_tables('default', ARRAY['public']);

Once the provider node is configured, you can add subscribers. First, create the subscriber node:

SELECT spock.node_create(
    node_name := 'subscriber1',
    dsn := 'host=thishost port=5432 dbname=db'
);

Then, create the subscription which will start synchronization and replication on the subscriber node:

SELECT spock.sub_create(
    subscription_name := 'subscription1',
    provider_dsn := 'host=providerhost port=5432 dbname=db'
);

SELECT spock.sub_wait_for_sync('subscription1');