1 /*-------------------------------------------------------------------------
2  *
3  * pglogical_output_proto.h
4  *		pglogical protocol
5  *
6  * Copyright (c) 2015, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *		  pglogical_output_proto.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef PG_LOGICAL_OUTPUT_PROTO_H
14 #define PG_LOGICAL_OUTPUT_PROTO_H
15 
16 #include "lib/stringinfo.h"
17 #include "replication/reorderbuffer.h"
18 #include "utils/relcache.h"
19 
20 #include "pglogical_output_plugin.h"
21 
22 /*
23  * Protocol capabilities
24  *
25  * PGLOGICAL_PROTO_VERSION_NUM is our native protocol and the greatest version
26  * we can support. PGLOGICAL_PROTO_MIN_VERSION_NUM is the oldest version we
27  * have backwards compatibility for. We negotiate protocol versions during the
28  * startup handshake. See the protocol documentation for details.
29  */
30 #define PGLOGICAL_PROTO_VERSION_NUM 1
31 #define PGLOGICAL_PROTO_MIN_VERSION_NUM 1
32 
33 /*
34  * The startup parameter format is versioned separately to the rest of the wire
35  * protocol because we negotiate the wire protocol version using the startup
36  * parameters sent to us. It hopefully won't ever need to change, but this
37  * field is present in case we do need to change it, e.g. to a structured json
38  * object. We can look at the startup params version to see whether we can
39  * understand the startup params sent by the client and to fall back to
40  * reading an older format if needed.
41  */
42 #define PGLOGICAL_STARTUP_PARAM_FORMAT_FLAT 1
43 
44 /*
45  * For similar reasons to the startup params
46  * (PGLOGICAL_STARTUP_PARAM_FORMAT_FLAT) the startup reply message format is
47  * versioned separately to the rest of the protocol. The client has to be able
48  * to read it to find out what protocol version was selected by the upstream
49  * when using the native protocol.
50  */
51 #define PGLOGICAL_STARTUP_MSG_FORMAT_FLAT 1
52 
53 typedef enum PGLogicalProtoType
54 {
55 	PGLogicalProtoNative,
56 	PGLogicalProtoJson
57 } PGLogicalProtoType;
58 
59 typedef void (*pglogical_write_rel_fn) (StringInfo out, PGLogicalOutputData * data,
60 						   Relation rel, Bitmapset *att_list);
61 
62 typedef void (*pglogical_write_begin_fn) (StringInfo out, PGLogicalOutputData * data,
63 													  ReorderBufferTXN *txn);
64 typedef void (*pglogical_write_commit_fn) (StringInfo out, PGLogicalOutputData * data,
65 							   ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
66 
67 typedef void (*pglogical_write_origin_fn) (StringInfo out, const char *origin,
68 													   XLogRecPtr origin_lsn);
69 
70 typedef void (*pglogical_write_insert_fn) (StringInfo out, PGLogicalOutputData * data,
71 										   Relation rel, HeapTuple newtuple,
72 										   Bitmapset *att_list);
73 typedef void (*pglogical_write_update_fn) (StringInfo out, PGLogicalOutputData * data,
74 											Relation rel, HeapTuple oldtuple,
75 											HeapTuple newtuple,
76 											Bitmapset *att_list);
77 typedef void (*pglogical_write_delete_fn) (StringInfo out, PGLogicalOutputData * data,
78 										   Relation rel, HeapTuple oldtuple,
79 										   Bitmapset *att_list);
80 
81 typedef void (*write_startup_message_fn) (StringInfo out, List *msg);
82 
83 typedef struct PGLogicalProtoAPI
84 {
85 	pglogical_write_rel_fn write_rel;
86 	pglogical_write_begin_fn write_begin;
87 	pglogical_write_commit_fn write_commit;
88 	pglogical_write_origin_fn write_origin;
89 	pglogical_write_insert_fn write_insert;
90 	pglogical_write_update_fn write_update;
91 	pglogical_write_delete_fn write_delete;
92 	write_startup_message_fn write_startup_message;
93 } PGLogicalProtoAPI;
94 
95 extern PGLogicalProtoAPI *pglogical_init_api(PGLogicalProtoType typ);
96 
97 #endif /* PG_LOGICAL_OUTPUT_PROTO_H */
98