use crate as pg_sys;
use core::mem::offset_of;
use core::str::FromStr;
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;
#[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 GetMemoryChunkContext(pointer: *mut std::os::raw::c_void) -> pg_sys::MemoryContext {
#[cfg(any(feature = "pg12", feature = "pg13", feature = "pg14", feature = "pg15"))]
{
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
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
{
#[pgrx_macros::pg_guard]
extern "C" {
#[link_name = "GetMemoryChunkContext"]
pub fn extern_fn(pointer: *mut std::os::raw::c_void) -> pg_sys::MemoryContext;
}
extern_fn(pointer)
}
}
#[allow(non_snake_case)]
#[inline(always)]
pub unsafe fn MemoryContextIsValid(context: crate::MemoryContext) -> bool {
!context.is_null()
&& unsafe {
let tag = (*context.cast::<crate::Node>()).type_;
use crate::NodeTag::*;
matches!(tag, T_AllocSetContext | T_SlabContext | T_GenerationContext)
}
}
pub const VARHDRSZ_EXTERNAL: usize = offset_of!(super::varattrib_1b_e, va_data);
pub const VARHDRSZ_SHORT: usize = offset_of!(super::varattrib_1b, va_data);
#[inline]
pub fn get_pg_major_version_string() -> &'static str {
super::PG_MAJORVERSION.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 {
super::PG_VERSION_STR.to_str().unwrap()
}
#[inline]
pub fn get_pg_major_minor_version_string() -> &'static str {
super::PG_VERSION.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
}
#[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.add(((buffer as crate::Size) - 1) * crate::BLCKSZ as usize)
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()
}
}
}
#[cfg(any(feature = "pg12", feature = "pg13", feature = "pg14", feature = "pg15"))]
#[::pgrx_macros::pg_guard]
extern "C" {
pub fn planstate_tree_walker(
planstate: *mut super::PlanState,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::PlanState, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
) -> bool;
pub fn query_tree_walker(
query: *mut super::Query,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool;
pub fn query_or_expression_tree_walker(
node: *mut super::Node,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool;
pub fn range_table_entry_walker(
rte: *mut super::RangeTblEntry,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool;
pub fn range_table_walker(
rtable: *mut super::List,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool;
pub fn expression_tree_walker(
node: *mut super::Node,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
) -> bool;
pub fn raw_expression_tree_walker(
node: *mut super::Node,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
) -> bool;
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn planstate_tree_walker(
planstate: *mut super::PlanState,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::PlanState, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
) -> bool {
crate::planstate_tree_walker_impl(planstate, walker, context)
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn query_tree_walker(
query: *mut super::Query,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool {
crate::query_tree_walker_impl(query, walker, context, flags)
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn query_or_expression_tree_walker(
node: *mut super::Node,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool {
crate::query_or_expression_tree_walker_impl(node, walker, context, flags)
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn expression_tree_walker(
node: *mut crate::Node,
walker: Option<unsafe extern "C" fn(*mut crate::Node, *mut ::core::ffi::c_void) -> bool>,
context: *mut ::core::ffi::c_void,
) -> bool {
crate::expression_tree_walker_impl(node, walker, context)
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn range_table_entry_walker(
rte: *mut super::RangeTblEntry,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool {
crate::range_table_entry_walker_impl(rte, walker, context, flags)
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn range_table_walker(
rtable: *mut super::List,
walker: ::core::option::Option<
unsafe extern "C" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
>,
context: *mut ::core::ffi::c_void,
flags: ::core::ffi::c_int,
) -> bool {
crate::range_table_walker_impl(rtable, walker, context, flags)
}
#[cfg(any(feature = "pg16", feature = "pg17"))]
pub unsafe fn raw_expression_tree_walker(
node: *mut crate::Node,
walker: Option<unsafe extern "C" fn(*mut crate::Node, *mut ::core::ffi::c_void) -> bool>,
context: *mut ::core::ffi::c_void,
) -> bool {
crate::raw_expression_tree_walker_impl(node, walker, context)
}
#[inline(always)]
pub unsafe fn MemoryContextSwitchTo(context: crate::MemoryContext) -> crate::MemoryContext {
let old = crate::CurrentMemoryContext;
crate::CurrentMemoryContext = context;
old
}