-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplruby_spi.c
More file actions
1383 lines (1191 loc) · 34.3 KB
/
Copy pathplruby_spi.c
File metadata and controls
1383 lines (1191 loc) · 34.3 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
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
* plruby_spi.c - the database API PL/Ruby exposes to Ruby code, plus
* interpreter-level setup ($_SHARED, output redirection).
*
* This file grows feature by feature; the SPI query functions, prepared
* statements, transaction control and subtransactions are added on top of
* the setup and messaging helpers established here.
*
* Copyright (c) 2026 ChronicallyJD. MIT License; see LICENSE.
**********************************************************************/
#include "postgres.h"
#include "plruby.h"
#include "plruby_spi.h"
#include "plruby_io.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/tuplestore.h"
/* SRF support (shared with the SRF handler in plruby.c) */
FunctionCallInfo current_fcinfo = NULL;
TupleDesc current_tupledesc = NULL;
AttInMetadata *current_attinmeta = NULL;
MemoryContext current_memcxt = NULL;
Tuplestorestate *current_tuplestore = NULL;
VALUE current_srf_binding = Qnil;
/*
* Raise PLRuby::Error for a PostgreSQL error captured via CopyErrorData(),
* attaching the five-character SQLSTATE as @sqlstate. Does not return.
*/
static void
plruby_raise_pg_error(ErrorData *edata)
{
VALUE exc;
exc = rb_exc_new_str(rb_ePLRubyError,
plruby_str_from_pg(edata->message,
strlen(edata->message)));
rb_ivar_set(exc, rb_intern("@sqlstate"),
rb_str_new_cstr(unpack_sql_state(edata->sqlerrcode)));
if (edata->detail != NULL)
rb_ivar_set(exc, rb_intern("@detail"),
plruby_str_from_pg(edata->detail, strlen(edata->detail)));
if (edata->hint != NULL)
rb_ivar_set(exc, rb_intern("@hint"),
plruby_str_from_pg(edata->hint, strlen(edata->hint)));
FreeErrorData(edata);
rb_exc_raise(exc);
}
/* ---------------------------------------------------------------------
* Output redirection: Ruby's $stdout/$stderr forwarded to the PG log.
* ------------------------------------------------------------------- */
static StringInfo plruby_msgbuf = NULL;
static VALUE
plruby_do_log(VALUE self, VALUE str)
{
const char *p;
long len;
str = rb_obj_as_string(str);
p = RSTRING_PTR(str);
len = RSTRING_LEN(str);
if (plruby_msgbuf == NULL)
plruby_msgbuf = makeStringInfo();
appendBinaryStringInfo(plruby_msgbuf, p, len);
/* Flush once a complete line has accumulated (matches PL/php). */
if (plruby_msgbuf->len > 0 &&
plruby_msgbuf->data[plruby_msgbuf->len - 1] == '\n')
{
plruby_msgbuf->data[plruby_msgbuf->len - 1] = '\0';
elog(LOG, "%s", plruby_msgbuf->data);
resetStringInfo(plruby_msgbuf);
}
return LONG2NUM(len);
}
static const char *plruby_output_ruby =
"module PLRuby\n"
" class Output\n"
" def write(*a); n = 0; a.each { |x| s = x.to_s; PLRuby.__log(s); n += s.bytesize }; n; end\n"
" def print(*a); a.each { |x| write(x.to_s) }; nil; end\n"
" def puts(*a)\n"
" if a.empty? then write(\"\\n\")\n"
" else a.each { |x|\n"
" if x.is_a?(Array) then puts(*x)\n"
" else s = x.to_s; write(s); write(\"\\n\") unless s.end_with?(\"\\n\") end }\n"
" end; nil\n"
" end\n"
" def printf(f, *a); write(sprintf(f, *a)); nil; end\n"
" def <<(x); write(x.to_s); self; end\n"
" def flush; self; end\n"
" def sync; true; end\n"
" def sync=(v); v; end\n"
" def fileno; -1; end\n"
" def tty?; false; end\n"
" def isatty; false; end\n"
" end\n"
" OUT = Output.new\n"
"end\n";
/*
* The block form of spi_query and Cursor#each are layered in Ruby on top of the
* C primitives (__spi_query / spi_fetchrow / spi_cursor_close): the per-row loop
* runs at the Ruby level, and the cursor is always closed via ensure.
*/
static const char *plruby_spi_prelude =
"def spi_query(sql, &blk)\n"
" cur = __spi_query(sql)\n"
" return cur unless blk\n"
" begin\n"
" while (row = spi_fetchrow(cur)); blk.call(row); end\n"
" ensure\n"
" spi_cursor_close(cur)\n"
" end\n"
" nil\n"
"end\n"
"def spi_query_prepared(plan, *args, &blk)\n"
" cur = __spi_query_prepared(plan, *args)\n"
" return cur unless blk\n"
" begin\n"
" while (row = spi_fetchrow(cur)); blk.call(row); end\n"
" ensure\n"
" spi_cursor_close(cur)\n"
" end\n"
" nil\n"
"end\n"
"class PLRuby::Cursor\n"
" include Enumerable\n"
" def each\n"
" return enum_for(:each) unless block_given?\n"
" while (row = spi_fetchrow(self)); yield row; end\n"
" self\n"
" end\n"
"end\n";
/* ---------------------------------------------------------------------
* Messaging: elog / pg_raise
* ------------------------------------------------------------------- */
static int
plruby_parse_elevel(const char *level, bool allow_full)
{
if (allow_full && pg_strcasecmp(level, "DEBUG") == 0)
return DEBUG1;
if (allow_full && pg_strcasecmp(level, "LOG") == 0)
return LOG;
if (allow_full && pg_strcasecmp(level, "INFO") == 0)
return INFO;
if (pg_strcasecmp(level, "NOTICE") == 0)
return NOTICE;
if (pg_strcasecmp(level, "WARNING") == 0)
return WARNING;
if (pg_strcasecmp(level, "ERROR") == 0)
return ERROR;
return -1;
}
/*
* A Ruby string argument as a null-terminated palloc'd C string. Any embedded
* NUL truncates, which is fine for log messages and SQL text.
*/
static char *
plruby_str_arg(VALUE v)
{
VALUE s = rb_obj_as_string(v);
return pnstrdup(RSTRING_PTR(s), RSTRING_LEN(s));
}
static VALUE
plruby_elog(VALUE self, VALUE level, VALUE message)
{
char *lvl = plruby_str_arg(level);
char *msg = plruby_str_arg(message);
int elevel = plruby_parse_elevel(lvl, true);
if (elevel < 0)
rb_raise(rb_ePLRubyError, "elog: unrecognized level \"%s\"", lvl);
if (elevel == ERROR)
/* Unwind through Ruby so the top-level handler reports it cleanly */
rb_raise(rb_ePLRubyError, "%s", msg);
ereport(elevel, (errmsg("%s", msg)));
return Qnil;
}
/* A string value for the :key entry of an options Hash, or NULL. */
static char *
plruby_opt_str(VALUE opts, const char *key)
{
VALUE v;
if (!RB_TYPE_P(opts, T_HASH))
return NULL;
v = rb_hash_aref(opts, ID2SYM(rb_intern(key)));
if (NIL_P(v))
return NULL;
v = rb_obj_as_string(v);
return pnstrdup(RSTRING_PTR(v), RSTRING_LEN(v));
}
/*
* pg_raise(level, message [, detail:, hint:, sqlstate:])
*
* The keyword fields map onto the corresponding ereport fields. For ERROR
* the unwinding still happens through a Ruby exception (so Ruby code can
* rescue it); the fields ride along as instance variables and are re-applied
* by plruby_report_exception() if the exception reaches the top level.
*/
static VALUE
plruby_pg_raise(int argc, VALUE *argv, VALUE self)
{
char *lvl;
char *msg;
char *detail;
char *hint;
char *sqlstate;
VALUE opts = Qnil;
int elevel;
if (argc < 2 || argc > 3)
rb_raise(rb_ePLRubyError,
"pg_raise: expected 2 or 3 arguments, got %d", argc);
if (argc == 3)
{
if (!RB_TYPE_P(argv[2], T_HASH))
rb_raise(rb_ePLRubyError,
"pg_raise: options must be a Hash (detail:, hint:, sqlstate:)");
opts = argv[2];
}
lvl = plruby_str_arg(argv[0]);
msg = plruby_str_arg(argv[1]);
elevel = plruby_parse_elevel(lvl, false);
if (elevel < 0)
rb_raise(rb_ePLRubyError, "pg_raise: incorrect log level \"%s\"", lvl);
detail = plruby_opt_str(opts, "detail");
hint = plruby_opt_str(opts, "hint");
sqlstate = plruby_opt_str(opts, "sqlstate");
if (sqlstate != NULL)
{
int i;
for (i = 0; i < 5; i++)
if (sqlstate[i] == '\0' ||
!((sqlstate[i] >= '0' && sqlstate[i] <= '9') ||
(sqlstate[i] >= 'A' && sqlstate[i] <= 'Z')))
break;
if (i != 5 || sqlstate[5] != '\0')
rb_raise(rb_ePLRubyError,
"pg_raise: invalid sqlstate \"%s\"", sqlstate);
}
if (elevel == ERROR)
{
VALUE exc = rb_exc_new_cstr(rb_ePLRubyError, msg);
if (detail != NULL)
rb_ivar_set(exc, rb_intern("@detail"), rb_str_new_cstr(detail));
if (hint != NULL)
rb_ivar_set(exc, rb_intern("@hint"), rb_str_new_cstr(hint));
if (sqlstate != NULL)
rb_ivar_set(exc, rb_intern("@sqlstate"), rb_str_new_cstr(sqlstate));
rb_exc_raise(exc);
}
ereport(elevel,
(sqlstate ? errcode(MAKE_SQLSTATE(sqlstate[0], sqlstate[1],
sqlstate[2], sqlstate[3],
sqlstate[4])) : 0,
errmsg("%s", msg),
detail ? errdetail("%s", detail) : 0,
hint ? errhint("%s", hint) : 0));
return Qnil;
}
/* ---------------------------------------------------------------------
* Quoting helpers
* ------------------------------------------------------------------- */
static VALUE
plruby_quote_literal(VALUE self, VALUE arg)
{
char *in = plruby_str_arg(arg);
char *q = quote_literal_cstr(in);
VALUE r = rb_str_new_cstr(q);
pfree(q);
pfree(in);
return r;
}
static VALUE
plruby_quote_nullable(VALUE self, VALUE arg)
{
char *in;
char *q;
VALUE r;
if (NIL_P(arg))
return rb_str_new_cstr("NULL");
in = plruby_str_arg(arg);
q = quote_literal_cstr(in);
r = rb_str_new_cstr(q);
pfree(q);
pfree(in);
return r;
}
static VALUE
plruby_quote_ident(VALUE self, VALUE arg)
{
char *in = plruby_str_arg(arg);
/* quote_identifier may return its argument unchanged; do not free it */
const char *q = quote_identifier(in);
VALUE r = rb_str_new_cstr(q);
pfree(in);
return r;
}
/* ---------------------------------------------------------------------
* SPI query results, exposed to Ruby as PLRuby::SPIResult objects
* ------------------------------------------------------------------- */
typedef struct
{
SPITupleTable *tuptable;
uint64 processed;
uint64 current_row;
int status;
} plruby_spi_result;
/*
* The tuple table lives in the SPI memory context and is reclaimed when the
* SPI connection is finished (or the transaction ends), so we only free the
* wrapper struct here -- freeing the tuptable ourselves could double-free.
*/
static void
spi_result_free(void *p)
{
ruby_xfree(p);
}
static const rb_data_type_t plruby_spi_result_type = {
"PLRuby::SPIResult",
{NULL, spi_result_free, NULL},
NULL, NULL,
RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE cSPIResult;
static plruby_spi_result *
plruby_get_spi_result(VALUE v)
{
plruby_spi_result *r;
if (!rb_typeddata_is_kind_of(v, &plruby_spi_result_type))
rb_raise(rb_ePLRubyError, "expected an SPI result");
TypedData_Get_Struct(v, plruby_spi_result, &plruby_spi_result_type, r);
return r;
}
static VALUE
plruby_make_spi_result(int status)
{
plruby_spi_result *r;
VALUE obj;
obj = TypedData_Make_Struct(cSPIResult, plruby_spi_result,
&plruby_spi_result_type, r);
r->processed = SPI_processed;
r->tuptable = (status == SPI_OK_SELECT) ? SPI_tuptable : NULL;
r->current_row = 0;
r->status = status;
return obj;
}
static VALUE
plruby_spi_exec(int argc, VALUE *argv, VALUE self)
{
VALUE q,
lim;
char *query;
long limit;
long status;
MemoryContext oldcontext = CurrentMemoryContext;
ResourceOwner oldowner = CurrentResourceOwner;
if (argc < 1 || argc > 2)
rb_raise(rb_ePLRubyError,
"spi_exec: expected 1 or 2 arguments, got %d", argc);
q = argv[0];
lim = (argc == 2) ? argv[1] : Qnil;
query = plruby_str_arg(q);
limit = NIL_P(lim) ? 0 : NUM2LONG(lim);
BeginInternalSubTransaction(NULL);
MemoryContextSwitchTo(oldcontext);
PG_TRY();
{
status = SPI_exec(query, limit);
ReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
}
PG_CATCH();
{
ErrorData *edata;
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
RollbackAndReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
plruby_raise_pg_error(edata);
}
PG_END_TRY();
pfree(query);
return plruby_make_spi_result(status);
}
static VALUE
plruby_spi_fetch_row(VALUE self, VALUE res)
{
plruby_spi_result *r = plruby_get_spi_result(res);
VALUE row;
if (r->status != SPI_OK_SELECT || r->tuptable == NULL)
return Qnil;
if (r->current_row >= r->processed)
return Qnil;
row = plruby_hash_from_tuple(r->tuptable->vals[r->current_row],
r->tuptable->tupdesc);
r->current_row++;
return row;
}
static VALUE
plruby_spi_processed(VALUE self, VALUE res)
{
plruby_spi_result *r = plruby_get_spi_result(res);
return ULL2NUM(r->processed);
}
static VALUE
plruby_spi_status(VALUE self, VALUE res)
{
plruby_spi_result *r = plruby_get_spi_result(res);
return rb_str_new_cstr(SPI_result_code_string(r->status));
}
/*
* Column metadata for a result's tuple descriptor, as parallel Arrays over the
* (non-dropped) result columns -- the counterparts of PL/Python's colnames /
* coltypes / coltypmods. A result without a tuple table (a non-SELECT, like a
* plain INSERT) has no columns, so each returns an empty Array.
*/
typedef enum
{
PLRUBY_COL_NAME,
PLRUBY_COL_TYPE,
PLRUBY_COL_TYPMOD
} plruby_col_kind;
static VALUE
plruby_spi_colmeta(VALUE res, plruby_col_kind kind)
{
plruby_spi_result *r = plruby_get_spi_result(res);
VALUE out = rb_ary_new();
TupleDesc tupdesc;
int i;
if (r->tuptable == NULL)
return out;
tupdesc = r->tuptable->tupdesc;
for (i = 0; i < tupdesc->natts; i++)
{
Form_pg_attribute att = TupleDescAttr(tupdesc, i);
if (att->attisdropped)
continue;
switch (kind)
{
case PLRUBY_COL_NAME:
rb_ary_push(out, rb_str_new_cstr(NameStr(att->attname)));
break;
case PLRUBY_COL_TYPE:
rb_ary_push(out, UINT2NUM(att->atttypid));
break;
case PLRUBY_COL_TYPMOD:
rb_ary_push(out, INT2NUM(att->atttypmod));
break;
}
}
return out;
}
static VALUE
plruby_spi_colnames(VALUE self, VALUE res)
{
return plruby_spi_colmeta(res, PLRUBY_COL_NAME);
}
static VALUE
plruby_spi_coltypes(VALUE self, VALUE res)
{
return plruby_spi_colmeta(res, PLRUBY_COL_TYPE);
}
static VALUE
plruby_spi_coltypmods(VALUE self, VALUE res)
{
return plruby_spi_colmeta(res, PLRUBY_COL_TYPMOD);
}
static VALUE
plruby_spi_rewind(VALUE self, VALUE res)
{
plruby_spi_result *r = plruby_get_spi_result(res);
r->current_row = 0;
return Qnil;
}
/* ---------------------------------------------------------------------
* Prepared statements, exposed as PLRuby::SPIPlan objects
* ------------------------------------------------------------------- */
typedef struct
{
SPIPlanPtr plan;
int nargs;
Oid *argtypes; /* malloc'd, length nargs */
Oid *typinput;
Oid *typioparam;
} plruby_spi_plan;
static void
spi_plan_free(void *p)
{
plruby_spi_plan *pl = p;
if (pl == NULL)
return;
if (pl->plan != NULL)
SPI_freeplan(pl->plan);
free(pl->argtypes);
free(pl->typinput);
free(pl->typioparam);
ruby_xfree(pl);
}
static const rb_data_type_t plruby_spi_plan_type = {
"PLRuby::SPIPlan",
{NULL, spi_plan_free, NULL},
NULL, NULL,
RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE cSPIPlan;
static plruby_spi_plan *
plruby_get_spi_plan(VALUE v)
{
plruby_spi_plan *pl;
if (!rb_typeddata_is_kind_of(v, &plruby_spi_plan_type))
rb_raise(rb_ePLRubyError, "expected a prepared SPI plan");
TypedData_Get_Struct(v, plruby_spi_plan, &plruby_spi_plan_type, pl);
return pl;
}
static VALUE
plruby_spi_prepare(int argc, VALUE *argv, VALUE self)
{
char *query;
int ntypes;
Oid *argtypes = NULL;
Oid *typinput = NULL;
Oid *typioparam = NULL;
SPIPlanPtr spiplan = NULL;
plruby_spi_plan *pl;
VALUE obj;
int i;
MemoryContext oldcontext = CurrentMemoryContext;
ResourceOwner oldowner = CurrentResourceOwner;
if (argc < 1)
rb_raise(rb_ePLRubyError, "spi_prepare: missing query text");
query = plruby_str_arg(argv[0]);
ntypes = argc - 1;
if (ntypes > 0)
{
argtypes = (Oid *) malloc(ntypes * sizeof(Oid));
typinput = (Oid *) malloc(ntypes * sizeof(Oid));
typioparam = (Oid *) malloc(ntypes * sizeof(Oid));
}
PG_TRY();
{
for (i = 0; i < ntypes; i++)
{
char *typ = plruby_str_arg(argv[i + 1]);
Oid typid;
int32 typmod;
parseTypeString(typ, &typid, &typmod, NULL);
argtypes[i] = typid;
getTypeInputInfo(typid, &typinput[i], &typioparam[i]);
pfree(typ);
}
spiplan = SPI_prepare(query, ntypes, argtypes);
if (spiplan == NULL)
elog(ERROR, "spi_prepare: SPI_prepare failed: %s",
SPI_result_code_string(SPI_result));
if (SPI_keepplan(spiplan) != 0)
elog(ERROR, "spi_prepare: SPI_keepplan failed");
}
PG_CATCH();
{
ErrorData *edata;
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
CurrentResourceOwner = oldowner;
free(argtypes);
free(typinput);
free(typioparam);
plruby_raise_pg_error(edata);
}
PG_END_TRY();
pfree(query);
obj = TypedData_Make_Struct(cSPIPlan, plruby_spi_plan,
&plruby_spi_plan_type, pl);
pl->plan = spiplan;
pl->nargs = ntypes;
pl->argtypes = argtypes;
pl->typinput = typinput;
pl->typioparam = typioparam;
return obj;
}
static VALUE
plruby_spi_exec_prepared(int argc, VALUE *argv, VALUE self)
{
plruby_spi_plan *pl;
long status;
Datum *values = NULL;
char *nulls = NULL;
int i,
nargs;
MemoryContext oldcontext = CurrentMemoryContext;
ResourceOwner oldowner = CurrentResourceOwner;
if (argc < 1)
rb_raise(rb_ePLRubyError, "spi_exec_prepared: missing plan");
pl = plruby_get_spi_plan(argv[0]);
if (pl->plan == NULL)
rb_raise(rb_ePLRubyError,
"spi_exec_prepared: plan has already been freed");
nargs = argc - 1;
if (nargs != pl->nargs)
rb_raise(rb_ePLRubyError,
"spi_exec_prepared: plan expects %d argument(s), got %d",
pl->nargs, nargs);
if (pl->nargs > 0)
{
values = (Datum *) palloc(pl->nargs * sizeof(Datum));
nulls = (char *) palloc(pl->nargs * sizeof(char));
}
BeginInternalSubTransaction(NULL);
MemoryContextSwitchTo(oldcontext);
PG_TRY();
{
for (i = 0; i < pl->nargs; i++)
{
char *val = plruby_value_to_cstring(argv[i + 1], true, true);
if (val == NULL)
{
nulls[i] = 'n';
values[i] = (Datum) 0;
}
else
{
nulls[i] = ' ';
values[i] = OidInputFunctionCall(pl->typinput[i], val,
pl->typioparam[i], -1);
pfree(val);
}
}
status = SPI_execute_plan(pl->plan, values, nulls, false, 0);
ReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
}
PG_CATCH();
{
ErrorData *edata;
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
RollbackAndReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
plruby_raise_pg_error(edata);
}
PG_END_TRY();
return plruby_make_spi_result(status);
}
static VALUE
plruby_spi_freeplan(VALUE self, VALUE planobj)
{
plruby_spi_plan *pl = plruby_get_spi_plan(planobj);
if (pl->plan != NULL)
{
SPI_freeplan(pl->plan);
pl->plan = NULL;
}
return Qtrue;
}
/* ---------------------------------------------------------------------
* Cursor streaming: spi_query opens a portal, spi_fetchrow reads one row at a
* time, so large result sets are consumed without materializing them all.
* ------------------------------------------------------------------- */
/*
* Rows are fetched from the portal a batch at a time and buffered as a Ruby
* Array, so streaming a large result never materializes it all at once.
*/
#define PLRUBY_CURSOR_BATCH 256
typedef struct
{
Portal portal; /* NULL once closed or exhausted */
SPIPlanPtr plan; /* saved plan backing the portal, freed on close */
VALUE buffer; /* Ruby Array of buffered row Hashes, or Qnil */
long pos; /* next index into buffer */
} plruby_cursor;
static void
cursor_mark(void *p)
{
plruby_cursor *c = p;
if (c != NULL)
rb_gc_mark(c->buffer);
}
/*
* Portals are closed by SPI_finish / transaction end, and by GC time SPI may be
* gone, so the finalizer only frees the wrapper struct.
*/
static void
cursor_free(void *p)
{
ruby_xfree(p);
}
static const rb_data_type_t plruby_cursor_type = {
"PLRuby::Cursor",
{cursor_mark, cursor_free, NULL},
NULL, NULL,
RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE cCursor;
static plruby_cursor *
plruby_get_cursor(VALUE v)
{
plruby_cursor *c;
if (!rb_typeddata_is_kind_of(v, &plruby_cursor_type))
rb_raise(rb_ePLRubyError, "expected a cursor from spi_query");
TypedData_Get_Struct(v, plruby_cursor, &plruby_cursor_type, c);
return c;
}
static void
plruby_cursor_do_close(plruby_cursor *c)
{
if (c->portal != NULL)
{
SPI_cursor_close(c->portal);
c->portal = NULL;
}
if (c->plan != NULL)
{
SPI_freeplan(c->plan);
c->plan = NULL;
}
/* Drop any buffered rows: a closed cursor must not keep yielding. */
c->buffer = Qnil;
c->pos = 0;
}
/*
* Refill the cursor's row buffer with the next batch of up to
* PLRUBY_CURSOR_BATCH rows, freeing the tuple table afterward so streaming
* memory stays bounded. Returns false at end of result (closing the cursor).
*/
static bool
plruby_cursor_refill(plruby_cursor *c)
{
VALUE batch = Qnil;
MemoryContext oldcontext = CurrentMemoryContext;
if (c->portal == NULL)
return false;
/*
* Fetch the next batch directly on the portal (no per-fetch subtransaction:
* running a continuing portal under a subtransaction that is then released
* corrupts the portal). A query error therefore propagates as a Ruby
* exception but is terminal for the surrounding statement, like a PL/pgSQL
* cursor loop.
*/
PG_TRY();
{
SPI_cursor_fetch(c->portal, true, PLRUBY_CURSOR_BATCH);
}
PG_CATCH();
{
ErrorData *edata;
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
plruby_cursor_do_close(c);
plruby_raise_pg_error(edata);
}
PG_END_TRY();
if (SPI_processed > 0 && SPI_tuptable != NULL)
{
uint64 i;
/* batch is a C-stack local, protected from GC while we build it */
batch = rb_ary_new_capa(SPI_processed);
for (i = 0; i < SPI_processed; i++)
rb_ary_push(batch,
plruby_hash_from_tuple(SPI_tuptable->vals[i],
SPI_tuptable->tupdesc));
}
/* Free the batch's tuple table so a long stream does not accumulate them. */
if (SPI_tuptable != NULL)
SPI_freetuptable(SPI_tuptable);
if (NIL_P(batch))
{
plruby_cursor_do_close(c);
c->buffer = Qnil;
c->pos = 0;
return false;
}
c->buffer = batch;
c->pos = 0;
return true;
}
/*
* Fetch the next row as a Hash, or nil at end of result. Serves rows from the
* buffer, refilling from the portal a batch at a time.
*/
static VALUE
plruby_cursor_fetch_one(plruby_cursor *c)
{
if (NIL_P(c->buffer) || c->pos >= RARRAY_LEN(c->buffer))
{
if (!plruby_cursor_refill(c))
return Qnil;
}
return rb_ary_entry(c->buffer, c->pos++);
}
/*
* __spi_query(sql) -> a cursor over the query. The block form of the public
* spi_query is layered on top of this in Ruby (see plruby_spi_prelude), so the
* per-row iteration runs at the Ruby level rather than through a C rb_yield
* loop across fetches.
*/
static VALUE
plruby_spi_query(int argc, VALUE *argv, VALUE self)
{
char *query;
SPIPlanPtr plan;
Portal portal;
plruby_cursor *c;
VALUE obj;
MemoryContext oldcontext = CurrentMemoryContext;
if (argc != 1)
rb_raise(rb_ePLRubyError, "spi_query: expected 1 argument (query text)");
query = plruby_str_arg(argv[0]);
/*
* Open the portal at the function's transaction level (no subtransaction):
* a portal created inside a subtransaction that is then released loses its
* ownership and crashes on a continuation fetch. Rows are fetched in
* per-batch subtransactions (see plruby_cursor_refill).
*/
PG_TRY();
{
plan = SPI_prepare(query, 0, NULL);
if (plan == NULL)
elog(ERROR, "spi_query: SPI_prepare failed: %s",
SPI_result_code_string(SPI_result));
/*
* Save the plan: an unsaved SPI_prepare plan can be released after the
* first fetch, leaving the portal referencing freed memory on the next
* one. It is freed when the cursor closes.
*/
if (SPI_keepplan(plan) != 0)
elog(ERROR, "spi_query: SPI_keepplan failed");
portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
if (portal == NULL)
elog(ERROR, "spi_query: could not open cursor");
}
PG_CATCH();
{
ErrorData *edata;
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
pfree(query);
plruby_raise_pg_error(edata);
}
PG_END_TRY();
pfree(query);
obj = TypedData_Make_Struct(cCursor, plruby_cursor,
&plruby_cursor_type, c);
c->portal = portal;
c->plan = plan;
c->buffer = Qnil;
c->pos = 0;
return obj;
}
/*
* __spi_query_prepared(plan, args...) -> a cursor streaming a prepared
* plan's results. Unlike __spi_query, the cursor does not own the plan:
* closing it leaves the plan reusable, and spi_freeplan stays the caller's
* job.
*/
static VALUE
plruby_spi_query_prepared(int argc, VALUE *argv, VALUE self)
{