-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgo_fdw.go
379 lines (326 loc) · 8.92 KB
/
go_fdw.go
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
package main
//#cgo CFLAGS: -I/usr/include/postgresql/9.6/server -I/usr/include/postgresql/internal
//// ignoring unresolved symbols means all postgres functions can be dynamically
//// linked in when the extension is run
//#cgo LDFLAGS: -Wl,-unresolved-symbols=ignore-all
//#include "postgres.h"
//#include "funcapi.h"
//#include "catalog/pg_type.h"
//#include "commands/defrem.h"
//#include "foreign/fdwapi.h"
//#include "foreign/foreign.h"
//#include "nodes/extensible.h"
//#include "optimizer/pathnode.h"
//#include "utils/tuplestore.h"
//
//typedef struct GoFdwExecutionState
//{
// uint tok;
//} GoFdwExecutionState;
//
//static inline GoFdwExecutionState* makeState(){
// GoFdwExecutionState *s = (GoFdwExecutionState *) malloc(sizeof(GoFdwExecutionState));
// return s;
//}
//
//static inline DefElem* cellGetDef(ListCell *n) { return (DefElem*)n->data.ptr_value; }
//
//static inline void freeState(GoFdwExecutionState * s){ if (s) free(s); }
import "C"
import (
"bytes"
"fmt"
"sync"
"unsafe"
)
var table Table
// SetTable sets a Go objects that will receive all FDW requests.
// Should be called with user-defined implementation in init().
func SetTable(t Table) { table = t }
// Explainer is an helper build an EXPLAIN response.
type Explainer struct {
es *C.ExplainState
}
// Property adds a key-value property to results of EXPLAIN query.
func (e Explainer) Property(k, v string) {
C.ExplainPropertyText(C.CString(k), C.CString(v), e.es)
}
// Options is a set of FDW options provided by user during table creation.
type Options map[string]string
// Table is a main interface for FDW table.
//
// If there are multiple tables created with this module, they can be identified by table options.
type Table interface {
// Stats returns stats for a table.
Stats(opts Options) TableStats
// Scan starts a new scan of the table.
// Iterator should not load data instantly, since Scan will be called for EXPLAIN as well.
// Results should be fetched during Next calls.
Scan(rel *Relation, opts Options) Iterator
}
// Iterator is an interface for table scanner implementations.
type Iterator interface {
// Next returns next row (tuple). Nil slice means there is no more rows to scan.
Next() []interface{}
// Reset restarts an iterator from the beginning (possible with a new data snapshot).
Reset()
// Close stops an iteration and frees any resources.
Close() error
}
// Explainable is an optional interface for Iterator that can explain it's execution plan.
type Explainable interface {
// Explain is called during EXPLAIN query.
Explain(e Explainer)
}
type Relation struct {
ID Oid
IsValid bool
Attr *TupleDesc
}
type TupleDesc struct {
TypeID Oid
TypeMod int
HasOid bool
Attrs []Attr // columns
}
type Attr struct {
Name string
Type Oid
NotNull bool
}
type TableStats struct {
Rows uint // an estimated number of rows
StartCost Cost
TotalCost Cost
}
// Cost is a approximate cost of an operation. See Postgres docs for details.
type Cost float64
// Oid is a Postgres internal object ID.
type Oid uint
// A list of constants for Postgres data types
const (
TypeBool = Oid(C.BOOLOID)
TypeBytes = Oid(C.BYTEAOID)
TypeChar = Oid(C.CHAROID)
TypeName = Oid(C.NAMEOID)
TypeInt64 = Oid(C.INT8OID)
TypeInt16 = Oid(C.INT2OID)
TypeInt16Vector = Oid(C.INT2VECTOROID)
TypeInt32 = Oid(C.INT4OID)
TypeRegProc = Oid(C.REGPROCOID)
TypeText = Oid(C.TEXTOID)
TypeOid = Oid(C.OIDOID)
TypeJson = Oid(C.JSONOID)
TypeXml = Oid(C.XMLOID)
TypeFloat32 = Oid(C.FLOAT4OID)
TypeFloat64 = Oid(C.FLOAT8OID)
TypeTimestamp = Oid(C.TIMESTAMPOID)
TypeInterval = Oid(C.INTERVALOID)
)
// FIXME: some black magic here; we save pointers to all necessary functions (passed by glue C code)
// FIXME: it would be better to link to pg executable properly
var (
fmu sync.Mutex
)
//export goAnalyzeForeignTable
func goAnalyzeForeignTable(relation C.Relation, fnc *C.AcquireSampleRowsFunc, totalpages *C.BlockNumber) C.bool {
*totalpages = 1
return 1
}
//export goGetForeignRelSize
func goGetForeignRelSize(root *C.PlannerInfo, baserel *C.RelOptInfo, foreigntableid C.Oid) {
// Obtain relation size estimates for a foreign table
opts := getFTableOptions(Oid(foreigntableid))
st := table.Stats(opts)
baserel.rows = C.double(st.Rows)
baserel.fdw_private = nil
}
//export goExplainForeignScan
func goExplainForeignScan(node *C.ForeignScanState, es *C.ExplainState) {
s := getState(node.fdw_state)
if s == nil {
return
}
// Produce extra output for EXPLAIN
if e, ok := s.Iter.(Explainable); ok {
e.Explain(Explainer{es: es})
}
cs := (*C.GoFdwExecutionState)(node.fdw_state)
clearState(uint64(cs.tok))
C.freeState(cs)
node.fdw_state = nil
}
//export goGetForeignPaths
func goGetForeignPaths(root *C.PlannerInfo, baserel *C.RelOptInfo, foreigntableid C.Oid) {
// Create possible access paths for a scan on the foreign table
opts := getFTableOptions(Oid(foreigntableid))
st := table.Stats(opts)
C.add_path(baserel,
(*C.Path)(unsafe.Pointer(C.create_foreignscan_path(
root,
baserel,
baserel.reltarget,
baserel.rows,
C.Cost(st.StartCost),
C.Cost(st.TotalCost),
nil, // no pathkeys
nil, // no outer rel either
nil, // no extra plan
nil,
))),
)
}
//export goBeginForeignScan
func goBeginForeignScan(node *C.ForeignScanState, eflags C.int) {
rel := buildRelation(node.ss.ss_currentRelation)
opts := getFTableOptions(rel.ID)
s := &State{
Rel: rel, Opts: opts,
Iter: table.Scan(rel, opts),
}
i := saveState(s)
//plan := node.ss.ps.plan
//plan := (*C.ForeignScan)(unsafe.Pointer(node.ss.ps.plan))
cs := C.makeState()
cs.tok = C.uint(i)
node.fdw_state = unsafe.Pointer(cs)
//if eflags&C.EXEC_FLAG_EXPLAIN_ONLY != 0 {
// return // Do nothing in EXPLAIN
//}
}
//export goIterateForeignScan
func goIterateForeignScan(node *C.ForeignScanState) *C.TupleTableSlot {
s := getState(node.fdw_state)
slot := node.ss.ss_ScanTupleSlot
C.ExecClearTuple(slot)
row := s.Iter.Next()
if row == nil {
return slot
}
values := make([]*C.char, len(s.Rel.Attr.Attrs))
for i := range s.Rel.Attr.Attrs {
v := row[i]
if v == nil {
p := unsafe.Pointer(uintptr(unsafe.Pointer(slot.tts_isnull)) + uintptr(C.sizeof_bool))
*((*C.bool)(p)) = 1
continue
}
values[i] = C.CString(fmt.Sprint(v))
}
rel := node.ss.ss_currentRelation
attinmeta := C.TupleDescGetAttInMetadata(rel.rd_att)
tuple := C.BuildTupleFromCStrings(attinmeta, (**C.char)(&values[0]))
C.ExecStoreTuple(tuple, slot, C.InvalidBuffer, 1)
return slot
}
//export goReScanForeignScan
func goReScanForeignScan(node *C.ForeignScanState) {
// Rescan table, possibly with new parameters
s := getState(node.fdw_state)
s.Iter.Reset()
}
//export goEndForeignScan
func goEndForeignScan(node *C.ForeignScanState) {
// Finish scanning foreign table and dispose objects used for this scan
s := getState(node.fdw_state)
if s == nil {
return
}
cs := (*C.GoFdwExecutionState)(node.fdw_state)
clearState(uint64(cs.tok))
C.freeState(cs)
node.fdw_state = nil
}
type State struct {
Rel *Relation
Opts map[string]string
Iter Iterator
}
var (
mu sync.RWMutex
si uint64
sess = make(map[uint64]*State)
)
func saveState(s *State) uint64 {
mu.Lock()
si++
i := si
sess[i] = s
mu.Unlock()
return i
}
func clearState(i uint64) {
mu.Lock()
delete(sess, i)
mu.Unlock()
}
func getState(p unsafe.Pointer) *State {
if p == nil {
return nil
}
cs := (*C.GoFdwExecutionState)(p)
i := uint64(cs.tok)
mu.RLock()
s := sess[i]
mu.RUnlock()
return s
}
func getFTableOptions(id Oid) Options {
f := C.GetForeignTable(C.Oid(id))
return getOptions(f.options)
}
func getOptions(opts *C.List) Options {
m := make(Options)
for it := opts.head; it != nil; it = it.next {
el := C.cellGetDef(it)
name := C.GoString(el.defname)
val := C.GoString(C.defGetString(el))
m[name] = val
}
return m
}
func buildRelation(rel C.Relation) *Relation {
r := &Relation{
ID: Oid(rel.rd_id),
IsValid: goBool(rel.rd_isvalid),
Attr: buildTupleDesc(rel.rd_att),
}
return r
}
func goBool(b C.bool) bool {
return b != 0
}
func goString(p unsafe.Pointer, n int) string {
b := C.GoBytes(p, C.int(n))
i := bytes.IndexByte(b, 0)
if i < 0 {
i = len(b)
}
return string(b[:i])
}
func buildTupleDesc(desc C.TupleDesc) *TupleDesc {
if desc == nil {
return nil
}
d := &TupleDesc{
TypeID: Oid(desc.tdtypeid),
TypeMod: int(desc.tdtypmod),
HasOid: goBool(desc.tdhasoid),
Attrs: make([]Attr, 0, int(desc.natts)),
}
for i := 0; i < cap(d.Attrs); i++ {
off := uintptr(i) * uintptr(C.sizeof_Form_pg_attribute)
p := *(*C.Form_pg_attribute)(unsafe.Pointer(uintptr(unsafe.Pointer(desc.attrs)) + off))
d.Attrs = append(d.Attrs, buildAttr(p))
}
return d
}
const nameLen = C.NAMEDATALEN
func buildAttr(attr *C.FormData_pg_attribute) (out Attr) {
out.Name = goString(unsafe.Pointer(&attr.attname.data[0]), nameLen)
out.Type = Oid(attr.atttypid)
out.NotNull = goBool(attr.attnotnull)
return
}
// required by buildmode=c-archive
func main() {}