1 /*-------------------------------------------------------------------------
2  *
3  * logicalproto.h
4  *		logical replication protocol
5  *
6  * Copyright (c) 2015-2020, 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 "replication/reorderbuffer.h"
17 #include "utils/rel.h"
18 
19 /*
20  * Protocol capabilities
21  *
22  * LOGICALREP_PROTO_VERSION_NUM is our native protocol and the greatest version
23  * we can support. LOGICALREP_PROTO_MIN_VERSION_NUM is the oldest version we
24  * have backwards compatibility for. The client requests protocol version at
25  * connect time.
26  */
27 #define LOGICALREP_PROTO_MIN_VERSION_NUM 1
28 #define LOGICALREP_PROTO_VERSION_NUM 1
29 
30 /* Tuple coming via logical replication. */
31 typedef struct LogicalRepTupleData
32 {
33 	/* column values in text format, or NULL for a null value: */
34 	char	   *values[MaxTupleAttributeNumber];
35 	/* markers for changed/unchanged column values: */
36 	bool		changed[MaxTupleAttributeNumber];
37 } LogicalRepTupleData;
38 
39 typedef uint32 LogicalRepRelId;
40 
41 /* Relation information */
42 typedef struct LogicalRepRelation
43 {
44 	/* Info coming from the remote side. */
45 	LogicalRepRelId remoteid;	/* unique id of the relation */
46 	char	   *nspname;		/* schema name */
47 	char	   *relname;		/* relation name */
48 	int			natts;			/* number of columns */
49 	char	  **attnames;		/* column names */
50 	Oid		   *atttyps;		/* column types */
51 	char		replident;		/* replica identity */
52 	char		relkind;		/* remote relation kind */
53 	Bitmapset  *attkeys;		/* Bitmap of key columns */
54 } LogicalRepRelation;
55 
56 /* Type mapping info */
57 typedef struct LogicalRepTyp
58 {
59 	Oid			remoteid;		/* unique id of the remote type */
60 	char	   *nspname;		/* schema name of remote type */
61 	char	   *typname;		/* name of the remote type */
62 } LogicalRepTyp;
63 
64 /* Transaction info */
65 typedef struct LogicalRepBeginData
66 {
67 	XLogRecPtr	final_lsn;
68 	TimestampTz committime;
69 	TransactionId xid;
70 } LogicalRepBeginData;
71 
72 typedef struct LogicalRepCommitData
73 {
74 	XLogRecPtr	commit_lsn;
75 	XLogRecPtr	end_lsn;
76 	TimestampTz committime;
77 } LogicalRepCommitData;
78 
79 extern void logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn);
80 extern void logicalrep_read_begin(StringInfo in,
81 								  LogicalRepBeginData *begin_data);
82 extern void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn,
83 									XLogRecPtr commit_lsn);
84 extern void logicalrep_read_commit(StringInfo in,
85 								   LogicalRepCommitData *commit_data);
86 extern void logicalrep_write_origin(StringInfo out, const char *origin,
87 									XLogRecPtr origin_lsn);
88 extern char *logicalrep_read_origin(StringInfo in, XLogRecPtr *origin_lsn);
89 extern void logicalrep_write_insert(StringInfo out, Relation rel,
90 									HeapTuple newtuple);
91 extern LogicalRepRelId logicalrep_read_insert(StringInfo in, LogicalRepTupleData *newtup);
92 extern void logicalrep_write_update(StringInfo out, Relation rel, HeapTuple oldtuple,
93 									HeapTuple newtuple);
94 extern LogicalRepRelId logicalrep_read_update(StringInfo in,
95 											  bool *has_oldtuple, LogicalRepTupleData *oldtup,
96 											  LogicalRepTupleData *newtup);
97 extern void logicalrep_write_delete(StringInfo out, Relation rel,
98 									HeapTuple oldtuple);
99 extern LogicalRepRelId logicalrep_read_delete(StringInfo in,
100 											  LogicalRepTupleData *oldtup);
101 extern void logicalrep_write_truncate(StringInfo out, int nrelids, Oid relids[],
102 									  bool cascade, bool restart_seqs);
103 extern List *logicalrep_read_truncate(StringInfo in,
104 									  bool *cascade, bool *restart_seqs);
105 extern void logicalrep_write_rel(StringInfo out, Relation rel);
106 extern LogicalRepRelation *logicalrep_read_rel(StringInfo in);
107 extern void logicalrep_write_typ(StringInfo out, Oid typoid);
108 extern void logicalrep_read_typ(StringInfo out, LogicalRepTyp *ltyp);
109 
110 #endif							/* LOGICAL_PROTO_H */
111