spg_engine/execute.rs
1//! Statement execution + prepared-statement dispatch, split out of
2//! `lib.rs` (lib.rs split 17). The public `execute` / `execute_in` /
3//! `execute_with_cancel` entry points, the `prepare` / `prepare_cached`
4//! / `describe_prepared` / `execute_prepared` prepared-statement path,
5//! and the internal pipeline (`execute_inner_with_cancel` →
6//! `execute_stmt_with_cancel`) that pre-resolves clock / sequence /
7//! placeholder rewrites and routes each parsed Statement to its domain
8//! handler (DDL / DML / SELECT / transaction / SHOW / …). Whole
9//! `impl Engine` methods reached via the Engine type, so the public
10//! surface is unchanged; `execute_stmt_with_cancel` is pub(crate) for
11//! the plpgsql + trigger re-entry paths.
12
13use alloc::string::String;
14use alloc::vec::Vec;
15
16use spg_sql::ast::Statement;
17use spg_sql::parser::{self, ParseError};
18use spg_storage::{ColumnSchema, Value};
19
20use crate::describe;
21use crate::{
22 CancelToken, Engine, EngineError, IMPLICIT_TX, QueryResult, TxId, expand_group_by_all,
23 plan_cache, reorder, resolve_order_by_position, rewrite_clock_calls, substitute_placeholders,
24};
25
26/// v7.38 Epic P — turn a caught panic payload into an
27/// [`EngineError::Internal`]. Recovers a human-readable detail from the
28/// common payload shapes (`&str` / `String`, and the injection framework's
29/// typed `InjectedError`) so the wire layer sends a clean message; falls
30/// back to a generic string when the payload type is opaque.
31// r1051 — a stray `#[cfg(feature = "std")]` sat here (misattached
32// between two doc comments) and gated this core/alloc-only function
33// out of the no_std build while its two call sites stayed: no_std had
34// not compiled since the GUC round, and nothing in the gate builds
35// no_std, so nothing said so. The suite's first no_std probe did.
36/// v7.38 (read01 P3.17) — reject a clearly-invalid value for a handful of
37/// well-known typed GUCs (boolean / memory-size / duration), so a typo
38/// like `SET work_mem = 'bogus'` errors like PG instead of silently
39/// storing junk. Conservative: only GUCs whose type is unambiguous are
40/// checked; every other name is accepted so pg_dump preambles and
41/// unknown settings still load.
42fn validate_known_guc(name: &str, value: &str) -> Result<(), EngineError> {
43 let key = name.to_ascii_lowercase();
44 let bad = || {
45 EngineError::Unsupported(alloc::format!(
46 "invalid value for parameter \"{name}\": \"{value}\""
47 ))
48 };
49 let is_bool = matches!(
50 value.trim().to_ascii_lowercase().as_str(),
51 "on" | "off" | "true" | "false" | "yes" | "no" | "1" | "0"
52 );
53 // Split a `<number><unit>` GUC value into its numeric head + unit tail.
54 let split_unit = |s: &str| -> (String, String) {
55 let st = s.trim();
56 let cut = st
57 .find(|c: char| c.is_ascii_alphabetic())
58 .unwrap_or(st.len());
59 (
60 String::from(st[..cut].trim()),
61 st[cut..].trim().to_ascii_lowercase(),
62 )
63 };
64 let (num, unit) = split_unit(value);
65 let is_size =
66 num.parse::<f64>().is_ok() && matches!(unit.as_str(), "" | "b" | "kb" | "mb" | "gb" | "tb");
67 let is_duration = num.parse::<i64>().is_ok()
68 && matches!(unit.as_str(), "" | "us" | "ms" | "s" | "min" | "h" | "d");
69 match key.as_str() {
70 "enable_seqscan"
71 | "enable_indexscan"
72 | "enable_bitmapscan"
73 | "enable_indexonlyscan"
74 | "enable_hashjoin"
75 | "enable_mergejoin"
76 | "enable_nestloop"
77 | "autovacuum"
78 | "fsync"
79 | "full_page_writes" => {
80 if !is_bool {
81 return Err(bad());
82 }
83 }
84 "work_mem"
85 | "maintenance_work_mem"
86 | "shared_buffers"
87 | "temp_buffers"
88 | "effective_cache_size"
89 | "wal_buffers" => {
90 if !is_size {
91 return Err(bad());
92 }
93 }
94 "statement_timeout" | "lock_timeout" | "idle_in_transaction_session_timeout" => {
95 if !is_duration {
96 return Err(bad());
97 }
98 }
99 // v7.39 (round 171) — synchronous_commit is a real, session-level
100 // durability control now (the embedded execute path gates its
101 // WAL-fsync wait on it); validate PG's value domain.
102 "synchronous_commit" => {
103 if !matches!(
104 value.to_ascii_lowercase().as_str(),
105 "on" | "off"
106 | "local"
107 | "remote_write"
108 | "remote_apply"
109 | "true"
110 | "false"
111 | "0"
112 | "1"
113 ) {
114 return Err(bad());
115 }
116 }
117 // v7.39 (round 204) — enum GUCs reject an out-of-domain value
118 // like PG (`SET client_min_messages = bogus` errors). PG's
119 // message quotes the value with a trailing hint listing the
120 // valid set; we match the leading, stable clause.
121 "client_min_messages" => {
122 if !matches!(
123 value.trim().to_ascii_lowercase().as_str(),
124 "debug5"
125 | "debug4"
126 | "debug3"
127 | "debug2"
128 | "debug1"
129 | "log"
130 | "notice"
131 | "warning"
132 | "error"
133 | "fatal"
134 | "panic"
135 ) {
136 return Err(bad());
137 }
138 }
139 // v7.39 (GUC knife 3) — the render GUCs reject invalid values
140 // with PG's own texts (canonical-caps parameter names).
141 "datestyle" => {
142 if crate::session::parse_datestyle_parts(value, crate::eval::RenderStyle::default())
143 .is_none()
144 {
145 return Err(EngineError::Unsupported(alloc::format!(
146 "invalid value for parameter \"DateStyle\": \"{value}\""
147 )));
148 }
149 }
150 "intervalstyle" => {
151 if crate::session::parse_intervalstyle(value).is_none() {
152 return Err(EngineError::Unsupported(alloc::format!(
153 "invalid value for parameter \"IntervalStyle\": \"{value}\""
154 )));
155 }
156 }
157 "extra_float_digits" => match value.trim().parse::<i64>() {
158 Ok(n) if (-15..=3).contains(&n) => {}
159 Ok(n) => {
160 return Err(EngineError::Unsupported(alloc::format!(
161 "{n} is outside the valid range for parameter \
162 \"extra_float_digits\" (-15 .. 3)"
163 )));
164 }
165 Err(_) => return Err(bad()),
166 },
167 _ => {}
168 }
169 Ok(())
170}
171
172fn panic_payload_to_engine_error(payload: &(dyn core::any::Any + Send)) -> EngineError {
173 // The injection framework panics with a typed error; surface its
174 // message so tests get a deterministic, informative string.
175 #[cfg(feature = "injection-points")]
176 if let Some(inj) = payload.downcast_ref::<crate::testkit::injection::InjectedError>() {
177 return EngineError::Internal(alloc::format!("query aborted by internal error: {inj}"));
178 }
179 let detail = payload
180 .downcast_ref::<&'static str>()
181 .map(|s| String::from(*s))
182 .or_else(|| payload.downcast_ref::<String>().cloned());
183 match detail {
184 Some(d) => EngineError::Internal(alloc::format!("query aborted by internal error: {d}")),
185 None => EngineError::Internal(String::from("query aborted by internal error")),
186 }
187}
188
189impl Engine {
190 pub fn execute(&mut self, sql: &str) -> Result<QueryResult, EngineError> {
191 self.execute_in_with_cancel(sql, IMPLICIT_TX, CancelToken::none())
192 }
193
194 /// v7.38 (read01 P3.20) — handle a bare `SELECT set_config(name, value,
195 /// is_local)` by writing the GUC to the same session store `SET` uses
196 /// (honouring `is_local` via the transaction undo log), so set_config,
197 /// SHOW, current_setting, and pg_settings all agree. Returns `None`
198 /// (fall through to the ordinary read-only path) unless the statement is
199 /// exactly that shape — set_config buried in a FROM/WHERE/CTE, or over a
200 /// non-text name, keeps the old value-returning behaviour.
201 fn try_exec_set_config(
202 &mut self,
203 s: &spg_sql::ast::SelectStatement,
204 ) -> Result<Option<QueryResult>, EngineError> {
205 use spg_sql::ast::{Expr, SelectItem};
206 if s.from.is_some() || s.where_.is_some() || !s.ctes.is_empty() || s.items.len() != 1 {
207 return Ok(None);
208 }
209 let SelectItem::Expr { expr, .. } = &s.items[0] else {
210 return Ok(None);
211 };
212 let Expr::FunctionCall { name, args } = expr else {
213 return Ok(None);
214 };
215 if !(name.eq_ignore_ascii_case("set_config")
216 || name.eq_ignore_ascii_case("pg_catalog.set_config"))
217 || !(args.len() == 2 || args.len() == 3)
218 {
219 return Ok(None);
220 }
221 // Evaluate the arguments against an empty row.
222 let empty: Vec<ColumnSchema> = Vec::new();
223 let (name_v, value_v, local_v);
224 {
225 let ctx = self.ev_ctx(&empty, None);
226 let dummy = spg_storage::Row::new(Vec::new());
227 name_v = crate::eval::eval_expr(&args[0], &dummy, &ctx).map_err(EngineError::Eval)?;
228 value_v = crate::eval::eval_expr(&args[1], &dummy, &ctx).map_err(EngineError::Eval)?;
229 local_v = if args.len() == 3 {
230 crate::eval::eval_expr(&args[2], &dummy, &ctx).map_err(EngineError::Eval)?
231 } else {
232 Value::Bool(false)
233 };
234 }
235 let single = |v: Value<'static>| QueryResult::Rows {
236 columns: alloc::vec![ColumnSchema::new(
237 "set_config",
238 spg_storage::DataType::Text,
239 true
240 )],
241 rows: alloc::vec![spg_storage::Row::new(alloc::vec![v])],
242 };
243 let pname = match name_v {
244 Value::Text(s) => s.into_owned(),
245 // set_config(NULL, …) is a no-op returning NULL (PG).
246 Value::Null => return Ok(Some(single(Value::Null))),
247 _ => return Ok(None),
248 };
249 let is_local = matches!(local_v, Value::Bool(true));
250 // A NULL value resets the GUC to its default (PG), returning NULL.
251 let pval = match value_v {
252 Value::Text(s) => s.into_owned(),
253 Value::Null => {
254 self.session_params.remove(&pname.to_ascii_lowercase());
255 self.refresh_render_style();
256 return Ok(Some(single(Value::Null)));
257 }
258 _ => return Ok(None),
259 };
260 validate_known_guc(&pname, &pval)?;
261 if is_local {
262 if self.in_transaction() {
263 let prior = self.session_param(&pname).map(String::from);
264 self.local_guc_saves.push((pname.clone(), prior));
265 self.set_session_param(pname, spg_sql::ast::SetValue::String(pval.clone()));
266 }
267 } else {
268 self.set_session_param(pname, spg_sql::ast::SetValue::String(pval.clone()));
269 }
270 Ok(Some(single(Value::text(pval))))
271 }
272
273 /// v4.5 — write path with cooperative cancellation. Same dispatch
274 /// as `execute_in_with_cancel(sql, IMPLICIT_TX, cancel)`. Kept as
275 /// a separate entry point for backward-compat with the v4.5
276 /// public API.
277 pub fn execute_with_cancel(
278 &mut self,
279 sql: &str,
280 cancel: CancelToken<'_>,
281 ) -> Result<QueryResult, EngineError> {
282 self.execute_in_with_cancel(sql, IMPLICIT_TX, cancel)
283 }
284
285 /// v4.41.1 multi-slot write entry. Routes `sql` through the TX
286 /// slot identified by `tx_id` so spg-server dispatch can scope
287 /// each implicit-wrap BEGIN..stmt..COMMIT to its own slot in
288 /// `tx_catalogs`. `IMPLICIT_TX` is the legacy single-slot path
289 /// every other caller (engine self-tests, replay, spg-embedded)
290 /// implicitly takes via `execute()` / `execute_with_cancel()`.
291 pub fn execute_in(&mut self, sql: &str, tx_id: TxId) -> Result<QueryResult, EngineError> {
292 self.execute_in_with_cancel(sql, tx_id, CancelToken::none())
293 }
294
295 /// v4.41.1 write path with cooperative cancellation + explicit TX
296 /// scope. Sets `self.current_tx` for the duration of the call so
297 /// every `exec_*` helper transparently sees its TX's shadow
298 /// catalog and savepoint stack; restores on exit so the field is
299 /// only valid mid-call (no leakage across calls).
300 pub fn execute_in_with_cancel(
301 &mut self,
302 sql: &str,
303 tx_id: TxId,
304 cancel: CancelToken<'_>,
305 ) -> Result<QueryResult, EngineError> {
306 // v7.38 P0 元机制 A — establish the per-engine injection
307 // scope for the duration of this execute. The guard pops
308 // the store on drop so nested or sibling engines don't see
309 // ours. No-op in release builds (feature off).
310 let _inj = self.enter_injection_scope();
311 // v7.39 (read01 round 46) — NOTICEs are per-statement: clear the
312 // buffer here so one statement's "…, skipping" can never leak into
313 // the next one's NoticeResponse batch.
314 self.pending_notices.clear();
315 let saved = self.current_tx;
316 self.current_tx = Some(tx_id);
317 // v7.37.15 (Epic W slice 2) — memoized autocommit writer version
318 // is scoped to one statement. Save + reset like `current_tx` so
319 // a re-entrant execute (e.g. deferred trigger SQL) can't leak its
320 // version into ours, and ours never leaks to the next statement.
321 let saved_stmt_wv = self.stmt_writer_version;
322 self.stmt_writer_version = None;
323 // v7.34 (crash-recovery P0 #2) — row-level redo capture. Arm the
324 // active catalog before dispatch; on success drain the physical
325 // changes into `last_redo` for the embedding layer's WAL, on
326 // failure discard them (a failed statement leaves no redo; the
327 // drain clears the tables' capture buffers either way).
328 // v7.39 (round 736, S14/B3) — a delta-maintainable materialized
329 // view needs the same physical change stream the WAL reads, so
330 // its presence enables capture too (the fan-out below copies;
331 // `last_redo` stays the embedding layer's alone).
332 let matview_capture = !self.matview_maintainable.is_empty();
333 if self.redo_capture || matview_capture {
334 self.active_catalog_mut().enable_redo_all();
335 }
336 // v7.38 Epic P (panic isolation) — run statement execution
337 // behind a catch_unwind firewall so a panic in query
338 // processing surfaces as an ordinary `EngineError` (after
339 // rolling the in-flight tx back) instead of unwinding through
340 // the server's engine `RwLock` write guard (which would poison
341 // it) or aborting the process. NO-OP under the release
342 // `panic = "abort"` profile — the process aborts before any
343 // unwind reaches here; active in dev/test (`panic = "unwind"`)
344 // and once a later slice flips the release profile.
345 let pre_in_tx = self.in_transaction();
346 let result = self.execute_inner_catching(sql, cancel);
347 // v7.39 (round 426) — MySQL's ROW_COUNT() reads what the LAST
348 // statement did. Measured on MariaDB 11: a DML statement leaves the
349 // number of rows it changed (0 when it matched none), a
350 // row-returning statement leaves -1, and DDL leaves 0. One place,
351 // because every statement funnels through here — and it must be
352 // AFTER the dispatch, so ROW_COUNT()'s own SELECT is what sets -1
353 // for the call after it (as MariaDB does).
354 //
355 // A failed statement leaves the previous value alone: MariaDB keeps
356 // the last successful statement's count through an error.
357 if let Ok(res) = &result {
358 self.row_count = match res {
359 QueryResult::CommandOk { affected, .. } => i64::try_from(*affected).unwrap_or(-1),
360 QueryResult::Rows { .. } => -1,
361 };
362 }
363 // v7.39 (pg_stat knife A) — PG counts every statement outside a
364 // transaction block as one implicit xact (commit on success,
365 // rollback on error). Statements INSIDE a block are counted
366 // once, by exec_commit / exec_rollback; BEGIN itself (state
367 // flips outside -> inside) and the block-closers (inside ->
368 // outside, counted in their exec fns) are skipped here.
369 if !pre_in_tx && !self.in_transaction() {
370 let ctr = if result.is_ok() {
371 &self.xact_commit
372 } else {
373 &self.xact_rollback
374 };
375 ctr.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
376 }
377 // r196 — a statement that did NOT run inside its own open tx
378 // slot (autocommit, or a COMMIT/ROLLBACK that just closed its
379 // slot) may have moved the committed base; bump the epoch so
380 // OTHER open txs know their next RC rebase is real. The test
381 // must be per-statement (`tx_catalogs` membership of THIS
382 // call's tx_id), not the global `in_transaction()` — a
383 // concurrent autocommit while some tx is open is exactly the
384 // case the rebase exists for (the first cut used the global
385 // check and 10 isolation pins caught the missed bumps).
386 // Deliberately over-approximate (reads bump too — an extra
387 // rebase is only slower, never wrong).
388 if !self.tx_catalogs.contains_key(&tx_id) {
389 self.commit_epoch = self.commit_epoch.wrapping_add(1);
390 // v7.39 (round 306) — large-object descriptors live only as
391 // long as the transaction that opened them, so this is
392 // exactly where they die: an autocommit statement (the
393 // implicit transaction just ended) or the COMMIT / ROLLBACK
394 // that closed the slot. Numbering restarts from 0, as PG's
395 // does. Same per-slot witness as the epoch bump above —
396 // another connection's open transaction must not keep this
397 // one's descriptors alive.
398 self.lo_descriptors.clear();
399 self.lo_next_fd = 0;
400 }
401 if self.redo_capture || matview_capture {
402 let mut drained = self.active_catalog_mut().drain_redo();
403 if result.is_ok() {
404 if matview_capture {
405 self.fan_out_matview_deltas(&drained);
406 }
407 // v7.37.15 (Epic W slice 2) — stamp the real committing
408 // writer version onto every change this statement
409 // produced. All changes from one statement share the one
410 // version (the statement's xmin/xmax): in autocommit it's
411 // the memoized value the writes already used; inside an
412 // explicit tx it's the deterministic tx entry. Purely
413 // additive metadata — replay still resolves by physical
414 // position and ignores `writer_version` (later slice).
415 if !drained.is_empty() {
416 let v = self.writer_version_for_current_stmt();
417 for change in &mut drained {
418 change.set_writer_version(v);
419 }
420 }
421 if self.redo_capture {
422 self.last_redo = drained;
423 }
424 }
425 }
426 self.current_tx = saved;
427 self.stmt_writer_version = saved_stmt_wv;
428 result
429 }
430
431 /// v6.1.1 — parse and pre-process a SQL string ONCE so the
432 /// resulting [`Statement`] can be cached and re-executed via
433 /// [`Engine::execute_prepared`]. Returns the same `Statement`
434 /// the simple-query path would synthesise internally (clock
435 /// rewrites + ORDER BY position-ref resolution applied at
436 /// prepare time, since both are session-independent). The
437 /// `$N` placeholders in the SQL stay as `Expr::Placeholder(n)`
438 /// nodes; they're resolved to concrete values per-call by
439 /// `execute_prepared`'s substitution walk.
440 ///
441 /// Pgwire's `Parse` (P) message lands here.
442 pub fn prepare(&self, sql: &str) -> Result<Statement, ParseError> {
443 let mut stmt = parser::parse_statement_with(sql, self.backslash_escapes)?;
444 self.preprocess(&mut stmt);
445 Ok(stmt)
446 }
447
448 /// r1043 — every pre-pass a parsed statement gets before execution,
449 /// in one place.
450 ///
451 /// There were two copies. `prepare` had clock rewrites, `GROUP BY
452 /// ALL` expansion, ORDER BY position resolution and the JOIN reorder;
453 /// `execute_readonly_with_cancel` — the path EVERY autocommit SELECT
454 /// takes over the wire — had the same list minus the `GROUP BY ALL`
455 /// expansion, and then r1042 added constant folding to one of them.
456 ///
457 /// The result was a plan that `EXPLAIN` described and the wire did not
458 /// run: `WHERE b = decode(lpad(to_hex(7),16,'0'),'hex')` planned as an
459 /// index scan and took 194 ms, against 0.009 ms for the same statement
460 /// through the embedded API, on the same build and the same 400,000
461 /// rows. EXPLAIN went through `prepare`; the query did not.
462 ///
463 /// One function, both callers. A pass added here reaches every route
464 /// by construction rather than by remembering.
465 pub(crate) fn preprocess(&self, stmt: &mut Statement) {
466 let now_micros = self.clock.map(|f| f());
467 rewrite_clock_calls(
468 stmt,
469 now_micros,
470 self.backslash_escapes,
471 now_micros.map_or(0, |n| self.session_tz_offset_at(n)),
472 );
473 // r1042 — evaluate the constant parts of every predicate once,
474 // here, instead of once per row. A cast on a literal is the
475 // common case and it was costing an index seek: `WHERE id = 7`
476 // sought and `WHERE id = 7::int` scanned, 23x apart at 400k rows.
477 crate::constfold::fold_statement(stmt);
478 if let Statement::Select(s) = stmt {
479 // v6.4.1 — expand `GROUP BY ALL` to every non-aggregate
480 // SELECT-list item BEFORE position / alias resolution so
481 // downstream passes see the explicit list.
482 expand_group_by_all(s);
483 resolve_order_by_position(s);
484 // v6.2.3 — cost-based JOIN reorder. No-op for
485 // single-table FROMs or any non-INNER join shape.
486 // v7.38 元机制 D — `SPG_TEST_PLAN_DETERMINISTIC=1` gates
487 // this so regression tests pin a stable join order.
488 reorder::reorder_joins_with(
489 s,
490 &self.catalog,
491 &self.statistics,
492 self.env_cfg.plan_deterministic,
493 );
494 }
495 }
496
497 /// v6.3.0 — cached prepare. Returns a cloned `Statement` from
498 /// the plan cache on hit, runs the full `prepare()` path on miss
499 /// and inserts the resulting plan before returning. Skipping the
500 /// parse + JOIN-reorder pipeline on hit is the dominant win for
501 /// JDBC / sqlx / pgx clients that reuse the same SQL string.
502 ///
503 /// Returns a cloned `Statement` (not a borrow) because the
504 /// pgwire layer owns its `PreparedStmt` map per-session and the
505 /// engine-level cache must stay available for other sessions.
506 /// Clone cost on a 5-table JOIN AST is well under the parse cost
507 /// it replaces.
508 /// v7.39 (round 192) — bump the engine-side per-table DML
509 /// counters (pg_stat_user_tables n_tup_*). Non-transactional by
510 /// design, like PG's stats collector.
511 pub(crate) fn note_table_write(&mut self, table: &str, ins: u64, upd: u64, del: u64) {
512 let e = self
513 .table_write_stats
514 .entry(alloc::string::String::from(table))
515 .or_insert((0, 0, 0));
516 e.0 = e.0.saturating_add(ins);
517 e.1 = e.1.saturating_add(upd);
518 e.2 = e.2.saturating_add(del);
519 }
520
521 pub fn prepare_cached(&mut self, sql: &str) -> Result<Statement, ParseError> {
522 // v7.39 (round 200) — don't cache LARGE statements. A 24 KB
523 // multi-row VALUES INSERT paid a full AST deep-clone (~640 µs)
524 // just to enter the plan cache, where a unique bulk statement
525 // is never reused — and at that size a cache hit would only
526 // save the ~190 µs re-parse anyway. The threshold keeps every
527 // ORM-shaped statement (small, repeated) on the cached path.
528 const PLAN_CACHE_MAX_SQL_BYTES: usize = 4096;
529 if sql.len() > PLAN_CACHE_MAX_SQL_BYTES {
530 return self.prepare(sql);
531 }
532 // v6.3.1 — version-aware lookup. If the cached plan was
533 // prepared before the most recent ANALYZE, evict and replan.
534 let current_version = self.statistics.version();
535 if let Some(plan) = self.plan_cache.get(sql) {
536 if plan.statistics_version == current_version {
537 return Ok(plan.stmt.clone());
538 }
539 // Stale entry — fall through to evict + re-prepare.
540 }
541 self.plan_cache.evict(sql);
542 let stmt = self.prepare(sql)?;
543 let source_tables = plan_cache::collect_source_tables(&stmt);
544 let plan = plan_cache::PreparedPlan {
545 stmt: stmt.clone(),
546 statistics_version: current_version,
547 source_tables,
548 describe_columns: alloc::vec::Vec::new(),
549 };
550 self.plan_cache.insert(String::from(sql), plan);
551 Ok(stmt)
552 }
553
554 /// v6.3.0 — read-only accessor for tests and v6.3.1 invalidation.
555 pub fn plan_cache(&self) -> &plan_cache::PlanCache {
556 &self.plan_cache
557 }
558
559 /// v7.38 (mailrs prod 7.35 pool-exhaustion incident) — boot-time
560 /// plan-IR cache warm-up. Walks `sqls`, calls `prepare_cached`
561 /// on each one. Each successful prepare leaves the parsed +
562 /// reordered + clock-rewritten `Statement` in the engine-wide
563 /// plan cache; subsequent `Engine::execute` / `execute_prepared`
564 /// for the same SQL skips parse + JOIN reorder. Returns the
565 /// count of successfully cached statements.
566 ///
567 /// The mailrs `Database::new` boot path is the expected caller:
568 /// pre-warm the top-N query shapes (inbox listing, contacts
569 /// search, stats) so the first user-facing request doesn't
570 /// pay the 2-3 s first-fire cost on the readonly-blocking
571 /// sqlx pool — which (under prod concurrency) exhausts the
572 /// pool and stalls the whole UI.
573 pub fn warm_up_plan_cache(&mut self, sqls: &[&str]) -> usize {
574 let mut warmed = 0;
575 for sql in sqls {
576 if self.prepare_cached(sql).is_ok() {
577 warmed += 1;
578 }
579 }
580 warmed
581 }
582
583 /// v7.38 (mailrs prod 7.35 pool-exhaustion incident) — boot-time
584 /// cold-tier OS page-cache warm-up. Walks every table in the
585 /// active catalog, iterates the cold rows via the existing
586 /// BTree-driven `iter_cold_rows_of_table`, drops the rows on
587 /// the floor. The walk's side effect is that every cold
588 /// segment file gets mmap-read once — the OS page cache then
589 /// serves subsequent queries without disk I/O.
590 ///
591 /// Returns the total cold rows touched across all tables.
592 /// On a hot-only catalog (no `cold_segments` populated) the
593 /// call is a near-no-op.
594 pub fn warm_up_cold_tier(&self) -> usize {
595 let catalog = self.active_catalog();
596 let mut total = 0;
597 for name in catalog.table_names() {
598 if let Some(table) = catalog.get(&name) {
599 let rows = self.iter_cold_rows_of_table(table);
600 total += rows.len();
601 }
602 }
603 total
604 }
605
606 /// v6.3.0 — mutable accessor for v6.3.1 invalidation hooks.
607 pub fn plan_cache_mut(&mut self) -> &mut plan_cache::PlanCache {
608 &mut self.plan_cache
609 }
610
611 /// v6.3.3 — Describe a prepared `Statement` without executing.
612 /// Returns `(parameter_oids, output_columns)`. Empty
613 /// `output_columns` means the statement has no row-producing shape
614 /// we could resolve here — the pgwire layer maps that to `NoData`.
615 ///
616 /// v7.39 (round 462) — a SELECT over a system catalog view resolves
617 /// against the same materialised catalog execution builds, so the
618 /// two paths cannot disagree about what a system view looks like.
619 pub fn describe_prepared(&self, stmt: &Statement) -> (Vec<u32>, Vec<ColumnSchema>) {
620 if let Statement::Select(s) = stmt {
621 if crate::system_catalog::select_references_meta_view(s)
622 && let Ok(catalog) = self.meta_view_catalog(s)
623 {
624 return describe::describe_prepared(stmt, &catalog);
625 }
626 if let Some(catalog) = self.admin_view_catalog(s) {
627 return describe::describe_prepared(stmt, &catalog);
628 }
629 }
630 describe::describe_prepared(stmt, self.active_catalog())
631 }
632
633 /// v6.1.1 — execute a [`Statement`] previously returned by
634 /// [`Engine::prepare`], substituting `Expr::Placeholder(n)`
635 /// nodes for the corresponding [`Value`] in `params` (1-based
636 /// per PG: `$1` → `params[0]`). Bind-time string parameters
637 /// are decoded into typed `Value`s by the pgwire layer before
638 /// this call so the resulting AST hits the same execution
639 /// path as a simple query — no SQL re-parse.
640 ///
641 /// Pgwire's `Execute` (E) message after a `Bind` (B) lands here.
642 pub fn execute_prepared(
643 &mut self,
644 stmt: Statement,
645 params: &[Value<'static>],
646 ) -> Result<QueryResult, EngineError> {
647 self.execute_prepared_with_cancel(stmt, params, CancelToken::none())
648 }
649
650 /// v7.37 (SPGS small-query bar) — borrow-based SELECT entry for
651 /// the pgwire `Execute` hot path when the portal has no bound
652 /// parameters. Skips both the AST clone the prepared path used
653 /// to do at the pgwire call site AND the `substitute_
654 /// placeholders` walk (a no-op when params are empty). Caller
655 /// must already hold the engine write lock — read would be
656 /// cleaner, but `current_tx` mutation keeps it `&mut`.
657 pub fn execute_prepared_select_no_params(
658 &mut self,
659 stmt: &spg_sql::ast::SelectStatement,
660 cancel: CancelToken<'_>,
661 ) -> Result<QueryResult, EngineError> {
662 let saved = self.current_tx;
663 self.current_tx = Some(IMPLICIT_TX);
664 // v7.38 Epic P (panic isolation) — Slice 3: route this read-only
665 // prepared-SELECT hot path (pgwire `Execute` with no bound params)
666 // through the SAME `catch_unwind` firewall as the write paths. A
667 // panic in `exec_select_cancel` is caught inside the engine and
668 // returned as `EngineError::Internal`, so it never unwinds through
669 // the caller's engine `RwLock` write guard (poisoning it) or aborts
670 // the process. This path is read-only, so the firewall's
671 // `discard_tx_on_panic` is a no-op (no shadow / writer version to
672 // drop) — exactly right: nothing to roll back, just catch + survive.
673 // `exec_select_cancel` materialises its `QueryResult` synchronously,
674 // so the whole result is produced inside the catch (statement
675 // boundary only — no per-row cost).
676 #[cfg(feature = "std")]
677 let result = self.catch_stmt_panic(|s| s.exec_select_cancel(stmt, cancel));
678 #[cfg(not(feature = "std"))]
679 let result = self.exec_select_cancel(stmt, cancel);
680 self.current_tx = saved;
681 result
682 }
683
684 /// v7.37.17 — `SHOW <name>` / `SHOW ALL`. Extracted (r1058) so the
685 /// READ-ONLY dispatcher can serve it too: the wire routes SHOW as a
686 /// read, and its fallthrough (unknown-to-the-wire names) landed on
687 /// `WriteRequired` instead of this answer.
688 pub(crate) fn exec_show_parameter(
689 &self,
690 name: alloc::string::String,
691 ) -> Result<QueryResult, EngineError> {
692 use spg_storage::{ColumnSchema, DataType, Row, Value};
693 // v7.37.17 (17.6 sibling) — `SHOW ALL` returns a
694 // (name, setting, description) triple for every
695 // parameter SPG knows about. PG's shape is the same.
696 // Emitting a fixed curated inventory here keeps the
697 // client shape stable without wire-tapping every
698 // per-session parameter.
699 // v7.38 (read01 P3.20/P3.23) — SHOW reads the same canonical
700 // GUC inventory as pg_settings, so `SHOW <name>` / `SHOW ALL`
701 // and pg_settings never disagree on which params exist.
702 let canon = crate::system_catalog::canonical_gucs();
703 let effective = |n: &str, boot: &str| -> alloc::string::String {
704 self.session_params
705 .iter()
706 .find(|(k, _)| k.eq_ignore_ascii_case(n))
707 .map(|(_, v)| v.clone())
708 .unwrap_or_else(|| boot.into())
709 };
710 if name.eq_ignore_ascii_case("all") {
711 let cols = alloc::vec![
712 ColumnSchema::new("name", DataType::Text, false),
713 ColumnSchema::new("setting", DataType::Text, false),
714 ColumnSchema::new("description", DataType::Text, false),
715 ];
716 let mut rows: Vec<Row> = Vec::new();
717 // Dynamic params outside the static canonical table.
718 rows.push(Row::new(alloc::vec![
719 Value::text(alloc::string::String::from("transaction_isolation")),
720 Value::text(alloc::string::String::from(
721 self.current_isolation_level.as_pg_str(),
722 )),
723 Value::text(alloc::string::String::from(
724 "Shows the current transaction's isolation level.",
725 )),
726 ]));
727 // v7.38.18 (C5) — `is_superuser` is deliberately NOT here.
728 // PG 18.4 answers `SHOW is_superuser` with `on` and has no
729 // row for it in `pg_settings`, so `SHOW ALL` does not list
730 // it either — measured, and the single reason SPG returned
731 // 399 rows against PG's 398 after this list was completed.
732 // `SHOW is_superuser` still answers, below, exactly as PG's
733 // does.
734 // v7.38.18 (C5) — every parameter PG 18.4 has, and PG's own
735 // one-line description in the third column. It used to be
736 // thirty-three rows carrying the CATEGORY under a heading
737 // that says `description`, which is neither PG's count nor
738 // PG's content. SPG's own list wins where the two overlap,
739 // because its value for `work_mem` is the real one.
740 for (n, boot, cat, _, _) in canon {
741 let d = crate::guc_catalog::guc_short_desc(n).unwrap_or(*cat);
742 rows.push(Row::new(alloc::vec![
743 Value::text(alloc::string::String::from(*n)),
744 Value::text(effective(n, boot)),
745 Value::text(alloc::string::String::from(d)),
746 ]));
747 }
748 for &(n, _, human, _, _, _, _, d) in crate::guc_catalog::PG_GUC_CONTEXTS {
749 if canon.iter().any(|(c, ..)| (*c).eq_ignore_ascii_case(n)) {
750 continue;
751 }
752 // ...nor the two pushed above with live values. PG has
753 // rows for both, so without this `SHOW ALL` returned 400
754 // where PG returns 398, with two names twice.
755 if rows
756 .iter()
757 .take(1)
758 .any(|r| matches!(&r.values[0], Value::Text(t) if t.eq_ignore_ascii_case(n)))
759 {
760 continue;
761 }
762 rows.push(Row::new(alloc::vec![
763 Value::text(alloc::string::String::from(n)),
764 Value::text(effective(n, human)),
765 Value::text(alloc::string::String::from(d)),
766 ]));
767 }
768 // PG lists them in name order and a client reading `SHOW ALL`
769 // as a table sees that order; the two lists above are each
770 // sorted but interleave.
771 rows.sort_by_key(|r| match &r.values[0] {
772 Value::Text(t) => alloc::string::String::from(t.as_ref()),
773 other => alloc::format!("{other:?}"),
774 });
775 return Ok(QueryResult::Rows {
776 columns: cols,
777 rows,
778 });
779 }
780 // v7.38.18 (C12) — `SHOW WARNINGS`, MySQL's diagnostics area.
781 //
782 // Non-strict `sql_mode` bends a value that would not fit — the
783 // bending has been byte-for-byte MySQL's since v7.39 round 470
784 // — and MySQL TELLS you it did. Until now SPG bent the value
785 // silently, so an application that checks after an insert had
786 // no way to learn its data had been changed.
787 //
788 // Three columns, named and ordered as MySQL returns them.
789 // Reading does not clear: only the next warning-generating
790 // statement does.
791 // MySQL-only. PostgreSQL 18.4 answers `ERROR: unrecognized
792 // configuration parameter "warnings"`, and a PG session must
793 // keep getting that: a compatibility surface that leaks into
794 // the other dialect is a divergence of its own, and I put one
795 // there for a few minutes before checking.
796 // v7.38.18 (C12) — `SHOW COUNT(*) WARNINGS`, the size of that
797 // same diagnostics area. MySQL 9 answers it with one row of one
798 // column NAMED `@@session.warning_count`, which is the name a
799 // client library keys on; the parser folds the whole phrase to
800 // this one sentinel so no GUC can collide with it. Same dialect
801 // gate as `SHOW WARNINGS` above, for the same reason.
802 if self.backslash_escapes && name.eq_ignore_ascii_case("count(*) warnings") {
803 let cols = alloc::vec![ColumnSchema::new(
804 alloc::string::String::from("@@session.warning_count"),
805 spg_storage::DataType::BigInt,
806 false
807 )];
808 let n = i64::try_from(self.mysql_warnings.len()).unwrap_or(i64::MAX);
809 return Ok(QueryResult::Rows {
810 columns: cols,
811 rows: alloc::vec![Row::new(alloc::vec![Value::BigInt(n)])],
812 });
813 }
814
815 if self.backslash_escapes && name.eq_ignore_ascii_case("warnings") {
816 let cols = alloc::vec![
817 ColumnSchema::new(
818 alloc::string::String::from("Level"),
819 spg_storage::DataType::Text,
820 false
821 ),
822 ColumnSchema::new(
823 alloc::string::String::from("Code"),
824 spg_storage::DataType::Int,
825 false
826 ),
827 ColumnSchema::new(
828 alloc::string::String::from("Message"),
829 spg_storage::DataType::Text,
830 false
831 ),
832 ];
833 let rows: Vec<Row> = self
834 .mysql_warnings
835 .iter()
836 .map(|w| {
837 Row::new(alloc::vec![
838 Value::text(w.level),
839 Value::Int(i32::from(w.code)),
840 Value::text(w.message.clone()),
841 ])
842 })
843 .collect();
844 return Ok(QueryResult::Rows {
845 columns: cols,
846 rows,
847 });
848 }
849
850 let value: alloc::string::String = match name.to_ascii_lowercase().as_str() {
851 "transaction_isolation" => {
852 alloc::string::String::from(self.current_isolation_level.as_pg_str())
853 }
854 "is_superuser" => alloc::string::String::from("on"),
855 _ => {
856 // Canonical GUC? report the session override or its
857 // boot default. Otherwise a user-set custom GUC, or a
858 // recognised-name error pointing at pg_settings.
859 if let Some((_, boot, ..)) =
860 canon.iter().find(|(n, ..)| n.eq_ignore_ascii_case(&name))
861 {
862 effective(&name, boot)
863 } else if let Some(v) = self.session_param(&name) {
864 alloc::string::String::from(v)
865 } else if let Some(boot) = crate::guc_catalog::guc_boot_value(&name) {
866 // v7.39 (round 534) — a parameter PG18 knows but
867 // SPG does not model reports its compiled-in
868 // default. `SHOW random_page_cost` printed
869 // nothing at all before, and `SHOW fsync` with
870 // it.
871 alloc::string::String::from(boot)
872 } else {
873 return Err(EngineError::Unsupported(alloc::format!(
874 "SHOW {name:?}: parameter not recognised; \
875 see `SELECT name, setting FROM pg_settings` for \
876 the full inventory"
877 )));
878 }
879 }
880 };
881 Ok(QueryResult::Rows {
882 columns: alloc::vec![ColumnSchema::new(name, DataType::Text, false)],
883 rows: alloc::vec![Row::new(alloc::vec![Value::text(value)])],
884 })
885 }
886
887 /// v7.37 — streaming SELECT for the pgwire `Execute` hot path.
888 /// Emits one `StreamItem::Header(cols)` then one
889 /// `StreamItem::Row(&[&Value])` per surviving row. Returns the
890 /// total row count for the `CommandComplete` tag.
891 ///
892 /// For shapes where the engine can stream directly (non-aggregate
893 /// join projection of bound columns, no ORDER BY / DISTINCT / etc.)
894 /// no `Vec<Row<'static>>` is materialised — cell references come straight
895 /// out of the source tables. For non-streamable shapes the engine
896 /// runs the full `exec_select_cancel`, then walks the materialised
897 /// `Vec<Row<'static>>` driving the same emit callback (no engine-side win,
898 /// but pgwire dispatches every Execute through one path).
899 pub fn execute_prepared_select_streaming<F>(
900 &mut self,
901 stmt: &spg_sql::ast::SelectStatement,
902 cancel: CancelToken<'_>,
903 mut emit: F,
904 ) -> Result<usize, EngineError>
905 where
906 F: FnMut(StreamItem<'_>) -> Result<(), EngineError>,
907 {
908 let saved = self.current_tx;
909 self.current_tx = Some(IMPLICIT_TX);
910 // v7.38 Epic P (panic isolation) — Slice 3: route the streaming
911 // read-only SELECT hot path through the SAME `catch_unwind` firewall.
912 //
913 // Catch SCOPE (verified): `exec_select_streaming` uses a *push*
914 // model — it drives the caller's `emit` callback synchronously via
915 // `?` for the header and every row (both the true-streaming
916 // `try_exec_joined_streaming` fast path and the materialising
917 // fall-back), and only returns once the whole result has been
918 // emitted. It does NOT hand a lazy iterator back to the wire layer to
919 // pull rows from later. Therefore a panic in the per-row streaming
920 // phase unwinds *inside* this call and IS caught by wrapping the one
921 // `exec_select_streaming` call — the entire streaming phase is
922 // covered, not just setup. This is a single statement-boundary catch
923 // (the `catch_unwind` landing pad is armed once, the whole emit loop
924 // runs inside it) — NOT a per-row catch, so there is no hot-path cost.
925 // Read-only, so `discard_tx_on_panic` is a no-op (correct: nothing to
926 // roll back). A panic caught mid-stream (after some rows were encoded
927 // into the wire buffer) leaves the same partial-`wbuf` + `Err` state
928 // the wire layer already handles when `emit` itself returns `Err`
929 // mid-stream, so no new torn-state concern is introduced.
930 #[cfg(feature = "std")]
931 let inner = self.catch_stmt_panic(|s| s.exec_select_streaming(stmt, cancel, &mut emit));
932 #[cfg(not(feature = "std"))]
933 let inner = self.exec_select_streaming(stmt, cancel, &mut emit);
934 self.current_tx = saved;
935 inner
936 }
937
938 /// v7.37 — internal streaming dispatcher. Phase 1: fall-back path
939 /// only — runs the materialising `exec_select_cancel`, then drives
940 /// the emit callback from the resulting `Vec<Row<'static>>`. Phase 2 will
941 /// add a true streaming path for the joined-projection shape.
942 fn exec_select_streaming<F>(
943 &mut self,
944 stmt: &spg_sql::ast::SelectStatement,
945 cancel: CancelToken<'_>,
946 emit: &mut F,
947 ) -> Result<usize, EngineError>
948 where
949 F: FnMut(StreamItem<'_>) -> Result<(), EngineError>,
950 {
951 // v7.37 — true-streaming fast path for joined-non-aggregate
952 // projection of bound columns. Skips `Vec<Row<'static>>` + per-cell
953 // `.cloned()` (about 4 ms saved on the 25 k-row PROJ shape).
954 // Unresolved subqueries / pull-up shapes / non-streamable
955 // structure (ORDER BY, DISTINCT, …) fall through to the
956 // materialising path.
957 if !crate::subquery::expr_tree_has_subquery(stmt) {
958 if let Some(n) = self.try_exec_joined_streaming(stmt, cancel, emit)? {
959 return Ok(n);
960 }
961 }
962 // Fall-back: materialise then iterate.
963 let QueryResult::Rows { columns, rows } = self.exec_select_cancel(stmt, cancel)? else {
964 return Err(EngineError::Unsupported(alloc::string::String::from(
965 "streaming SELECT got a non-Rows result",
966 )));
967 };
968 emit_materialised(&columns, &rows, cancel, emit)
969 }
970}
971
972/// Hand an already-materialised result to a streaming consumer: one
973/// `Header`, then every row, checking for cancellation as it goes.
974///
975/// v7.37 (round 824) — this loop existed three times, in
976/// `exec_select_streaming` and twice in the read-only entry points, and
977/// none of the three checked cancellation. A `statement_timeout` — and
978/// `CancelRequest`, which shares the token — therefore did not bound any
979/// shape the streaming path declines: arithmetic and function
980/// projections, `ORDER BY`, `DISTINCT`. Measured over 200k rows of 200
981/// bytes under a 120ms timeout, every one of them ran to completion,
982/// all 200000 rows, no error.
983///
984/// The loop reads like the cheap half of the work, since the rows
985/// already exist. It is not: handing them to `emit` is what encodes them
986/// and pushes them at the socket, and that is most of the elapsed time
987/// (first row out at 30ms of 400ms). So the interruption a client asked
988/// for never happened, and it never happened for the shapes most likely
989/// to need it.
990///
991/// It is one function now so that the next copy cannot go missing the
992/// check — which is how all three came to be missing it.
993pub(crate) fn emit_materialised<F>(
994 columns: &[ColumnSchema],
995 rows: &[spg_storage::Row<'static>],
996 cancel: CancelToken<'_>,
997 emit: &mut F,
998) -> Result<usize, EngineError>
999where
1000 F: FnMut(StreamItem<'_>) -> Result<(), EngineError>,
1001{
1002 emit(StreamItem::Header(columns))?;
1003 let mut cell_refs: Vec<&Value> = Vec::with_capacity(columns.len());
1004 for (i, row) in rows.iter().enumerate() {
1005 // Same cadence as the streaming path's own check.
1006 if i.is_multiple_of(256) {
1007 cancel.check()?;
1008 }
1009 cell_refs.clear();
1010 for v in &row.values {
1011 cell_refs.push(v);
1012 }
1013 emit(StreamItem::Row(RowCells::Refs(&cell_refs)))?;
1014 }
1015 Ok(rows.len())
1016}
1017
1018/// One row's cells, in whichever shape the producer already holds them.
1019///
1020/// The channel used to be `&[&Value]` only, which cost a `Vec<&Value>`
1021/// per row at the two producers that build their cells into a
1022/// contiguous buffer: they had a `&[Value]` in hand and collected a
1023/// second vector of pointers into it purely to satisfy the type. That
1024/// is one heap allocation and one free per row — measured at 400k rows
1025/// (round 957) as **9 ns/row**, which on a narrow scan was 54-56% of
1026/// the whole walk and on a wide one 8-19%.
1027///
1028/// The reason it could not simply reuse one buffer is that the values
1029/// buffer is refilled each row, so any pointers into it die at the top
1030/// of the next iteration; only an owner of the storage (the
1031/// materialising path, whose rows outlive the loop) can hoist the
1032/// pointer vector out. Handing the contiguous slice over directly
1033/// removes the question instead of answering it.
1034///
1035/// `Refs` stays for producers whose cells really are scattered (a join
1036/// projecting out of several rows).
1037#[derive(Debug, Clone, Copy)]
1038pub enum RowCells<'a> {
1039 Refs(&'a [&'a Value<'static>]),
1040 Values(&'a [Value<'static>]),
1041}
1042
1043impl<'a> RowCells<'a> {
1044 pub fn len(&self) -> usize {
1045 match self {
1046 RowCells::Refs(v) => v.len(),
1047 RowCells::Values(v) => v.len(),
1048 }
1049 }
1050
1051 pub fn is_empty(&self) -> bool {
1052 self.len() == 0
1053 }
1054
1055 pub fn get(&self, i: usize) -> Option<&'a Value<'static>> {
1056 match self {
1057 RowCells::Refs(v) => v.get(i).copied(),
1058 RowCells::Values(v) => v.get(i),
1059 }
1060 }
1061}
1062
1063/// v7.37 — one item in the streaming SELECT emit channel. The
1064/// engine yields exactly one `Header` (before any row) then zero
1065/// or more `Row`s. Pgwire (or any other consumer) decides how to
1066/// turn those into wire bytes.
1067#[derive(Debug)]
1068pub enum StreamItem<'a> {
1069 Header(&'a [ColumnSchema]),
1070 Row(RowCells<'a>),
1071}
1072
1073impl Engine {
1074 /// v7.17.0 Phase 2.3 — prepared-statement entry that honors a
1075 /// caller-supplied `CancelToken`. Mirrors `execute_prepared`'s
1076 /// `current_tx` save/restore so the extended-query path stays
1077 /// transactionally consistent with the simple-query path.
1078 /// v7.39 (round 280) — `CREATE STATISTICS`.
1079 fn exec_create_statistics(
1080 &mut self,
1081 name: String,
1082 if_not_exists: bool,
1083 kinds: alloc::vec::Vec<String>,
1084 columns: alloc::vec::Vec<String>,
1085 table: String,
1086 ) -> Result<QueryResult, EngineError> {
1087 if columns.len() < 2 {
1088 return Err(EngineError::Unsupported(String::from(
1089 "extended statistics require at least 2 columns",
1090 )));
1091 }
1092 if self.active_catalog().get(&table).is_none() {
1093 return Err(EngineError::Unsupported(alloc::format!(
1094 "relation \"{table}\" does not exist"
1095 )));
1096 }
1097 // PG's default kind set is all three.
1098 let kinds = if kinds.is_empty() {
1099 alloc::vec![String::from("d"), String::from("f"), String::from("m")]
1100 } else {
1101 kinds
1102 };
1103 let def = spg_storage::StatisticsExtDef {
1104 name: name.clone(),
1105 table,
1106 kinds,
1107 columns,
1108 };
1109 let cat = self.active_catalog_mut();
1110 if let Err(taken) = cat.create_statistics_ext(def) {
1111 if if_not_exists {
1112 return Ok(QueryResult::CommandOk {
1113 affected: 0,
1114 modified_catalog: false,
1115 });
1116 }
1117 return Err(EngineError::Unsupported(alloc::format!(
1118 "statistics object \"{taken}\" already exists"
1119 )));
1120 }
1121 Ok(QueryResult::CommandOk {
1122 affected: 0,
1123 modified_catalog: true,
1124 })
1125 }
1126
1127 /// v7.39 (round 280) — `DROP STATISTICS`.
1128 fn exec_drop_statistics(
1129 &mut self,
1130 name: &str,
1131 if_exists: bool,
1132 ) -> Result<QueryResult, EngineError> {
1133 let dropped = self.active_catalog_mut().drop_statistics_ext(name);
1134 if !dropped && !if_exists {
1135 return Err(EngineError::Unsupported(alloc::format!(
1136 "statistics object \"{name}\" does not exist"
1137 )));
1138 }
1139 Ok(QueryResult::CommandOk {
1140 affected: 0,
1141 modified_catalog: dropped,
1142 })
1143 }
1144
1145 /// v7.39 (round 277) — `PREPARE`. Session-scoped, and a duplicate
1146 /// name is an error in PG rather than a silent replace.
1147 fn exec_prepare(
1148 &mut self,
1149 name: String,
1150 param_types: alloc::vec::Vec<String>,
1151 body: Statement,
1152 source: String,
1153 ) -> Result<QueryResult, EngineError> {
1154 if self.prepared_statements.contains_key(&name) {
1155 return Err(EngineError::Unsupported(alloc::format!(
1156 "prepared statement \"{name}\" already exists"
1157 )));
1158 }
1159 // v7.38.4 (sentori 6a, at their request) — PG refuses a PREPARE
1160 // whose parameter cannot be deduced consistently. Their
1161 // assert-stats upsert puts `$4` in a bigint column and again in
1162 // `CASE WHEN $4 > 0`, where the literal is integer: PG answers
1163 // "inconsistent types deduced for parameter $4". SPG let the
1164 // last context win silently.
1165 //
1166 // Only when the type was NOT declared. `PREPARE p (…, bigint, …)`
1167 // is accepted by PG and runs, and sqlx always declares — which is
1168 // why the statement works in their production and why refusing a
1169 // declared parameter would break every driver that does the
1170 // right thing.
1171 if let Some((n, first, second)) =
1172 crate::describe::conflicting_parameter_deductions(&body, self.active_catalog())
1173 && param_types.get((n as usize).saturating_sub(1)).is_none()
1174 {
1175 return Err(EngineError::Unsupported(alloc::format!(
1176 "inconsistent types deduced for parameter ${n} DETAIL: {first} versus {second}"
1177 )));
1178 }
1179 self.prepared_statements.insert(
1180 name,
1181 crate::PreparedSqlStatement {
1182 body,
1183 param_types,
1184 source,
1185 },
1186 );
1187 Ok(QueryResult::CommandOk {
1188 affected: 0,
1189 modified_catalog: false,
1190 })
1191 }
1192
1193 /// v7.39 (round 277) — `EXECUTE`. The arguments evaluate as
1194 /// constants and splice into the body's `$N` placeholders through
1195 /// the same `execute_prepared_with_cancel` the extended-query path
1196 /// uses, so a SQL EXECUTE and a wire Bind take the identical route.
1197 fn exec_execute(
1198 &mut self,
1199 name: &str,
1200 args: &[spg_sql::ast::Expr],
1201 cancel: CancelToken<'_>,
1202 ) -> Result<QueryResult, EngineError> {
1203 let Some(entry) = self.prepared_statements.get(name) else {
1204 return Err(EngineError::Unsupported(alloc::format!(
1205 "prepared statement \"{name}\" does not exist"
1206 )));
1207 };
1208 let body = entry.body.clone();
1209 let empty: alloc::vec::Vec<spg_storage::ColumnSchema> = alloc::vec::Vec::new();
1210 let ctx = self.ev_ctx(&empty, None);
1211 let blank = spg_storage::Row::new(alloc::vec::Vec::new());
1212 let mut params: alloc::vec::Vec<spg_storage::Value<'static>> =
1213 alloc::vec::Vec::with_capacity(args.len());
1214 for a in args {
1215 params.push(crate::eval::eval_expr(a, &blank, &ctx).map_err(EngineError::Eval)?);
1216 }
1217 self.execute_prepared_with_cancel(body, ¶ms, cancel)
1218 }
1219
1220 /// v7.39 (round 277) — `DEALLOCATE <name>` / `DEALLOCATE ALL`.
1221 /// Dropping a name that does not exist is an error in PG; ALL is
1222 /// unconditional.
1223 fn exec_deallocate(&mut self, name: Option<&str>) -> Result<QueryResult, EngineError> {
1224 match name {
1225 None => {
1226 self.prepared_statements.clear();
1227 Ok(QueryResult::CommandOk {
1228 affected: 0,
1229 modified_catalog: false,
1230 })
1231 }
1232 Some(n) => {
1233 if self.prepared_statements.remove(n).is_none() {
1234 return Err(EngineError::Unsupported(alloc::format!(
1235 "prepared statement \"{n}\" does not exist"
1236 )));
1237 }
1238 Ok(QueryResult::CommandOk {
1239 affected: 0,
1240 modified_catalog: false,
1241 })
1242 }
1243 }
1244 }
1245
1246 pub fn execute_prepared_with_cancel(
1247 &mut self,
1248 stmt: Statement,
1249 params: &[Value<'static>],
1250 cancel: CancelToken<'_>,
1251 ) -> Result<QueryResult, EngineError> {
1252 self.execute_prepared_in_with_cancel(stmt, params, IMPLICIT_TX, cancel)
1253 }
1254
1255 /// v7.39 (round 303, V22) — like [`Self::execute_prepared_with_cancel`]
1256 /// but binds the statement to an explicit transaction slot instead of
1257 /// the implicit one. The mysql-wire binary-protocol path uses this so a
1258 /// prepared INSERT/UPDATE lands in the connection's own `BEGIN`-opened
1259 /// transaction (and never collides with another connection on slot 0),
1260 /// mirroring what pgwire's `Bind`+`Execute` achieves by rendering
1261 /// bind-final SQL through [`Self::execute_in`].
1262 pub fn execute_prepared_in(
1263 &mut self,
1264 stmt: Statement,
1265 params: &[Value<'static>],
1266 tx_id: TxId,
1267 ) -> Result<QueryResult, EngineError> {
1268 self.execute_prepared_in_with_cancel(stmt, params, tx_id, CancelToken::none())
1269 }
1270
1271 pub fn execute_prepared_in_with_cancel(
1272 &mut self,
1273 mut stmt: Statement,
1274 params: &[Value<'static>],
1275 tx_id: TxId,
1276 cancel: CancelToken<'_>,
1277 ) -> Result<QueryResult, EngineError> {
1278 substitute_placeholders(&mut stmt, params)?;
1279 // v7.16.0 — set `current_tx` for the duration of the
1280 // dispatch so the `exec_*` helpers see the right TX
1281 // slot (matches what `execute_in_with_cancel` does for
1282 // simple-query). Pre-v7.16 the simple-query path
1283 // worked because every public entry point routed
1284 // through `execute_in_with_cancel`; the prepared path
1285 // skipped the wrap and so its INSERTs/UPDATEs landed
1286 // in the no-tx default slot, silently invisible to a
1287 // BEGIN/COMMIT-bracketed flow. Caught by spg-sqlx's
1288 // first transaction-visibility test.
1289 let saved = self.current_tx;
1290 self.current_tx = Some(tx_id);
1291 // v7.38 Epic P (panic isolation) — Slice 2: route the
1292 // prepared / extended-query path (the one sqlx / asyncpg / most
1293 // drivers actually use via pgwire `Bind`+`Execute`) through the
1294 // SAME `catch_unwind` firewall as the simple-query path (Slice 1,
1295 // `execute_inner_catching`). A panic in an extended-protocol
1296 // statement is caught inside the engine, the in-flight tx is
1297 // rolled back (shared `discard_tx_on_panic`), and the caller sees
1298 // an ordinary `EngineError::Internal` — never a poisoned write
1299 // guard or an aborted process. `current_tx` is `Some(IMPLICIT_TX)`
1300 // for the duration, so a caught panic rolls back the right tx; the
1301 // `saved` restore below still runs because the catch converts the
1302 // unwind into a normal `Result` return.
1303 let result = self.execute_stmt_catching(stmt, cancel);
1304 self.current_tx = saved;
1305 // r1059 — the r196 per-slot epoch witness, on THIS entry path
1306 // too. The bump lived only in `execute_in_with_cancel`, so an
1307 // autocommit write arriving over the extended protocol's
1308 // direct route moved the committed base with the epoch
1309 // unchanged; a concurrent RC transaction whose last rebase
1310 // matched the stale epoch then skipped its commit-time rebase
1311 // and installed its whole shadow — erasing the write. The
1312 // sqlx gate flaked ~1-in-6 on exactly this: DROP/CREATE
1313 // vanishing under a neighbouring transaction's COMMIT. Same
1314 // over-approximation as r196 (an extra rebase is only slower,
1315 // never wrong); large-object descriptors die at the same
1316 // boundary for the same per-slot reason (round 306).
1317 if !self.tx_catalogs.contains_key(&tx_id) {
1318 self.commit_epoch = self.commit_epoch.wrapping_add(1);
1319 self.lo_descriptors.clear();
1320 self.lo_next_fd = 0;
1321 }
1322 result
1323 }
1324
1325 /// v7.38 Epic P (panic isolation) — shared `catch_unwind` firewall
1326 /// (hosted `std` builds) used by every engine statement entry path: the
1327 /// simple-query ([`Self::execute_inner_catching`]), the prepared /
1328 /// extended-query ([`Self::execute_stmt_catching`]), and the read-only
1329 /// prepared-SELECT hot paths ([`Self::execute_prepared_select_no_params`]
1330 /// / [`Self::execute_prepared_select_streaming`]). Runs `run` under
1331 /// `catch_unwind`; a panic that unwinds out of statement execution is
1332 /// caught here and converted to [`EngineError::Internal`] after
1333 /// discarding the in-flight tx's shadow, so the caller sees a normal SQL
1334 /// error and the engine stays alive. This is the single place the
1335 /// rollback-on-panic policy lives — neither entry path reimplements it.
1336 ///
1337 /// **Why the post-catch engine state is consistent (COW shadow argument):**
1338 /// every uncommitted write of the panicked statement lives in
1339 /// `tx_catalogs[current_tx].catalog` — a per-tx *shadow* catalog that is
1340 /// only merged into the committed `self.catalog` at COMMIT (see
1341 /// `exec_commit`). The committed catalog is therefore never touched
1342 /// mid-statement, so dropping the shadow (mirroring `exec_rollback`)
1343 /// discards all half-applied work and leaves `self.catalog` exactly as it
1344 /// was before the statement. Redo-capture buffers live inside the
1345 /// shadow's tables and die with it, so no partial `RowChange` leaks into
1346 /// `last_redo` either (the caller publishes `last_redo` only on `Ok`).
1347 ///
1348 /// The `catch_unwind` closure holds `&mut self`; wrapping it in
1349 /// `AssertUnwindSafe` is sound precisely because of the above — the only
1350 /// caller-visible state a caught panic can leave behind is the discarded
1351 /// shadow, which is the correct rollback outcome, not a torn invariant.
1352 ///
1353 /// Generic over the closure's success type `T` so the read-only
1354 /// prepared-SELECT paths (which return a row count `usize`, not a
1355 /// `QueryResult`) reuse the *same* firewall — no second `catch_unwind`
1356 /// site. On those read-only paths `discard_tx_on_panic` is a no-op (a
1357 /// SELECT opens no shadow / writer version), which is the correct outcome:
1358 /// nothing to roll back, the point is purely to catch the unwind, return
1359 /// `Internal`, and leave the caller's write guard un-poisoned.
1360 #[cfg(feature = "std")]
1361 fn catch_stmt_panic<T>(
1362 &mut self,
1363 run: impl FnOnce(&mut Self) -> Result<T, EngineError>,
1364 ) -> Result<T, EngineError> {
1365 extern crate std;
1366 let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(self)));
1367 match caught {
1368 Ok(result) => result,
1369 Err(payload) => {
1370 // The panic unwound past every `?`-return in the executor.
1371 // `current_tx` is Some here (set by the caller); roll that tx
1372 // back by discarding its shadow. A statement that panicked in
1373 // autocommit before any shadow was opened simply has nothing
1374 // to drop (`discard_tx_on_panic` is infallible).
1375 let tx_id = self.current_tx.unwrap_or(IMPLICIT_TX);
1376 self.discard_tx_on_panic(tx_id);
1377 Err(panic_payload_to_engine_error(payload.as_ref()))
1378 }
1379 }
1380 }
1381
1382 /// v7.38 Epic P (panic isolation) — simple-query path wrapper: run
1383 /// [`Self::execute_inner_with_cancel`] behind the shared
1384 /// [`Self::catch_stmt_panic`] firewall.
1385 #[cfg(feature = "std")]
1386 fn execute_inner_catching(
1387 &mut self,
1388 sql: &str,
1389 cancel: CancelToken<'_>,
1390 ) -> Result<QueryResult, EngineError> {
1391 self.catch_stmt_panic(|s| s.execute_inner_with_cancel(sql, cancel))
1392 }
1393
1394 /// `no_std` variant — there is no unwinding runtime, so statement
1395 /// execution runs directly with no catch.
1396 #[cfg(not(feature = "std"))]
1397 fn execute_inner_catching(
1398 &mut self,
1399 sql: &str,
1400 cancel: CancelToken<'_>,
1401 ) -> Result<QueryResult, EngineError> {
1402 self.execute_inner_with_cancel(sql, cancel)
1403 }
1404
1405 /// v7.38 Epic P (panic isolation) — Slice 2: prepared / extended-query
1406 /// path wrapper. The extended-protocol path already holds a resolved
1407 /// [`Statement`] (no re-parse), so it cannot reuse the `&str`-taking
1408 /// [`Self::execute_inner_catching`]; instead it runs
1409 /// [`Self::execute_stmt_with_cancel`] behind the SAME shared
1410 /// [`Self::catch_stmt_panic`] firewall — identical rollback + error
1411 /// semantics, zero duplicated policy.
1412 #[cfg(feature = "std")]
1413 fn execute_stmt_catching(
1414 &mut self,
1415 stmt: Statement,
1416 cancel: CancelToken<'_>,
1417 ) -> Result<QueryResult, EngineError> {
1418 self.catch_stmt_panic(|s| s.execute_stmt_with_cancel(stmt, cancel))
1419 }
1420
1421 /// `no_std` variant — there is no unwinding runtime, so statement
1422 /// execution runs directly with no catch.
1423 #[cfg(not(feature = "std"))]
1424 fn execute_stmt_catching(
1425 &mut self,
1426 stmt: Statement,
1427 cancel: CancelToken<'_>,
1428 ) -> Result<QueryResult, EngineError> {
1429 self.execute_stmt_with_cancel(stmt, cancel)
1430 }
1431
1432 /// v7.38 Epic P — discard an in-flight tx's shadow after a caught panic,
1433 /// mirroring the state cleanup of [`Engine::exec_rollback`] but
1434 /// infallibly. Drops the shadow catalog, marks the tx's writer version
1435 /// aborted, and releases its row locks. Leaves the committed
1436 /// `self.catalog` untouched (the COW model kept every uncommitted change
1437 /// inside the shadow), so this is a full rollback of the panicked
1438 /// statement's work.
1439 #[cfg(feature = "std")]
1440 fn discard_tx_on_panic(&mut self, tx_id: TxId) {
1441 self.tx_catalogs.remove(&tx_id);
1442 if let Some(v) = self.tx_writer_versions.remove(&tx_id) {
1443 self.abort_writer_version(v);
1444 self.release_tx_locks(v);
1445 }
1446 // Per-statement scratch: reset so no stale writer version leaks into
1447 // the next statement (the caller also restores the saved value).
1448 self.stmt_writer_version = None;
1449 }
1450
1451 fn execute_inner_with_cancel(
1452 &mut self,
1453 sql: &str,
1454 cancel: CancelToken<'_>,
1455 ) -> Result<QueryResult, EngineError> {
1456 cancel.check()?;
1457 let stmt = self.prepare(sql)?;
1458 // v6.5.1 — wrap the executor with a wall-clock window so we
1459 // can record into spg_stat_query. Skip when the engine has
1460 // no clock attached (no_std embedded callers).
1461 let start_us = self.clock.map(|f| f());
1462 let result = self.execute_stmt_with_cancel(stmt, cancel);
1463 if let (Some(t0), Ok(ok)) = (start_us, &result) {
1464 let now = self.clock.map_or(t0, |f| f());
1465 let elapsed = now.saturating_sub(t0).max(0) as u64;
1466 // v7.37.22 (22.9) — count rows produced (SELECT) or
1467 // affected (INSERT/UPDATE/DELETE) so pg_stat_statements'
1468 // `rows` column populates accurately.
1469 let row_count: u64 = match ok {
1470 QueryResult::Rows { rows, .. } => rows.len() as u64,
1471 QueryResult::CommandOk { affected, .. } => *affected as u64,
1472 };
1473 self.query_stats
1474 .record_with_rows(sql, elapsed, now as u64, row_count);
1475 // v6.5.6 — slow-query log: fire callback when elapsed
1476 // exceeds the configured floor.
1477 if let (Some(threshold), Some(logger)) =
1478 (self.slow_query_threshold_us, self.slow_query_logger)
1479 && elapsed >= threshold
1480 {
1481 logger(sql, elapsed);
1482 }
1483 }
1484 result
1485 }
1486
1487 /// v7.38 (read01 P3.26) — transaction-abort firewall around the raw
1488 /// statement dispatch. After a statement fails inside an explicit
1489 /// transaction PG aborts the whole block: every later statement except
1490 /// COMMIT / ROLLBACK / ROLLBACK TO SAVEPOINT is rejected, and a COMMIT
1491 /// is downgraded to a ROLLBACK so no partial work slips through. We
1492 /// mirror that here so both the embedded engine and the wire server
1493 /// enforce it uniformly.
1494 pub(crate) fn execute_stmt_with_cancel(
1495 &mut self,
1496 stmt: Statement,
1497 cancel: CancelToken<'_>,
1498 ) -> Result<QueryResult, EngineError> {
1499 // v7.39 (round 298) — ask THIS transaction, not "is any
1500 // transaction anywhere aborted".
1501 if self.current_tx_aborted() {
1502 match stmt {
1503 Statement::Rollback | Statement::RollbackToSavepoint(_) => {}
1504 // PG performs a ROLLBACK for a COMMIT in an aborted tx.
1505 Statement::Commit => {
1506 let r = self.dispatch_stmt_inner(Statement::Rollback, cancel);
1507 self.set_current_tx_aborted(false);
1508 return r;
1509 }
1510 _ => return Err(EngineError::InFailedTransaction),
1511 }
1512 }
1513 let is_rollback_to_savepoint = matches!(stmt, Statement::RollbackToSavepoint(_));
1514 // v7.37.17 (Phase E2) — READ COMMITTED per-statement visibility:
1515 // classify (the statement moves into dispatch below), rebase the
1516 // open RC tx's shadow onto the latest committed catalog, then
1517 // record the statement's targets afterwards. Both calls are
1518 // no-ops outside an explicit transaction.
1519 let tx_class = crate::classify_stmt_for_tx(&stmt);
1520 if !matches!(tx_class, crate::TxStmtClass::TxControl) {
1521 // v7.37.17 (E4 r3) — a unique-key collision found while
1522 // rebasing fails THIS statement with 40001 (the tx aborts
1523 // via the standard failed-statement path below, like PG's
1524 // in-statement 23505 after the lock wait).
1525 self.maybe_rc_rebase()?;
1526 }
1527 // v7.39 (round 552) — what a SERIALIZABLE tx READ, taken before
1528 // the statement is consumed, recorded after it succeeds.
1529 let read_tables = crate::transaction::read_tables_of(&stmt);
1530 let result = self.dispatch_stmt_inner(stmt, cancel);
1531 if result.is_ok() {
1532 self.record_tx_stmt(&tx_class);
1533 self.record_tx_reads(read_tables);
1534 }
1535 // v7.39 (round 298) — the witness is THIS connection's slot.
1536 // `in_transaction()` is true whenever ANY connection holds a
1537 // transaction, so an autocommit failure used to abort a block
1538 // that belonged to somebody else.
1539 let mine_open = self.current_tx.is_some_and(|tx| self.is_tx_open(tx));
1540 if !mine_open {
1541 // The tx ended (COMMIT / ROLLBACK) or we were in autocommit;
1542 // either way there is no aborted block to remember.
1543 self.set_current_tx_aborted(false);
1544 } else if result.is_ok() && is_rollback_to_savepoint {
1545 // Rolling back to a savepoint recovers the transaction.
1546 self.set_current_tx_aborted(false);
1547 } else if matches!(result, Err(EngineError::LockWouldBlock)) {
1548 // v7.39 (round 300) — NOT a failure: the server drops the
1549 // engine lock and retries. Marking the block aborted here
1550 // made the FIRST block poison the transaction, so the
1551 // retry hit the abort firewall and the waiter lost a
1552 // deadlock it should have won.
1553 } else if result.is_err() {
1554 // A failure inside an open transaction aborts the whole block.
1555 self.set_current_tx_aborted(true);
1556 }
1557 result
1558 }
1559
1560 /// v7.38.18 (C12) — the one gate MySQL's diagnostics area passes
1561 /// through. Every statement runs inside this; the body below is the
1562 /// dispatch itself.
1563 pub(crate) fn dispatch_stmt_inner(
1564 &mut self,
1565 stmt: Statement,
1566 cancel: CancelToken<'_>,
1567 ) -> Result<QueryResult, EngineError> {
1568 // v7.38.18 (C12) — MySQL's diagnostics area belongs to ONE
1569 // statement, and a read returns the PREVIOUS statement's.
1570 // Measured on MySQL 9, after an INSERT that bends two values:
1571 //
1572 // SELECT @@warning_count -> 2 (the INSERT's)
1573 // SELECT @@warning_count -> 0 (the first SELECT's own)
1574 // SHOW COUNT(*) WARNINGS -> unchanged; reads do not publish
1575 //
1576 // So the visible set is replaced when a statement ENDS, not
1577 // when it starts — clearing on entry would make the first read
1578 // answer 0 and lose the warning entirely. Readers publish
1579 // nothing, which is why two reads in a row agree.
1580 //
1581 // Without any of this the warnings never expire, and a client
1582 // that checks after the wrong statement is told its data was
1583 // bent when it was not — worse than silence, because it is a
1584 // claim. I shipped exactly that a few hours ago; it took
1585 // writing `SHOW COUNT(*) WARNINGS` to notice.
1586 //
1587 // Same place as the NOTICE clear below, and for the same
1588 // reason: the one point the simple-query and prepared paths
1589 // both pass through.
1590 let publishes_warnings = !matches!(&stmt, Statement::ShowParameter(n)
1591 if n.eq_ignore_ascii_case("warnings")
1592 || n.eq_ignore_ascii_case("count(*) warnings"));
1593 if publishes_warnings {
1594 self.mysql_stmt_warnings.clear();
1595 }
1596 let result = self.dispatch_stmt_body(stmt, cancel);
1597 if publishes_warnings {
1598 self.mysql_warnings = core::mem::take(&mut self.mysql_stmt_warnings);
1599 }
1600 result
1601 }
1602
1603 fn dispatch_stmt_body(
1604 &mut self,
1605 stmt: Statement,
1606 cancel: CancelToken<'_>,
1607 ) -> Result<QueryResult, EngineError> {
1608 cancel.check()?;
1609 // v7.17.0 Phase 1.1 — pre-resolve nextval / currval /
1610 // setval calls in the statement tree. Walks SELECT
1611 // projection, INSERT VALUES, UPDATE SET, DELETE WHERE,
1612 // and DEFAULT exprs; replaces sequence FunctionCall
1613 // nodes with concrete Literal values minted against the
1614 // catalog. This is the only place that mutates sequence
1615 // state from a SELECT-shaped path (exec_select_cancel is
1616 // `&self` and can't reach the catalog mutably).
1617 //
1618 // Fast-path: when no sequences exist anywhere in the
1619 // catalog (the typical hot-path INSERT load), skip the
1620 // walker entirely. Single map-emptiness check on the
1621 // catalog beats walking every expression on every call.
1622 let mut stmt = stmt;
1623 // v7.17 dump-compat — the fast-path check
1624 // `sequences().is_empty()` skips pre-resolve when no
1625 // sequence exists in the *currently active* catalog
1626 // snapshot. The committed catalog or the implicit-TX
1627 // catalog may legitimately disagree on this between
1628 // CREATE SEQUENCE and a later setval(): always run the
1629 // resolver — the walk is O(expr-count) and dwarfed by
1630 // the parse cost we just paid.
1631 self.pre_resolve_sequence_calls_in_statement(&mut stmt)?;
1632 // v7.39 (round 305, V23) — evaluate any non-constant LIMIT /
1633 // OFFSET down to a literal row count. It belongs here, at the
1634 // one point both the simple-query and the prepared path pass
1635 // through, because every executor reads the row count as
1636 // `Option<u32>` and takes `None` for "no limit": an expression
1637 // that reached execution would silently widen the result to the
1638 // whole table rather than fail.
1639 self.resolve_limit_exprs_in_statement(&mut stmt, cancel)?;
1640 // v7.39 (read01 round 57) — the table-privilege gate. A superuser
1641 // session (the default login, or `SET ROLE admin`) skips it entirely,
1642 // so nothing changes for a customer who never assumes another role.
1643 self.acl_check_statement(&stmt)?;
1644 // v7.39 (round 435) — MySQL commits an open transaction BEFORE it
1645 // runs DDL (and before a nested START TRANSACTION), where PG keeps
1646 // the DDL inside the transaction. A MySQL client that writes rows,
1647 // runs DDL and then rolls back keeps those rows on MySQL and lost
1648 // them on SPG — silently, since nothing errors. This is the one
1649 // point every path (simple query, prepared, extended) passes
1650 // through, so the commit cannot be skipped by a spelling.
1651 //
1652 // v7.39 (round 444) — the witness is THIS connection's slot, not
1653 // `in_transaction()`. That predicate is true whenever ANY connection
1654 // holds a transaction, so a second client's `BEGIN` tried to commit a
1655 // slot of its own that held nothing and answered an error instead —
1656 // caught by `two_mysql_connections_can_each_hold_a_transaction`, which
1657 // had been failing since round 435 introduced this hook. Same
1658 // global-vs-slot confusion rounds 279 / 283 / 298 / 304 each fixed
1659 // elsewhere; `current_tx` is the connection's own slot here, set by
1660 // `execute_in_with_cancel` before dispatch.
1661 let in_own_tx = self.current_tx.is_some_and(|t| self.is_tx_open(t));
1662 if self.backslash_escapes && in_own_tx && stmt.mysql_implicit_commit() {
1663 self.exec_commit()?;
1664 }
1665 let result = match stmt {
1666 // v7.39 (round 547) — `ALTER ROLE … SET/RESET` and
1667 // `ALTER DATABASE … SET/RESET`. These reported success and
1668 // changed nothing: both fell into the parser's pg_dump
1669 // no-op tail, so a DBA setting a per-role default got no
1670 // effect and no error.
1671 Statement::SetDbRoleSetting(st) => {
1672 // PG refuses a scope that names something absent.
1673 // v7.39 (round 696) — one predicate. This wrote its own
1674 // (`users.any(…) || postgres`), `acl_check_role_exists`
1675 // wrote a third, and round 652 already recorded what
1676 // happens when a role predicate and the catalog it reflects
1677 // disagree. `role_exists` is the one that answers.
1678 if let Some(role) = &st.role
1679 && !self.role_exists(role)
1680 {
1681 return Err(EngineError::Unsupported(alloc::format!(
1682 "role \"{role}\" does not exist"
1683 )));
1684 }
1685 if let Some(db) = &st.database {
1686 let current = self
1687 .session_params
1688 .get("spg.database")
1689 .cloned()
1690 .unwrap_or_else(|| alloc::string::String::from("spg"));
1691 if !db.eq_ignore_ascii_case(¤t) {
1692 return Err(EngineError::Unsupported(alloc::format!(
1693 "database \"{db}\" does not exist"
1694 )));
1695 }
1696 }
1697 let db = st.database.clone().unwrap_or_default();
1698 let role = st.role.clone().unwrap_or_default();
1699 let cat = self.active_catalog_mut();
1700 match (&st.param, &st.value) {
1701 (None, _) => cat.reset_db_role_settings(&db, &role),
1702 (Some(p), v) => cat.set_db_role_setting(&db, &role, p, v.as_deref()),
1703 }
1704 Ok(QueryResult::CommandOk {
1705 affected: 0,
1706 modified_catalog: true,
1707 })
1708 }
1709 // v7.39 (round 430) — MySQL USER variables. The value is an
1710 // arbitrary expression, evaluated against an empty row (a
1711 // user-variable assignment is a statement, not a per-row thing),
1712 // and stored in the session's own namespace.
1713 //
1714 // Every right-hand side sees the state as it was BEFORE the
1715 // statement — the assignments do NOT become visible to each
1716 // other. Measured on MariaDB 11: with both fresh,
1717 // `SET @p = 1, @q = @p + 1` leaves @q NULL; with @r already 100,
1718 // `SET @r = 1, @s = @r + 1` leaves @s at 101, i.e. @r's OLD
1719 // value. (Separate statements do chain, as you would expect.)
1720 // So: evaluate them all, THEN apply them all.
1721 Statement::SetUserVars(assigns, settings) => {
1722 let mut resolved: Vec<(String, spg_storage::Value<'static>)> =
1723 Vec::with_capacity(assigns.len());
1724 for (name, mut expr) in assigns {
1725 // `SET @total = (SELECT SUM(v) FROM t)` is ordinary MySQL,
1726 // so the scalar subqueries have to be materialised the way
1727 // every other statement's do — eval_expr itself refuses to
1728 // meet one.
1729 self.resolve_expr_subqueries(&mut expr, cancel)?;
1730 let cols: Vec<ColumnSchema> = Vec::new();
1731 let value = {
1732 let ctx = self.ev_ctx(&cols, None);
1733 let empty = spg_storage::Row::new(Vec::new());
1734 crate::eval::eval_expr(&expr, &empty, &ctx).map_err(EngineError::Eval)?
1735 };
1736 resolved.push((name, value.into_owned()));
1737 }
1738 for (name, value) in resolved {
1739 self.user_vars.insert(name, value);
1740 }
1741 // v7.39 (round 554) — the session settings written in
1742 // the same statement, applied after the saves. Routed
1743 // through the ordinary SET path so `SQL_MODE` still
1744 // flips strictness and the rest land where a plain
1745 // `SET x = y` puts them.
1746 for (name, value) in settings {
1747 let rendered = match crate::conversions::literal_expr_to_value_in(
1748 value.clone(),
1749 Some(self.active_catalog()),
1750 ) {
1751 Ok(v) => crate::eval::value_to_text(&v),
1752 Err(_) => alloc::format!("{value}"),
1753 };
1754 let _ = self.execute(&alloc::format!("SET {name} = '{rendered}'"));
1755 }
1756 Ok(QueryResult::CommandOk {
1757 affected: 0,
1758 modified_catalog: false,
1759 })
1760 }
1761 // v7.39 (round 277) — SQL-level prepared statements.
1762 Statement::Prepare {
1763 name,
1764 param_types,
1765 body,
1766 source,
1767 } => self.exec_prepare(name, param_types, *body, source),
1768 Statement::Execute { name, args } => self.exec_execute(&name, &args, cancel),
1769 Statement::Deallocate(name) => self.exec_deallocate(name.as_deref()),
1770 // v7.39 (round 278) — both were accepted and dropped. They
1771 // are reported as MISSING OBJECTS rather than as syntax
1772 // errors, because the SQL parses fine; what is absent is a
1773 // procedure catalog and a prepared-transaction registry.
1774 // v7.39 (round 280) — extended statistics as a real
1775 // catalog object. The planner does not consult them yet;
1776 // recording them is what makes a pg_dump restore and
1777 // reflection honest, instead of the statement vanishing.
1778 Statement::CreateStatistics {
1779 name,
1780 if_not_exists,
1781 kinds,
1782 columns,
1783 table,
1784 } => self.exec_create_statistics(name, if_not_exists, kinds, columns, table),
1785 Statement::DropStatistics { name, if_exists } => {
1786 self.exec_drop_statistics(&name, if_exists)
1787 }
1788 Statement::Call(name) => Err(EngineError::Unsupported(alloc::format!(
1789 "procedure {name}() does not exist HINT: No procedure matches the given name \
1790 and argument types. You might need to add explicit type casts."
1791 ))),
1792 Statement::PrepareTransaction(_) => Err(EngineError::Unsupported(String::from(
1793 "prepared transactions are disabled HINT: Set \"max_prepared_transactions\" \
1794 to a nonzero value.",
1795 ))),
1796 Statement::CreateTable(s) => self.exec_create_table(s),
1797 // v7.39 (round 218) — server-side cursors.
1798 Statement::DeclareCursor {
1799 name,
1800 scroll,
1801 hold,
1802 query,
1803 } => self.exec_declare_cursor(name, scroll, hold, *query),
1804 Statement::FetchCursor { name, direction } => self.exec_fetch_cursor(&name, direction),
1805 Statement::MoveCursor { name, direction } => self.exec_move_cursor(&name, direction),
1806 Statement::CloseCursor { name } => self.exec_close_cursor(name.as_deref()),
1807 // v7.39 (round 222) — LISTEN/NOTIFY with real delivery.
1808 Statement::Listen(ch) => self.exec_listen(ch),
1809 Statement::Notify { channel, payload } => self.exec_notify(channel, payload),
1810 Statement::Unlisten(ch) => self.exec_unlisten(ch),
1811 // v7.9.15 — CREATE EXTENSION is a no-op on SPG. Returns
1812 // CommandOk with affected=0; modified_catalog=false so
1813 // the WAL doesn't grow a useless entry. mailrs F3.
1814 Statement::CreateExtension(_) => Ok(QueryResult::CommandOk {
1815 affected: 0,
1816 modified_catalog: false,
1817 }),
1818 // v7.16.2 — DO $$ ... $$ block. mailrs round-10 A.2
1819 // — the pre-v7.9.27 no-op SILENTLY swallowed every
1820 // mailrs migrate-038/-040/-042 idempotent rename
1821 // (the IF EXISTS … THEN ALTER … END block never
1822 // ran). v7.16.2 dispatches to exec_do_block which
1823 // runs the PlPgSqlBlock at top level via the same
1824 // execute_stmts machinery the trigger executor
1825 // uses (NEW=None, OLD=None — DO blocks have no
1826 // row context).
1827 Statement::DoBlock(body) => self.exec_do_block(body),
1828 // v7.14.0 — empty-statement no-op for pg_dump /
1829 // mysqldump preamble lines that collapse to nothing
1830 // after comment-stripping.
1831 Statement::Empty => Ok(QueryResult::CommandOk {
1832 affected: 0,
1833 modified_catalog: false,
1834 }),
1835 // v7.39 (round 695) — `ALTER SYSTEM SET|RESET <name>`. SPG has
1836 // no postgresql.auto.conf to write, so nothing is APPLIED; what
1837 // changed is that a name PG18 does not know is now refused
1838 // instead of accepted. It reuses the session's own GUC check —
1839 // one place decides what a parameter name means, so `SET` and
1840 // `ALTER SYSTEM` cannot drift apart in what they accept.
1841 //
1842 // The F31 audit found this: the test was called
1843 // `alter_system_set_no_op` and set `work_mem`, a name that
1844 // exists, so it could never have caught a name that does not.
1845 // v7.39 (round 696) — the four statements the F31 sweep found
1846 // accepting a name that does not exist. SPG still performs
1847 // nothing for any of them; what changed is that it no longer
1848 // says "understood" about an object that is not there.
1849 // v7.39 (round 707) — see Statement::DropAggregate. Existence
1850 // first across the whole list (PG's order, measured), canonical
1851 // type names in the signature, and every SPG aggregate is a
1852 // built-in, so a name that exists is undroppable.
1853 Statement::DropAggregate { if_exists, items } => {
1854 let render = |name: &str, args: &Option<Vec<String>>| -> alloc::string::String {
1855 match args {
1856 None => alloc::format!("{name}(*)"),
1857 Some(a) => {
1858 let canon: Vec<alloc::string::String> = a
1859 .iter()
1860 .map(|t| {
1861 crate::conversions::type_name_to_data_type(t).map_or_else(
1862 || t.clone(),
1863 crate::conversions::pg_type_name_for_error,
1864 )
1865 })
1866 .collect();
1867 alloc::format!("{name}({})", canon.join(", "))
1868 }
1869 }
1870 };
1871 for (name, args) in &items {
1872 if !crate::aggregate::is_aggregate_name(name.as_str()) {
1873 if if_exists {
1874 continue;
1875 }
1876 return Err(EngineError::Unsupported(alloc::format!(
1877 "aggregate {} does not exist",
1878 render(name, args)
1879 )));
1880 }
1881 }
1882 if let Some((name, args)) = items
1883 .iter()
1884 .find(|(n, _)| crate::aggregate::is_aggregate_name(n.as_str()))
1885 {
1886 return Err(EngineError::Unsupported(alloc::format!(
1887 "cannot drop function {} because it is required by the database system",
1888 render(name, args)
1889 )));
1890 }
1891 Ok(QueryResult::CommandOk {
1892 affected: 0,
1893 modified_catalog: false,
1894 })
1895 }
1896 // v7.39 (round 750) — `ALTER ROLE … PASSWORD` really rotates
1897 // the credential now (it was a recorded no-op — ledgered as a
1898 // security defect in round 710: `ALTER USER x PASSWORD 'new'`
1899 // answered ALTER ROLE and the OLD password kept working).
1900 Statement::AlterRolePassword { name, password } => {
1901 if !self.role_exists(name.as_str()) {
1902 return Err(EngineError::Unsupported(alloc::format!(
1903 "role \"{name}\" does not exist"
1904 )));
1905 }
1906 self.alter_user_password(&name, password.as_deref())
1907 .map_err(|e| EngineError::Unsupported(alloc::format!("ALTER ROLE: {e}")))?;
1908 Ok(QueryResult::CommandOk {
1909 affected: 0,
1910 modified_catalog: self.catalog_change_is_committed(),
1911 })
1912 }
1913 Statement::ValidateOnly { kind, names } => {
1914 use spg_sql::ast::ValidateOnlyKind as K;
1915 match kind {
1916 K::LockTable => {
1917 for n in names {
1918 if self.catalog.get(n.as_str()).is_none() {
1919 return Err(EngineError::Storage(
1920 spg_storage::StorageError::TableNotFound { name: n.clone() },
1921 ));
1922 }
1923 }
1924 }
1925 K::RoleName => {
1926 for n in names {
1927 if !self.role_exists(n.as_str()) {
1928 return Err(EngineError::Unsupported(alloc::format!(
1929 "role \"{n}\" does not exist"
1930 )));
1931 }
1932 }
1933 }
1934 // PG18 refuses this whatever it names, because no label
1935 // provider is loaded — and SPG has none either, so the
1936 // refusal is the honest answer rather than a stand-in.
1937 // v7.39 (round 697) — one list answers both, which is
1938 // why these and `pg_extension` cannot disagree.
1939 //
1940 // A WARNING, not an error, and that is a deliberate
1941 // departure from PG. PG can error because an extension
1942 // can be installed there; SPG cannot be installed into,
1943 // so refusing would turn a customer dump that restores
1944 // today into one that needs editing. Saying nothing was
1945 // the actual defect: `CREATE EXTENSION hstore` reported
1946 // success and nothing hstore-shaped worked afterwards.
1947 K::ExtensionAvailable | K::ExtensionInstalled => {
1948 for n in names {
1949 if !crate::system_catalog::INSTALLED_EXTENSIONS
1950 .iter()
1951 .any(|(e, _)| e.eq_ignore_ascii_case(n.as_str()))
1952 {
1953 self.warning(alloc::format!(
1954 "extension \"{n}\" is not provided by this build; SPG \
1955 accepts the statement so a dump restores, but nothing \
1956 that extension supplies will be available"
1957 ));
1958 }
1959 }
1960 }
1961 // v7.39 (round 708) — ALTER TYPE's no-op forms validate
1962 // the name against the three user-type catalogs.
1963 K::TypeName => {
1964 for n in &names {
1965 let cat = self.active_catalog();
1966 if !cat.enum_types().contains_key(n)
1967 && !cat.domain_types().contains_key(n)
1968 && !cat.composite_types().contains_key(n)
1969 {
1970 return Err(EngineError::Unsupported(alloc::format!(
1971 "type \"{n}\" does not exist"
1972 )));
1973 }
1974 }
1975 }
1976 // v7.39 (round 708) — names[0] = aggregate, rest = arg
1977 // type names; existence by name (round 707's residual on
1978 // overloads applies here too).
1979 K::AggregateName => {
1980 let Some(name) = names.first() else {
1981 return Ok(QueryResult::CommandOk {
1982 affected: 0,
1983 modified_catalog: false,
1984 });
1985 };
1986 if !crate::aggregate::is_aggregate_name(name.as_str()) {
1987 let canon: Vec<alloc::string::String> = names[1..]
1988 .iter()
1989 .map(|t| {
1990 if t == "*" {
1991 alloc::string::String::from("*")
1992 } else {
1993 crate::conversions::type_name_to_data_type(t).map_or_else(
1994 || t.clone(),
1995 crate::conversions::pg_type_name_for_error,
1996 )
1997 }
1998 })
1999 .collect();
2000 return Err(EngineError::Unsupported(alloc::format!(
2001 "aggregate {name}({}) does not exist",
2002 canon.join(", ")
2003 )));
2004 }
2005 }
2006 // v7.39 (round 708) — SPG ships no conversions at all,
2007 // so PG's not-found answer is total here.
2008 K::ConversionName => {
2009 if let Some(n) = names.first() {
2010 return Err(EngineError::Unsupported(alloc::format!(
2011 "conversion \"{n}\" does not exist"
2012 )));
2013 }
2014 }
2015 // v7.39 (round 708) — the shipped languages are
2016 // required; anything else does not exist. Both wordings
2017 // are PG18 measurements.
2018 K::LanguageName => {
2019 // One name per statement; PG errors on the first
2020 // either way, so `first` says what the loop only
2021 // implied (clippy: never actually loops).
2022 if let Some(n) = names.first() {
2023 let lc = n.to_ascii_lowercase();
2024 return Err(EngineError::Unsupported(match lc.as_str() {
2025 "plpgsql" => alloc::format!(
2026 "cannot drop language {lc} because extension {lc} requires it"
2027 ),
2028 "sql" | "internal" | "c" => alloc::format!(
2029 "cannot drop language {lc} because it is required by the database system"
2030 ),
2031 _ => alloc::format!("language \"{n}\" does not exist"),
2032 }));
2033 }
2034 }
2035 // v7.39 (round 709) — batch-2 name checks, each wording
2036 // a PG18 measurement.
2037 K::CollationName => {
2038 for n in &names {
2039 if !crate::collate::is_supported(n) {
2040 return Err(EngineError::Unsupported(alloc::format!(
2041 "collation \"{n}\" for encoding \"UTF8\" does not exist"
2042 )));
2043 }
2044 }
2045 }
2046 K::TsConfigName => {
2047 for n in &names {
2048 // One list with the pg_ts_config synth: SPG
2049 // ships `simple` and `english`.
2050 if !n.eq_ignore_ascii_case("simple")
2051 && !n.eq_ignore_ascii_case("english")
2052 {
2053 return Err(EngineError::Unsupported(alloc::format!(
2054 "text search configuration \"{n}\" does not exist"
2055 )));
2056 }
2057 }
2058 }
2059 K::EventTriggerName => {
2060 if let Some(n) = names.first() {
2061 return Err(EngineError::Unsupported(alloc::format!(
2062 "event trigger \"{n}\" does not exist"
2063 )));
2064 }
2065 }
2066 K::TablespaceName => {
2067 if let Some(n) = names.first() {
2068 return Err(EngineError::Unsupported(
2069 if n.eq_ignore_ascii_case("pg_default")
2070 || n.eq_ignore_ascii_case("pg_global")
2071 {
2072 alloc::format!("permission denied for tablespace {n}")
2073 } else {
2074 alloc::format!("tablespace \"{n}\" does not exist")
2075 },
2076 ));
2077 }
2078 }
2079 K::LargeObjectOid => {
2080 if let Some(n) = names.first() {
2081 let oid: u32 = n.parse().unwrap_or(0);
2082 if !self.active_catalog().large_objects().contains_key(&oid) {
2083 return Err(EngineError::Unsupported(alloc::format!(
2084 "large object {n} does not exist"
2085 )));
2086 }
2087 }
2088 }
2089 // v7.39 (round 706) — see ValidateOnlyKind::ForeignInfra
2090 // for why this warns instead of copying PG's refusal.
2091 K::ForeignInfra => {
2092 self.warning(alloc::string::String::from(
2093 "foreign-data infrastructure is not provided by this build; \
2094 SPG accepts the statement so a dump restores, but no foreign \
2095 server, wrapper or table it defines will function",
2096 ));
2097 }
2098 K::SecurityLabel => {
2099 return Err(EngineError::Unsupported(alloc::string::String::from(
2100 "no security label providers have been loaded",
2101 )));
2102 }
2103 }
2104 Ok(QueryResult::CommandOk {
2105 affected: 0,
2106 modified_catalog: false,
2107 })
2108 }
2109 Statement::DropDatabase { name, if_exists } => {
2110 // PG refuses this inside a transaction block; so does
2111 // CREATE DATABASE, and both go through the same guard.
2112 self.require_no_transaction_block("DROP DATABASE")?;
2113 // SPG serves one database, so the name is either the one
2114 // this session is connected to or a name that does not
2115 // exist here. PG has wording for both and never lets
2116 // either succeed, which is the whole behaviour.
2117 let is_current = self
2118 .session_param("spg.database")
2119 .unwrap_or("spg")
2120 .eq_ignore_ascii_case(&name);
2121 if is_current {
2122 return Err(EngineError::Unsupported(alloc::string::String::from(
2123 "cannot drop the currently open database",
2124 )));
2125 }
2126 if if_exists {
2127 self.notice(alloc::format!(
2128 "database \"{name}\" does not exist, skipping"
2129 ));
2130 return Ok(QueryResult::CommandOk {
2131 affected: 0,
2132 modified_catalog: false,
2133 });
2134 }
2135 Err(EngineError::Unsupported(alloc::format!(
2136 "database \"{name}\" does not exist"
2137 )))
2138 }
2139 Statement::NoOpPreventedInTransaction { what, collation } => {
2140 self.require_no_transaction_block(&what)?;
2141 // v7.38.18 — the NAME is a no-op here; the collation is
2142 // not. A bootstrap script that says `LC_COLLATE
2143 // 'de_DE.utf8'` and gets the container's `LANG` instead
2144 // has a different answer to every ORDER BY it runs, and
2145 // nothing told it so.
2146 let mut modified_catalog = false;
2147 if let Some(c) = collation {
2148 if self.declare_database_collation(&c)? {
2149 modified_catalog = true;
2150 } else {
2151 self.warning(alloc::format!(
2152 "collation {c:?} was not applied: this database \
2153 already has tables, and their index keys were \
2154 built under {:?}",
2155 self.catalog.db_collation()
2156 ));
2157 }
2158 }
2159 Ok(QueryResult::CommandOk {
2160 affected: 0,
2161 modified_catalog,
2162 })
2163 }
2164 Statement::AlterSystem { parameter } => {
2165 // PG refuses this inside a transaction block (25001): it
2166 // edits postgresql.auto.conf, which no rollback undoes.
2167 self.require_no_transaction_block("ALTER SYSTEM")?;
2168 if let Some(name) = parameter
2169 && let Some(msg) = self.reject_unsettable_guc(name.as_str())
2170 {
2171 return Err(EngineError::Unsupported(msg));
2172 }
2173 Ok(QueryResult::CommandOk {
2174 affected: 0,
2175 modified_catalog: false,
2176 })
2177 }
2178 Statement::DropTable { names, if_exists } => self.exec_drop_table(names, if_exists),
2179 Statement::DropIndex { name, if_exists } => self.exec_drop_index(name, if_exists),
2180 Statement::CreateIndex(s) => {
2181 // PG bars only the CONCURRENTLY form inside a transaction
2182 // block (25001); a plain CREATE INDEX there is fine.
2183 if s.concurrently {
2184 self.require_no_transaction_block("CREATE INDEX CONCURRENTLY")?;
2185 }
2186 self.exec_create_index(s)
2187 }
2188 Statement::Insert(s) => {
2189 // v7.39 (pg_stat knife A) — per-table n_tup_ins. Charged
2190 // to the statement's target (a partition-routed insert
2191 // charges the parent; ON CONFLICT updates count here
2192 // too — split is a recorded residual).
2193 let stat_table = s.table.clone();
2194 let r = self.exec_insert(s)?;
2195 if let QueryResult::CommandOk { affected, .. } = &r {
2196 self.stat_tup_inserted =
2197 self.stat_tup_inserted.saturating_add(*affected as u64);
2198 // r192 — engine-side, non-transactional (see
2199 // table_write_stats): in-tx bumps used to land on
2200 // the shadow table and vanish in the RC rebase.
2201 self.note_table_write(&stat_table, *affected as u64, 0, 0);
2202 }
2203 Ok(r)
2204 }
2205 Statement::Update(mut s) => {
2206 // Materialise uncorrelated subqueries in SET / WHERE
2207 // before the row walk — the SELECT path has done this
2208 // since v4.10; UPDATE gained it for mailrs's
2209 // `UPDATE … WHERE id IN (SELECT … FOR UPDATE SKIP
2210 // LOCKED)` claim pattern (embed round-12).
2211 // v7.39 (round 157) — NOT with a WITH clause: the CTE
2212 // temps aren't installed yet here, so a subquery reading
2213 // a CTE either failed ("relation does not exist") or —
2214 // when a same-named real table existed — silently read
2215 // THAT. exec_update_with_ctes resolves after the temps
2216 // install instead.
2217 if s.ctes.is_empty() {
2218 for (_, e) in &mut s.assignments {
2219 self.resolve_expr_subqueries(e, cancel)?;
2220 }
2221 if let Some(w) = &mut s.where_ {
2222 self.resolve_expr_subqueries(w, cancel)?;
2223 }
2224 }
2225 let r = self.exec_update_cancel(&s, cancel)?;
2226 if let QueryResult::CommandOk { affected, .. } = &r {
2227 self.stat_tup_updated = self.stat_tup_updated.saturating_add(*affected as u64);
2228 self.note_table_write(&s.table, 0, *affected as u64, 0);
2229 }
2230 Ok(r)
2231 }
2232 Statement::Delete(mut s) => {
2233 // v7.39 (round 157) — see the Update arm: with a WITH
2234 // clause the resolve runs after the CTE temps install.
2235 if s.ctes.is_empty()
2236 && let Some(w) = &mut s.where_
2237 {
2238 self.resolve_expr_subqueries(w, cancel)?;
2239 }
2240 let r = self.exec_delete_cancel(&s, cancel)?;
2241 if let QueryResult::CommandOk { affected, .. } = &r {
2242 self.stat_tup_deleted = self.stat_tup_deleted.saturating_add(*affected as u64);
2243 self.note_table_write(&s.table, 0, 0, *affected as u64);
2244 }
2245 Ok(r)
2246 }
2247 Statement::Merge(s) => self.exec_merge_cancel(&s, cancel),
2248 // v7.39 (round 295, E3 Phase 1b) — a locking SELECT takes its
2249 // locks in a `&mut self` pre-pass that respects LIMIT, then
2250 // runs the ordinary read path with the rows another
2251 // transaction holds excluded.
2252 Statement::Select(ref sel) if sel.locking.is_some() => {
2253 let sel = sel.clone();
2254 self.lock_skip_rows = None;
2255 let pre = self.run_locking_prepass(&sel);
2256 if let Err(e) = pre {
2257 self.lock_skip_rows = None;
2258 return Err(e);
2259 }
2260 let out = self.exec_select_cancel(&sel, cancel);
2261 self.lock_skip_rows = None;
2262 out
2263 }
2264 Statement::Select(s) => {
2265 // v7.38 (read01 P3.20) — `SELECT set_config(name, value,
2266 // is_local)` is the writing sibling of SHOW / current_setting;
2267 // apply it to the session store (respecting is_local) so the
2268 // four GUC surfaces stay unified. pg_dump's
2269 // `SELECT set_config('search_path', '', false)` relies on this.
2270 if let Some(r) = self.try_exec_set_config(&s)? {
2271 return Ok(r);
2272 }
2273 if s.ctes.iter().any(|c| c.body.is_modifying()) {
2274 self.exec_select_with_modifying_ctes(s, cancel)
2275 } else {
2276 self.exec_select_cancel(&s, cancel)
2277 }
2278 }
2279 // v7.39 (round 249) — the engine is no_std: the HOST reads the
2280 // file and calls `copy_from_buffer`. Reaching this arm means a
2281 // host that hasn't wired the file endpoint.
2282 Statement::CopyFromFile { path, .. } => Err(EngineError::Unsupported(alloc::format!(
2283 "COPY FROM file: the host must read {path:?} and call copy_from_buffer"
2284 ))),
2285 Statement::CopyTo {
2286 table,
2287 columns,
2288 query,
2289 options,
2290 } => self.exec_copy_to(
2291 &table,
2292 columns.as_deref(),
2293 query.as_deref(),
2294 &options,
2295 cancel,
2296 ),
2297 // v7.39 (round 252) — the engine is no_std: the HOST renders
2298 // via `copy_to_buffer` and writes the file itself.
2299 Statement::CopyToFile { path, .. } => Err(EngineError::Unsupported(alloc::format!(
2300 "COPY TO file: the host must render via copy_to_buffer and write {path:?}"
2301 ))),
2302 // v7.39 (round 475) — a redundant BEGIN inside a transaction.
2303 //
2304 // SPG raised "a transaction is already open" AND left the
2305 // transaction in the aborted state, so the next statement failed
2306 // with "current transaction is aborted" and the whole block was
2307 // lost. A connection pooler or a framework that wraps its own
2308 // BEGIN around one the caller already opened does this routinely.
2309 //
2310 // The two oracles genuinely differ, and both were measured:
2311 // PG18 WARNING: there is already a transaction in
2312 // progress — the BEGIN is a no-op and the existing
2313 // transaction continues (a later ROLLBACK undoes
2314 // everything, both rows in the probe).
2315 // MariaDB 11 START TRANSACTION implicitly COMMITS the open one
2316 // and begins a new one (the first row survives the
2317 // rollback, the second does not).
2318 // The predicate is THIS connection's slot, not the engine-global
2319 // `in_transaction()`: the server shares one Engine, so the global
2320 // form makes connection B's BEGIN see connection A's transaction
2321 // (rounds 279 / 283 / 298 / 304 / 443 / 444 are the same trap).
2322 Statement::Begin(_)
2323 if self.current_tx.is_some_and(|t| self.is_tx_open(t))
2324 && !self.backslash_escapes =>
2325 {
2326 self.warning(alloc::string::String::from(
2327 "there is already a transaction in progress",
2328 ));
2329 Ok(QueryResult::CommandOk {
2330 affected: 0,
2331 modified_catalog: false,
2332 })
2333 }
2334 Statement::Begin(isolation) if self.current_tx.is_some_and(|t| self.is_tx_open(t)) => {
2335 // MySQL dialect: commit what is open, then start fresh.
2336 self.exec_commit()?;
2337 self.exec_begin(isolation)
2338 }
2339 Statement::Begin(isolation) => self.exec_begin(isolation),
2340 // v7.39 (round 435) — a bare COMMIT / ROLLBACK outside a
2341 // transaction is a no-op that SUCCEEDS. Measured on both
2342 // oracles: PG18 answers `WARNING: there is no transaction in
2343 // progress` and still reports COMMIT / ROLLBACK; MariaDB 11
2344 // succeeds silently. SPG answered "no active transaction" as an
2345 // ERROR to both dialects — a divergence from each of them.
2346 // It moved onto the hot path with the implicit-commit rule
2347 // above, which leaves a client's trailing ROLLBACK with nothing
2348 // to roll back.
2349 Statement::Commit | Statement::Rollback if !self.in_transaction() => {
2350 if !self.backslash_escapes {
2351 self.warning(alloc::string::String::from(
2352 "there is no transaction in progress",
2353 ));
2354 }
2355 Ok(QueryResult::CommandOk {
2356 affected: 0,
2357 modified_catalog: false,
2358 })
2359 }
2360 Statement::Commit => self.exec_commit(),
2361 Statement::Rollback => self.exec_rollback(),
2362 Statement::Savepoint(name) => self.exec_savepoint(name),
2363 Statement::RollbackToSavepoint(name) => self.exec_rollback_to_savepoint(&name),
2364 Statement::ReleaseSavepoint(name) => self.exec_release_savepoint(&name),
2365 Statement::ShowTables => Ok(self.exec_show_tables()),
2366 Statement::ShowDatabases => Ok(self.exec_show_databases()),
2367 Statement::ShowCreateTable(name) => self.exec_show_create_table(&name),
2368 Statement::ShowIndexes(name) => self.exec_show_indexes(&name),
2369 Statement::ShowStatus => Ok(self.exec_show_status()),
2370 Statement::ShowVariables => Ok(self.exec_show_variables()),
2371 Statement::ShowVariablesLike(p) => Ok(self.exec_show_variables_like(&p)),
2372 Statement::ShowProcesslist => Ok(self.exec_show_processlist()),
2373 Statement::Kill { query_only, id } => self.exec_kill(query_only, &id),
2374 Statement::Discard(target) => self.exec_discard(target),
2375 Statement::ShowColumns(table) => self.exec_show_columns(&table),
2376 Statement::ShowUsers => Ok(self.exec_show_users()),
2377 Statement::ShowPublications => Ok(self.exec_show_publications()),
2378 Statement::ShowSubscriptions => Ok(self.exec_show_subscriptions()),
2379 Statement::CreateUser(s) => self.exec_create_user(&s),
2380 Statement::DropUser { name, if_exists } => self.exec_drop_user(&name, if_exists),
2381 Statement::SetRole(role) => {
2382 match role {
2383 Some(name) => {
2384 // v7.39 (read01 round 58) — PG rejects a SET ROLE to a
2385 // role that does not exist. Before roles were real
2386 // there was nothing to check against, so any name was
2387 // accepted — and a typo silently put the session into
2388 // a role that held nothing.
2389 self.acl_check_role_exists(&name)?;
2390 self.session_params.insert(
2391 alloc::string::String::from(crate::session::CURRENT_ROLE_KEY),
2392 name,
2393 );
2394 }
2395 None => {
2396 self.session_params.remove(crate::session::CURRENT_ROLE_KEY);
2397 }
2398 }
2399 Ok(QueryResult::CommandOk {
2400 affected: 0,
2401 modified_catalog: false,
2402 })
2403 }
2404 Statement::Grant(g) => self.exec_grant(&g, true),
2405 Statement::Revoke(g) => self.exec_grant(&g, false),
2406 Statement::CreatePolicy(s) => self.exec_create_policy(s),
2407 Statement::AlterPolicy(s) => self.exec_alter_policy(s),
2408 Statement::DropPolicy(s) => self.exec_drop_policy(s),
2409 // v7.39 (round 286) — ANALYZE over DML really executes, so it
2410 // needs the `&mut self` sibling. Everything else (including
2411 // plain EXPLAIN of a write) stays on the read-only renderer.
2412 // v7.39 (round 288) — SET CONSTRAINTS sets the timing for the
2413 // rest of the transaction. IMMEDIATE also runs everything the
2414 // transaction has postponed, right here — PG raises the
2415 // violation at this statement, not at COMMIT.
2416 Statement::SetConstraints { names, deferred } => {
2417 self.exec_set_constraints(&names, deferred)
2418 }
2419 Statement::Explain(e)
2420 if e.analyze
2421 && !e.suggest
2422 && matches!(
2423 &*e.inner,
2424 Statement::Insert(_) | Statement::Update(_) | Statement::Delete(_)
2425 ) =>
2426 {
2427 self.exec_explain_analyze_dml(&e, cancel)
2428 }
2429 Statement::Explain(e) => self.exec_explain(&e, cancel),
2430 Statement::AlterIndex(s) => self.exec_alter_index(s),
2431 Statement::AlterTable(s) => self.exec_alter_table(s),
2432 Statement::CreatePublication(s) => self.exec_create_publication(s),
2433 Statement::DropPublication { name, if_exists } => {
2434 self.exec_drop_publication(&name, if_exists)
2435 }
2436 Statement::CreateSubscription(s) => self.exec_create_subscription(s),
2437 Statement::DropSubscription { name, if_exists } => {
2438 self.exec_drop_subscription(&name, if_exists)
2439 }
2440 // v6.1.7 — WAIT FOR WAL POSITION needs `lag_state`,
2441 // which lives in spg-server's ServerState. The engine
2442 // surfaces a clear error; the server-layer dispatch
2443 // intercepts the SQL before it reaches the engine on
2444 // a server build, so this arm only fires for
2445 // engine-only callers (spg-embedded, lib tests).
2446 Statement::WaitForWalPosition { .. } => Err(EngineError::Unsupported(
2447 "WAIT FOR WAL POSITION must be handled by the server layer".into(),
2448 )),
2449 // v6.2.0 — ANALYZE recomputes per-column histograms.
2450 Statement::Analyze(target) => self.exec_analyze(target.as_deref()),
2451 // v7.39 (round 535) — REINDEX / CLUSTER. SPG has neither index
2452 // bloat to rebuild nor a clustering order to impose, so the
2453 // work is a no-op — but PG VALIDATES the target, and both
2454 // statements were swallowed at parse time AND intercepted at
2455 // the wire, so `REINDEX TABLE typo` answered `REINDEX`. A
2456 // maintenance script that misspells a table was told it
2457 // succeeded.
2458 Statement::Maintain {
2459 kind,
2460 concurrently,
2461 target,
2462 } => {
2463 use spg_sql::ast::MaintainKind;
2464 if concurrently {
2465 self.require_no_transaction_block(match kind {
2466 MaintainKind::ClusterRelation => "CLUSTER",
2467 _ => "REINDEX CONCURRENTLY",
2468 })?;
2469 }
2470 match (kind, target.as_deref()) {
2471 (MaintainKind::ReindexRelation | MaintainKind::ClusterRelation, Some(t)) => {
2472 // An INDEX is a relation too — `REINDEX INDEX ix`
2473 // names one, and looking only at tables refused a
2474 // name that is right there.
2475 let is_index = self
2476 .active_catalog()
2477 .table_names()
2478 .iter()
2479 .filter_map(|n| self.active_catalog().get(n))
2480 .any(|tbl| {
2481 tbl.indices().iter().any(|i| i.name.eq_ignore_ascii_case(t))
2482 });
2483 if !is_index && self.active_catalog().get(t).is_none() {
2484 return Err(EngineError::Storage(
2485 spg_storage::StorageError::TableNotFound { name: t.into() },
2486 ));
2487 }
2488 }
2489 (MaintainKind::ReindexSchema, Some(t)) => {
2490 if !spg_storage::is_builtin_schema(t)
2491 && !self.active_catalog().schema_exists(t)
2492 {
2493 return Err(EngineError::Unsupported(alloc::format!(
2494 "schema \"{t}\" does not exist"
2495 )));
2496 }
2497 }
2498 // `REINDEX SYSTEM` / `REINDEX DATABASE` / a bare
2499 // `CLUSTER` name nothing to check.
2500 _ => {}
2501 }
2502 Ok(QueryResult::CommandOk {
2503 affected: 0,
2504 modified_catalog: false,
2505 })
2506 }
2507 // v7.39 (round 169) — VACUUM does real work under the MVCC
2508 // gate (tombstoned versions are actual bloat); the pre-MVCC
2509 // parse-time no-op silently ignored a customer's manual
2510 // reclaim. Gate-off stays a provable no-op inside vacuum.
2511 Statement::Vacuum { table, analyze } => {
2512 // PG 18.4, measured: every VACUUM form — bare, with a
2513 // table, and VACUUM ANALYZE — is refused inside a
2514 // transaction block with 25001, while a plain ANALYZE is
2515 // allowed. Reclaiming storage cannot be rolled back, so
2516 // it must not be able to join a transaction that can.
2517 self.require_no_transaction_block("VACUUM")?;
2518 match &table {
2519 Some(t) => {
2520 // v7.39 (round 535) — PG refuses a VACUUM whose
2521 // relation does not exist; `vacuum_one_table`
2522 // simply found nothing to do and said nothing,
2523 // so a typo'd table reported success.
2524 if self.active_catalog().get(t).is_none() {
2525 return Err(EngineError::Storage(
2526 spg_storage::StorageError::TableNotFound { name: t.clone() },
2527 ));
2528 }
2529 self.vacuum_one_table(t);
2530 }
2531 None => {
2532 let _ = self.vacuum_pass(false);
2533 }
2534 }
2535 if analyze {
2536 self.exec_analyze(table.as_deref())?;
2537 }
2538 Ok(QueryResult::CommandOk {
2539 affected: 0,
2540 modified_catalog: false,
2541 })
2542 }
2543 // v7.37.17 (17.6 sibling) — TRUNCATE [TABLE] <t>[, ...]
2544 // [RESTART IDENTITY] [CASCADE]. Clears every row from
2545 // each named table. CASCADE currently accepts the syntax
2546 // + records the flag; the FK-referring cascade walk lands
2547 // when FK-cascade delete surface gets extended to
2548 // multi-relation batching (v7.38).
2549 Statement::Truncate {
2550 tables,
2551 restart_identity,
2552 cascade: _,
2553 only,
2554 } => {
2555 for t in &tables {
2556 self.bump_table_change(t);
2557 }
2558 self.exec_truncate(tables.as_slice(), restart_identity, only)
2559 }
2560 // v6.7.3 — COMPACT COLD SEGMENTS.
2561 Statement::CompactColdSegments => self.exec_compact_cold_segments(),
2562 // v7.12.1 — SET / RESET session parameter. Engine
2563 // tracks the value in `session_params`; FTS dispatcher
2564 // reads `default_text_search_config`. Everything else
2565 // is a recorded no-op (PG dump compat).
2566 Statement::SetParameter { name, value, local } => {
2567 // v7.39 (round 501) — a name PG18 does not know, or one a
2568 // session cannot change, is an error there and was
2569 // silently accepted here (round 500).
2570 if let Some(msg) = self.reject_unsettable_guc(&name) {
2571 return Err(EngineError::Unsupported(msg));
2572 }
2573 // v7.38 (read01) — SPG serves the wire as UTF8, so a
2574 // non-UTF8 client_encoding can't be honoured (the bytes
2575 // stay UTF8). Reject it rather than silently store a value
2576 // that would mislabel the stream; an unusable name is
2577 // rejected the way PG rejects an invalid one.
2578 if name.eq_ignore_ascii_case("client_encoding") {
2579 let v: &str = match &value {
2580 spg_sql::ast::SetValue::String(s)
2581 | spg_sql::ast::SetValue::Ident(s)
2582 | spg_sql::ast::SetValue::Number(s) => s.as_str(),
2583 spg_sql::ast::SetValue::Default => "UTF8",
2584 };
2585 let norm: alloc::string::String = v
2586 .trim()
2587 .to_ascii_uppercase()
2588 .chars()
2589 .filter(|c| *c != '-' && *c != '_')
2590 .collect();
2591 if !matches!(norm.as_str(), "UTF8" | "UNICODE") {
2592 return Err(EngineError::Unsupported(alloc::format!(
2593 "invalid value for parameter \"client_encoding\": \"{v}\" \
2594 (SPG serves UTF8 only)"
2595 )));
2596 }
2597 }
2598 // v7.38 (read01 P3.17) — reject a clearly-invalid value for
2599 // a handful of well-known typed GUCs (`SET work_mem =
2600 // 'bogus'` errors like PG). Unknown GUCs stay accept-and-
2601 // record for pg_dump compat.
2602 if let spg_sql::ast::SetValue::String(s)
2603 | spg_sql::ast::SetValue::Ident(s)
2604 | spg_sql::ast::SetValue::Number(s) = &value
2605 {
2606 validate_known_guc(&name, s)?;
2607 // v7.39 (tz epic) — timezone accepts UTC / fixed
2608 // offsets / abbreviations (resolve_zone_offset) and
2609 // IANA names (host tzdb); anything else is PG's
2610 // invalid-parameter error. Named zones store their
2611 // canonical spelling (SHOW returns 'Asia/Tokyo'
2612 // after SET 'asia/tokyo').
2613 if name.eq_ignore_ascii_case("timezone")
2614 || name.eq_ignore_ascii_case("time zone")
2615 {
2616 let canon = self.canonicalize_timezone(s)?;
2617 let local = local;
2618 if local {
2619 if self.in_transaction() {
2620 let prior = self.session_param("timezone").map(String::from);
2621 self.local_guc_saves.push(("timezone".into(), prior));
2622 self.set_session_param(
2623 "timezone".into(),
2624 spg_sql::ast::SetValue::String(canon),
2625 );
2626 }
2627 } else {
2628 self.set_session_param(
2629 "timezone".into(),
2630 spg_sql::ast::SetValue::String(canon),
2631 );
2632 }
2633 return Ok(QueryResult::CommandOk {
2634 affected: 0,
2635 modified_catalog: false,
2636 });
2637 }
2638 }
2639 // v7.38 (read01 P3.19) — `SET LOCAL` scopes the change to
2640 // the current transaction: record the prior value in the
2641 // undo log so COMMIT / ROLLBACK (and ROLLBACK TO) restore
2642 // it. Outside a transaction block it has no lasting effect
2643 // (PG scopes it to the implicit single-statement txn), so
2644 // it is dropped rather than persisted to the session.
2645 if local {
2646 if self.in_transaction() {
2647 let prior = self.session_param(&name).map(String::from);
2648 self.local_guc_saves.push((name.clone(), prior));
2649 self.set_session_param(name, value);
2650 }
2651 } else {
2652 self.set_session_param(name, value);
2653 }
2654 Ok(QueryResult::CommandOk {
2655 affected: 0,
2656 modified_catalog: false,
2657 })
2658 }
2659 // v7.38 轴 4 — `SET TRANSACTION ISOLATION LEVEL …`. The
2660 // surface is recorded on `Engine::current_isolation_level`
2661 // and visible via `SHOW transaction_isolation`. Behavioural
2662 // implementation (REPEATABLE READ snapshot / SERIALIZABLE
2663 // SSI) lands separately; today every level reads as
2664 // effective READ COMMITTED (same as PG's silent upgrade
2665 // of READ UNCOMMITTED).
2666 Statement::SetTransaction { isolation } => {
2667 // v7.37.17 (Phase E3) — PG rejects an isolation switch
2668 // after the transaction's first query (SQLSTATE 25001);
2669 // silently applying it to the remaining statements would
2670 // give a tx that is half one level, half another.
2671 if let Some(tx_id) = self.current_tx
2672 && self
2673 .tx_catalogs
2674 .get(&tx_id)
2675 .is_some_and(|st| st.stmts_run > 0)
2676 {
2677 return Err(EngineError::Unsupported(
2678 "SET TRANSACTION ISOLATION LEVEL must be called before any query".into(),
2679 ));
2680 }
2681 self.current_isolation_level = isolation;
2682 // v7.37.17 (Phase E2) — inside an open tx, switching to
2683 // RR/SER BEFORE the first query freezes the tx's view by
2684 // caching a snapshot now (PG allows the switch until the
2685 // first query; the RC rebase keys off cached_snapshot).
2686 // Switching (back) to RC/RU clears it so the rebase
2687 // resumes.
2688 if let Some(tx_id) = self.current_tx
2689 && self.tx_catalogs.contains_key(&tx_id)
2690 {
2691 let cache = match isolation {
2692 spg_sql::ast::IsolationLevel::RepeatableRead
2693 | spg_sql::ast::IsolationLevel::Serializable => {
2694 Some(self.current_snapshot())
2695 }
2696 spg_sql::ast::IsolationLevel::ReadUncommitted
2697 | spg_sql::ast::IsolationLevel::ReadCommitted => None,
2698 };
2699 if let Some(st) = self.tx_catalogs.get_mut(&tx_id) {
2700 st.cached_snapshot = cache;
2701 }
2702 }
2703 Ok(QueryResult::CommandOk {
2704 affected: 0,
2705 modified_catalog: false,
2706 })
2707 }
2708 // v7.38 轴 4 surface expansion — `SHOW <parameter>`
2709 // returns a 1-row 1-column TEXT result (the PG psql
2710 // wire shape). The handler dispatches per-name:
2711 //
2712 // 1. transaction_isolation — direct read of
2713 // current_isolation_level (the v7.38 axis-4 surface).
2714 // 2. PG preset / engine-tracked params — values mirror
2715 // pg_catalog.pg_settings to keep ORM /
2716 // driver-connect probes happy (sqlx asks
2717 // server_version + standard_conforming_strings +
2718 // client_encoding; npgsql asks application_name;
2719 // asyncpg asks search_path). Any
2720 // SET-tracked override on self.session_params wins.
2721 // 3. Anything else — error with a list-pointer to
2722 // pg_settings (which lists every recognised name).
2723 Statement::ShowParameter(name) => self.exec_show_parameter(name),
2724 // v7.14.0 — MySQL multi-assignment SET. Each pair runs
2725 // through `set_session_param` so engine-known params
2726 // (FOREIGN_KEY_CHECKS, session_replication_role, …) take
2727 // effect; unknown pairs (including `@VAR` LHS from the
2728 // mysqldump preamble) are recorded then ignored.
2729 Statement::SetParameterList(pairs) => {
2730 // Same validation as the single form (round 501).
2731 for (name, _) in &pairs {
2732 if let Some(msg) = self.reject_unsettable_guc(name) {
2733 return Err(EngineError::Unsupported(msg));
2734 }
2735 }
2736 for (name, value) in pairs {
2737 self.set_session_param(name, value);
2738 }
2739 Ok(QueryResult::CommandOk {
2740 affected: 0,
2741 modified_catalog: false,
2742 })
2743 }
2744 // v7.12.4 — CREATE FUNCTION / CREATE TRIGGER / DROP …
2745 // for the PL/pgSQL trigger surface. exec_* methods are
2746 // defined alongside the existing CREATE handlers below.
2747 Statement::CreateFunction(s) => self.exec_create_function(s),
2748 Statement::CreateTrigger(s) => self.exec_create_trigger(s),
2749 Statement::DropTrigger {
2750 name,
2751 table,
2752 if_exists,
2753 } => self.exec_drop_trigger(&name, &table, if_exists),
2754 Statement::CreateRule(s) => self.exec_create_rule(s),
2755 Statement::DropRule {
2756 name,
2757 table,
2758 if_exists,
2759 } => self.exec_drop_rule(&name, &table, if_exists),
2760 Statement::DropFunction {
2761 name,
2762 args,
2763 if_exists,
2764 } => self.exec_drop_function(&name, args.as_deref(), if_exists),
2765 Statement::CreateSequence(s) => self.exec_create_sequence(s),
2766 Statement::AlterSequence(s) => self.exec_alter_sequence(s),
2767 Statement::DropSequence { names, if_exists } => {
2768 self.exec_drop_sequence(&names, if_exists)
2769 }
2770 Statement::CreateView(s) => self.exec_create_view(s),
2771 Statement::DropView { names, if_exists } => self.exec_drop_view(&names, if_exists),
2772 Statement::CreateMaterializedView(s) => self.exec_create_materialized_view(s),
2773 Statement::RefreshMaterializedView { name, with_data } => {
2774 self.exec_refresh_materialized_view(&name, with_data)
2775 }
2776 Statement::DropMaterializedView { names, if_exists } => {
2777 self.exec_drop_materialized_view(&names, if_exists)
2778 }
2779 Statement::CreateType(s) => self.exec_create_type(s),
2780 Statement::CommentOn {
2781 kind,
2782 name,
2783 comment,
2784 } => self.exec_comment_on(&kind, &name, comment.as_deref()),
2785 Statement::AlterTypeRenameValue {
2786 type_name,
2787 old,
2788 new,
2789 } => {
2790 self.active_catalog_mut()
2791 .rename_enum_value(&type_name, &old, &new)
2792 .map_err(EngineError::Storage)?;
2793 Ok(QueryResult::CommandOk {
2794 affected: 0,
2795 modified_catalog: self.catalog_change_is_committed(),
2796 })
2797 }
2798 Statement::AlterTypeAddValue {
2799 type_name,
2800 label,
2801 if_not_exists,
2802 position,
2803 } => {
2804 let added = self
2805 .active_catalog_mut()
2806 .add_enum_value(&type_name, &label, if_not_exists, position)
2807 .map_err(EngineError::Storage)?;
2808 Ok(QueryResult::CommandOk {
2809 affected: 0,
2810 modified_catalog: added,
2811 })
2812 }
2813 Statement::DropType { names, if_exists } => self.exec_drop_type(&names, if_exists),
2814 Statement::CreateDomain(s) => self.exec_create_domain(s),
2815 Statement::AlterDomain { name, action } => self.exec_alter_domain(&name, action),
2816 Statement::DropDomain { names, if_exists } => self.exec_drop_domain(&names, if_exists),
2817 Statement::CreateSchema {
2818 name,
2819 if_not_exists,
2820 } => self.exec_create_schema(name, if_not_exists),
2821 Statement::DropSchema { names, if_exists } => self.exec_drop_schema(&names, if_exists),
2822 Statement::ResetParameter(target) => {
2823 match target {
2824 // v7.39 (round 320, V53) — RESET ALL resets GUCs. It
2825 // must NOT throw away the two internal keys the server
2826 // parks in the same map: the connection's login
2827 // identity and its database. PG has no way to reset
2828 // those with RESET ALL (they are not GUCs), and
2829 // clearing them here made `current_user` fall back to
2830 // the admin default mid-session.
2831 None => self.reset_all_gucs(),
2832 Some(name) => {
2833 self.session_params.remove(&name.to_ascii_lowercase());
2834 }
2835 }
2836 self.refresh_render_style();
2837 Ok(QueryResult::CommandOk {
2838 affected: 0,
2839 modified_catalog: false,
2840 })
2841 }
2842 };
2843 self.enforce_row_limit(result)
2844 }
2845}
2846
2847impl Engine {
2848 /// v7.39 (round 247) — resolve the CSV-only extras. QUOTE / ESCAPE /
2849 /// FORCE_QUOTE outside CSV mode are PG's 0A000 refusals (SPG used to
2850 /// ignore a text-mode QUOTE silently); the returned mask marks the
2851 /// force-quoted columns of `column_names`.
2852 fn resolve_copy_csv_extras(
2853 options: &spg_sql::ast::CopyOptions,
2854 is_csv: bool,
2855 quote: char,
2856 column_names: &[alloc::string::String],
2857 ) -> Result<(char, Option<alloc::vec::Vec<bool>>), EngineError> {
2858 if !is_csv {
2859 if options.quote.is_some() {
2860 return Err(EngineError::Unsupported(
2861 "COPY QUOTE requires CSV mode".into(),
2862 ));
2863 }
2864 if options.escape.is_some() {
2865 return Err(EngineError::Unsupported(
2866 "COPY ESCAPE requires CSV mode".into(),
2867 ));
2868 }
2869 }
2870 // v7.39 (round 265) — the direction-dependent rules (FORCE_QUOTE is
2871 // TO-only, FORCE_NOT_NULL / FORCE_NULL are FROM-only), sharing one
2872 // validator with the FROM path.
2873 crate::copy::validate_copy_option_direction(options, true)?;
2874 let escape = options.escape.unwrap_or(quote);
2875 let force = match &options.force_quote {
2876 None => None,
2877 Some(cols) if cols.is_empty() => Some(alloc::vec![true; column_names.len()]),
2878 Some(cols) => {
2879 let mut mask = alloc::vec![false; column_names.len()];
2880 for c in cols {
2881 let pos = column_names
2882 .iter()
2883 .position(|n| n.eq_ignore_ascii_case(c))
2884 .ok_or_else(|| {
2885 EngineError::Unsupported(alloc::format!(
2886 "column \"{c}\" does not exist"
2887 ))
2888 })?;
2889 mask[pos] = true;
2890 }
2891 Some(mask)
2892 }
2893 };
2894 Ok((escape, force))
2895 }
2896
2897 /// v7.39 (round 249) — resolve the effective COPY FROM target column
2898 /// list, running PG's pre-file checks in PG's order: the relation
2899 /// must exist, an explicit column must exist on it, and no column
2900 /// may appear twice — all before a single data row is looked at.
2901 ///
2902 /// # Errors
2903 /// `relation "t" does not exist`, `column "x" of relation "t" does
2904 /// not exist` (42703), `column "x" specified more than once` (42701).
2905 /// v7.39 (round 343, V40) — store a file the host just read as a
2906 /// large object. The host does the IO (the engine is `no_std`); the
2907 /// catalog side is the same `create_large_object` the rest of the
2908 /// lo_* family uses, so an imported object is indistinguishable from
2909 /// one built with `lo_from_bytea`.
2910 pub fn lo_import_bytes(
2911 &mut self,
2912 want_oid: u32,
2913 data: alloc::vec::Vec<u8>,
2914 ) -> Result<u32, EngineError> {
2915 self.active_catalog_mut()
2916 .create_large_object(want_oid, data)
2917 .map_err(EngineError::Unsupported)
2918 }
2919
2920 /// v7.39 (round 343, V40) — the bytes the host is about to write out.
2921 /// PG's message for a missing object, verbatim.
2922 pub fn lo_export_bytes(&self, oid: u32) -> Result<alloc::vec::Vec<u8>, EngineError> {
2923 self.active_catalog()
2924 .large_object(oid)
2925 .map(<[u8]>::to_vec)
2926 .ok_or_else(|| {
2927 EngineError::Unsupported(alloc::format!("large object {oid} does not exist"))
2928 })
2929 }
2930
2931 pub fn copy_target_columns(
2932 &self,
2933 table: &str,
2934 columns: Option<&[alloc::string::String]>,
2935 ) -> Result<alloc::vec::Vec<alloc::string::String>, EngineError> {
2936 let table_ref = self.active_catalog().get(table).ok_or_else(|| {
2937 EngineError::Storage(spg_storage::StorageError::TableNotFound {
2938 name: alloc::string::String::from(table),
2939 })
2940 })?;
2941 let schema_cols = &table_ref.schema().columns;
2942 match columns {
2943 None => Ok(schema_cols.iter().map(|c| c.name.clone()).collect()),
2944 Some(cols) => {
2945 for (i, name) in cols.iter().enumerate() {
2946 if !schema_cols
2947 .iter()
2948 .any(|c| c.name.eq_ignore_ascii_case(name))
2949 {
2950 return Err(EngineError::Unsupported(alloc::format!(
2951 "column \"{name}\" of relation \"{table}\" does not exist"
2952 )));
2953 }
2954 if cols[..i].iter().any(|p| p.eq_ignore_ascii_case(name)) {
2955 return Err(EngineError::Unsupported(alloc::format!(
2956 "column \"{name}\" specified more than once"
2957 )));
2958 }
2959 }
2960 Ok(cols.to_vec())
2961 }
2962 }
2963 }
2964
2965 /// v7.39 (round 249) — execute a parsed `COPY … FROM '<file>'` whose
2966 /// file contents the HOST has already read (the engine is no_std and
2967 /// performs no I/O). Lowers to per-row INSERTs via
2968 /// [`crate::copy::copy_buffer_inserts`]; outside an explicit
2969 /// transaction the rows are wrapped in one, so a bad row aborts the
2970 /// whole COPY exactly as in PG.
2971 ///
2972 /// # Errors
2973 /// The failing row's INSERT error propagates (after rollback).
2974 pub fn copy_from_buffer(
2975 &mut self,
2976 table: &str,
2977 columns: Option<&[alloc::string::String]>,
2978 options: &spg_sql::ast::CopyOptions,
2979 data: &str,
2980 ) -> Result<QueryResult, EngineError> {
2981 let target = self.copy_target_columns(table, columns)?;
2982 let inserts = crate::copy::copy_buffer_inserts(table, columns, &target, options, data)?;
2983 let wrap = !self.in_transaction();
2984 if wrap {
2985 self.execute("BEGIN")?;
2986 }
2987 let mut affected: usize = 0;
2988 for insert in &inserts {
2989 match self.execute(insert) {
2990 Ok(QueryResult::CommandOk { affected: n, .. }) => affected += n,
2991 Ok(_) => affected += 1,
2992 Err(e) => {
2993 if wrap {
2994 let _ = self.execute("ROLLBACK");
2995 }
2996 return Err(e);
2997 }
2998 }
2999 }
3000 if wrap {
3001 self.execute("COMMIT")?;
3002 }
3003 Ok(QueryResult::CommandOk {
3004 affected,
3005 modified_catalog: false,
3006 })
3007 }
3008
3009 /// v7.39 (round 252) — render a `COPY … TO '<file>'` payload for the
3010 /// HOST to write (the engine is no_std and performs no I/O). Returns
3011 /// the encoded bytes (one line per record, trailing newline) and the
3012 /// DATA row count for the `COPY n` tag — the HEADER line, when
3013 /// present, is part of the payload but not of the count.
3014 ///
3015 /// # Errors
3016 /// Same surface as `COPY … TO STDOUT` (missing relation / column,
3017 /// CSV-mode option refusals).
3018 pub fn copy_to_buffer(
3019 &mut self,
3020 table: &str,
3021 columns: Option<&[alloc::string::String]>,
3022 query: Option<&Statement>,
3023 options: &spg_sql::ast::CopyOptions,
3024 ) -> Result<(alloc::string::String, usize), EngineError> {
3025 let result = self.exec_copy_to(table, columns, query, options, CancelToken::none())?;
3026 let QueryResult::Rows { rows, .. } = result else {
3027 return Err(EngineError::Unsupported(
3028 "COPY TO rendered a non-row result".into(),
3029 ));
3030 };
3031 let mut payload = alloc::string::String::new();
3032 for row in &rows {
3033 if let Some(Value::Text(line)) = row.values.first() {
3034 payload.push_str(line);
3035 }
3036 payload.push('\n');
3037 }
3038 let data_rows = rows.len().saturating_sub(usize::from(options.header));
3039 Ok((payload, data_rows))
3040 }
3041
3042 /// `COPY table [(cols)] TO STDOUT` — render the visible rows
3043 /// in COPY text format (tab-separated, `\N` nulls, backslash
3044 /// escapes) as a single-text-column result set. Embedded
3045 /// consumers read the lines directly; the wire layer streams
3046 /// CopyData frames from them.
3047 fn exec_copy_to(
3048 &mut self,
3049 table_name: &str,
3050 columns: Option<&[String]>,
3051 query: Option<&Statement>,
3052 options: &spg_sql::ast::CopyOptions,
3053 cancel: CancelToken<'_>,
3054 ) -> Result<QueryResult, EngineError> {
3055 use spg_sql::ast::CopyFormat;
3056 // v7.39 (read01 round 94) — `COPY (<query>) TO STDOUT`: run the inner
3057 // statement and render its result set with the same per-format cell
3058 // encoder the table form uses. Kept as an early branch so the
3059 // battle-tested table path below is untouched.
3060 if let Some(q) = query {
3061 return self.exec_copy_to_query(q, options, cancel);
3062 }
3063 let table = self.active_catalog().get(table_name).ok_or_else(|| {
3064 EngineError::Storage(spg_storage::StorageError::TableNotFound {
3065 name: alloc::string::String::from(table_name),
3066 })
3067 })?;
3068 let schema_cols = table.schema().columns.clone();
3069 let positions: alloc::vec::Vec<usize> = match columns {
3070 Some(cols) => cols
3071 .iter()
3072 .map(|c| {
3073 schema_cols
3074 .iter()
3075 .position(|s| s.name.eq_ignore_ascii_case(c))
3076 .ok_or_else(|| {
3077 EngineError::Eval(crate::eval::EvalError::ColumnNotFound {
3078 name: c.clone(),
3079 })
3080 })
3081 })
3082 .collect::<Result<_, _>>()?,
3083 None => (0..schema_cols.len()).collect(),
3084 };
3085 // Per-format defaults: text = tab / `\N`; csv = comma / `` / `"`.
3086 let is_csv = options.format == CopyFormat::Csv;
3087 let delimiter = options.delimiter.unwrap_or(if is_csv { ',' } else { '\t' });
3088 let quote = options.quote.unwrap_or('"');
3089 let null_str = options
3090 .null_str
3091 .clone()
3092 .unwrap_or_else(|| alloc::string::String::from(if is_csv { "" } else { "\\N" }));
3093 // v7.39 (round 247) — the FORCE_QUOTE mask follows the emitted
3094 // column order (the projection), not the table order.
3095 let out_names: alloc::vec::Vec<alloc::string::String> = positions
3096 .iter()
3097 .filter_map(|&p| schema_cols.get(p).map(|c| c.name.clone()))
3098 .collect();
3099 let (escape, force_mask) =
3100 Self::resolve_copy_csv_extras(options, is_csv, quote, &out_names)?;
3101 let encode_cells = |cells: &[Option<alloc::string::String>]| -> alloc::string::String {
3102 if is_csv {
3103 crate::copy::encode_copy_csv_cells_opts(
3104 cells,
3105 delimiter,
3106 quote,
3107 escape,
3108 force_mask.as_deref(),
3109 &null_str,
3110 )
3111 } else {
3112 crate::copy::encode_copy_text_cells_opts(cells, delimiter, &null_str)
3113 }
3114 };
3115 let snap = self.current_snapshot();
3116 let mut out_rows: alloc::vec::Vec<spg_storage::Row<'static>> = alloc::vec::Vec::new();
3117 // HEADER: the selected column names as the first line, encoded
3118 // per the same format rules (a name is never NULL).
3119 if options.header {
3120 let names: alloc::vec::Vec<Option<alloc::string::String>> = positions
3121 .iter()
3122 .map(|&p| Some(schema_cols[p].name.clone()))
3123 .collect();
3124 out_rows.push(spg_storage::Row::new(alloc::vec![Value::text(
3125 encode_cells(&names)
3126 )]));
3127 }
3128 // COPY renders each value with its type's output function, the
3129 // same as the wire — notably bool as `t` / `f`, not the engine's
3130 // debug-ish `true` / `false`.
3131 // v7.38 (T-tstz Phase 1) — `ty` is the column's declared type, needed
3132 // only to tell timestamptz from timestamp: PG's COPY renders the former
3133 // with its offset. Everything else renders identically either way.
3134 let cell_text = |v: &Value, ty: spg_storage::DataType| -> Option<alloc::string::String> {
3135 match v {
3136 Value::Null => None,
3137 Value::Bool(b) => Some(alloc::string::String::from(if *b { "t" } else { "f" })),
3138 Value::Timestamp(t) if matches!(ty, spg_storage::DataType::Timestamptz) => {
3139 Some(crate::eval::format_timestamptz(*t))
3140 }
3141 other => Some(crate::eval::values::value_to_text(other)),
3142 }
3143 };
3144 let encode = |row: &spg_storage::Row<'static>| {
3145 let cells: alloc::vec::Vec<Option<alloc::string::String>> = positions
3146 .iter()
3147 .map(|&p| {
3148 row.values
3149 .get(p)
3150 .and_then(|v| cell_text(v, schema_cols[p].ty))
3151 })
3152 .collect();
3153 encode_cells(&cells)
3154 };
3155 for (_, row) in table.scan_visible(&snap) {
3156 cancel.check()?;
3157 out_rows.push(spg_storage::Row::new(alloc::vec![Value::text(encode(row))]));
3158 }
3159 for row in self.iter_cold_rows_of_table(table) {
3160 cancel.check()?;
3161 out_rows.push(spg_storage::Row::new(alloc::vec![Value::text(encode(
3162 &row
3163 ))]));
3164 }
3165 Ok(QueryResult::Rows {
3166 columns: alloc::vec![spg_storage::ColumnSchema::new(
3167 alloc::string::String::from("copy"),
3168 spg_storage::DataType::Text,
3169 false,
3170 )],
3171 rows: out_rows,
3172 })
3173 }
3174
3175 /// v7.39 (read01 round 94) — the `COPY (<query>) TO STDOUT` renderer.
3176 /// Executes the inner statement and encodes its result set into a single
3177 /// `copy` text column (one row per COPY line, header first when asked),
3178 /// exactly like the table form's tail — the difference is only where the
3179 /// rows and their column types come from.
3180 fn exec_copy_to_query(
3181 &mut self,
3182 query: &Statement,
3183 options: &spg_sql::ast::CopyOptions,
3184 cancel: CancelToken<'_>,
3185 ) -> Result<QueryResult, EngineError> {
3186 use spg_sql::ast::CopyFormat;
3187 let (result_cols, result_rows) = match self.dispatch_stmt_inner(query.clone(), cancel)? {
3188 QueryResult::Rows { columns, rows } => (columns, rows),
3189 _ => {
3190 return Err(EngineError::Unsupported(
3191 "COPY (query) source did not produce a result set".into(),
3192 ));
3193 }
3194 };
3195 let is_csv = options.format == CopyFormat::Csv;
3196 let delimiter = options.delimiter.unwrap_or(if is_csv { ',' } else { '\t' });
3197 let quote = options.quote.unwrap_or('"');
3198 let null_str = options
3199 .null_str
3200 .clone()
3201 .unwrap_or_else(|| alloc::string::String::from(if is_csv { "" } else { "\\N" }));
3202 let out_names: alloc::vec::Vec<alloc::string::String> =
3203 result_cols.iter().map(|c| c.name.clone()).collect();
3204 let (escape, force_mask) =
3205 Self::resolve_copy_csv_extras(options, is_csv, quote, &out_names)?;
3206 let encode_cells = |cells: &[Option<alloc::string::String>]| -> alloc::string::String {
3207 if is_csv {
3208 crate::copy::encode_copy_csv_cells_opts(
3209 cells,
3210 delimiter,
3211 quote,
3212 escape,
3213 force_mask.as_deref(),
3214 &null_str,
3215 )
3216 } else {
3217 crate::copy::encode_copy_text_cells_opts(cells, delimiter, &null_str)
3218 }
3219 };
3220 let cell_text = |v: &Value, ty: spg_storage::DataType| -> Option<alloc::string::String> {
3221 match v {
3222 Value::Null => None,
3223 Value::Bool(b) => Some(alloc::string::String::from(if *b { "t" } else { "f" })),
3224 Value::Timestamp(t) if matches!(ty, spg_storage::DataType::Timestamptz) => {
3225 Some(crate::eval::format_timestamptz(*t))
3226 }
3227 other => Some(crate::eval::values::value_to_text(other)),
3228 }
3229 };
3230 let mut out_rows: alloc::vec::Vec<spg_storage::Row<'static>> = alloc::vec::Vec::new();
3231 if options.header {
3232 let names: alloc::vec::Vec<Option<alloc::string::String>> =
3233 result_cols.iter().map(|c| Some(c.name.clone())).collect();
3234 out_rows.push(spg_storage::Row::new(alloc::vec![Value::text(
3235 encode_cells(&names)
3236 )]));
3237 }
3238 for row in &result_rows {
3239 cancel.check()?;
3240 let cells: alloc::vec::Vec<Option<alloc::string::String>> = result_cols
3241 .iter()
3242 .enumerate()
3243 .map(|(p, c)| row.values.get(p).and_then(|v| cell_text(v, c.ty)))
3244 .collect();
3245 out_rows.push(spg_storage::Row::new(alloc::vec![Value::text(
3246 encode_cells(&cells)
3247 )]));
3248 }
3249 Ok(QueryResult::Rows {
3250 columns: alloc::vec![spg_storage::ColumnSchema::new(
3251 alloc::string::String::from("copy"),
3252 spg_storage::DataType::Text,
3253 false,
3254 )],
3255 rows: out_rows,
3256 })
3257 }
3258}
3259
3260impl Engine {
3261 /// PG's `PreventInTransactionBlock`: statements whose effect no
3262 /// rollback can undo are refused inside an explicit transaction with
3263 /// 25001, naming themselves in the message.
3264 ///
3265 /// The witness is THIS connection's slot, not the global
3266 /// `in_transaction()`: the engine is shared, so a global check would
3267 /// refuse an autocommit VACUUM merely because a different connection
3268 /// had a transaction open. Same predicate `DISCARD ALL` already uses.
3269 /// Whether a catalog change this statement made is already committed,
3270 /// i.e. THIS connection is not inside an explicit transaction block.
3271 ///
3272 /// It rides out on `QueryResult::modified_catalog`, and the server
3273 /// takes it as "persist and audit this now": in no-WAL mode it drives
3274 /// the snapshot write, and it gates the audit append in every mode.
3275 ///
3276 /// The witness has to be this connection's slot. Asking the
3277 /// engine-wide `in_transaction()` — true while ANY connection holds a
3278 /// transaction — reported an autocommit DDL as uncommitted, and both
3279 /// consequences were measured in round 795: the statement was missing
3280 /// from the audit log entirely, and after `kill -9` plus a restart the
3281 /// table it created was gone, having been acked to the client. A
3282 /// second connection idling inside a BEGIN was the whole cause.
3283 pub(crate) fn catalog_change_is_committed(&self) -> bool {
3284 !self.current_tx.is_some_and(|tx| self.is_tx_open(tx))
3285 }
3286
3287 pub(crate) fn require_no_transaction_block(&self, what: &str) -> Result<(), EngineError> {
3288 if self.current_tx.is_some_and(|tx| self.is_tx_open(tx)) {
3289 return Err(EngineError::Unsupported(alloc::format!(
3290 "{what} cannot run inside a transaction block"
3291 )));
3292 }
3293 Ok(())
3294 }
3295}