PostgreSQL Source Code  git master
logicalproto.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * logicalproto.h
4  * logical replication protocol
5  *
6  * Copyright (c) 2015-2021, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/include/replication/logicalproto.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef LOGICAL_PROTO_H
14 #define LOGICAL_PROTO_H
15 
16 #include "access/xact.h"
18 #include "utils/rel.h"
19 
20 /*
21  * Protocol capabilities
22  *
23  * LOGICALREP_PROTO_VERSION_NUM is our native protocol.
24  * LOGICALREP_PROTO_MAX_VERSION_NUM is the greatest version we can support.
25  * LOGICALREP_PROTO_MIN_VERSION_NUM is the oldest version we
26  * have backwards compatibility for. The client requests protocol version at
27  * connect time.
28  *
29  * LOGICALREP_PROTO_STREAM_VERSION_NUM is the minimum protocol version with
30  * support for streaming large transactions. Introduced in PG14.
31  *
32  * LOGICALREP_PROTO_TWOPHASE_VERSION_NUM is the minimum protocol version with
33  * support for two-phase commit decoding (at prepare time). Introduced in PG15.
34  */
35 #define LOGICALREP_PROTO_MIN_VERSION_NUM 1
36 #define LOGICALREP_PROTO_VERSION_NUM 1
37 #define LOGICALREP_PROTO_STREAM_VERSION_NUM 2
38 #define LOGICALREP_PROTO_TWOPHASE_VERSION_NUM 3
39 #define LOGICALREP_PROTO_MAX_VERSION_NUM LOGICALREP_PROTO_TWOPHASE_VERSION_NUM
40 
41 /*
42  * Logical message types
43  *
44  * Used by logical replication wire protocol.
45  *
46  * Note: though this is an enum, the values are used to identify message types
47  * in logical replication protocol, which uses a single byte to identify a
48  * message type. Hence the values should be single-byte wide and preferably
49  * human-readable characters.
50  */
51 typedef enum LogicalRepMsgType
52 {
73 
74 /*
75  * This struct stores a tuple received via logical replication.
76  * Keep in mind that the columns correspond to the *remote* table.
77  */
78 typedef struct LogicalRepTupleData
79 {
80  /* Array of StringInfos, one per column; some may be unused */
82  /* Array of markers for null/unchanged/text/binary, one per column */
83  char *colstatus;
84  /* Length of above arrays */
85  int ncols;
87 
88 /* Possible values for LogicalRepTupleData.colstatus[colnum] */
89 /* These values are also used in the on-the-wire protocol */
90 #define LOGICALREP_COLUMN_NULL 'n'
91 #define LOGICALREP_COLUMN_UNCHANGED 'u'
92 #define LOGICALREP_COLUMN_TEXT 't'
93 #define LOGICALREP_COLUMN_BINARY 'b' /* added in PG14 */
94 
96 
97 /* Relation information */
98 typedef struct LogicalRepRelation
99 {
100  /* Info coming from the remote side. */
101  LogicalRepRelId remoteid; /* unique id of the relation */
102  char *nspname; /* schema name */
103  char *relname; /* relation name */
104  int natts; /* number of columns */
105  char **attnames; /* column names */
106  Oid *atttyps; /* column types */
107  char replident; /* replica identity */
108  char relkind; /* remote relation kind */
109  Bitmapset *attkeys; /* Bitmap of key columns */
111 
112 /* Type mapping info */
113 typedef struct LogicalRepTyp
114 {
115  Oid remoteid; /* unique id of the remote type */
116  char *nspname; /* schema name of remote type */
117  char *typname; /* name of the remote type */
118 } LogicalRepTyp;
119 
120 /* Transaction info */
121 typedef struct LogicalRepBeginData
122 {
127 
128 typedef struct LogicalRepCommitData
129 {
134 
135 /*
136  * Prepared transaction protocol information for begin_prepare, and prepare.
137  */
139 {
144  char gid[GIDSIZE];
146 
147 /*
148  * Prepared transaction protocol information for commit prepared.
149  */
151 {
156  char gid[GIDSIZE];
158 
159 /*
160  * Rollback Prepared transaction protocol information. The prepare information
161  * prepare_end_lsn and prepare_time are used to check if the downstream has
162  * received this prepared transaction in which case it can apply the rollback,
163  * otherwise, it can skip the rollback operation. The gid alone is not
164  * sufficient because the downstream node can have a prepared transaction with
165  * same identifier.
166  */
168 {
174  char gid[GIDSIZE];
176 
177 extern void logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn);
178 extern void logicalrep_read_begin(StringInfo in,
179  LogicalRepBeginData *begin_data);
181  XLogRecPtr commit_lsn);
182 extern void logicalrep_read_commit(StringInfo in,
183  LogicalRepCommitData *commit_data);
186  LogicalRepPreparedTxnData *begin_data);
188  XLogRecPtr prepare_lsn);
189 extern void logicalrep_read_prepare(StringInfo in,
190  LogicalRepPreparedTxnData *prepare_data);
192  XLogRecPtr commit_lsn);
194  LogicalRepCommitPreparedTxnData *prepare_data);
196  XLogRecPtr prepare_end_lsn,
197  TimestampTz prepare_time);
199  LogicalRepRollbackPreparedTxnData *rollback_data);
201  XLogRecPtr prepare_lsn);
203  LogicalRepPreparedTxnData *prepare_data);
204 
205 extern void logicalrep_write_origin(StringInfo out, const char *origin,
206  XLogRecPtr origin_lsn);
207 extern char *logicalrep_read_origin(StringInfo in, XLogRecPtr *origin_lsn);
209  Relation rel, HeapTuple newtuple,
210  bool binary);
213  Relation rel, HeapTuple oldtuple,
214  HeapTuple newtuple, bool binary);
216  bool *has_oldtuple, LogicalRepTupleData *oldtup,
217  LogicalRepTupleData *newtup);
219  Relation rel, HeapTuple oldtuple,
220  bool binary);
222  LogicalRepTupleData *oldtup);
224  int nrelids, Oid relids[],
225  bool cascade, bool restart_seqs);
227  bool *cascade, bool *restart_seqs);
229  bool transactional, const char *prefix, Size sz, const char *message);
230 extern void logicalrep_write_rel(StringInfo out, TransactionId xid,
231  Relation rel);
233 extern void logicalrep_write_typ(StringInfo out, TransactionId xid,
234  Oid typoid);
235 extern void logicalrep_read_typ(StringInfo out, LogicalRepTyp *ltyp);
237  bool first_segment);
239  bool *first_segment);
242  XLogRecPtr commit_lsn);
244  LogicalRepCommitData *commit_data);
246  TransactionId subxid);
248  TransactionId *subxid);
250 
251 #endif /* LOGICAL_PROTO_H */
TransactionId xid
Definition: logicalproto.h:125
struct LogicalRepPreparedTxnData LogicalRepPreparedTxnData
void logicalrep_write_message(StringInfo out, TransactionId xid, XLogRecPtr lsn, bool transactional, const char *prefix, Size sz, const char *message)
Definition: proto.c:627
void logicalrep_write_update(StringInfo out, TransactionId xid, Relation rel, HeapTuple oldtuple, HeapTuple newtuple, bool binary)
Definition: proto.c:444
void logicalrep_write_stream_prepare(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: proto.c:350
uint32 TransactionId
Definition: c.h:587
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:75
int64 TimestampTz
Definition: timestamp.h:39
TransactionId logicalrep_read_stream_commit(StringInfo out, LogicalRepCommitData *commit_data)
Definition: proto.c:1109
void logicalrep_write_stream_stop(StringInfo out)
Definition: proto.c:1075
void logicalrep_read_typ(StringInfo out, LogicalRepTyp *ltyp)
Definition: proto.c:739
StringInfoData * colvalues
Definition: logicalproto.h:81
void logicalrep_write_stream_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:1084
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:109
void logicalrep_write_rel(StringInfo out, TransactionId xid, Relation rel)
Definition: proto.c:654
LogicalRepRelId remoteid
Definition: logicalproto.h:101
void logicalrep_read_commit_prepared(StringInfo in, LogicalRepCommitPreparedTxnData *prepare_data)
Definition: proto.c:264
void logicalrep_read_prepare(StringInfo in, LogicalRepPreparedTxnData *prepare_data)
Definition: proto.c:225
struct LogicalRepCommitData LogicalRepCommitData
List * logicalrep_read_truncate(StringInfo in, bool *cascade, bool *restart_seqs)
Definition: proto.c:602
void logicalrep_read_begin_prepare(StringInfo in, LogicalRepPreparedTxnData *begin_data)
Definition: proto.c:131
struct LogicalRepRollbackPreparedTxnData LogicalRepRollbackPreparedTxnData
#define GIDSIZE
Definition: xact.h:31
void logicalrep_write_origin(StringInfo out, const char *origin, XLogRecPtr origin_lsn)
Definition: proto.c:371
char * logicalrep_read_origin(StringInfo in, XLogRecPtr *origin_lsn)
Definition: proto.c:387
void logicalrep_write_stream_start(StringInfo out, TransactionId xid, bool first_segment)
Definition: proto.c:1041
void logicalrep_write_typ(StringInfo out, TransactionId xid, Oid typoid)
Definition: proto.c:708
unsigned int uint32
Definition: c.h:441
char * logicalrep_message_type(LogicalRepMsgType action)
Definition: proto.c:1164
LogicalRepRelation * logicalrep_read_rel(StringInfo in)
Definition: proto.c:683
struct LogicalRepTyp LogicalRepTyp
struct LogicalRepRelation LogicalRepRelation
XLogRecPtr final_lsn
Definition: logicalproto.h:123
void logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn)
Definition: proto.c:46
LogicalRepRelId logicalrep_read_insert(StringInfo in, LogicalRepTupleData *newtup)
Definition: proto.c:422
struct LogicalRepTupleData LogicalRepTupleData
struct LogicalRepBeginData LogicalRepBeginData
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void logicalrep_write_truncate(StringInfo out, TransactionId xid, int nrelids, Oid relids[], bool cascade, bool restart_seqs)
Definition: proto.c:570
void logicalrep_write_delete(StringInfo out, TransactionId xid, Relation rel, HeapTuple oldtuple, bool binary)
Definition: proto.c:518
LogicalRepMsgType
Definition: logicalproto.h:51
void logicalrep_read_commit(StringInfo in, LogicalRepCommitData *commit_data)
Definition: proto.c:95
TransactionId logicalrep_read_stream_start(StringInfo in, bool *first_segment)
Definition: proto.c:1059
size_t Size
Definition: c.h:540
LogicalRepRelId logicalrep_read_update(StringInfo in, bool *has_oldtuple, LogicalRepTupleData *oldtup, LogicalRepTupleData *newtup)
Definition: proto.c:477
struct LogicalRepCommitPreparedTxnData LogicalRepCommitPreparedTxnData
void logicalrep_read_stream_prepare(StringInfo in, LogicalRepPreparedTxnData *prepare_data)
Definition: proto.c:362
void logicalrep_write_stream_abort(StringInfo out, TransactionId xid, TransactionId subxid)
Definition: proto.c:1135
void logicalrep_read_begin(StringInfo in, LogicalRepBeginData *begin_data)
Definition: proto.c:60
LogicalRepRelId logicalrep_read_delete(StringInfo in, LogicalRepTupleData *oldtup)
Definition: proto.c:548
void logicalrep_write_begin_prepare(StringInfo out, ReorderBufferTXN *txn)
Definition: proto.c:113
void logicalrep_write_insert(StringInfo out, TransactionId xid, Relation rel, HeapTuple newtuple, bool binary)
Definition: proto.c:400
void logicalrep_read_rollback_prepared(StringInfo in, LogicalRepRollbackPreparedTxnData *rollback_data)
Definition: proto.c:322
void logicalrep_write_prepare(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: proto.c:184
TimestampTz committime
Definition: logicalproto.h:124
Definition: pg_list.h:50
void logicalrep_write_commit_prepared(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:234
TimestampTz committime
Definition: logicalproto.h:132
uint32 LogicalRepRelId
Definition: logicalproto.h:95
void logicalrep_read_stream_abort(StringInfo in, TransactionId *xid, TransactionId *subxid)
Definition: proto.c:1151
void logicalrep_write_rollback_prepared(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: proto.c:290