これはPGroonga 2.X用のドキュメントです。古いPGroongaを使っているならPGroonga 1.xのドキュメントを見てください。

ロジカルレプリケーション

PGroongaは1.2.4からPostgreSQL組み込みのロジカルレプリケーションをサポートしています。ロジカルレプリケーションはPostgreSQL 10以降が必要です。

このドキュメントではPostgreSQL組み込みのロジカルレプリケーション機能をPGroonga用に設定する方法を説明します。多くの手順は通常のロジカルレプリケーションの設定手順です。いくつかPGroonga固有の手順があります。

ロジカルレプリケーションはレプリケーション元とレプリケーション先で同じスキーマを持つ必要がありません。

そのため、ここでは、レプリケーション先にのみインデックスを設定します。

概要

PostgreSQL組み込みのロジカルレプリケーション機能をPGroonga用に設定する手順は次の通りです。「[通常]」タグは通常のロジカルレプリケーションの手順であることを示しています。「[固有]」タグはPGroonga固有の手順であることを示しています。

  1. [通常] パブリッシャーとサブスクライバーにPostgreSQLをインストールします。

  2. [固有] サブスクライバーにPGroongaをインストールします。

  3. [通常] パブリッシャーとサブスクライバーのPostgreSQLデータベースを初期化します。

  4. [通常] パブリッシャーのpostgresql.confpg_hba.confにロジカルレプリケーションの設定を追加します。

  5. [通常] パブリッシャーとサブスクライバーにテーブルを作成します。

  6. [通常] パブリッシャーにパブリケーションを作成します。

  7. [通常] サブスクライバーにサブスクリプションを作成します。

  8. [固有] サブスクライバーにPGroongaのインデックスを作成します。

  9. [通常] パブリッシャーにデータを挿入します。

例で使う環境

このドキュメントでは次の環境を使います。

このドキュメントではCentOS 7用のコマンドラインを書いています。もし、他のプラットフォームを使っている場合は自分でコマンドラインを調整してください。

[通常] パブリッシャーとサブスクライバーにPostgreSQLをインストールする

これは通常の手順です。

パブリッシャーとサブスクライバーに PostgreSQL 16 をインストールします。

パブリッシャーとサブスクライバー

% sudo -H yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -qf --queryformat="%{VERSION}" /etc/redhat-release)-$(rpm -qf --queryformat="%{ARCH}" /etc/redhat-release)/pgdg-redhat-repo-latest.noarch.rpm
% sudo yum install postgresql16-server

PostgreSQL: Linux downloads (CentOS)も参照してください。

[固有] サブスクライバーにPGroongaをインストールする

これはPGroonga固有の手順です。

サブスクライバーにPGroongaをインストールします。

サブスクライバー:

% sudo -H yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -qf --queryformat="%{VERSION}" /etc/redhat-release)-$(rpm -qf --queryformat="%{ARCH}" /etc/redhat-release)/pgdg-redhat-repo-latest.noarch.rpm
% sudo -H yum install -y https://packages.groonga.org/centos/groonga-release-latest.noarch.rpm
% sudo -H yum install -y postgresql16-pgroonga

[通常] パブリッシャーとサブスクライバーのPostreSQLのデータベースを初期化する

これは通常の手順です。

パブリッシャーとサブスクライバーのPostgreSQLのデータベースを初期化します。

パブリッシャーとサブスクライバー

% sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
% sudo systemctl enable --now postgresql-16

[通常] パブリッシャーのpostgresql.conにロジカルレプリケーションの設定を追加する

これは通常の手順です。

パブリッシャーの postresql.conf にのみ、以下のロジカルレプリケーションの設定を追記します。

/var/lib/pgsql/16/data/postgresql.conf:

変更前:

#listen_address = 'localhost'

#wal_level = minimal
#max_wal_senders = 0
#max_replication_slots = 0

変更後:

listen_address = '*'

wal_level = logical
max_wal_senders = 2
max_replication_slots = 1

パブリッシャーの pg_hba.conf にのみ、以下のロジカルレプリケーションの設定を追加します。

