From ccb1edc22d89b2025dfe38e9e7b2ca173994dd56 Mon Sep 17 00:00:00 2001 From: Sage Griffin Date: Thu, 13 Aug 2026 12:04:47 -0600 Subject: [PATCH 1/2] Remove uneccessary AST copy We were previously parsing an AST only to immediatley copy it onto a new memory arena and throw the original away. I've added a function on the parser side to allow us to skip this step. We can start using it now --- pgdog/src/frontend/router/parser/cache/ast.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pgdog/src/frontend/router/parser/cache/ast.rs b/pgdog/src/frontend/router/parser/cache/ast.rs index e6308c0a1..9007ae1ca 100644 --- a/pgdog/src/frontend/router/parser/cache/ast.rs +++ b/pgdog/src/frontend/router/parser/cache/ast.rs @@ -77,7 +77,6 @@ impl Ast { search_path: Option<&ParameterValue>, ) -> Result { let now = Instant::now(); - let ast = pg_raw_parse::parse(query.query_without_comment).map_err(Error::Parse)?; // Run the rewrite unconditionally. Even when a shard comment will // route the query to a specific shard, we need to know whether the @@ -94,10 +93,7 @@ impl Ast { }); let mut rewrite_plan = Default::default(); let ast = make::try_owned(|mem| { - // FIXME(sage): We should have a parse function on mem so we don't - // need to parse and then copy the parsed tree just to throw the - // original away - let mut copy = mem.make_unique(&*ast.into_inner()); + let mut copy = mem.parse(query.query_without_comment)?; if let Some(stmt) = copy.as_mut().into_iter().next() { rewrite_plan = rewriter.maybe_rewrite(stmt, mem)?; } From fe8d7c6f09d4a7aa363f48304c359f7165592108 Mon Sep 17 00:00:00 2001 From: Sage Griffin Date: Thu, 13 Aug 2026 12:11:38 -0600 Subject: [PATCH 2/2] It's not a copy it's an ast --- pgdog/src/frontend/router/parser/cache/ast.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pgdog/src/frontend/router/parser/cache/ast.rs b/pgdog/src/frontend/router/parser/cache/ast.rs index 9007ae1ca..471f5f531 100644 --- a/pgdog/src/frontend/router/parser/cache/ast.rs +++ b/pgdog/src/frontend/router/parser/cache/ast.rs @@ -93,11 +93,11 @@ impl Ast { }); let mut rewrite_plan = Default::default(); let ast = make::try_owned(|mem| { - let mut copy = mem.parse(query.query_without_comment)?; - if let Some(stmt) = copy.as_mut().into_iter().next() { + let mut ast = mem.parse(query.query_without_comment)?; + if let Some(stmt) = ast.as_mut().into_iter().next() { rewrite_plan = rewriter.maybe_rewrite(stmt, mem)?; } - Ok::<_, Error>(copy) + Ok::<_, Error>(ast) })?; let elapsed = now.elapsed();