forked from enova/pgl_ddl_deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgl_ddl_deploy.c
More file actions
146 lines (134 loc) · 4.45 KB
/
Copy pathpgl_ddl_deploy.c
File metadata and controls
146 lines (134 loc) · 4.45 KB
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
#include "postgres.h"
#include "fmgr.h"
#include "catalog/pg_type.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "parser/parser.h"
#include "nodes/nodes.h"
#include "utils/ruleutils.h"
#include "postgres_deparse.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pgl_ddl_deploy_current_query);
PG_FUNCTION_INFO_V1(sql_command_tags);
PG_FUNCTION_INFO_V1(rewrite_transaction_safe);
/* Our own version of debug_query_string - see below */
const char *pgl_ddl_deploy_debug_query_string;
/*
* A near-copy of the current_query postgres function which caches the value, ignoring
* the change if the string is changed to NULL, as is being done in pglogical 2.2.2.
* This allows multiple subsequent calls to pglogical.replicate_ddl_command without
* losing access to current_query.
*
* Please revisit if pglogical changes behavior and stop setting debug_query_string to NULL.
*/
Datum
pgl_ddl_deploy_current_query(PG_FUNCTION_ARGS)
{
/* If debug_query_string is set, we always want the same value */
if (debug_query_string)
{
pgl_ddl_deploy_debug_query_string = debug_query_string;
PG_RETURN_TEXT_P(cstring_to_text(pgl_ddl_deploy_debug_query_string));
}
/* If it is NULL, we want to take pgl_ddl_deploy_debug_query_string instead,
which in most cases in this code path is used in pgl_ddl_deploy we expect
is because pglogical has reset the string to null. But we still want to
return the same value in this SQL statement we are executing.
*/
else if (pgl_ddl_deploy_debug_query_string)
{
PG_RETURN_TEXT_P(cstring_to_text(pgl_ddl_deploy_debug_query_string));
}
else
/* If both are NULL, that is legit and we want to return NULL. */
{
PG_RETURN_NULL();
}
}
/*
* Return a text array of the command tags in SQL command
*/
Datum
sql_command_tags(PG_FUNCTION_ARGS)
{
text *sql_t = PG_GETARG_TEXT_P(0);
char *sql;
List *parsetree_list;
ListCell *parsetree_item;
const char *commandTag;
ArrayBuildState *astate = NULL;
/*
* Get the SQL parsetree
*/
sql = text_to_cstring(sql_t);
parsetree_list = pg_parse_query(sql);
/*
* Iterate through each parsetree_item to get CommandTag
*/
foreach(parsetree_item, parsetree_list)
{
Node *parsetree = (Node *) lfirst(parsetree_item);
#if PG_VERSION_NUM >= 130000
commandTag = CreateCommandName(parsetree);
#else
commandTag = CreateCommandTag(parsetree);
#endif
astate = accumArrayResult(astate, CStringGetTextDatum(commandTag),
false, TEXTOID, CurrentMemoryContext);
}
if (astate == NULL)
elog(ERROR, "Invalid sql command");
PG_RETURN_ARRAYTYPE_P(DatumGetPointer(makeArrayResult(astate, CurrentMemoryContext)));
}
Datum
rewrite_transaction_safe(PG_FUNCTION_ARGS)
{
text *sql_t = PG_GETARG_TEXT_P(0);
char *sql;
List *parsetree_list;
ListCell *parsetree_item;
StringInfoData str;
text *result;
bool isFirst = true;
initStringInfo(&str);
/*
* Get the SQL parsetree
*/
sql = text_to_cstring(sql_t);
parsetree_list = pg_parse_query(sql);
/*
* Iterate through each parsetree_item to get CommandTag
*/
foreach(parsetree_item, parsetree_list)
{
RawStmt *parsetree = (RawStmt *) lfirst(parsetree_item);
bool shouldEmit = true;
if(IsA(parsetree->stmt, TransactionStmt)) {
TransactionStmtKind kind = ((TransactionStmt *) parsetree->stmt)->kind;
shouldEmit = kind != TRANS_STMT_BEGIN && kind != TRANS_STMT_START && kind != TRANS_STMT_COMMIT;
}
if(IsA(parsetree->stmt, IndexStmt)) {
IndexStmt *indexStmt = (IndexStmt *) parsetree->stmt;
indexStmt->concurrent = false;
}
if(IsA(parsetree->stmt, DropStmt)) {
DropStmt *dropStmt = (DropStmt *) parsetree->stmt;
dropStmt->concurrent = false;
}
if(shouldEmit) {
if(!isFirst)
{
appendStringInfoChar(&str, ' ');
}
else
{
isFirst = false;
}
deparseRawStmt(&str, parsetree);
appendStringInfoChar(&str, ';');
}
}
result = cstring_to_text(str.data);
pfree(str.data);
PG_RETURN_TEXT_P(result);
}