Skip to content

Commit 884d363

Browse files
burmeciaCopilot
andauthored
fix: memory context release error when drop fdw modify state (supabase#536)
* fix: fdw modify state drop issue * Update supabase-wrappers/src/memctx.rs Co-authored-by: Copilot <[email protected]> * add bigquery fdw test case for unsupoorted type --------- Co-authored-by: Copilot <[email protected]>
1 parent 7574fa5 commit 884d363

3 files changed

Lines changed: 84 additions & 21 deletions

File tree

supabase-wrappers/src/modify.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,15 @@ pub(super) extern "C-unwind" fn exec_foreign_insert<
369369
(*rinfo).ri_FdwState as *mut FdwModifyState<E, W>,
370370
);
371371

372-
PgMemoryContexts::For(state.tmp_ctx).switch_to(|_| {
372+
let result = PgMemoryContexts::For(state.tmp_ctx).switch_to(|_| {
373373
let row = utils::tuple_table_slot_to_row(slot);
374-
let result = state.insert(&row);
375-
if result.is_err() {
376-
drop_fdw_modify_state(state.as_ptr());
377-
(*rinfo).ri_FdwState = ptr::null::<FdwModifyState<E, W>>() as _;
378-
result.report_unwrap();
379-
}
374+
state.insert(&row)
380375
});
376+
if result.is_err() {
377+
drop_fdw_modify_state(state.as_ptr());
378+
(*rinfo).ri_FdwState = ptr::null::<FdwModifyState<E, W>>() as _;
379+
result.report_unwrap();
380+
}
381381
}
382382

383383
slot
@@ -408,17 +408,19 @@ pub(super) extern "C-unwind" fn exec_foreign_delete<
408408
(*rinfo).ri_FdwState as *mut FdwModifyState<E, W>,
409409
);
410410

411-
PgMemoryContexts::For(state.tmp_ctx).switch_to(|_| {
411+
let result = PgMemoryContexts::For(state.tmp_ctx).switch_to(|_| {
412412
let cell = get_rowid_cell(&state, plan_slot);
413413
if let Some(rowid) = cell {
414-
let result = state.delete(&rowid);
415-
if result.is_err() {
416-
drop_fdw_modify_state(state.as_ptr());
417-
(*rinfo).ri_FdwState = ptr::null::<FdwModifyState<E, W>>() as _;
418-
result.report_unwrap();
419-
}
414+
state.delete(&rowid)
415+
} else {
416+
Ok(())
420417
}
421418
});
419+
if result.is_err() {
420+
drop_fdw_modify_state(state.as_ptr());
421+
(*rinfo).ri_FdwState = ptr::null::<FdwModifyState<E, W>>() as _;
422+
result.report_unwrap();
423+
}
422424
}
423425

424426
slot
@@ -440,7 +442,7 @@ pub(super) extern "C-unwind" fn exec_foreign_update<
440442
(*rinfo).ri_FdwState as *mut FdwModifyState<E, W>,
441443
);
442444

443-
PgMemoryContexts::For(state.tmp_ctx).switch_to(|_| {
445+
let result = PgMemoryContexts::For(state.tmp_ctx).switch_to(|_| {
444446
let rowid_cell = get_rowid_cell(&state, plan_slot);
445447
if let Some(rowid) = rowid_cell {
446448
let mut new_row = utils::tuple_table_slot_to_row(plan_slot);
@@ -465,14 +467,16 @@ pub(super) extern "C-unwind" fn exec_foreign_update<
465467
}
466468
});
467469

468-
let result = state.update(&rowid, &new_row);
469-
if result.is_err() {
470-
drop_fdw_modify_state(state.as_ptr());
471-
(*rinfo).ri_FdwState = ptr::null::<FdwModifyState<E, W>>() as _;
472-
result.report_unwrap();
473-
}
470+
state.update(&rowid, &new_row)
471+
} else {
472+
Ok(())
474473
}
475474
});
475+
if result.is_err() {
476+
drop_fdw_modify_state(state.as_ptr());
477+
(*rinfo).ri_FdwState = ptr::null::<FdwModifyState<E, W>>() as _;
478+
result.report_unwrap();
479+
}
476480
}
477481

478482
slot

wrappers/dockerfiles/bigquery/data.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ projects:
5656
attrs: '{"aa": 123.45}'
5757
signup_dt: "2023-10-21"
5858
created_at: "2023-10-21T00:00:00"
59+
- id: test_unsupported_type
60+
columns:
61+
- name: id
62+
type: INTEGER
63+
mode: REQUIRED
64+
- name: time_col
65+
type: TIME
66+
data:
67+
- id: 1
68+
time_col: "12:30:00.45"

wrappers/src/fdw/bigquery_fdw/tests.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,53 @@ mod tests {
152152
assert_eq!(results, vec!["foo", "bar"]);
153153
});
154154
}
155+
156+
#[pg_test]
157+
#[should_panic]
158+
fn bigquery_unsupported_type() {
159+
Spi::connect_mut(|c| {
160+
c.update(
161+
r#"CREATE FOREIGN DATA WRAPPER bigquery_wrapper
162+
HANDLER big_query_fdw_handler VALIDATOR big_query_fdw_validator"#,
163+
None,
164+
&[],
165+
)
166+
.unwrap();
167+
c.update(
168+
r#"CREATE SERVER my_bigquery_server
169+
FOREIGN DATA WRAPPER bigquery_wrapper
170+
OPTIONS (
171+
project_id 'test_project',
172+
dataset_id 'test_dataset',
173+
api_endpoint 'http://localhost:9111',
174+
mock_auth 'true'
175+
)"#,
176+
None,
177+
&[],
178+
)
179+
.unwrap();
180+
c.update(
181+
r#"
182+
CREATE FOREIGN TABLE test_unsupported_type (
183+
id bigint,
184+
time_col time
185+
)
186+
SERVER my_bigquery_server
187+
OPTIONS (
188+
table 'test_unsupported_type',
189+
rowid_column 'id'
190+
)
191+
"#,
192+
None,
193+
&[],
194+
)
195+
.unwrap();
196+
197+
let _results = c.update(
198+
"INSERT INTO test_unsupported_type (id, time_col) VALUES (42, '12:30:00.45')",
199+
None,
200+
&[],
201+
);
202+
});
203+
}
155204
}

0 commit comments

Comments
 (0)