Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
CREATE SUBSCRIPTION
Prev UpSQL CommandsHome Next

CREATE SUBSCRIPTION

CREATE SUBSCRIPTION — define a new subscription

Synopsis

CREATE SUBSCRIPTIONsubscription_name    CONNECTION 'conninfo'    PUBLICATIONpublication_name [, ...]    [ WITH (subscription_parameter [=value] [, ... ] ) ]

Description

CREATE SUBSCRIPTION adds a new logical-replication subscription. The subscription name must be distinct from the name of any existing subscription in the current database.

A subscription represents a replication connection to the publisher. Hence, in addition to adding definitions in the local catalogs, this command normally creates a replication slot on the publisher.

A logical replication worker will be started to replicate data for the new subscription at the commit of the transaction where this command is run, unless the subscription is initially disabled.

Additional information about subscriptions and logical replication as a whole is available atSection 31.2 andChapter 31.

Parameters

subscription_name

The name of the new subscription.

CONNECTION 'conninfo'

Thelibpq connection string defining how to connect to the publisher database. For details seeSection 34.1.1.

PUBLICATIONpublication_name [, ...]

Names of the publications on the publisher to subscribe to.

WITH (subscription_parameter [=value] [, ... ] )

This clause specifies optional parameters for a subscription.

The following parameters control what happens during subscription creation:

connect (boolean)

Specifies whether theCREATE SUBSCRIPTION command should connect to the publisher at all. The default istrue. Setting this tofalse will force the values ofcreate_slot,enabled andcopy_data tofalse. (You cannot combine settingconnect tofalse with settingcreate_slot,enabled, orcopy_data totrue.)

Since no connection is made when this option isfalse, no tables are subscribed, and so after you enable the subscription nothing will be replicated. You will need to then runALTER SUBSCRIPTION ... REFRESH PUBLICATION for tables to be subscribed.

create_slot (boolean)

Specifies whether the command should create the replication slot on the publisher. The default istrue. If set tofalse, you are responsible for creating the publisher's slot in some other way.

enabled (boolean)

Specifies whether the subscription should be actively replicating or whether it should just be set up but not started yet. The default istrue.

slot_name (string)

Name of the publisher's replication slot to use. The default is to use the name of the subscription for the slot name.

Settingslot_name toNONE means there will be no replication slot associated with the subscription. Use this when you will be creating the replication slot later manually. Such subscriptions must also have bothenabled andcreate_slot set tofalse.

The following parameters control the subscription's replication behavior after it has been created:

binary (boolean)

Specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text). The default isfalse. Even when this option is enabled, only data types having binary send and receive functions will be transferred in binary.

When doing cross-version replication, it could be that the publisher has a binary send function for some data type, but the subscriber lacks a binary receive function for that type. In such a case, data transfer will fail, and thebinary option cannot be used.

copy_data (boolean)

Specifies whether to copy pre-existing data in the publications that are being subscribed to when the replication starts. The default istrue.

If the publications containWHERE clauses, it will affect what data is copied. Refer to theNotes for details.

streaming (boolean)

Specifies whether to enable streaming of in-progress transactions for this subscription. By default, all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole.

synchronous_commit (enum)

The value of this parameter overrides thesynchronous_commit setting within this subscription's apply worker processes. The default value isoff.

It is safe to useoff for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher.

A different setting might be appropriate when doing synchronous logical replication. The logical replication workers report the positions of writes and flushes to the publisher, and when using synchronous replication, the publisher will wait for the actual flush. This means that settingsynchronous_commit for the subscriber tooff when the subscription is used for synchronous replication might increase the latency forCOMMIT on the publisher. In this scenario, it can be advantageous to setsynchronous_commit tolocal or higher.

two_phase (boolean)

Specifies whether two-phase commit is enabled for this subscription. The default isfalse.

When two-phase commit is enabled, prepared transactions are sent to the subscriber at the time ofPREPARE TRANSACTION, and are processed as two-phase transactions on the subscriber too. Otherwise, prepared transactions are sent to the subscriber only when committed, and are then processed immediately by the subscriber.

The implementation of two-phase commit requires that replication has successfully finished the initial table synchronization phase. So even whentwo_phase is enabled for a subscription, the internal two-phase state remains temporarilypending until the initialization phase completes. See columnsubtwophasestate ofpg_subscription to know the actual two-phase state.

disable_on_error (boolean)

Specifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher. The default isfalse.

Notes

SeeSection 31.9 for details on how to configure access control between the subscription and the publication instance.

When creating a replication slot (the default behavior),CREATE SUBSCRIPTION cannot be executed inside a transaction block.

Creating a subscription that connects to the same database cluster (for example, to replicate between databases in the same cluster or to replicate within the same database) will only succeed if the replication slot is not created as part of the same command. Otherwise, theCREATE SUBSCRIPTION call will hang. To make this work, create the replication slot separately (using the functionpg_create_logical_replication_slot with the plugin namepgoutput) and create the subscription using the parametercreate_slot = false. This is an implementation restriction that might be lifted in a future release.

If any table in the publication has aWHERE clause, rows for which theexpression evaluates to false or null will not be published. If the subscription has several publications in which the same table has been published with differentWHERE clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied. In the case of differentWHERE clauses, if one of the publications has noWHERE clause (referring to that publish operation) or the publication is declared asFOR ALL TABLES orFOR TABLES IN SCHEMA, rows are always published regardless of the definition of the other expressions. If the subscriber is aPostgreSQL version before 15, then any row filtering is ignored during the initial data synchronization phase. For this case, the user might want to consider deleting any initially copied data that would be incompatible with subsequent filtering. Because initial data synchronization does not take into account the publicationpublish parameter when copying existing table data, some rows may be copied that would not be replicated using DML. SeeSection 31.2.2 for examples.

Subscriptions having several publications in which the same table has been published with different column lists are not supported.

We allow non-existent publications to be specified so that users can add those later. This meanspg_subscription can have non-existent publications.

Examples

Create a subscription to a remote server that replicates tables in the publicationsmypublication andinsert_only and starts replicating immediately on commit:

CREATE SUBSCRIPTION mysub         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'        PUBLICATION mypublication, insert_only;

Create a subscription to a remote server that replicates tables in theinsert_only publication and does not start replicating until enabled at a later time.

CREATE SUBSCRIPTION mysub         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'        PUBLICATION insert_only               WITH (enabled = false);

Compatibility

CREATE SUBSCRIPTION is aPostgreSQL extension.


Prev Up Next
CREATE STATISTICS Home CREATE TABLE
pdfepub
Go to PostgreSQL 15
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp