-
Notifications
You must be signed in to change notification settings - Fork 15
/
spock_output_config.c
570 lines (477 loc) · 16.6 KB
/
spock_output_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*-------------------------------------------------------------------------
*
* spock_output_config.c
* Logical Replication output plugin configuration handling
*
* Copyright (c) 2022-2023, pgEdge, Inc.
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, The Regents of the University of California
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/catversion.h"
#include "mb/pg_wchar.h"
#include "nodes/makefuncs.h"
#include "replication/reorderbuffer.h"
#include "utils/builtins.h"
#if PG_VERSION_NUM < 150000
#include "utils/int8.h"
#endif
#include "miscadmin.h"
#include "spock.h"
#include "spock_output_config.h"
#include "spock_output_proto.h"
#include "spock_repset.h"
typedef enum SpockOutputParamType
{
OUTPUT_PARAM_TYPE_BOOL,
OUTPUT_PARAM_TYPE_UINT32,
OUTPUT_PARAM_TYPE_INT32,
OUTPUT_PARAM_TYPE_STRING,
OUTPUT_PARAM_TYPE_QUALIFIED_NAME
} SpockOutputParamType;
/* param parsing */
static Datum get_param_value(DefElem *elem, bool null_ok,
SpockOutputParamType type);
static Datum get_param(List *options, const char *name, bool missing_ok,
bool null_ok, SpockOutputParamType type,
bool *found);
static bool parse_param_bool(DefElem *elem);
static uint32 parse_param_uint32(DefElem *elem);
static int32 parse_param_int32(DefElem *elem);
static void
process_parameters_v1(List *options, SpockOutputData *data);
enum {
PARAM_UNRECOGNISED,
PARAM_MAX_PROTOCOL_VERSION,
PARAM_MIN_PROTOCOL_VERSION,
PARAM_PROTOCOL_FORMAT,
PARAM_EXPECTED_ENCODING,
PARAM_BINARY_BIGENDIAN,
PARAM_BINARY_SIZEOF_DATUM,
PARAM_BINARY_SIZEOF_INT,
PARAM_BINARY_SIZEOF_LONG,
PARAM_BINARY_FLOAT4BYVAL,
PARAM_BINARY_FLOAT8BYVAL,
PARAM_BINARY_INTEGER_DATETIMES,
PARAM_BINARY_WANT_INTERNAL_BASETYPES,
PARAM_BINARY_WANT_BINARY_BASETYPES,
PARAM_BINARY_BASETYPES_MAJOR_VERSION,
PARAM_SPOCK_FORWARD_ORIGINS,
PARAM_SPOCK_REPLICATION_SET_NAMES,
PARAM_SPOCK_REPLICATE_ONLY_TABLE,
PARAM_HOOKS_SETUP_FUNCTION,
PARAM_PG_VERSION,
PARAM_NO_TXINFO
} OutputPluginParamKey;
typedef struct {
const char * const paramname;
int paramkey;
} OutputPluginParam;
/* Oh, if only C had switch on strings */
static OutputPluginParam param_lookup[] = {
{"max_proto_version", PARAM_MAX_PROTOCOL_VERSION},
{"min_proto_version", PARAM_MIN_PROTOCOL_VERSION},
{"proto_format", PARAM_PROTOCOL_FORMAT},
{"expected_encoding", PARAM_EXPECTED_ENCODING},
{"binary.bigendian", PARAM_BINARY_BIGENDIAN},
{"binary.sizeof_datum", PARAM_BINARY_SIZEOF_DATUM},
{"binary.sizeof_int", PARAM_BINARY_SIZEOF_INT},
{"binary.sizeof_long", PARAM_BINARY_SIZEOF_LONG},
{"binary.float4_byval", PARAM_BINARY_FLOAT4BYVAL},
{"binary.float8_byval", PARAM_BINARY_FLOAT8BYVAL},
{"binary.integer_datetimes", PARAM_BINARY_INTEGER_DATETIMES},
{"binary.want_internal_basetypes", PARAM_BINARY_WANT_INTERNAL_BASETYPES},
{"binary.want_binary_basetypes", PARAM_BINARY_WANT_BINARY_BASETYPES},
{"binary.basetypes_major_version", PARAM_BINARY_BASETYPES_MAJOR_VERSION},
{"spock.forward_origins", PARAM_SPOCK_FORWARD_ORIGINS},
{"spock.replication_set_names", PARAM_SPOCK_REPLICATION_SET_NAMES},
{"spock.replicate_only_table", PARAM_SPOCK_REPLICATE_ONLY_TABLE},
{"hooks.setup_function", PARAM_HOOKS_SETUP_FUNCTION},
{"pg_version", PARAM_PG_VERSION},
{"no_txinfo", PARAM_NO_TXINFO},
{NULL, PARAM_UNRECOGNISED}
};
/*
* Look up a param name to find the enum value for the
* param, or PARAM_UNRECOGNISED if not found.
*/
static int
get_param_key(const char * const param_name)
{
OutputPluginParam *param = ¶m_lookup[0];
do {
if (strcmp(param->paramname, param_name) == 0)
return param->paramkey;
param++;
} while (param->paramname != NULL);
return PARAM_UNRECOGNISED;
}
void
process_parameters_v1(List *options, SpockOutputData *data)
{
Datum val;
ListCell *lc;
/*
* max_proto_version and min_proto_version are specified
* as required, and must be parsed before anything else.
*
* TODO: We should still parse them as optional and
* delay the ERROR until after the startup reply.
*/
val = get_param(options, "max_proto_version", false, false,
OUTPUT_PARAM_TYPE_UINT32, NULL);
data->client_max_proto_version = DatumGetUInt32(val);
val = get_param(options, "min_proto_version", false, false,
OUTPUT_PARAM_TYPE_UINT32, NULL);
data->client_min_proto_version = DatumGetUInt32(val);
/* Examine all the other params in the v1 message. */
foreach(lc, options)
{
DefElem *elem = lfirst(lc);
Assert(elem->arg == NULL || IsA(elem->arg, String));
/* Check each param, whether or not we recognise it */
switch(get_param_key(elem->defname))
{
case PARAM_BINARY_BIGENDIAN:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_binary_bigendian_set = true;
data->client_binary_bigendian = DatumGetBool(val);
break;
case PARAM_BINARY_SIZEOF_DATUM:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_UINT32);
data->client_binary_sizeofdatum = DatumGetUInt32(val);
break;
case PARAM_BINARY_SIZEOF_INT:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_UINT32);
data->client_binary_sizeofint = DatumGetUInt32(val);
break;
case PARAM_BINARY_SIZEOF_LONG:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_UINT32);
data->client_binary_sizeoflong = DatumGetUInt32(val);
break;
case PARAM_BINARY_FLOAT4BYVAL:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_binary_float4byval_set = true;
data->client_binary_float4byval = DatumGetBool(val);
break;
case PARAM_BINARY_FLOAT8BYVAL:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_binary_float4byval_set = true;
data->client_binary_float4byval = DatumGetBool(val);
break;
case PARAM_BINARY_INTEGER_DATETIMES:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_binary_intdatetimes_set = true;
data->client_binary_intdatetimes = DatumGetBool(val);
break;
case PARAM_PROTOCOL_FORMAT:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_STRING);
data->client_protocol_format = DatumGetCString(val);
break;
case PARAM_EXPECTED_ENCODING:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_STRING);
data->client_expected_encoding = DatumGetCString(val);
break;
case PARAM_PG_VERSION:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_UINT32);
data->client_pg_version = DatumGetUInt32(val);
break;
case PARAM_BINARY_WANT_INTERNAL_BASETYPES:
/* check if we want to use internal data representation */
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_want_internal_basetypes_set = true;
data->client_want_internal_basetypes = DatumGetBool(val);
break;
case PARAM_BINARY_WANT_BINARY_BASETYPES:
/* check if we want to use binary data representation */
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_want_binary_basetypes_set = true;
data->client_want_binary_basetypes = DatumGetBool(val);
break;
case PARAM_BINARY_BASETYPES_MAJOR_VERSION:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_UINT32);
data->client_binary_basetypes_major_version = DatumGetUInt32(val);
break;
case PARAM_SPOCK_FORWARD_ORIGINS:
{
List *forward_origin_names;
ListCell *lc2;
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_STRING);
if (!SplitIdentifierString(DatumGetCString(val), ',', &forward_origin_names))
elog(ERROR, "Could not parse forward origin name list %s", DatumGetCString(val));
foreach (lc2, forward_origin_names)
{
char *origin_name = (char *) lfirst(lc2);
if (strcmp(origin_name, REPLICATION_ORIGIN_ALL) != 0)
elog(ERROR, "Only \"%s\" is allowed in forward origin name list at the moment, found \"%s\"",
REPLICATION_ORIGIN_ALL, origin_name);
}
data->forward_origins = forward_origin_names;
break;
}
case PARAM_SPOCK_REPLICATION_SET_NAMES:
{
List *replication_set_names;
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_STRING);
if (!SplitIdentifierString(strVal(elem->arg), ',', &replication_set_names))
elog(ERROR, "Could not parse replication set name list %s", strVal(elem->arg));
data->replication_sets =
get_replication_sets(data->local_node_id,
replication_set_names, false);
break;
}
case PARAM_SPOCK_REPLICATE_ONLY_TABLE:
{
List *replicate_only_table;
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_STRING);
if (!SplitIdentifierString(strVal(elem->arg), '.', &replicate_only_table))
elog(ERROR, "Could not parse replicate_only_table %s", strVal(elem->arg));
data->replicate_only_table = makeRangeVar(pstrdup(linitial(replicate_only_table)),
pstrdup(lsecond(replicate_only_table)), -1);
break;
}
case PARAM_NO_TXINFO:
val = get_param_value(elem, false, OUTPUT_PARAM_TYPE_BOOL);
data->client_no_txinfo = DatumGetBool(val);
break;
/* Backwards compat. */
case PARAM_HOOKS_SETUP_FUNCTION:
break;
case PARAM_UNRECOGNISED:
ereport(DEBUG1,
(errmsg("Unrecognised spock parameter %s ignored", elem->defname)));
break;
}
}
}
/*
* Read parameters sent by client at startup and store recognised
* ones in the parameters SpockOutputData.
*
* The SpockOutputData must have all client-supplied parameter fields
* zeroed, such as by memset or palloc0, since values not supplied
* by the client are not set.
*/
int
process_parameters(List *options, SpockOutputData *data)
{
Datum val;
int params_format;
val = get_param(options, "startup_params_format", false, false,
OUTPUT_PARAM_TYPE_UINT32, NULL);
params_format = DatumGetUInt32(val);
if (params_format == SPOCK_STARTUP_PARAM_FORMAT_FLAT)
process_parameters_v1(options, data);
else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("startup_params_format %d not supported, only version %d supported",
params_format, SPOCK_STARTUP_PARAM_FORMAT_FLAT)));
return params_format;
}
static Datum
get_param_value(DefElem *elem, bool null_ok, SpockOutputParamType type)
{
/* Check for NULL value */
if (elem->arg == NULL || strVal(elem->arg) == NULL)
{
if (null_ok)
return (Datum) 0;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("parameter \"%s\" cannot be NULL", elem->defname)));
}
switch (type)
{
case OUTPUT_PARAM_TYPE_UINT32:
return UInt32GetDatum(parse_param_uint32(elem));
case OUTPUT_PARAM_TYPE_INT32:
return Int32GetDatum(parse_param_int32(elem));
case OUTPUT_PARAM_TYPE_BOOL:
return BoolGetDatum(parse_param_bool(elem));
case OUTPUT_PARAM_TYPE_STRING:
return PointerGetDatum(pstrdup(strVal(elem->arg)));
case OUTPUT_PARAM_TYPE_QUALIFIED_NAME:
return PointerGetDatum(textToQualifiedNameList(cstring_to_text(pstrdup(strVal(elem->arg)))));
default:
elog(ERROR, "unknown parameter type %d", type);
}
}
/*
* Param parsing
*
* This is not exactly fast but since it's only called on replication start
* we'll leave it for now.
*/
static Datum
get_param(List *options, const char *name, bool missing_ok, bool null_ok,
SpockOutputParamType type, bool *found)
{
ListCell *option;
if (found != NULL)
*found = false;
else
Assert(!missing_ok);
foreach(option, options)
{
DefElem *elem = lfirst(option);
Assert(elem->arg == NULL || IsA(elem->arg, String));
/* Search until matching parameter found */
if (pg_strcasecmp(name, elem->defname))
continue;
if (found != NULL)
*found = true;
return get_param_value(elem, null_ok, type);
}
if (!missing_ok)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("missing required parameter \"%s\"", name)));
return (Datum) 0;
}
static bool
parse_param_bool(DefElem *elem)
{
bool res;
if (!parse_bool(strVal(elem->arg), &res))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse boolean value \"%s\" for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
return res;
}
static uint32
parse_param_uint32(DefElem *elem)
{
int64 res;
char *str;
bool error;
#if PG_VERSION_NUM >= 150000
char *endptr;
#endif
str = strVal(elem->arg);
#if PG_VERSION_NUM >= 150000
res = strtoi64(str, &endptr, 10);
error = (str == endptr || *endptr != '\0');
#else
error = (!scanint8(str, true, &res));
#endif
if (error)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse integer value \"%s\" for parameter \"%s\"",
str, elem->defname)));
if (res > PG_UINT32_MAX || res < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("value \"%s\" out of range for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
return (uint32) res;
}
static int32
parse_param_int32(DefElem *elem)
{
int64 res;
char *str;
bool error;
#if PG_VERSION_NUM >= 150000
char *endptr;
#endif
str = strVal(elem->arg);
#if PG_VERSION_NUM >= 150000
res = strtoi64(str, &endptr, 10);
error = (str == endptr || *endptr != '\0');
#else
error = (!scanint8(str, true, &res));
#endif
if (error)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse integer value \"%s\" for parameter \"%s\"",
str, elem->defname)));
if (res > PG_INT32_MAX || res < PG_INT32_MIN)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("value \"%s\" out of range for parameter \"%s\"",
strVal(elem->arg), elem->defname)));
return (int32) res;
}
static List*
add_startup_msg_s(List *l, char *key, char *val)
{
return lappend(l, makeDefElem(key, (Node*)makeString(val)));
}
static List*
add_startup_msg_i(List *l, char *key, int val)
{
return lappend(l, makeDefElem(key, (Node*)makeString(psprintf("%d", val))));
}
static List*
add_startup_msg_b(List *l, char *key, bool val)
{
return lappend(l, makeDefElem(key, (Node*)makeString(val ? "t" : "f")));
}
/*
* This builds the protocol startup message, which is always the first
* message on the wire after the client sends START_REPLICATION.
*
* It confirms to the client that we could apply requested options, and
* tells the client our capabilities.
*
* Any additional parameters provided by the startup hook are also output
* now.
*
* The output param 'msg' is a null-terminated char* palloc'd in the current
* memory context and the length 'len' of that string that is valid. The caller
* should pfree the result after use.
*
* This is a bit less efficient than direct pq_sendblah calls, but
* separates config handling from the protocol implementation, and
* it's not like startup msg performance matters much.
*/
List *
prepare_startup_message(SpockOutputData *data)
{
List *l = NIL;
l = add_startup_msg_s(l, "max_proto_version", "1");
l = add_startup_msg_s(l, "min_proto_version", "1");
/* We don't support understand column types yet */
l = add_startup_msg_b(l, "coltypes", false);
/* Info about our Pg host */
l = add_startup_msg_i(l, "pg_version_num", PG_VERSION_NUM);
l = add_startup_msg_s(l, "pg_version", PG_VERSION);
l = add_startup_msg_i(l, "pg_catversion", CATALOG_VERSION_NO);
l = add_startup_msg_s(l, "database_encoding", (char*)GetDatabaseEncodingName());
l = add_startup_msg_s(l, "encoding", (char*)pg_encoding_to_char(data->field_datum_encoding));
l = add_startup_msg_b(l, "forward_changeset_origins",
data->forward_changeset_origins);
l = add_startup_msg_i(l, "walsender_pid", MyProcPid);
/* and ourselves */
l = add_startup_msg_s(l, "spock_version",
SPOCK_VERSION);
l = add_startup_msg_i(l, "spock_version_num",
SPOCK_VERSION_NUM);
/* binary options enabled */
l = add_startup_msg_b(l, "binary.internal_basetypes",
data->allow_internal_basetypes);
l = add_startup_msg_b(l, "binary.binary_basetypes",
data->allow_binary_basetypes);
/* Binary format characteristics of server */
l = add_startup_msg_i(l, "binary.basetypes_major_version", PG_VERSION_NUM/100);
l = add_startup_msg_i(l, "binary.sizeof_int", sizeof(int));
l = add_startup_msg_i(l, "binary.sizeof_long", sizeof(long));
l = add_startup_msg_i(l, "binary.sizeof_datum", sizeof(Datum));
l = add_startup_msg_i(l, "binary.maxalign", MAXIMUM_ALIGNOF);
l = add_startup_msg_b(l, "binary.bigendian", server_bigendian());
l = add_startup_msg_b(l, "binary.float4_byval", server_float4_byval());
l = add_startup_msg_b(l, "binary.float8_byval", server_float8_byval());
l = add_startup_msg_b(l, "binary.integer_datetimes", server_integer_datetimes());
/* We don't know how to send in anything except our host's format */
l = add_startup_msg_i(l, "binary.binary_pg_version",
PG_VERSION_NUM/100);
l = add_startup_msg_b(l, "no_txinfo", data->client_no_txinfo);
return l;
}