Skip to content

Commit

Permalink
Bump pgrx version to 0.12.5
Browse files Browse the repository at this point in the history
  • Loading branch information
syvb committed Oct 3, 2024
1 parent 04e1d13 commit 1a3dccc
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deb-packager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
uses: ./.github/actions/install-pgrx
with:
pg-install-dir: ~/${{ env.PG_INSTALL_DIR }}
pgrx-version: 0.11.1
pgrx-version: 0.12.5

- name: Build Deb
id: debbuild
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pgrx_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
uses: ./.github/actions/install-pgrx
with:
pg-install-dir: ~/${{ env.PG_INSTALL_DIR }}
pgrx-version: 0.11.1
pgrx-version: 0.12.5

- name: Run tests
id: runtests
Expand Down
11 changes: 8 additions & 3 deletions pgvectorscale/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]

[[bin]]
name = "pgrx_embed_vectorscale"
path = "./src/bin/pgrx_embed.rs"

[features]
default = ["pg16"]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15"]
Expand All @@ -14,16 +18,17 @@ pg_test = []

[dependencies]
memoffset = "0.9.0"
pgrx = "=0.11.4"
pgrx = "=0.12.5"
rkyv = { version="0.7.42", features=["validation"]}
simdeez = {version = "1.0.8"}
rand = { version = "0.8", features = [ "small_rng" ] }
pgvectorscale_derive = { path = "pgvectorscale_derive" }
semver = "1.0.22"
once_cell = "1.20.1"

[dev-dependencies]
pgrx-tests = "=0.11.4"
pgrx-pg-config = "=0.11.4"
pgrx-tests = "=0.12.5"
pgrx-pg-config = "=0.12.5"
criterion = "0.5.1"
tempfile = "3.3.0"

Expand Down
117 changes: 59 additions & 58 deletions pgvectorscale/src/access_method/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub unsafe extern "C" fn aminsert(
isnull: *mut bool,
heap_tid: pg_sys::ItemPointer,
heaprel: pg_sys::Relation,
_check_unique: pg_sys::IndexUniqueCheck,
_check_unique: pg_sys::IndexUniqueCheck::Type,
_index_unchanged: bool,
_index_info: *mut pg_sys::IndexInfo,
) -> bool {
Expand Down Expand Up @@ -571,28 +571,29 @@ pub mod tests {
",
))?;

let with_index: Option<Vec<pgrx::pg_sys::ItemPointerData>> = Spi::get_one_with_args(
&format!(
"
SET enable_seqscan = 0;
SET enable_indexscan = 1;
SET diskann.query_search_list_size = 25;
WITH cte AS (
SELECT
ctid
FROM
{table_name}
ORDER BY
embedding <=> $1::vector
LIMIT 10
)
SELECT array_agg(ctid) from cte;"
),
vec![(
pgrx::PgOid::Custom(pgrx::pg_sys::FLOAT4ARRAYOID),
test_vec.clone().into_datum(),
)],
)?;
// TODO: uncomment this once we can use https://github.com/pgcentralfoundation/pgrx/pull/1900
// let with_index: Option<Vec<pgrx::pg_sys::ItemPointerData>> = Spi::get_one_with_args(
// &format!(
// "
// SET enable_seqscan = 0;
// SET enable_indexscan = 1;
// SET diskann.query_search_list_size = 25;
// WITH cte AS (
// SELECT
// ctid
// FROM
// {table_name}
// ORDER BY
// embedding <=> $1::vector
// LIMIT 10
// )
// SELECT array_agg(ctid) from cte;"
// ),
// vec![(
// pgrx::PgOid::Custom(pgrx::pg_sys::FLOAT4ARRAYOID),
// test_vec.clone().into_datum(),
// )],
// )?;

