Skip to content

Commit d2f87ba

Browse files
committed
fix: check mvcc in heap fetch (#336)
#335 --------- Signed-off-by: usamoi <usamoi@outlook.com>
1 parent c1a7f76 commit d2f87ba

6 files changed

Lines changed: 99 additions & 22 deletions

File tree

.github/workflows/check.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,28 @@ jobs:
137137
uses: actions/checkout@v4
138138

139139
- name: Clippy
140-
run: cargo clippy --workspace --exclude vchord
140+
run: cargo clippy --locked --workspace --exclude vchord
141141

142142
- name: Cargo Test
143-
run: cargo test --workspace --exclude vchord --exclude simd --no-fail-fast
143+
run: cargo test --locked --workspace --exclude vchord --exclude simd --no-fail-fast
144144

145145
- name: Cargo Test (simd)
146146
run: |
147147
if [ "$(uname -m)" == "x86_64" ]; then
148148
cargo \
149149
--config 'target.'\''cfg(all())'\''.runner = ["/opt/sde/sde64", "-spr", "--"]' \
150-
test -p simd -- --no-capture
150+
test --locked -p simd -- --no-capture
151151
fi
152152
if [ "$(uname -m)" == "aarch64" ]; then
153153
cargo \
154154
--config 'target.'\''cfg(all())'\''.runner = ["qemu-aarch64-static", "-cpu", "max,sve-default-vector-length=16"]' \
155-
test -p simd -- --no-capture
155+
test --locked -p simd -- --no-capture
156156
cargo \
157157
--config 'target.'\''cfg(all())'\''.runner = ["qemu-aarch64-static", "-cpu", "max,sve-default-vector-length=32"]' \
158-
test -p simd -- --no-capture
158+
test --locked -p simd -- --no-capture
159159
cargo \
160160
--config 'target.'\''cfg(all())'\''.runner = ["qemu-aarch64-static", "-cpu", "max,sve-default-vector-length=64"]' \
161-
test -p simd -- --no-capture
161+
test --locked -p simd -- --no-capture
162162
fi
163163
164164
psql:
@@ -218,7 +218,7 @@ jobs:
218218
uses: actions/checkout@v4
219219

220220
- name: Clippy
221-
run: cargo clippy --target ${{ matrix.arch }}-unknown-linux-gnu -p vchord --features pg${{ matrix.version }} --no-deps
221+
run: cargo clippy --locked --target ${{ matrix.arch }}-unknown-linux-gnu -p vchord --features pg${{ matrix.version }} --no-deps
222222

223223
- name: Install
224224
run: |
@@ -301,7 +301,7 @@ jobs:
301301

302302
- name: Clippy
303303
run: |
304-
cargo clippy -p vchord --target ${{ matrix.arch }}-apple-darwin --features pg${{ matrix.version }} --no-deps
304+
cargo clippy --locked -p vchord --target ${{ matrix.arch }}-apple-darwin --features pg${{ matrix.version }} --no-deps
305305
306306
- name: Install
307307
run: |
@@ -407,7 +407,7 @@ jobs:
407407

408408
- name: Clippy
409409
run: |
410-
cargo clippy --target ${{ matrix.arch }}-pc-windows-msvc -p vchord --features pg${{ matrix.version }} --no-deps
410+
cargo clippy --locked --target ${{ matrix.arch }}-pc-windows-msvc -p vchord --features pg${{ matrix.version }} --no-deps
411411
412412
- name: Install
413413
run: |
@@ -514,7 +514,7 @@ jobs:
514514
- name: Clippy
515515
run: |
516516
. "$HOME/.cargo/env"
517-
cargo clippy --target ${{ matrix.arch }}-unknown-linux-musl -p vchord --features pg${{ matrix.version }} --no-deps
517+
cargo clippy --locked --target ${{ matrix.arch }}-unknown-linux-musl -p vchord --features pg${{ matrix.version }} --no-deps
518518
519519
- name: Install
520520
run: |
@@ -711,10 +711,10 @@ jobs:
711711
- name: Clippy & Test
712712
run: |
713713
PGRX_PG_CONFIG_PATH=pg_config \
714-
cargo clippy --target ${{ matrix.rust_triple }} \
714+
cargo clippy --locked --target ${{ matrix.rust_triple }} \
715715
--workspace --features pg${{ matrix.version }}
716716
PGRX_PG_CONFIG_PATH=pg_config \
717-
cargo test --target ${{ matrix.rust_triple }} \
717+
cargo test --locked --target ${{ matrix.rust_triple }} \
718718
--workspace --exclude vchord --no-fail-fast \
719719
-- --no-capture
720720

crates/make/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ fn build(
168168
) -> Result<PathBuf, Box<dyn Error>> {
169169
let mut command = Command::new("cargo");
170170
command
171-
.args(["build", "-p", "vchord", "--lib"])
171+
.args(["build", "--locked"])
172+
.args(["-p", "vchord", "--lib"])
172173
.args(["--profile", profile])
173174
.args(["--target", target])
174175
.args(["--features", pg_version])
@@ -265,7 +266,8 @@ fn generate(
265266
)?;
266267
let mut command = Command::new("cargo");
267268
command
268-
.args(["rustc", "-p", "vchord", "--bin", "pgrx_embed_vchord"])
269+
.args(["rustc", "--locked"])
270+
.args(["-p", "vchord", "--bin", "pgrx_embed_vchord"])
269271
.args(["--profile", profile])
270272
.args(["--target", target])
271273
.args(["--features", pg_version])

src/index/fetcher.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct HeapFetcher {
4848
econtext: *mut pgrx::pg_sys::ExprContext,
4949
heap_relation: pgrx::pg_sys::Relation,
5050
snapshot: pgrx::pg_sys::Snapshot,
51+
heapfetch: *mut pgrx::pg_sys::IndexFetchTableData,
5152
slot: *mut pgrx::pg_sys::TupleTableSlot,
5253
values: [Datum; 32],
5354
is_nulls: [bool; 32],
@@ -59,6 +60,7 @@ impl HeapFetcher {
5960
index_relation: pgrx::pg_sys::Relation,
6061
heap_relation: pgrx::pg_sys::Relation,
6162
snapshot: pgrx::pg_sys::Snapshot,
63+
heapfetch: *mut pgrx::pg_sys::IndexFetchTableData,
6264
hack: *mut pgrx::pg_sys::IndexScanState,
6365
) -> Self {
6466
unsafe {
@@ -71,6 +73,7 @@ impl HeapFetcher {
7173
econtext,
7274
heap_relation,
7375
snapshot,
76+
heapfetch,
7477
slot: pgrx::pg_sys::table_slot_create(heap_relation, std::ptr::null_mut()),
7578
values: [Datum::null(); 32],
7679
is_nulls: [true; 32],
@@ -99,16 +102,49 @@ impl Fetcher for HeapFetcher {
99102
use pgrx::pg_sys::ffi::pg_guard_ffi_boundary;
100103
let mut ctid = key_to_ctid(key);
101104
let table_am = (*self.heap_relation).rd_tableam;
102-
let fetch_row_version = (*table_am)
103-
.tuple_fetch_row_version
105+
let index_fetch_tuple = (*table_am)
106+
.index_fetch_tuple
104107
.expect("unsupported heap access method");
105-
#[allow(ffi_unwind_calls, reason = "protected by pg_guard_ffi_boundary")]
106-
if pg_guard_ffi_boundary(|| {
107-
!fetch_row_version(self.heap_relation, &mut ctid, self.snapshot, self.slot)
108-
}) {
109-
return None;
108+
let found = 'a: {
109+
let mut call_again = false;
110+
let mut all_dead = false;
111+
#[allow(ffi_unwind_calls, reason = "protected by pg_guard_ffi_boundary")]
112+
let found = pg_guard_ffi_boundary(|| {
113+
index_fetch_tuple(
114+
self.heapfetch,
115+
&mut ctid,
116+
self.snapshot,
117+
self.slot,
118+
&mut call_again,
119+
&mut all_dead,
120+
)
121+
});
122+
if found {
123+
break 'a true;
124+
}
125+
while call_again {
126+
#[allow(ffi_unwind_calls, reason = "protected by pg_guard_ffi_boundary")]
127+
let found = pg_guard_ffi_boundary(|| {
128+
index_fetch_tuple(
129+
self.heapfetch,
130+
&mut ctid,
131+
self.snapshot,
132+
self.slot,
133+
&mut call_again,
134+
&mut all_dead,
135+
)
136+
});
137+
if found {
138+
break 'a true;
139+
}
140+
}
141+
false
142+
};
143+
if found {
144+
Some(HeapTuple { this: self })
145+
} else {
146+
None
110147
}
111-
Some(HeapTuple { this: self })
112148
}
113149
}
114150
}

src/index/vchordg/am/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ pub unsafe extern "C-unwind" fn amrescan(
365365
(*scan).indexRelation,
366366
(*scan).heapRelation,
367367
(*scan).xs_snapshot,
368+
(*scan).xs_heapfetch,
368369
if let Some(hack) = hack {
369370
hack.as_ptr()
370371
} else {

src/index/vchordrq/am/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ pub unsafe extern "C-unwind" fn amrescan(
448448
(*scan).indexRelation,
449449
(*scan).heapRelation,
450450
(*scan).xs_snapshot,
451+
(*scan).xs_heapfetch,
451452
if let Some(hack) = hack {
452453
hack.as_ptr()
453454
} else {

tests/vchordrq/issue_335.slt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
statement ok
2+
SET enable_seqscan = off;
3+
4+
statement ok
5+
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3), visible boolean default true);
6+
7+
statement ok
8+
INSERT INTO items (embedding) SELECT ARRAY[i * 0.001, i * 0.001, i * 0.001]::real[] FROM generate_series(1, 1000) s(i);
9+
10+
statement ok
11+
CREATE INDEX ON items USING vchordrq (embedding vector_l2_ops);
12+
13+
statement ok
14+
SET vchordrq.prefilter = on;
15+
16+
query I
17+
SELECT id FROM items WHERE visible = true ORDER BY embedding <-> '[0.0031,0.0031,0.0031]' LIMIT 3;
18+
----
19+
3
20+
4
21+
2
22+
23+
statement ok
24+
UPDATE items SET visible = false WHERE id = 3;
25+
26+
statement ok
27+
UPDATE items SET visible = true WHERE id = 3;
28+
29+
query I
30+
SELECT id FROM items WHERE visible = true ORDER BY embedding <-> '[0.0031,0.0031,0.0031]' LIMIT 3;
31+
----
32+
3
33+
4
34+
2
35+
36+
statement ok
37+
DROP TABLE items;

0 commit comments

Comments
 (0)