Skip to content

Commit f6efa22

Browse files
marcobambiniclaude
andcommitted
fix: preserve prepared plan across databasevm_reset() in PostgreSQL backend
Previously, databasevm_reset() called databasevm_clear_bindings() which destroyed the SPIPlanPtr on every reset, forcing a full SPI_prepare on each bind/step cycle. This negated the benefit of caching statements in cloudsync_table_context. Now reset() only clears parameter values while keeping the plan, types, and nparams intact for reuse. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6746f5b commit f6efa22

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/postgresql/database_postgresql.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,15 @@ void databasevm_reset (dbvm_t *vm) {
17261726
SPI_tuptable = NULL;
17271727
}
17281728
stmt->executed_nonselect = false;
1729-
databasevm_clear_bindings(vm);
1729+
1730+
// Reset parameter values but keep the plan, types, and nparams intact.
1731+
// The prepared plan can be reused with new values of the same types,
1732+
// avoiding the cost of re-planning on every iteration.
1733+
if (stmt->bind_mcxt) MemoryContextReset(stmt->bind_mcxt);
1734+
for (int i = 0; i < stmt->nparams; i++) {
1735+
stmt->values[i] = (Datum) 0;
1736+
stmt->nulls[i] = 'n';
1737+
}
17301738
}
17311739

17321740
void databasevm_clear_bindings (dbvm_t *vm) {

0 commit comments

Comments
 (0)