PG_CREATESUBSCRIBER(1) | PostgreSQL 18.0 Documentation | PG_CREATESUBSCRIBER(1) |
NAME¶
pg_createsubscriber - convert a physical replica into a new logical replica
SYNOPSIS¶
pg_createsubscriber [option...] {-d | --database}dbname {-D | --pgdata}datadir {-P | --publisher-server}connstr
DESCRIPTION¶
pg_createsubscriber creates a new logical replica from a physical standby server. All tables in the specified database are included in the logical replication setup. A pair of publication and subscription objects are created for each database. It must be run at the target server.
After a successful run, the state of the target server is analogous to a fresh logical replication setup. The main difference between the logical replication setup and pg_createsubscriber is how the data synchronization is done. pg_createsubscriber does not copy the initial table data. It does only the synchronization phase, which ensures each table is brought up to a synchronized state.
pg_createsubscriber targets large database systems because in logical replication setup, most of the time is spent doing the initial data copy. Furthermore, a side effect of this long time spent synchronizing data is usually a large amount of changes to be applied (that were produced during the initial data copy), which increases even more the time when the logical replica will be available. For smaller databases, it is recommended to set up logical replication with initial data synchronization. For details, see the CREATE SUBSCRIPTION copy_data option.
OPTIONS¶
pg_createsubscriber accepts the following command-line arguments:
-a
--all
-d dbname
--database=dbname
-D directory
--pgdata=directory
-n
--dry-run
-p port
--subscriber-port=port
-P connstr
--publisher-server=connstr
-s dir
--socketdir=dir
-t seconds
--recovery-timeout=seconds
-T
--enable-two-phase
-U username
--subscriber-username=username
-v
--verbose
--clean=objtype
The objects selected to be dropped are individually logged, including during a --dry-run. There is no opportunity to affect or stop the dropping of the selected objects, so consider taking a backup of them using pg_dump.
--config-file=filename
--publication=name
--replication-slot=name
--subscription=name
-V
--version
-?
--help
NOTES¶
Prerequisites¶
There are some prerequisites for pg_createsubscriber to convert the target server into a logical replica. If these are not met, an error will be reported. The source and target servers must have the same major version as the pg_createsubscriber. The given target data directory must have the same system identifier as the source data directory. The given database user for the target data directory must have privileges for creating subscriptions and using pg_replication_origin_advance().
The target server must be used as a physical standby. The target server must have max_active_replication_origins and max_logical_replication_workers configured to a value greater than or equal to the number of specified databases. The target server must have max_worker_processes configured to a value greater than the number of specified databases. The target server must accept local connections. If you are planning to use the --enable-two-phase switch then you will also need to set the max_prepared_transactions appropriately.
The source server must accept connections from the target server. The source server must not be in recovery. The source server must have wal_level as logical. The source server must have max_replication_slots configured to a value greater than or equal to the number of specified databases plus existing replication slots. The source server must have max_wal_senders configured to a value greater than or equal to the number of specified databases and existing WAL sender processes.
Warnings¶
If pg_createsubscriber fails after the target server was promoted, then the data directory is likely not in a state that can be recovered. In such case, creating a new standby server is recommended.
pg_createsubscriber usually starts the target server with different connection settings during transformation. Hence, connections to the target server should fail.
Since DDL commands are not replicated by logical replication, avoid executing DDL commands that change the database schema while running pg_createsubscriber. If the target server has already been converted to logical replica, the DDL commands might not be replicated, which might cause an error.
If pg_createsubscriber fails while processing, objects (publications, replication slots) created on the source server are removed. The removal might fail if the target server cannot connect to the source server. In such a case, a warning message will inform the objects left. If the target server is running, it will be stopped.
If the replication is using primary_slot_name, it will be removed from the source server after the logical replication setup.
If the target server is a synchronous replica, transaction commits on the primary might wait for replication while running pg_createsubscriber.
Unless the --enable-two-phase switch is specified, pg_createsubscriber sets up logical replication with two-phase commit disabled. This means that any prepared transactions will be replicated at the time of COMMIT PREPARED, without advance preparation. Once setup is complete, you can manually drop and re-create the subscription(s) with the two_phase option enabled.
pg_createsubscriber changes the system identifier using pg_resetwal. It would avoid situations in which the target server might use WAL files from the source server. If the target server has a standby, replication will break and a fresh standby should be created.
Replication failures can occur if required WAL files are missing. To prevent this, the source server must set max_slot_wal_keep_size to -1 to ensure that required WAL files are not prematurely removed.
How It Works¶
The basic idea is to have a replication start point from the source server and set up a logical replication to start from this point:
EXAMPLES¶
To create a logical replica for databases hr and finance from a physical replica at foo:
$ pg_createsubscriber -D /usr/local/pgsql/data -P "host=foo" -d hr -d finance
SEE ALSO¶
2025 | PostgreSQL 18.0 |