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
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(non_upper_case_globals)]
#![allow(improper_ctypes)]
#![allow(clippy::unneeded_field_pattern)]
#![cfg_attr(nightly, feature(strict_provenance))]
#[cfg(
any(
not(any(feature = "pg11", feature = "pg12", feature = "pg13", feature = "pg14", feature = "pg15")),
))]
std::compile_error!("exactly one one feature must be provided (pg11, pg12, pg13, pg14, pg15)");
pub mod submodules;
use core::ffi::CStr;
use core::ptr::NonNull;
use std::os::raw::c_char;
pub use submodules::*;
#[cfg(all(feature = "pg11", not(docsrs)))]
mod pg11 {
include!(concat!(env!("OUT_DIR"), "/pg11.rs"));
}
#[cfg(all(feature = "pg11", docsrs))]
mod pg11;
#[cfg(all(feature = "pg12", not(docsrs)))]
mod pg12 {
include!(concat!(env!("OUT_DIR"), "/pg12.rs"));
}
#[cfg(all(feature = "pg12", docsrs))]
mod pg12;
#[cfg(all(feature = "pg13", not(docsrs)))]
mod pg13 {
include!(concat!(env!("OUT_DIR"), "/pg13.rs"));
}
#[cfg(all(feature = "pg13", docsrs))]
mod pg13;
#[cfg(all(feature = "pg14", not(docsrs)))]
mod pg14 {
include!(concat!(env!("OUT_DIR"), "/pg14.rs"));
}
#[cfg(all(feature = "pg14", docsrs))]
mod pg14;
#[cfg(all(feature = "pg15", not(docsrs)))]
mod pg15 {
include!(concat!(env!("OUT_DIR"), "/pg15.rs"));
}
#[cfg(all(feature = "pg15", docsrs))]
mod pg15;
#[cfg(feature = "pg11")]
pub use pg11::*;
#[cfg(feature = "pg12")]
pub use pg12::*;
#[cfg(feature = "pg13")]
pub use pg13::*;
#[cfg(feature = "pg14")]
pub use pg14::*;
#[cfg(feature = "pg15")]
pub use pg15::*;
#[cfg(all(feature = "pg11", not(docsrs)))]
mod pg11_oids {
include!(concat!(env!("OUT_DIR"), "/pg11_oids.rs"));
}
#[cfg(all(feature = "pg11", docsrs))]
mod pg11;
#[cfg(all(feature = "pg12", not(docsrs)))]
mod pg12_oids {
include!(concat!(env!("OUT_DIR"), "/pg12_oids.rs"));
}
#[cfg(all(feature = "pg12", docsrs))]
mod pg12_oids;
#[cfg(all(feature = "pg13", not(docsrs)))]
mod pg13_oids {
include!(concat!(env!("OUT_DIR"), "/pg13_oids.rs"));
}
#[cfg(all(feature = "pg13", docsrs))]
mod pg13_oids;
#[cfg(all(feature = "pg14", not(docsrs)))]
mod pg14_oids {
include!(concat!(env!("OUT_DIR"), "/pg14_oids.rs"));
}
#[cfg(all(feature = "pg14", docsrs))]
mod pg14_oids;
#[cfg(all(feature = "pg15", not(docsrs)))]
mod pg15_oids {
include!(concat!(env!("OUT_DIR"), "/pg15_oids.rs"));
}
#[cfg(all(feature = "pg15", docsrs))]
mod pg15_oids;
#[cfg(feature = "pg11")]
pub use pg11_oids::*;
#[cfg(feature = "pg12")]
pub use pg12_oids::*;
#[cfg(feature = "pg13")]
pub use pg13_oids::*;
#[cfg(feature = "pg14")]
pub use pg14_oids::*;
#[cfg(feature = "pg15")]
pub use pg15_oids::*;
pub use all_versions::*;
#[cfg(feature = "pg11")]
pub use internal::pg11::IndexBuildHeapScan;
#[cfg(feature = "pg11")]
pub use internal::pg11::*;
#[cfg(feature = "pg12")]
pub use internal::pg12::*;
#[cfg(feature = "pg13")]
pub use internal::pg13::*;
#[cfg(feature = "pg14")]
pub use internal::pg14::*;
#[cfg(feature = "pg15")]
pub use internal::pg15::*;
pub trait PgNode: seal::Sealed {
#[inline]
fn display_node(&self) -> std::string::String {
unsafe { display_node_impl(NonNull::from(self).cast()) }
}
}
mod seal {
pub trait Sealed {}
}
#[warn(unsafe_op_in_unsafe_fn)]
pub(crate) unsafe fn display_node_impl(node: NonNull<crate::Node>) -> std::string::String {
unsafe {
let node_cstr = crate::nodeToString(node.as_ptr().cast());
let result = match CStr::from_ptr(node_cstr).to_str() {
Ok(cstr) => cstr.to_string(),
Err(e) => format!("<ffi error: {:?}>", e),
};
crate::pfree(node_cstr.cast());
result
}
}
pub trait AsPgCStr {
fn as_pg_cstr(self) -> *mut std::os::raw::c_char;
}
impl<'a> AsPgCStr for &'a str {
fn as_pg_cstr(self) -> *mut std::os::raw::c_char {
let self_bytes = self.as_bytes();
let pg_cstr = unsafe { crate::palloc0(self_bytes.len() + 1) as *mut std::os::raw::c_uchar };
let slice = unsafe { std::slice::from_raw_parts_mut(pg_cstr, self_bytes.len()) };
slice.copy_from_slice(self_bytes);
pg_cstr as *mut std::os::raw::c_char
}
}
impl<'a> AsPgCStr for Option<&'a str> {
fn as_pg_cstr(self) -> *mut c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}
impl AsPgCStr for std::string::String {
fn as_pg_cstr(self) -> *mut std::os::raw::c_char {
self.as_str().as_pg_cstr()
}
}
impl AsPgCStr for &std::string::String {
fn as_pg_cstr(self) -> *mut std::os::raw::c_char {
self.as_str().as_pg_cstr()
}
}
impl AsPgCStr for Option<std::string::String> {
fn as_pg_cstr(self) -> *mut c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}
impl AsPgCStr for Option<&std::string::String> {
fn as_pg_cstr(self) -> *mut c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}
impl AsPgCStr for &Option<std::string::String> {
fn as_pg_cstr(self) -> *mut c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}
mod all_versions {
use crate as pg_sys;
use pgx_macros::*;
use memoffset::*;
use std::str::FromStr;
pub use crate::submodules::htup::*;
pub const InvalidOid: crate::Oid = crate::Oid::INVALID;
pub const InvalidOffsetNumber: super::OffsetNumber = 0;
pub const FirstOffsetNumber: super::OffsetNumber = 1;
pub const MaxOffsetNumber: super::OffsetNumber =
(super::BLCKSZ as usize / std::mem::size_of::<super::ItemIdData>()) as super::OffsetNumber;
pub const InvalidBlockNumber: u32 = 0xFFFF_FFFF as crate::BlockNumber;
pub const VARHDRSZ: usize = std::mem::size_of::<super::int32>();
pub const InvalidTransactionId: super::TransactionId = 0 as super::TransactionId;
pub const InvalidCommandId: super::CommandId = (!(0 as super::CommandId)) as super::CommandId;
pub const FirstCommandId: super::CommandId = 0 as super::CommandId;
pub const BootstrapTransactionId: super::TransactionId = 1 as super::TransactionId;
pub const FrozenTransactionId: super::TransactionId = 2 as super::TransactionId;
pub const FirstNormalTransactionId: super::TransactionId = 3 as super::TransactionId;
pub const MaxTransactionId: super::TransactionId = 0xFFFF_FFFF as super::TransactionId;
#[cfg(feature = "cshim")]
#[pgx_macros::pg_guard]
extern "C" {
pub fn pgx_list_nth(list: *mut super::List, nth: i32) -> *mut std::os::raw::c_void;
pub fn pgx_list_nth_int(list: *mut super::List, nth: i32) -> i32;
pub fn pgx_list_nth_oid(list: *mut super::List, nth: i32) -> super::Oid;
pub fn pgx_list_nth_cell(list: *mut super::List, nth: i32) -> *mut super::ListCell;
}
#[inline(always)]
pub unsafe fn GETSTRUCT(tuple: crate::HeapTuple) -> *mut std::os::raw::c_char {
(*tuple).t_data.cast::<std::os::raw::c_char>().add((*(*tuple).t_data).t_hoff as _)
}
#[allow(non_snake_case)]
#[inline(always)]
pub const unsafe fn TYPEALIGN(alignval: usize, len: usize) -> usize {
((len) + ((alignval) - 1)) & !((alignval) - 1)
}
#[allow(non_snake_case)]
#[inline(always)]
pub const unsafe fn MAXALIGN(len: usize) -> usize {
TYPEALIGN(pg_sys::MAXIMUM_ALIGNOF as _, len)
}
#[allow(non_snake_case)]
pub unsafe fn GetMemoryContextChunk(
pointer: *mut std::os::raw::c_void,
) -> pg_sys::MemoryContext {
assert!(!pointer.is_null());
assert_eq!(pointer, MAXALIGN(pointer as usize) as *mut ::std::os::raw::c_void);
let context = unsafe {
*(pointer
.cast::<::std::os::raw::c_char>()
.sub(std::mem::size_of::<*mut ::std::os::raw::c_void>())
.cast())
};
assert!(MemoryContextIsValid(context));
context
}
#[allow(non_snake_case)]
#[inline(always)]
pub unsafe fn MemoryContextIsValid(context: *mut crate::MemoryContextData) -> bool {
!context.is_null()
&& unsafe {
(*context).type_ == crate::NodeTag_T_AllocSetContext
|| (*context).type_ == crate::NodeTag_T_SlabContext
|| (*context).type_ == crate::NodeTag_T_GenerationContext
}
}
#[inline]
pub fn VARHDRSZ_EXTERNAL() -> usize {
offset_of!(super::varattrib_1b_e, va_data)
}
#[inline]
pub fn VARHDRSZ_SHORT() -> usize {
offset_of!(super::varattrib_1b, va_data)
}
#[inline]
pub fn get_pg_major_version_string() -> &'static str {
let mver = core::ffi::CStr::from_bytes_with_nul(super::PG_MAJORVERSION).unwrap();
mver.to_str().unwrap()
}
#[inline]
pub fn get_pg_major_version_num() -> u16 {
u16::from_str(super::get_pg_major_version_string()).unwrap()
}
#[inline]
pub fn get_pg_version_string() -> &'static str {
let ver = core::ffi::CStr::from_bytes_with_nul(super::PG_VERSION_STR).unwrap();
ver.to_str().unwrap()
}
#[inline]
pub fn get_pg_major_minor_version_string() -> &'static str {
let mver = core::ffi::CStr::from_bytes_with_nul(super::PG_VERSION).unwrap();
mver.to_str().unwrap()
}
#[inline]
pub fn TransactionIdIsNormal(xid: super::TransactionId) -> bool {
xid >= FirstNormalTransactionId
}
#[inline]
pub unsafe fn type_is_array(typoid: super::Oid) -> bool {
super::get_element_type(typoid) != InvalidOid
}
#[cfg(feature = "cshim")]
#[pg_guard]
extern "C" {
#[link_name = "pgx_planner_rt_fetch"]
pub fn planner_rt_fetch(
index: super::Index,
root: *mut super::PlannerInfo,
) -> *mut super::RangeTblEntry;
}
#[cfg(feature = "cshim")]
#[inline]
pub unsafe fn rt_fetch(
index: super::Index,
range_table: *mut super::List,
) -> *mut super::RangeTblEntry {
pgx_list_nth(range_table, index as i32 - 1) as *mut super::RangeTblEntry
}
#[inline]
pub unsafe fn BufferGetPage(buffer: crate::Buffer) -> crate::Page {
BufferGetBlock(buffer) as crate::Page
}
#[inline]
pub unsafe fn BufferGetBlock(buffer: crate::Buffer) -> crate::Block {
if BufferIsLocal(buffer) {
*crate::LocalBufferBlockPointers.offset(((-buffer) - 1) as isize)
} else {
crate::BufferBlocks
.offset((((buffer as crate::Size) - 1) * crate::BLCKSZ as usize) as isize)
as crate::Block
}
}
#[inline]
pub unsafe fn BufferIsLocal(buffer: crate::Buffer) -> bool {
buffer < 0
}
#[inline]
pub unsafe fn heap_tuple_get_struct<T>(htup: super::HeapTuple) -> *mut T {
if htup.is_null() {
std::ptr::null_mut()
} else {
unsafe {
GETSTRUCT(htup).cast()
}
}
}
#[pg_guard]
extern "C" {
pub fn query_tree_walker(
query: *mut super::Query,
walker: ::std::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::std::os::raw::c_void) -> bool,
>,
context: *mut ::std::os::raw::c_void,
flags: ::std::os::raw::c_int,
) -> bool;
}
#[pg_guard]
extern "C" {
pub fn expression_tree_walker(
node: *mut super::Node,
walker: ::std::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::std::os::raw::c_void) -> bool,
>,
context: *mut ::std::os::raw::c_void,
) -> bool;
}
#[cfg(feature = "cshim")]
#[pgx_macros::pg_guard]
extern "C" {
#[link_name = "pgx_SpinLockInit"]
pub fn SpinLockInit(lock: *mut pg_sys::slock_t);
#[link_name = "pgx_SpinLockAcquire"]
pub fn SpinLockAcquire(lock: *mut pg_sys::slock_t);
#[link_name = "pgx_SpinLockRelease"]
pub fn SpinLockRelease(lock: *mut pg_sys::slock_t);
#[link_name = "pgx_SpinLockFree"]
pub fn SpinLockFree(lock: *mut pg_sys::slock_t) -> bool;
}
#[inline(always)]
pub unsafe fn MemoryContextSwitchTo(context: crate::MemoryContext) -> crate::MemoryContext {
let old = crate::CurrentMemoryContext;
crate::CurrentMemoryContext = context;
old
}
}
mod internal {
#[cfg(feature = "pg11")]
pub(crate) mod pg11 {
pub use crate::pg11::tupleDesc as TupleDescData;
pub type QueryCompletion = std::os::raw::c_char;
pub unsafe fn IndexBuildHeapScan<T>(
heap_relation: crate::Relation,
index_relation: crate::Relation,
index_info: *mut crate::pg11::IndexInfo,
build_callback: crate::IndexBuildCallback,
build_callback_state: *mut T,
) {
crate::pg11::IndexBuildHeapScan(
heap_relation,
index_relation,
index_info,
true,
build_callback,
build_callback_state as *mut std::os::raw::c_void,
std::ptr::null_mut(),
);
}
}
#[cfg(feature = "pg12")]
pub(crate) mod pg12 {
pub use crate::pg12::AllocSetContextCreateInternal as AllocSetContextCreateExtended;
pub type QueryCompletion = std::os::raw::c_char;
pub const QTW_EXAMINE_RTES: u32 = crate::pg12::QTW_EXAMINE_RTES_BEFORE;
pub unsafe fn IndexBuildHeapScan<T>(
heap_relation: crate::Relation,
index_relation: crate::Relation,
index_info: *mut crate::pg12::IndexInfo,
build_callback: crate::IndexBuildCallback,
build_callback_state: *mut T,
) {
let heap_relation_ref = heap_relation.as_ref().unwrap();
let table_am = heap_relation_ref.rd_tableam.as_ref().unwrap();
table_am.index_build_range_scan.unwrap()(
heap_relation,
index_relation,
index_info,
true,
false,
true,
0,
crate::InvalidBlockNumber,
build_callback,
build_callback_state as *mut std::os::raw::c_void,
std::ptr::null_mut(),
);
}
}
#[cfg(feature = "pg13")]
pub(crate) mod pg13 {
pub use crate::pg13::AllocSetContextCreateInternal as AllocSetContextCreateExtended;
pub const QTW_EXAMINE_RTES: u32 = crate::pg13::QTW_EXAMINE_RTES_BEFORE;
pub unsafe fn IndexBuildHeapScan<T>(
heap_relation: crate::Relation,
index_relation: crate::Relation,
index_info: *mut crate::IndexInfo,
build_callback: crate::IndexBuildCallback,
build_callback_state: *mut T,
) {
let heap_relation_ref = heap_relation.as_ref().unwrap();
let table_am = heap_relation_ref.rd_tableam.as_ref().unwrap();
table_am.index_build_range_scan.unwrap()(
heap_relation,
index_relation,
index_info,
true,
false,
true,
0,
crate::InvalidBlockNumber,
build_callback,
build_callback_state as *mut std::os::raw::c_void,
std::ptr::null_mut(),
);
}
}
#[cfg(feature = "pg14")]
pub(crate) mod pg14 {
pub use crate::pg14::AllocSetContextCreateInternal as AllocSetContextCreateExtended;
pub const QTW_EXAMINE_RTES: u32 = crate::pg14::QTW_EXAMINE_RTES_BEFORE;
pub unsafe fn IndexBuildHeapScan<T>(
heap_relation: crate::Relation,
index_relation: crate::Relation,
index_info: *mut crate::IndexInfo,
build_callback: crate::IndexBuildCallback,
build_callback_state: *mut T,
) {
let heap_relation_ref = heap_relation.as_ref().unwrap();
let table_am = heap_relation_ref.rd_tableam.as_ref().unwrap();
table_am.index_build_range_scan.unwrap()(
heap_relation,
index_relation,
index_info,
true,
false,
true,
0,
crate::InvalidBlockNumber,
build_callback,
build_callback_state as *mut std::os::raw::c_void,
std::ptr::null_mut(),
);
}
}
#[cfg(feature = "pg15")]
pub(crate) mod pg15 {
pub use crate::pg15::AllocSetContextCreateInternal as AllocSetContextCreateExtended;
pub const QTW_EXAMINE_RTES: u32 = crate::pg15::QTW_EXAMINE_RTES_BEFORE;
pub unsafe fn IndexBuildHeapScan<T>(
heap_relation: crate::Relation,
index_relation: crate::Relation,
index_info: *mut crate::IndexInfo,
build_callback: crate::IndexBuildCallback,
build_callback_state: *mut T,
) {
let heap_relation_ref = heap_relation.as_ref().unwrap();
let table_am = heap_relation_ref.rd_tableam.as_ref().unwrap();
table_am.index_build_range_scan.unwrap()(
heap_relation,
index_relation,
index_info,
true,
false,
true,
0,
crate::InvalidBlockNumber,
build_callback,
build_callback_state as *mut std::os::raw::c_void,
std::ptr::null_mut(),
);
}
}
}
#[cfg(target_os = "linux")]
#[link(name = "resolv")]
extern "C" {}