]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_SUBSCRIPTION.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_SUBSCRIPTION.7
1 '\" t
2 .\"     Title: CREATE SUBSCRIPTION
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE SUBSCRIPTION" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_SUBSCRIPTION \- define a new subscription
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE SUBSCRIPTION \fIsubscription_name\fR
36     CONNECTION \*(Aq\fIconninfo\fR\*(Aq
37     PUBLICATION \fIpublication_name\fR [, \&.\&.\&.]
38     [ WITH ( \fIsubscription_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] ) ]
39 .fi
40 .SH "DESCRIPTION"
41 .PP
42 \fBCREATE SUBSCRIPTION\fR
43 adds a new logical\-replication subscription\&. The user that creates a subscription becomes the owner of the subscription\&. The subscription name must be distinct from the name of any existing subscription in the current database\&.
44 .PP
45 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\&.
46 .PP
47 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\&.
48 .PP
49 To be able to create a subscription, you must have the privileges of the
50 pg_create_subscription
51 role, as well as
52 CREATE
53 privileges on the current database\&.
54 .PP
55 Additional information about subscriptions and logical replication as a whole is available at
56 Section\ \&29.2
57 and
58 Chapter\ \&29\&.
59 .SH "PARAMETERS"
60 .PP
61 \fIsubscription_name\fR
62 .RS 4
63 The name of the new subscription\&.
64 .RE
65 .PP
66 CONNECTION \*(Aq\fIconninfo\fR\*(Aq
67 .RS 4
68 The
69 libpq
70 connection string defining how to connect to the publisher database\&. For details see
71 Section\ \&32.1.1\&.
72 .RE
73 .PP
74 PUBLICATION \fIpublication_name\fR [, \&.\&.\&.]
75 .RS 4
76 Names of the publications on the publisher to subscribe to\&.
77 .RE
78 .PP
79 WITH ( \fIsubscription_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
80 .RS 4
81 This clause specifies optional parameters for a subscription\&.
82 .sp
83 The following parameters control what happens during subscription creation:
84 .PP
85 connect (boolean)
86 .RS 4
87 Specifies whether the
88 \fBCREATE SUBSCRIPTION\fR
89 command should connect to the publisher at all\&. The default is
90 true\&. Setting this to
91 false
92 will force the values of
93 create_slot,
94 enabled
95 and
96 copy_data
97 to
98 false\&. (You cannot combine setting
99 connect
100 to
101 false
102 with setting
103 create_slot,
104 enabled, or
105 copy_data
106 to
107 true\&.)
108 .sp
109 Since no connection is made when this option is
110 false, no tables are subscribed\&. To initiate replication, you must manually create the replication slot, enable the failover if required, enable the subscription, and refresh the subscription\&. See
111 Section\ \&29.2.3
112 for examples\&.
113 .RE
114 .PP
115 create_slot (boolean)
116 .RS 4
117 Specifies whether the command should create the replication slot on the publisher\&. The default is
118 true\&.
119 .sp
120 If set to
121 false, you are responsible for creating the publisher\*(Aqs slot in some other way\&. See
122 Section\ \&29.2.3
123 for examples\&.
124 .RE
125 .PP
126 enabled (boolean)
127 .RS 4
128 Specifies whether the subscription should be actively replicating or whether it should just be set up but not started yet\&. The default is
129 true\&.
130 .RE
131 .PP
132 slot_name (string)
133 .RS 4
134 Name of the publisher\*(Aqs replication slot to use\&. The default is to use the name of the subscription for the slot name\&.
135 .sp
136 Setting
137 slot_name
138 to
139 NONE
140 means there will be no replication slot associated with the subscription\&. Such subscriptions must also have both
141 enabled
142 and
143 create_slot
144 set to
145 false\&. Use this when you will be creating the replication slot later manually\&. See
146 Section\ \&29.2.3
147 for examples\&.
148 .sp
149 When setting
150 slot_name
151 to a valid name and
152 create_slot
153 to
154 false, the
155 failover
156 property value of the named slot may differ from the counterpart
157 failover
158 parameter specified in the subscription\&. Always ensure the slot property
159 failover
160 matches the counterpart parameter of the subscription and vice versa\&. Otherwise, the slot on the publisher may behave differently from what these subscription options say: for example, the slot on the publisher could either be synced to the standbys even when the subscription\*(Aqs
161 failover
162 option is disabled or could be disabled for sync even when the subscription\*(Aqs
163 failover
164 option is enabled\&.
165 .RE
166 .sp
167 The following parameters control the subscription\*(Aqs replication behavior after it has been created:
168 .PP
169 binary (boolean)
170 .RS 4
171 Specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text)\&. The default is
172 false\&. Any initial table synchronization copy (see
173 copy_data) also uses the same format\&. Binary format can be faster than the text format, but it is less portable across machine architectures and
174 PostgreSQL
175 versions\&. Binary format is very data type specific; for example, it will not allow copying from a
176 smallint
177 column to an
178 integer
179 column, even though that would work fine in text format\&. Even when this option is enabled, only data types having binary send and receive functions will be transferred in binary\&. Note that the initial synchronization requires all data types to have binary send and receive functions, otherwise the synchronization will fail (see
180 CREATE TYPE (\fBCREATE_TYPE\fR(7))
181 for more about send/receive functions)\&.
182 .sp
183 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 the
184 binary
185 option cannot be used\&.
186 .sp
187 If the publisher is a
188 PostgreSQL
189 version before 16, then any initial table synchronization will use text format even if
190 binary = true\&.
191 .RE
192 .PP
193 copy_data (boolean)
194 .RS 4
195 Specifies whether to copy pre\-existing data in the publications that are being subscribed to when the replication starts\&. The default is
196 true\&.
197 .sp
198 If the publications contain
199 WHERE
200 clauses, it will affect what data is copied\&. Refer to the
201 Notes
202 for details\&.
203 .sp
204 See
205 Notes
206 for details of how
207 copy_data = true
208 can interact with the
209 origin
210 parameter\&.
211 .RE
212 .PP
213 streaming (enum)
214 .RS 4
215 Specifies whether to enable streaming of in\-progress transactions for this subscription\&. The default value is
216 parallel, meaning incoming changes are directly applied via one of the parallel apply workers, if available\&. If no parallel apply worker is free to handle streaming transactions then the changes are written to temporary files and applied after the transaction is committed\&. Note that if an error happens in a parallel apply worker, the finish LSN of the remote transaction might not be reported in the server log\&.
217 .if n \{\
218 .sp
219 .\}
220 .RS 4
221 .it 1 an-trap
222 .nr an-no-space-flag 1
223 .nr an-break-flag 1
224 .br
225 .ps +1
226 \fBCaution\fR
227 .ps -1
228 .br
229 There is a risk of deadlock when the schemas of the publisher and subscriber differ, although such cases are rare\&. The apply worker is equipped to retry these transactions automatically\&.
230 .sp .5v
231 .RE
232 If set to
233 on, the incoming changes are written to temporary files and then applied only after the transaction is committed on the publisher and received by the subscriber\&.
234 .sp
235 If set to
236 off, all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole\&.
237 .RE
238 .PP
239 synchronous_commit (enum)
240 .RS 4
241 The value of this parameter overrides the
242 synchronous_commit
243 setting within this subscription\*(Aqs apply worker processes\&. The default value is
244 off\&.
245 .sp
246 It is safe to use
247 off
248 for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher\&.
249 .sp
250 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 setting
251 synchronous_commit
252 for the subscriber to
253 off
254 when the subscription is used for synchronous replication might increase the latency for
255 \fBCOMMIT\fR
256 on the publisher\&. In this scenario, it can be advantageous to set
257 synchronous_commit
258 to
259 local
260 or higher\&.
261 .RE
262 .PP
263 two_phase (boolean)
264 .RS 4
265 Specifies whether two\-phase commit is enabled for this subscription\&. The default is
266 false\&.
267 .sp
268 When two\-phase commit is enabled, prepared transactions are sent to the subscriber at the time of
269 \fBPREPARE TRANSACTION\fR, 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\&.
270 .sp
271 The implementation of two\-phase commit requires that replication has successfully finished the initial table synchronization phase\&. So even when
272 two_phase
273 is enabled for a subscription, the internal two\-phase state remains temporarily
274 \(lqpending\(rq
275 until the initialization phase completes\&. See column
276 subtwophasestate
277 of
278 pg_subscription
279 to know the actual two\-phase state\&.
280 .RE
281 .PP
282 disable_on_error (boolean)
283 .RS 4
284 Specifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher\&. The default is
285 false\&.
286 .RE
287 .PP
288 password_required (boolean)
289 .RS 4
290 If set to
291 true, connections to the publisher made as a result of this subscription must use password authentication and the password must be specified as a part of the connection string\&. This setting is ignored when the subscription is owned by a superuser\&. The default is
292 true\&. Only superusers can set this value to
293 false\&.
294 .RE
295 .PP
296 run_as_owner (boolean)
297 .RS 4
298 If true, all replication actions are performed as the subscription owner\&. If false, replication workers will perform actions on each table as the owner of that table\&. The latter configuration is generally much more secure; for details, see
299 Section\ \&29.11\&. The default is
300 false\&.
301 .RE
302 .PP
303 origin (string)
304 .RS 4
305 Specifies whether the subscription will request the publisher to only send changes that don\*(Aqt have an origin or send changes regardless of origin\&. Setting
306 origin
307 to
308 none
309 means that the subscription will request the publisher to only send changes that don\*(Aqt have an origin\&. Setting
310 origin
311 to
312 any
313 means that the publisher sends changes regardless of their origin\&. The default is
314 any\&.
315 .sp
316 See
317 Notes
318 for details of how
319 copy_data = true
320 can interact with the
321 origin
322 parameter\&.
323 .RE
324 .PP
325 failover (boolean)
326 .RS 4
327 Specifies whether the replication slots associated with the subscription are enabled to be synced to the standbys so that logical replication can be resumed from the new primary after failover\&. The default is
328 false\&.
329 .RE
330 .RE
331 .PP
332 When specifying a parameter of type
333 boolean, the
334 =
335 \fIvalue\fR
336 part can be omitted, which is equivalent to specifying
337 TRUE\&.
338 .SH "NOTES"
339 .PP
340 See
341 Section\ \&29.11
342 for details on how to configure access control between the subscription and the publication instance\&.
343 .PP
344 When creating a replication slot (the default behavior),
345 \fBCREATE SUBSCRIPTION\fR
346 cannot be executed inside a transaction block\&.
347 .PP
348 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, the
349 \fBCREATE SUBSCRIPTION\fR
350 call will hang\&. To make this work, create the replication slot separately (using the function
351 \fBpg_create_logical_replication_slot\fR
352 with the plugin name
353 pgoutput) and create the subscription using the parameter
354 create_slot = false\&. See
355 Section\ \&29.2.3
356 for examples\&. This is an implementation restriction that might be lifted in a future release\&.
357 .PP
358 If any table in the publication has a
359 WHERE
360 clause, rows for which the
361 \fIexpression\fR
362 evaluates to
363 false
364 or
365 NULL
366 will not be published\&. If the subscription has several publications in which the same table has been published with different
367 WHERE
368 clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied\&. In the case of different
369 WHERE
370 clauses, if one of the publications has no
371 WHERE
372 clause (referring to that publish operation) or the publication is declared as
373 FOR ALL TABLES
374 or
375 FOR TABLES IN SCHEMA, rows are always published regardless of the definition of the other expressions\&. If the subscriber is a
376 PostgreSQL
377 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 publication
378 publish
379 parameter when copying existing table data, some rows may be copied that would not be replicated using DML\&. See
380 Section\ \&29.2.2
381 for examples\&.
382 .PP
383 Subscriptions having several publications in which the same table has been published with different column lists are not supported\&.
384 .PP
385 We allow non\-existent publications to be specified so that users can add those later\&. This means
386 pg_subscription
387 can have non\-existent publications\&.
388 .PP
389 When using a subscription parameter combination of
390 copy_data = true
391 and
392 origin = NONE, the initial sync table data is copied directly from the publisher, meaning that knowledge of the true origin of that data is not possible\&. If the publisher also has subscriptions then the copied table data might have originated from further upstream\&. This scenario is detected and a WARNING is logged to the user, but the warning is only an indication of a potential problem; it is the user\*(Aqs responsibility to make the necessary checks to ensure the copied data origins are really as wanted or not\&.
393 .PP
394 To find which tables might potentially include non\-local origins (due to other subscriptions created on the publisher) try this SQL query:
395 .sp
396 .if n \{\
397 .RS 4
398 .\}
399 .nf
400 # substitute <pub\-names> below with your publication name(s) to be queried
401 SELECT DISTINCT PT\&.schemaname, PT\&.tablename
402 FROM pg_publication_tables PT
403      JOIN pg_class C ON (C\&.relname = PT\&.tablename)
404      JOIN pg_namespace N ON (N\&.nspname = PT\&.schemaname),
405      pg_subscription_rel PS
406 WHERE C\&.relnamespace = N\&.oid AND
407       (PS\&.srrelid = C\&.oid OR
408       C\&.oid IN (SELECT relid FROM pg_partition_ancestors(PS\&.srrelid) UNION
409                 SELECT relid FROM pg_partition_tree(PS\&.srrelid))) AND
410       PT\&.pubname IN (<pub\-names>);
411 .fi
412 .if n \{\
413 .RE
414 .\}
415 .SH "EXAMPLES"
416 .PP
417 Create a subscription to a remote server that replicates tables in the publications
418 mypublication
419 and
420 insert_only
421 and starts replicating immediately on commit:
422 .sp
423 .if n \{\
424 .RS 4
425 .\}
426 .nf
427 CREATE SUBSCRIPTION mysub
428          CONNECTION \*(Aqhost=192\&.168\&.1\&.50 port=5432 user=foo dbname=foodb\*(Aq
429         PUBLICATION mypublication, insert_only;
430 .fi
431 .if n \{\
432 .RE
433 .\}
434 .PP
435 Create a subscription to a remote server that replicates tables in the
436 insert_only
437 publication and does not start replicating until enabled at a later time\&.
438 .sp
439 .if n \{\
440 .RS 4
441 .\}
442 .nf
443 CREATE SUBSCRIPTION mysub
444          CONNECTION \*(Aqhost=192\&.168\&.1\&.50 port=5432 user=foo dbname=foodb\*(Aq
445         PUBLICATION insert_only
446                WITH (enabled = false);
447 .fi
448 .if n \{\
449 .RE
450 .\}
451 .SH "COMPATIBILITY"
452 .PP
453 \fBCREATE SUBSCRIPTION\fR
454 is a
455 PostgreSQL
456 extension\&.
457 .SH "SEE ALSO"
458 ALTER SUBSCRIPTION (\fBALTER_SUBSCRIPTION\fR(7)), DROP SUBSCRIPTION (\fBDROP_SUBSCRIPTION\fR(7)), CREATE PUBLICATION (\fBCREATE_PUBLICATION\fR(7)), ALTER PUBLICATION (\fBALTER_PUBLICATION\fR(7))