-
Notifications
You must be signed in to change notification settings - Fork 1
/
pglog.c
363 lines (314 loc) · 9.86 KB
/
pglog.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
/*-------------------------------------------------------------------------
*
* pglog.c
* PostgreSQL log via SQL
*
* Copyright (c) 2014, 2ndQuadrant Ltd
* Some parts Copyright (c) 2010-2013, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglog/pglog.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "pglog_helpers.h"
#include "pglog_spool.h"
#include "access/sysattr.h"
#include "catalog/pg_foreign_table.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "postmaster/syslogger.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/rel.h"
PG_MODULE_MAGIC;
/*
* SQL functions
*/
extern Datum pglog_handler(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pglog_handler);
/*
* Module load and unload functions
*/
void _PG_init(void);
void _PG_fini(void);
/*
* FDW callback routines
*/
static void pglogGetForeignRelSize(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid);
static void pglogGetForeignPaths(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid);
static ForeignScan *pglogGetForeignPlan(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
List *scan_clauses);
static void pglogBeginForeignScan(ForeignScanState *node, int eflags);
static TupleTableSlot *pglogIterateForeignScan(ForeignScanState *node);
static void pglogReScanForeignScan(ForeignScanState *node);
static void pglogEndForeignScan(ForeignScanState *node);
/*
* Foreign-data wrapper handler function: return a struct with pointers
* to my callback routines.
*/
Datum
pglog_handler(PG_FUNCTION_ARGS)
{
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
elog(DEBUG1,"Entering function %s",__func__);
/* Check if logging_collector is on and CSV is enabled */
if (! Pglog_directory || strlen(Pglog_directory) == 0 || ! Pglog_spooling_enabled)
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_HANDLE),
errmsg("Cannot instantiate the 'pglog' extension handler"),
errhint("'pglog' requires you to set 'pglog.directory' to the path of a writable directory")
));
/* Set handlers */
fdwroutine->GetForeignRelSize = pglogGetForeignRelSize;
fdwroutine->GetForeignPaths = pglogGetForeignPaths;
fdwroutine->GetForeignPlan = pglogGetForeignPlan;
fdwroutine->BeginForeignScan = pglogBeginForeignScan;
fdwroutine->IterateForeignScan = pglogIterateForeignScan;
fdwroutine->ReScanForeignScan = pglogReScanForeignScan;
fdwroutine->EndForeignScan = pglogEndForeignScan;
PG_RETURN_POINTER(fdwroutine);
}
/*
* pglogGetForeignRelSize
* Obtain relation size estimates for a foreign table
*/
static void
pglogGetForeignRelSize(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid)
{
PgLogPlanState *fdw_private;
elog(DEBUG1,"Entering function %s",__func__);
/*
* Fetch options. We only need filenames at this point, but we might as
* well get everything and not need to re-fetch it later in planning.
*/
fdw_private = (PgLogPlanState *) palloc(sizeof(PgLogPlanState));
fdw_private->i = 0;
fdw_private->filenames = initLogFileNames(Pglog_directory);
baserel->fdw_private = (void *) fdw_private;
/* Estimate relation size */
estimate_size(root, baserel, fdw_private);
}
/*
* pglogGetForeignPaths
* Create possible access paths for a scan on the foreign table
*
* Currently we don't support any push-down feature, so there is only one
* possible access path, which simply returns all records in the order in
* the data file.
*/
static void
pglogGetForeignPaths(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid)
{
PgLogPlanState *fdw_private = (PgLogPlanState *) baserel->fdw_private;
Cost startup_cost;
Cost total_cost;
List *columns;
List *coptions = NIL;
elog(DEBUG1,"Entering function %s",__func__);
/* Decide whether to selectively perform binary conversion */
if (check_selective_binary_conversion(baserel,
foreigntableid,
&columns))
coptions = list_make1(makeDefElem("convert_selectively",
(Node *) columns));
/* Estimate costs */
estimate_costs(root, baserel, fdw_private,
&startup_cost, &total_cost);
/*
* Create a ForeignPath node and add it as only possible path. We use the
* fdw_private list of the path to carry the convert_selectively option;
* it will be propagated into the fdw_private list of the Plan node.
*/
add_path(baserel, (Path *)
create_foreignscan_path(root, baserel,
baserel->rows,
startup_cost,
total_cost,
NIL, /* no pathkeys */
NULL, /* no outer rel either */
coptions));
/*
* If data file was sorted, and we knew it somehow, we could insert
* appropriate pathkeys into the ForeignPath node to tell the planner
* that.
*/
}
/*
* pglogGetForeignPlan
* Create a ForeignScan plan node for scanning the foreign table
*/
static ForeignScan *
pglogGetForeignPlan(PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
List *scan_clauses)
{
Index scan_relid = baserel->relid;
elog(DEBUG1,"Entering function %s",__func__);
/*
* We have no native ability to evaluate restriction clauses, so we just
* put all the scan_clauses into the plan node's qual list for the
* executor to check. So all we have to do here is strip RestrictInfo
* nodes from the clauses and ignore pseudoconstants (which will be
* handled elsewhere).
*/
scan_clauses = extract_actual_clauses(scan_clauses, false);
/* Create the ForeignScan node */
return make_foreignscan(tlist,
scan_clauses,
scan_relid,
NIL, /* no expressions to evaluate */
best_path->fdw_private);
}
/*
* pglogBeginForeignScan
* Initiate access to the log by creating CopyState
*/
static void
pglogBeginForeignScan(ForeignScanState *node, int eflags)
{
ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
PgLogExecutionState *festate;
elog(DEBUG1,"Entering function %s",__func__);
/*
* Do nothing in EXPLAIN (no ANALYZE) case. node->fdw_state stays NULL.
*/
if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
return;
/* Initialise the execution state */
festate = (PgLogExecutionState *) palloc(sizeof(PgLogExecutionState));
festate->filenames = initLogFileNames(Pglog_directory);
festate->i = 0;
/* Forces CSV format */
festate->options = list_make1(makeDefElem("format", (Node *) makeString("csv")));
/* Add any options from the plan (currently only convert_selectively) */
festate->options = list_concat(festate->options, plan->fdw_private);
/* Remember scan memory context */
festate->scan_cxt = CurrentMemoryContext;
/*
* Create CopyState from FDW options. We always acquire all columns, so
* as to match the expected ScanTupleSlot signature.
* Starts from the first file in the list.
*/
festate->cstate = NULL;
BeginNextCopy(node->ss.ss_currentRelation, festate);
elog(DEBUG1,"Copy state %p",festate->cstate);
/*
* Save state in node->fdw_state. We must save enough information to call
* BeginCopyFrom() again.
*/
node->fdw_state = (void *) festate;
}
/*
* pglogIterateForeignScan
* Read next record from the data file and store it into the
* ScanTupleSlot as a virtual tuple
*/
static TupleTableSlot *
pglogIterateForeignScan(ForeignScanState *node)
{
PgLogExecutionState *festate = (PgLogExecutionState *) node->fdw_state;
TupleTableSlot *slot = node->ss.ss_ScanTupleSlot;
bool found;
ErrorContextCallback errcallback;
/* Set up callback to identify error line number. */
errcallback.callback = CopyFromErrorCallback;
errcallback.arg = (void *) festate->cstate;
errcallback.previous = error_context_stack;
error_context_stack = &errcallback;
/*
* The protocol for loading a virtual tuple into a slot is first
* ExecClearTuple, then fill the values/isnull arrays, then
* ExecStoreVirtualTuple. If we don't find another row in the file, we
* just skip the last step, leaving the slot empty as required.
*
* We can pass ExprContext = NULL because we read all columns from the
* file, so no need to evaluate default expressions.
*
* We can also pass tupleOid = NULL because we don't allow oids for
* foreign tables.
*/
ExecClearTuple(slot);
found = GetNextRow(node->ss.ss_currentRelation, festate, slot);
if (found)
ExecStoreVirtualTuple(slot);
else if (! isLastLogFile(festate))
{
/* We could have reached the end of a log file
* We might have to start reading from the next
*/
elog(DEBUG1,"Reached end of file %s",festate->filenames[festate->i]);
festate->i++;
BeginNextCopy(node->ss.ss_currentRelation, festate);
found = GetNextRow(node->ss.ss_currentRelation, festate, slot);
if (found)
ExecStoreVirtualTuple(slot);
}
/* Remove error callback. */
error_context_stack = errcallback.previous;
return slot;
}
/*
* pglogReScanForeignScan
* Rescan table, possibly with new parameters
*/
static void
pglogReScanForeignScan(ForeignScanState *node)
{
PgLogExecutionState *festate = (PgLogExecutionState *) node->fdw_state;
elog(DEBUG1,"Entering function %s",__func__);
/* Restart reading from the beginning (first file) */
festate->i = 0;
BeginNextCopy(node->ss.ss_currentRelation, festate);
}
/*
* pglogEndForeignScan
* Finish scanning foreign table and dispose objects used for this scan
*/
static void
pglogEndForeignScan(ForeignScanState *node)
{
PgLogExecutionState *festate = (PgLogExecutionState *) node->fdw_state;
elog(DEBUG1,"Entering function %s",__func__);
/* if festate is NULL, we are in EXPLAIN; nothing to do */
if (festate)
EndCopyFrom(festate->cstate);
}
/*
* Module Load Callback
*/
void
_PG_init(void)
{
pglog_spool_init();
EmitWarningsOnPlaceholders("pglog");
}
/*
* Module Unload Callback
*/
void
_PG_fini(void)
{
pglog_spool_fini();
}