Implementation:2ndQuadrant Pglogical Create Subscription
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
SQL-callable function that establishes a replication link between a subscriber and a provider, specifying replication sets, synchronization options, forwarded origins, and apply delay.
Description
The pglogical.create_subscription() function creates a subscription edge connecting the local (subscriber) node to a remote (provider) node. It validates connectivity in both regular and replication modes, registers the remote node in the local catalog if not already present, checks for overlapping replication sets with existing subscriptions, creates a replication slot on the provider, generates a slot name, and creates the initial sync status record. The manager worker then spawns an apply worker to begin replication.
Usage
Call this function when:
- Connecting a subscriber to a provider to begin receiving changes
- Setting up cascading replication (subscriber also forwards to others)
- Configuring apply delay for controlled replication lag
- Using force_text_transfer for cross-version compatibility
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_functions.c (SQL wrapper), pglogical_node.c (catalog CRUD), pglogical_rpc.c (remote calls)
- Lines: pglogical_functions.c:L389-535; pglogical_node.c:L685-758 (create_subscription)
Signature
pglogical.create_subscription(
subscription_name name,
provider_dsn text,
replication_sets text[],
synchronize_structure bool,
synchronize_data bool,
forward_origins text[],
apply_delay interval,
force_text_transfer bool
) RETURNS oid
Import
-- Available after CREATE EXTENSION pglogical;
-- Called as: SELECT pglogical.create_subscription(...);
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| subscription_name | name | Yes | Unique subscription name |
| provider_dsn | text | Yes | Connection string to the provider node |
| replication_sets | text[] | Yes | Array of replication set names to subscribe to |
| synchronize_structure | bool | Yes | Copy schema from provider (pg_dump/pg_restore) |
| synchronize_data | bool | Yes | Copy table data from provider (COPY) |
| forward_origins | text[] | Yes | Origins to forward (empty array for none) |
| apply_delay | interval | Yes | Replication delay (0 for immediate) |
| force_text_transfer | bool | Yes | Force text-mode transfer (cross-version compat) |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | oid | Subscription OID |
| pglogical.subscription | catalog row | New subscription record with slot name, replication sets, enabled=true |
| remote node/interface | catalog rows | Provider node and interface registered locally if new |
| pglogical.local_sync_status | catalog row | Initial sync status (SYNC_STATUS_INIT) |
| replication slot | slot on provider | Logical replication slot created with slot name |
| apply worker | background process | Manager spawns apply worker for this subscription |
Usage Examples
Basic Subscription with Full Sync
-- Create subscription with schema and data sync
SELECT pglogical.create_subscription(
subscription_name := 'sub_provider1',
provider_dsn := 'host=provider.example.com port=5432 dbname=mydb',
replication_sets := ARRAY['default', 'insert_only'],
synchronize_structure := true,
synchronize_data := true,
forward_origins := ARRAY[''],
apply_delay := '0s'::interval,
force_text_transfer := false
);
Cross-Version Subscription (Force Text Transfer)
-- When provider and subscriber run different PG versions
SELECT pglogical.create_subscription(
subscription_name := 'cross_version_sub',
provider_dsn := 'host=provider-pg14.example.com dbname=mydb',
replication_sets := ARRAY['default'],
synchronize_structure := true,
synchronize_data := true,
forward_origins := ARRAY[''],
apply_delay := '0s'::interval,
force_text_transfer := true -- Avoid binary type incompatibility
);
Cascading Replication (Forward Origins)
-- Forward changes originating from 'upstream' node
SELECT pglogical.create_subscription(
subscription_name := 'cascading_sub',
provider_dsn := 'host=middle-node.example.com dbname=mydb',
replication_sets := ARRAY['default'],
synchronize_structure := true,
synchronize_data := true,
forward_origins := ARRAY['upstream_node'],
apply_delay := '0s'::interval,
force_text_transfer := false
);
Related Pages
Implements Principle
- Principle:2ndQuadrant_Pglogical_Subscription_Management
- Principle:2ndQuadrant_Pglogical_Logical_Replication
- Principle:2ndQuadrant_Pglogical_Initial_Data_Synchronization