-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpg_zlog.c
831 lines (692 loc) · 19 KB
/
pg_zlog.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
#include "postgres.h"
#include "fmgr.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/skey.h"
#include "access/stratnum.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/extension.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/planner.h"
#include "parser/parse_func.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/tqual.h"
#include "pg_zlog.h"
#include <zlog/capi.h>
/* declarations for dynamic loading */
PG_MODULE_MAGIC;
/* whether writes go through zlog */
static bool ZLogEnabled = true;
/* whether to allow mutable functions */
static bool AllowMutableFunctions = false;
/* restore these hooks on unload */
static planner_hook_type PreviousPlannerHook = NULL;
static ExecutorStart_hook_type PreviousExecutorStartHook = NULL;
static ProcessUtility_hook_type PreviousProcessUtilityHook = NULL;
/*
* TODO:
* - what is the purpose of checking things like the extension being
* present? the only thing I can think of is that there may be a race
* condition with loading or unloading the extension.
*/
static int IsPgZLogActive(void)
{
Oid oid;
Oid namespace_oid;
if (!ZLogEnabled)
{
return false;
}
if (!IsTransactionState())
{
return false;
}
oid = get_extension_oid(PG_ZLOG_EXTENSION_NAME, true);
if (oid == InvalidOid)
{
return false;
}
namespace_oid = get_namespace_oid(PG_ZLOG_METADATA_SCHEMA_NAME, true);
if (namespace_oid == InvalidOid)
{
return false;
}
oid = get_relname_relid(PG_ZLOG_REPLICATED_TABLE_NAME, namespace_oid);
if (oid == InvalidOid)
{
return false;
}
return true;
}
/*
*
*/
static bool IsZLogTable(Oid tableOid)
{
bool isZLogTable;
RangeVar *heapRangeVar;
Relation heapRelation;
HeapScanDesc scanDesc;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple;
if (tableOid == InvalidOid)
return false;
heapRangeVar = makeRangeVar(PG_ZLOG_METADATA_SCHEMA_NAME,
PG_ZLOG_REPLICATED_TABLE_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], ATTR_NUM_REPLICATED_TABLES_RELATION_ID,
InvalidStrategy, F_OIDEQ, ObjectIdGetDatum(tableOid));
scanDesc = heap_beginscan(heapRelation, SnapshotSelf, scanKeyCount, scanKey);
heapTuple = heap_getnext(scanDesc, ForwardScanDirection);
isZLogTable = HeapTupleIsValid(heapTuple);
heap_endscan(scanDesc);
relation_close(heapRelation, AccessShareLock);
return isZLogTable;
}
static Oid ExtractTableOid(Node *node)
{
Oid tableOid = InvalidOid;
NodeTag nodeType = nodeTag(node);
if (nodeType == T_RangeTblEntry)
{
RangeTblEntry *rangeTableEntry = (RangeTblEntry *) node;
tableOid = rangeTableEntry->relid;
}
else if (nodeType == T_RangeVar)
{
RangeVar *rangeVar = (RangeVar *) node;
tableOid = RangeVarGetRelid(rangeVar, NoLock, true);
}
return tableOid;
}
/*
* Does the range contain a ZLog replicated table
*/
static bool HasZLogTable(List *rangeTableList)
{
ListCell *rangeTableCell = NULL;
foreach(rangeTableCell, rangeTableList)
{
Oid rangeTableOid = ExtractTableOid((Node *) lfirst(rangeTableCell));
if (IsZLogTable(rangeTableOid))
{
return true;
}
}
return false;
}
/*
*
*/
static char *ZLogTableLog(Oid zlogTableOid)
{
char *logName;
RangeVar *heapRangeVar;
Relation heapRelation;
HeapScanDesc scanDesc;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple;
heapRangeVar = makeRangeVar(PG_ZLOG_METADATA_SCHEMA_NAME,
PG_ZLOG_REPLICATED_TABLE_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], ATTR_NUM_REPLICATED_TABLES_RELATION_ID, InvalidStrategy,
F_OIDEQ, ObjectIdGetDatum(zlogTableOid));
scanDesc = heap_beginscan(heapRelation, SnapshotSelf, scanKeyCount, scanKey);
heapTuple = heap_getnext(scanDesc, ForwardScanDirection);
if (HeapTupleIsValid(heapTuple))
{
TupleDesc tupleDescriptor = RelationGetDescr(heapRelation);
bool isNull = false;
Datum logNameDatum = heap_getattr(heapTuple,
ATTR_NUM_REPLICATED_TABLES_LOG, tupleDescriptor, &isNull);
logName = TextDatumGetCString(logNameDatum);
}
else
{
logName = NULL;
}
heap_endscan(scanDesc);
relation_close(heapRelation, AccessShareLock);
return logName;
}
/*
*
*/
static char *GetLogName(List *rangeTableList)
{
ListCell *rangeTableCell;
char *queryLogName = NULL;
foreach(rangeTableCell, rangeTableList)
{
char *tableLogName;
Oid rangeTableOid = ExtractTableOid((Node*)lfirst(rangeTableCell));
if (rangeTableOid == InvalidOid)
{
continue;
}
tableLogName = ZLogTableLog(rangeTableOid);
if (tableLogName == NULL)
{
char *relationName = get_rel_name(rangeTableOid);
ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("relation \"%s\" is not managed by pg_zlog", relationName)));
}
else if (queryLogName == NULL)
{
queryLogName = tableLogName;
}
else
{
int compare = strncmp(tableLogName, queryLogName, MAX_ZLOG_LOG_NAME_LENGTH);
if (compare)
{
ereport(ERROR, (errmsg("cannot run queries spanning more than a single log.")));
}
}
}
return queryLogName;
}
static void
ZLogSetApplied(const char *logName, int64 pos)
{
Oid argTypes[] = {
TEXTOID,
INT8OID
};
Datum argValues[] = {
CStringGetTextDatum(logName),
Int64GetDatum(pos)
};
SPI_connect();
SPI_execute_with_args(
"UPDATE pgzlog_metadata.log SET last_applied_pos = $2 WHERE name = $1",
2, argTypes, argValues, NULL, false, 1);
SPI_finish();
}
/*
*
*/
PG_FUNCTION_INFO_V1(pgzlog_read_log);
Datum
pgzlog_read_log(PG_FUNCTION_ARGS)
{
char readQuery[4096];
const char *log_name;
ZLogConn *conn;
int64 pos;
int ret;
log_name = text_to_cstring(PG_GETARG_TEXT_P(0));
pos = PG_GETARG_INT64(1);
conn = GetConnection(log_name);
MemSet(readQuery, 0, sizeof(readQuery));
ret = zlog_read(conn->log, pos, readQuery, sizeof(readQuery)-1);
if (ret < 0)
{
ereport(ERROR, (errmsg("pg_zlog cannot read log: %d", ret)));
}
PG_RETURN_TEXT_P(cstring_to_text(readQuery));
}
static void
ApplyLogEntries(const char *log_name, int64 tail)
{
Oid argTypes[] = {TEXTOID, INT8OID};
Datum argValues[] = {
CStringGetTextDatum(log_name),
Int64GetDatum(tail)
};
SPI_connect();
SPI_execute_with_args("SELECT pgzlog_apply_log($1,$2)",
2, argTypes, argValues, NULL, false, 1);
SPI_finish();
}
/*
* TODO:
* - support optimistic consistency mode (read-your-own-writes)
*/
static void PrepareConsistentRead(const char *logName)
{
uint64_t tail;
int ret;
ZLogConn *conn;
conn = GetConnection(logName);
ret = zlog_checktail(conn->log, &tail);
if (ret)
{
ereport(ERROR, (errmsg("pg_zlog could not check tail: %d", ret)));
}
ApplyLogEntries(logName, (int64)tail);
CommandCounterIncrement();
}
static void PrepareConsistentWrite(const char *logName, const char *queryString)
{
int ret;
uint64_t appended_pos;
ZLogConn *conn;
conn = GetConnection(logName);
ret = zlog_append(conn->log, (void*)queryString,
strlen(queryString)+1, &appended_pos);
if (ret)
{
ereport(ERROR, (errmsg("pg_zlog could not append to log: %d", ret)));
}
ApplyLogEntries(logName, (int64)appended_pos);
CommandCounterIncrement();
ZLogSetApplied(logName, (int64)appended_pos);
CommandCounterIncrement();
}
/*
* Intercept utility statements.
*/
static void PgZLogProcessUtility(Node *parsetree, const char *queryString,
ProcessUtilityContext context, ParamListInfo params,
DestReceiver *dest, char *completionTag)
{
if (IsPgZLogActive())
{
NodeTag statementType = nodeTag(parsetree);
if (statementType == T_TruncateStmt)
{
TruncateStmt *truncateStatement = (TruncateStmt *) parsetree;
List *relations = truncateStatement->relations;
if (HasZLogTable(relations))
{
char *logName = GetLogName(relations);
PrepareConsistentWrite(logName, queryString);
}
}
else if (statementType == T_IndexStmt)
{
IndexStmt *indexStatement = (IndexStmt *) parsetree;
Oid tableOid = ExtractTableOid((Node *) indexStatement->relation);
if (IsZLogTable(tableOid))
{
char *logName = ZLogTableLog(tableOid);
PrepareConsistentWrite(logName, queryString);
}
}
else if (statementType == T_AlterTableStmt)
{
AlterTableStmt *alterStatement = (AlterTableStmt *) parsetree;
Oid tableOid = ExtractTableOid((Node *) alterStatement->relation);
if (IsZLogTable(tableOid))
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("ALTER TABLE commands on zlog tables are unsupported")));
}
}
else if (statementType == T_CopyStmt)
{
CopyStmt *copyStatement = (CopyStmt *) parsetree;
RangeVar *relation = copyStatement->relation;
Node *rawQuery = copyObject(copyStatement->query);
if (relation)
{
Oid tableOid = ExtractTableOid((Node *) relation);
if (IsZLogTable(tableOid))
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("COPY commands on zlog tables are unsupported")));
}
}
else if (rawQuery)
{
Query *parsedQuery = NULL;
List *queryList = pg_analyze_and_rewrite(rawQuery,
queryString, NULL, 0);
if (list_length(queryList) != 1)
{
ereport(ERROR, (errmsg("unexpected rewrite result")));
}
parsedQuery = (Query *) linitial(queryList);
if (HasZLogTable(parsedQuery->rtable))
{
char *logName = GetLogName(parsedQuery->rtable);
PrepareConsistentRead(logName);
}
}
}
}
if (PreviousProcessUtilityHook)
{
PreviousProcessUtilityHook(parsetree, queryString, context,
params, dest, completionTag);
}
else
{
standard_ProcessUtility(parsetree, queryString, context,
params, dest, completionTag);
}
}
/*
* ErrorIfQueryNotSupported checks if the query contains unsupported features,
* and errors out if it does.
*/
static void
ErrorIfQueryNotSupported(Query *queryTree)
{
if (!AllowMutableFunctions && contain_mutable_functions((Node *) queryTree))
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("queries containing mutable functions "
"may load to inconsistencies"),
errhint("To allow mutable functions, set "
"pg_zlog.allow_mutable_functions to on")));
}
}
/*
* FidModificationQuery walks a query tree to find a modification query.
*/
static bool
FindModificationQueryWalker(Node *node, bool *isModificationQuery)
{
bool walkIsComplete;
if (node == NULL)
{
return false;
}
if (IsA(node, Query))
{
Query *query = (Query *) node;
CmdType commandType = query->commandType;
if (commandType == CMD_INSERT || commandType == CMD_UPDATE ||
commandType == CMD_DELETE)
{
*isModificationQuery = true;
walkIsComplete = true;
}
else
{
walkIsComplete = query_tree_walker(query,
FindModificationQueryWalker, isModificationQuery, 0);
}
}
else
{
walkIsComplete = expression_tree_walker(node,
FindModificationQueryWalker, isModificationQuery);
}
return walkIsComplete;
}
/*
* ExtractRangeTableEntryWalker walks over a query tree, and finds all range
* table entries. For recursing into the query tree, this function uses the
* query tree walker since the expression tree walker doesn't recurse into
* sub-queries.
*/
static bool
ExtractRangeTableEntryWalker(Node *node, List **rangeTableList)
{
bool walkIsComplete = false;
if (node == NULL)
{
return false;
}
if (IsA(node, RangeTblEntry))
{
RangeTblEntry *rangeTable = (RangeTblEntry *) node;
(*rangeTableList) = lappend(*rangeTableList, rangeTable);
}
else if (IsA(node, Query))
{
walkIsComplete = query_tree_walker((Query *) node,
ExtractRangeTableEntryWalker, rangeTableList, QTW_EXAMINE_RTES);
}
else
{
walkIsComplete = expression_tree_walker(node,
ExtractRangeTableEntryWalker, rangeTableList);
}
return walkIsComplete;
}
/*
* ZLogExecuteFuncId returns the OID of the zlog_execute function.
*/
static Oid
ZLogExecuteFuncId(void)
{
static Oid cachedOid = 0;
List *nameList = NIL;
Oid paramOids[1] = { INTERNALOID };
if (cachedOid == InvalidOid)
{
nameList = list_make2(makeString("public"),
makeString("zlog_execute"));
cachedOid = LookupFuncName(nameList, 1, paramOids, false);
}
return cachedOid;
}
/*
* CreateZLogExecutePlan creates a plan to call zlog_execute(queryString),
* which is intercepted by the executor hook, which logs the query and executes
* it using the regular planner.
*/
static Plan *
CreateZLogExecutePlan(char *queryString)
{
FunctionScan *execFunctionScan;
RangeTblFunction *execFunction;
FuncExpr *execFuncExpr;
Const *queryData;
/* store the query string as a cstring */
queryData = makeNode(Const);
queryData->consttype = CSTRINGOID;
queryData->constlen = -2;
queryData->constvalue = CStringGetDatum(queryString);
queryData->constbyval = false;
queryData->constisnull = queryString == NULL;
queryData->location = -1;
execFuncExpr = makeNode(FuncExpr);
execFuncExpr->funcid = ZLogExecuteFuncId();
execFuncExpr->funcretset = true;
execFuncExpr->funcresulttype = VOIDOID;
execFuncExpr->location = -1;
execFuncExpr->args = list_make1(queryData);
execFunction = makeNode(RangeTblFunction);
execFunction->funcexpr = (Node *) execFuncExpr;
execFunctionScan = makeNode(FunctionScan);
execFunctionScan->functions = lappend(execFunctionScan->functions, execFunction);
return (Plan *) execFunctionScan;
}
static PlannedStmt *
NextPlannerHook(Query *query, int cursorOptions, ParamListInfo boundParams)
{
if (PreviousPlannerHook != NULL)
{
return PreviousPlannerHook(query, cursorOptions, boundParams);
}
else
{
return standard_planner(query, cursorOptions, boundParams);
}
}
static PlannedStmt *
PgZLogPlanner(Query *query, int cursorOptions, ParamListInfo boundParams)
{
bool isModificationQuery;
List *rangeTableList = NIL;
if (!IsPgZLogActive())
{
return NextPlannerHook(query, cursorOptions, boundParams);
}
FindModificationQueryWalker((Node *) query, &isModificationQuery);
if (!isModificationQuery)
{
return NextPlannerHook(query, cursorOptions, boundParams);
}
ExtractRangeTableEntryWalker((Node *) query, &rangeTableList);
if (HasZLogTable(rangeTableList))
{
PlannedStmt *plannedStatement;
Plan *paxosExecutePlan;
/* Build the DML query to log */
Query *paxosQuery = copyObject(query);
StringInfo queryString = makeStringInfo();
/* call standard planner first to have Query transformations performed */
plannedStatement = standard_planner(paxosQuery, cursorOptions, boundParams);
ErrorIfQueryNotSupported(paxosQuery);
/* get the transformed query string */
deparse_query(paxosQuery, queryString);
/* define the plan as a call to paxos_execute(queryString) */
paxosExecutePlan = CreateZLogExecutePlan(queryString->data);
/* PortalStart will use the targetlist from the plan */
paxosExecutePlan->targetlist = plannedStatement->planTree->targetlist;
plannedStatement->planTree = paxosExecutePlan;
return plannedStatement;
}
else
{
return NextPlannerHook(query, cursorOptions, boundParams);
}
}
/*
* GetZLogQueryString returns NULL if the plan is not a zlog execute plan,
* or the original query string.
*/
static char *
GetZLogQueryString(PlannedStmt *plan)
{
FunctionScan *execFunctionScan;
RangeTblFunction *execFunction;
FuncExpr *execFuncExpr;
Const *queryData;
char *queryString;
if (!IsA(plan->planTree, FunctionScan))
{
return NULL;
}
execFunctionScan = (FunctionScan *) plan->planTree;
if (list_length(execFunctionScan->functions) != 1)
{
return NULL;
}
execFunction = linitial(execFunctionScan->functions);
if (!IsA(execFunction->funcexpr, FuncExpr))
{
return NULL;
}
execFuncExpr = (FuncExpr *) execFunction->funcexpr;
if (execFuncExpr->funcid != ZLogExecuteFuncId())
{
return NULL;
}
if (list_length(execFuncExpr->args) != 1)
{
ereport(ERROR, (errmsg("unexpected number of function "
"arguments to zlog_execute")));
}
queryData = (Const *) linitial(execFuncExpr->args);
Assert(IsA(queryData, Const));
Assert(queryData->consttype == CSTRINGOID);
queryString = DatumGetCString(queryData->constvalue);
return queryString;
}
static void
NextExecutorStartHook(QueryDesc *queryDesc, int eflags)
{
/* call into the standard executor start, or hook if set */
if (PreviousExecutorStartHook != NULL)
{
PreviousExecutorStartHook(queryDesc, eflags);
}
else
{
standard_ExecutorStart(queryDesc, eflags);
}
}
static void
PgZLogExecutorStart(QueryDesc *queryDesc, int eflags)
{
PlannedStmt *plannedStmt = queryDesc->plannedstmt;
List *rangeTableList = plannedStmt->rtable;
char *paxosQueryString;
if (!IsPgZLogActive() || (eflags & EXEC_FLAG_EXPLAIN_ONLY) != 0)
{
NextExecutorStartHook(queryDesc, eflags);
return;
}
paxosQueryString = GetZLogQueryString(plannedStmt);
if (paxosQueryString != NULL)
{
/* paxos write query */
char *logName;
const bool isTopLevel = true;
List *parseTreeList;
Node *parseTreeNode;
List *queryTreeList;
Query *query;
/* disallow transactions during paxos commands */
PreventTransactionChain(isTopLevel, "zlog commands");
logName = GetLogName(rangeTableList);
PrepareConsistentWrite(logName, paxosQueryString);
queryDesc->snapshot->curcid = GetCurrentCommandId(false);
elog(DEBUG1, "Executing: %s %d", paxosQueryString, plannedStmt->hasReturning);
/* replan the query */
parseTreeList = pg_parse_query(paxosQueryString);
if (list_length(parseTreeList) != 1)
{
ereport(ERROR, (errmsg("can only execute single-statement queries on "
"replicated tables")));
}
parseTreeNode = (Node *) linitial(parseTreeList);
queryTreeList = pg_analyze_and_rewrite(parseTreeNode, paxosQueryString, NULL, 0);
query = (Query *) linitial(queryTreeList);
queryDesc->plannedstmt = pg_plan_query(query, 0, queryDesc->params);
}
else if (HasZLogTable(rangeTableList))
{
/* paxos read query */
char *logName;
logName = GetLogName(rangeTableList);
PrepareConsistentRead(logName);
queryDesc->snapshot->curcid = GetCurrentCommandId(false);
}
NextExecutorStartHook(queryDesc, eflags);
}
/*
* zlog_execute is a placeholder function to store a query string in
* in plain postgres node trees.
*/
PG_FUNCTION_INFO_V1(zlog_execute);
Datum
zlog_execute(PG_FUNCTION_ARGS)
{
PG_RETURN_NULL();
}
void _PG_init(void);
void _PG_init(void)
{
DefineCustomBoolVariable("pg_zlog.enabled",
"If enabled, pg_zlog handles queries on zlog tables",
NULL, &ZLogEnabled, true, PGC_USERSET,
GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE, NULL, NULL, NULL);
DefineCustomBoolVariable("pg_zlog.allow_mutable_functions",
"If enabled, mutable functions in queries are allowed",
NULL, &AllowMutableFunctions, false, PGC_USERSET,
0, NULL, NULL, NULL);
PreviousPlannerHook = planner_hook;
planner_hook = PgZLogPlanner;
PreviousExecutorStartHook = ExecutorStart_hook;
ExecutorStart_hook = PgZLogExecutorStart;
PreviousProcessUtilityHook = ProcessUtility_hook;
ProcessUtility_hook = PgZLogProcessUtility;
}
void _PG_fini(void);
void _PG_fini(void)
{
ProcessUtility_hook = PreviousProcessUtilityHook;
ExecutorStart_hook = PreviousExecutorStartHook;
planner_hook = PreviousPlannerHook;
}