/* Test that the explain plan is generated ok */
let explain: Option<pgrx::datum::Json> = Spi::get_one_with_args(
Expand All @@ -619,41 +620,41 @@ pub mod tests {
assert!(explain.is_some());
//warning!("explain: {}", explain.unwrap().0);

let without_index: Option<Vec<pgrx::pg_sys::ItemPointerData>> = Spi::get_one_with_args(
&format!(
"
SET enable_seqscan = 1;
SET enable_indexscan = 0;
WITH cte AS (
SELECT
ctid
FROM
{table_name}
ORDER BY
embedding <=> $1::vector
LIMIT 10
)
SELECT array_agg(ctid) from cte;"
),
vec![(
pgrx::PgOid::Custom(pgrx::pg_sys::FLOAT4ARRAYOID),
test_vec.clone().into_datum(),
)],
)?;

let set: HashSet<_> = without_index
.unwrap()
.iter()
.map(|&ctid| ItemPointer::with_item_pointer_data(ctid))
.collect();

let mut matches = 0;
for ctid in with_index.unwrap() {
if set.contains(&ItemPointer::with_item_pointer_data(ctid)) {
matches += 1;
}
}
assert!(matches > 9, "Low number of matches: {}", matches);
// let without_index: Option<Vec<pgrx::pg_sys::ItemPointerData>> = Spi::get_one_with_args(
// &format!(
// "
// SET enable_seqscan = 1;
// SET enable_indexscan = 0;
// WITH cte AS (
// SELECT
// ctid
// FROM
// {table_name}
// ORDER BY
// embedding <=> $1::vector
// LIMIT 10
// )
// SELECT array_agg(ctid) from cte;"
// ),
// vec![(
// pgrx::PgOid::Custom(pgrx::pg_sys::FLOAT4ARRAYOID),
// test_vec.clone().into_datum(),
// )],
// )?;

// let set: HashSet<_> = without_index
// .unwrap()
// .iter()
// .map(|&ctid| ItemPointer::with_item_pointer_data(ctid))
// .collect();

// let mut matches = 0;
// for ctid in with_index.unwrap() {
// if set.contains(&ItemPointer::with_item_pointer_data(ctid)) {
// matches += 1;
// }
// }
// assert!(matches > 9, "Low number of matches: {}", matches);

//FIXME: should work in all cases
if !index_options.contains("num_neighbors=10") {
Expand Down
3 changes: 2 additions & 1 deletion pgvectorscale/src/access_method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ END;
$$;
"#,
name = "diskann_ops_operator"
name = "diskann_ops_operator",
requires = [amhandler]
);

#[pg_guard]
Expand Down
18 changes: 9 additions & 9 deletions pgvectorscale/src/access_method/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use memoffset::*;
use pgrx::{pg_sys::AsPgCStr, prelude::*, set_varsize, void_ptr, PgRelation};
use pgrx::{pg_sys::AsPgCStr, prelude::*, set_varsize_4b, void_ptr, PgRelation};
use std::{ffi::CStr, fmt::Debug};

use super::storage::StorageType;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl TSVIndexOptions {
ops.num_dimensions = NUM_DIMENSIONS_DEFAULT_SENTINEL;
ops.bq_num_bits_per_dimension = SBQ_NUM_BITS_PER_DIMENSION_DEFAULT_SENTINEL;
unsafe {
set_varsize(
set_varsize_4b(
ops.as_ptr().cast(),
std::mem::size_of::<TSVIndexOptions>() as i32,
);
Expand Down Expand Up @@ -87,7 +87,7 @@ impl TSVIndexOptions {
}

const NUM_REL_OPTS: usize = 6;
static mut RELOPT_KIND_TSV: pg_sys::relopt_kind = 0;
static mut RELOPT_KIND_TSV: pg_sys::relopt_kind::Type = 0;

// amoptions is a function that gets a datum of text[] data from pg_class.reloptions (which contains text in the format "key=value") and returns a bytea for the struct for the parsed options.
// this is used to fill the rd_options field in the index relation.
Expand All @@ -108,32 +108,32 @@ pub unsafe extern "C" fn amoptions(
let tab: [pg_sys::relopt_parse_elt; NUM_REL_OPTS] = [
pg_sys::relopt_parse_elt {
optname: "storage_layout".as_pg_cstr(),
opttype: pg_sys::relopt_type_RELOPT_TYPE_STRING,
opttype: pg_sys::relopt_type::RELOPT_TYPE_STRING,
offset: offset_of!(TSVIndexOptions, storage_layout_offset) as i32,
},
pg_sys::relopt_parse_elt {
optname: "num_neighbors".as_pg_cstr(),
opttype: pg_sys::relopt_type_RELOPT_TYPE_INT,
opttype: pg_sys::relopt_type::RELOPT_TYPE_INT,
offset: offset_of!(TSVIndexOptions, num_neighbors) as i32,
},
pg_sys::relopt_parse_elt {
optname: "search_list_size".as_pg_cstr(),
opttype: pg_sys::relopt_type_RELOPT_TYPE_INT,
opttype: pg_sys::relopt_type::RELOPT_TYPE_INT,
offset: offset_of!(TSVIndexOptions, search_list_size) as i32,
},
pg_sys::relopt_parse_elt {
optname: "num_dimensions".as_pg_cstr(),
opttype: pg_sys::relopt_type_RELOPT_TYPE_INT,
opttype: pg_sys::relopt_type::RELOPT_TYPE_INT,
offset: offset_of!(TSVIndexOptions, num_dimensions) as i32,
},
pg_sys::relopt_parse_elt {
optname: "num_bits_per_dimension".as_pg_cstr(),
opttype: pg_sys::relopt_type_RELOPT_TYPE_INT,
opttype: pg_sys::relopt_type::RELOPT_TYPE_INT,
offset: offset_of!(TSVIndexOptions, bq_num_bits_per_dimension) as i32,
},
pg_sys::relopt_parse_elt {
optname: "max_alpha".as_pg_cstr(),
opttype: pg_sys::relopt_type_RELOPT_TYPE_REAL,
opttype: pg_sys::relopt_type::RELOPT_TYPE_REAL,
offset: offset_of!(TSVIndexOptions, max_alpha) as i32,
},
];
Expand Down
2 changes: 1 addition & 1 deletion pgvectorscale/src/access_method/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ pub extern "C" fn amrescan(
#[pg_guard]
pub extern "C" fn amgettuple(
scan: pg_sys::IndexScanDesc,
_direction: pg_sys::ScanDirection,
_direction: pg_sys::ScanDirection::Type,
) -> bool {
let scan: PgBox<pg_sys::IndexScanDescData> = unsafe { PgBox::from_pg(scan) };
let state = unsafe { (scan.opaque as *mut TSVScanState).as_mut() }.expect("no scandesc state");
Expand Down
4 changes: 2 additions & 2 deletions pgvectorscale/src/access_method/vacuum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub extern "C" fn ambulkdelete(
let nblocks = unsafe {
pg_sys::RelationGetNumberOfBlocksInFork(
index_relation.as_ptr(),
pg_sys::ForkNumber_MAIN_FORKNUM,
pg_sys::ForkNumber::MAIN_FORKNUM,
)
};

Expand Down Expand Up @@ -127,7 +127,7 @@ pub extern "C" fn amvacuumcleanup(

(*stats).num_pages = pg_sys::RelationGetNumberOfBlocksInFork(
index_relation.as_ptr(),
pg_sys::ForkNumber_MAIN_FORKNUM,
pg_sys::ForkNumber::MAIN_FORKNUM,
);

stats
Expand Down
1 change: 1 addition & 0 deletions pgvectorscale/src/bin/pgrx_embed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::pgrx::pgrx_embed!();
20 changes: 10 additions & 10 deletions pgvectorscale/src/util/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::ops::Deref;
use pgrx::*;

use pgrx::pg_sys::{
BlockNumber, Buffer, BufferGetBlockNumber, ForkNumber_MAIN_FORKNUM, InvalidBlockNumber,
ReadBufferMode_RBM_NORMAL,
BlockNumber, Buffer, BufferGetBlockNumber, ForkNumber, InvalidBlockNumber,
ReadBufferMode
};

pub struct LockRelationForExtension<'a> {
Expand Down Expand Up @@ -74,13 +74,13 @@ impl<'a> LockedBufferExclusive<'a> {

/// Safety: unsafe because tje block number is not verifiwed
unsafe fn read_unchecked(index: &'a PgRelation, block: BlockNumber) -> Self {
let fork_number = ForkNumber_MAIN_FORKNUM;
let fork_number = ForkNumber::MAIN_FORKNUM;

let buf = pg_sys::ReadBufferExtended(
index.as_ptr(),
fork_number,
block,
ReadBufferMode_RBM_NORMAL,
ReadBufferMode::RBM_NORMAL,
std::ptr::null_mut(),
);

Expand All @@ -95,13 +95,13 @@ impl<'a> LockedBufferExclusive<'a> {
/// Obtaining this lock is more restrictive. It will only be obtained once the pin
/// count is 1. Refer to the PG code for `LockBufferForCleanup` for more info
pub unsafe fn read_for_cleanup(index: &'a PgRelation, block: BlockNumber) -> Self {
let fork_number = ForkNumber_MAIN_FORKNUM;
let fork_number = ForkNumber::MAIN_FORKNUM;

let buf = pg_sys::ReadBufferExtended(
index.as_ptr(),
fork_number,
block,
ReadBufferMode_RBM_NORMAL,
ReadBufferMode::RBM_NORMAL,
std::ptr::null_mut(),
);

Expand Down Expand Up @@ -153,14 +153,14 @@ impl<'a> LockedBufferShare<'a> {
///
/// Safety: Safe because it checks the block number doesn't overflow. ReadBufferExtended will throw an error if the block number is out of range for the relation
pub fn read(index: &'a PgRelation, block: BlockNumber) -> Self {
let fork_number = ForkNumber_MAIN_FORKNUM;
let fork_number = ForkNumber::MAIN_FORKNUM;

unsafe {
let buf = pg_sys::ReadBufferExtended(
index.as_ptr(),
fork_number,
block,
ReadBufferMode_RBM_NORMAL,
ReadBufferMode::RBM_NORMAL,
std::ptr::null_mut(),
);

Expand Down Expand Up @@ -210,14 +210,14 @@ impl PinnedBufferShare {
///
/// Safety: Safe because it checks the block number doesn't overflow. ReadBufferExtended will throw an error if the block number is out of range for the relation
pub fn read(index: &PgRelation, block: BlockNumber) -> Self {
let fork_number = ForkNumber_MAIN_FORKNUM;
let fork_number = ForkNumber::MAIN_FORKNUM;

unsafe {
let buf = pg_sys::ReadBufferExtended(
index.as_ptr(),
fork_number,
block,
ReadBufferMode_RBM_NORMAL,
ReadBufferMode::RBM_NORMAL,
std::ptr::null_mut(),
);
PinnedBufferShare { buffer: buf }
Expand Down
6 changes: 3 additions & 3 deletions pgvectorscale/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ impl ItemPointer {
}

pub unsafe fn with_item_pointer_data(ctid: pgrx::pg_sys::ItemPointerData) -> Self {
let ip = pgrx::item_pointer_get_block_number(&ctid);
let off = pgrx::item_pointer_get_offset_number(&ctid);
let ip = pgrx::itemptr::item_pointer_get_block_number(&ctid);
let off = pgrx::itemptr::item_pointer_get_offset_number(&ctid);
Self::new(ip, off)
}

pub fn to_item_pointer_data(&self, ctid: &mut pgrx::pg_sys::ItemPointerData) {
pgrx::item_pointer_set_all(ctid, self.block_number, self.offset)
pgrx::itemptr::item_pointer_set_all(ctid, self.block_number, self.offset)
}

pub unsafe fn read_bytes(self, index: &PgRelation) -> ReadableBuffer {
Expand Down

0 comments on commit 1a3dccc

Please sign in to comment.