/var/lib/pgsql/16/data/pg_hba.conf:

変更前:

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                trust
host    replication     all        127.0.0.1/32            trust
host    replication     all        ::1/128                 trust

変更後:

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                trust
host    replication     all        127.0.0.1/32            trust
host    replication     all        ::1/128                 trust

# IPv4 remote connections:
host    all             replicator       172.16.0.2/32         md5

この設定を反映するためにPostgreSQLを再起動します。

% sudo -H systemctl restart postgresql-16

パブリッシャーにのみレプリケーションのためのユーザーを作成します。

% /usr/pgsql-16/bin/createuser --pwprompt replicator -U postgres
Enter password for new role: (passw0rd)
Enter it again: (passw0rd)

[通常] パブリッシャーとサブスクライバーにテーブルを作成する

これは通常の手順です。

サブスクライバーに通常ユーザーを作成します。

サブスクライバー:

% /usr/pgsql-16/bin/createuser ${USER} -U postgres

パブリッシャーとサブスクライバーでデータベースを作成します。

パブリッシャーとサブスクライバー

% /usr/pgsql-16/bin/createdb --owner ${USER} blog -U postgres

作成されたデータベース内でテーブルを作成する。 blog データベースへ接続する。

% /usr/pgsql-16/bin/psql blog -U ${USER}

entriesテーブルを作成します。

CREATE TABLE entries (
  title text,
  body text
);

[固有] サブスクライバーにPGroongaのインデックスを作成する

これはPGroonga固有の手順です。

このデータベースにPGroongaをインストールします。スーパーユーザー権限が必要です。

サブスクライバー:

CREATE EXTENSION pgroonga;

サブスクライバーにPGroongaのインデックスを作成します。

サブスクライバー:

CREATE INDEX entries_full_text_search ON entries USING pgroonga (title, body);

パブリッシャーにのみ、パブリケーションを作成します。

CREATE PUBLICATION pub_srv1_blog FOR TABLE entries;

サブスクライバーにのみ、サブスクリプションを作成します。

CREATE SUBSCRIPTION sub_srv2_blog CONNECTION 'dbname=blog hostaddr=172.16.0.2 port=5432 user=replicator password=passw0rd' PUBLICATION pub_srv1_blog;

[通常] パブリッシャーにのみデータを挿入する

作成したentriesテーブルにデータを追加します。

INSERT INTO entries VALUES ('PGroonga', 'PGroonga is a PostgreSQL extension for fast full text search that supports all languages. It will help us.');
INSERT INTO entries VALUES ('Groonga', 'Groonga is a full text search engine used by PGroonga. We did not know about it.');
INSERT INTO entries VALUES ('PGroonga and replication', 'PGroonga 1.1.6 supports WAL based streaming replication. We should try it!');

レプリケーションの確認:

パブリッシャー:

SELECT * FROM entries;
 title    |                                  body
 ---------+---------------------------------------------------------------------------
 PGroonga | PGroonga is a PostgreSQL extension for fast full text search that supports all languages. It will help us.
 Groonga  | Groonga is a full text search engine used by PGroonga. We did not know about it.
 PGroonga and replication | PGroonga 1.1.6 supports WAL based streaming replication. We should try it!
 (3 rows)

サブスクライバー:

SELECT * FROM entries;
 title    |                                  body
 ---------+---------------------------------------------------------------------------
 PGroonga | PGroonga is a PostgreSQL extension for fast full text search that supports all languages. It will help us.
 Groonga  | Groonga is a full text search engine used by PGroonga. We did not know about it.
 PGroonga and replication | PGroonga 1.1.6 supports WAL based streaming replication. We should try it!
 (3 rows)

これで、サブスクライバーで作成したPGroongaのインデックスで、サブスクライバーで複製したデータを検索できます。

SET enable_seqscan TO off;
SELECT * FROM entries WHERE body &@ 'Groonga';
  title  |                                       body
---------+----------------------------------------------------------------------------------
 Groonga | Groonga is a full text search engine used by PGroonga. We did not know about it.
(1 row)