Repository navigation
Description
Branch Name
main
Commit ID
Other Environment Information
- Hardware parameters: Apple M2 (arm64), 16 GB
- OS type: macOS 15 (darwin)
- Others: standalone
mo-servicebuilt at commit 429c277 (launch config: copy ofetc/launchwith every port offset by +20000 via LOOM_TEST_PORT_OFFSET, mysql protocol listen 26001, data dir under.loom-test/); client pymysql / [email protected] CLI with--default-character-set=utf8mb4; MySQL 8.0 reference instance for comparison
Actual Behavior
The orphan temporary-table GC (MOServer.cleanOrphanTempTables, pkg/frontend/server.go:300-352) reclaims mo_catalog.mo_tables rows with relkind='temporary_table' left behind by dead sessions (candidates come from queryTempTables at server.go:359-381 minus currently active sessions). It builds its cleanup statement with a raw backtick fmt.Sprintf that performs no identifier escaping:
// pkg/frontend/server.go:343
dropSQL := fmt.Sprintf("drop table if exists `%s`.`%s`", dbName, name)
res, err := iscp.ExecWithResult(ctx, dropSQL, mo.service, nil)
if err != nil {
logutil.Warnf("temp-table-cleanup: drop %s.%s failed: %v", dbName, name, err) // :346
continue // :347
}MO accepts MySQL backtick-escaped table aliases, and a temporary table's physical catalog row name embeds the user-supplied alias verbatim (__mo_tmp_<session>_<db>_<generation>_<alias>). Verified live on this build (single session):
create temporary table `a``b` (id int primary key); -- accepted
insert into `a``b` values (7); -- works
select relname, relkind from mo_catalog.mo_tables where relkind = 'temporary_table';
-- -> __mo_tmp_01a14ba3235e7b3c88c20a4e25db2507_loom_c17_62072c62-704d-4492-bf9e-4bd5014f420b_a`b (relkind='temporary_table')so a legal alias containing a backtick yields a catalog row whose physical name contains a raw backtick (a plain-alias control row does not). When such a table's owning session dies without cleanup (CN crash/restart - exactly the event this GC exists for), the GC formats
drop table if exists `loom_c17`.`__mo_tmp_..._a`b`
The backtick inside the name closes the identifier early, so the statement cannot be parsed by MO's own parser (verified in the test below): iscp.ExecWithResult returns a syntax error, the loop warns and continues, and the same failure is retried at every GC tick (interval hardcoded to 24h at server.go:133). The orphan is NEVER reclaimed: a permanent leftover row in mo_catalog plus a recurring temp-table-cleanup: drop ... failed warn, with no user-side recovery path (the owning session is gone; session-level cleanup already had its chance and uses correct quoting).
Injection execution is contained by current code (the malformed statement is rejected by the parser before planning, and the internal executor executes only stmts[0]), so the primary impact is the unreclaimable orphan and the permanently repeating cleanup failure, not executed SQL.
Checked on this build: database names containing a backtick are rejected at creation (create database db -> (20101, 'internal error: invalid input')), so the live trigger channel is the table-alias one; the fix should still quote both parts for safety.
Expected Behavior
The sibling in-session cleanup path already does it correctly, and the repo mandates this helper for exactly this reason (pkg/common/sqlquote is the repo's helper for interpolated identifiers; a raw backtick-wrapped Sprintf breaks the moment a name contains a backtick):
// pkg/frontend/session.go:2150 (dropSessionTempTables)
dropSQL := "DROP TABLE IF EXISTS " + sqlquote.QualifiedIdent(tbl.dbName, tbl.realName)The GC must build its statement the same way. With the sqlquote.QualifiedIdent construction the identical identifiers parse cleanly and resolve to exactly the intended physical row (verified by the test below), so the orphan would actually be reclaimed.
Steps to Reproduce
-
Create the trigger row live (single session):
use loom_c17; create temporary tableab` (id int primary key); insert into `abvalues (7); select relname, relkind from mo_catalog.mo_tables where relkind='temporary_table';- the create is accepted and the physical row embeds the raw backtick (transcript in Evidence, also proves insert/select work and shows a plain-alias control row). -
Deterministic proof of the GC failure (full test source in the repro; run with
go test -run 'TestLoomTempOrphanGcDropSqlBreaksOnBackticks' ./pkg/frontend/): the test formatsserver.go:343's exact dropSQL for this live-observed physical row and parses it with MO's own parser -> syntax error (orphan unreclaimable at every tick), while thesession.go:2150sqlquote shape parses and resolves exactly the samedb.tabletarget. -
Reachability of the orphan state: after the owning session dies without cleanup (e.g. CN process crash/restart before the session drop path runs), this row enters the GC candidate set on the next 24h tick and the failure loop begins; nothing ever removes the row.
Additional information
- One-line fix:
dropSQL := "drop table if exists " + sqlquote.QualifiedIdent(dbName, name)(mirror of server.go:343 -> session.go:2150). - Log signature per tick:
temp-table-cleanup: drop loom_c17.__mo_tmp_..._ab failed: You have an error in your SQL syntax; ... syntax error at line 1 column 124 near "b". - Scope (not duplicates): [Bug]: another same-account session can read, modify, or drop a temporary table via its physical name #30495 covers cross-session access THROUGH the physical name; [Bug]: Temporary-table orphan GC must preserve live sessions and reclaim safely quoted orphans #30496 covers multi-CN GC visibility claiming live tables. Neither is about the GC's own SQL construction; this report is solely that the cleanup statement is unbuildable/misparsed when the identifier contains a backtick.
- Duplicate check (matrixorigin/matrixone issues, open and closed): 'cleanOrphanTempTables', 'temp-table-cleanup', 'temporary table cleanup backtick' and 'sqlquote' -> no matching issues; 'orphan temporary table' -> GC-visibility/coverage discussions only. Previously accepted module findings [Bug]: another same-account session can read, modify, or drop a temporary table via its physical name #30495 (cross-session physical-name access), [Bug]: Temporary-table orphan GC must preserve live sessions and reclaim safely quoted orphans #30496 (multi-CN GC claiming live tables), [Bug]: temporary_table_clone.sql comment misstates the ROLLBACK boundary of CLONE-created temp tables #30497 (clone BVT comment) were each checked and have different root causes; no open PR touches the GC's cleanup-statement construction.
Full reproduction, exactly as Loom ran it
// Copyright 2026 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package frontend
// Minimal standalone repro for the bug report "orphan temp-table GC drop SQL
// breaks on backticks; orphan rows are never reclaimed".
//
// cleanOrphanTempTables (pkg/frontend/server.go) reclaims temporary-table
// rows left by dead sessions. It builds its cleanup statement with a raw
// backtick fmt.Sprintf (server.go:343):
//
// dropSQL := fmt.Sprintf("drop table if exists `%s`.`%s`", dbName, name)
//
// while the in-session cleanup uses sqlquote.QualifiedIdent
// (pkg/frontend/session.go:2150). MO accepts MySQL backtick-escaped table
// aliases, and a temporary table's physical row name embeds the alias
// verbatim. The row below was observed live on this build (single session):
//
// create temporary table `a``b` (id int primary key); -- accepted
// select relname from mo_catalog.mo_tables where relkind = 'temporary_table';
// -> __mo_tmp_01a14ba3235e7b3c88c20a4e25db2507_loom_c17_62072c62-704d-4492-bf9e-4bd5014f420b_a`b
//
// So a legal alias containing a backtick yields a catalog row whose
// GC-cleanup SQL cannot parse. The drop is then skipped (warn + continue,
// server.go:346-347) at every GC tick (hardcoded 24h, server.go:133) and
// the orphan is never reclaimed.
//
// (The database-name channel was checked too: `create database `d``b`` is
// rejected on this build, so the table-alias channel is the live one.)
import (
"context"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/matrixorigin/matrixone/pkg/common/sqlquote"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/matrixorigin/matrixone/pkg/sql/parsers"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
)
func loomGcReproParseDrop(t *testing.T, sql string) (string, error) {
t.Helper()
stmts, err := parsers.Parse(context.Background(), dialect.MYSQL, sql, 1)
if err != nil {
return "", err
}
defer func() {
for _, s := range stmts {
s.Free()
}
}()
drop, ok := stmts[0].(*tree.DropTable)
if !ok {
return "", fmt.Errorf("parsing %q produced no DropTable", sql)
}
target := ""
if len(drop.Names) > 0 {
n := drop.Names[0]
if n.ExplicitSchema {
target = string(n.SchemaName) + "."
}
target += string(n.ObjectName)
}
return target, nil
}
func TestLoomTempOrphanGcDropSqlBreaksOnBackticks(t *testing.T) {
// Physical row observed live on this build (see file comment).
sid := uuid.MustParse("01a14ba3-235e-7b3c-88c2-0a4e25db2507")
db := "loom_c17"
physical := defines.GenTempTableName(sid, db, "62072c62-704d-4492-bf9e-4bd5014f420b_a`b")
// Exact construction of pkg/frontend/server.go:343.
gcSQL := fmt.Sprintf("drop table if exists `%s`.`%s`", db, physical)
// The bug: MO's own parser rejects the GC's cleanup SQL.
if _, err := loomGcReproParseDrop(t, gcSQL); err != nil {
t.Logf("BUG: GC SQL cannot parse -> iscp.ExecWithResult fails, cleanOrphanTempTables logs "+
"'temp-table-cleanup: drop %s.%s failed' every GC tick and the orphan is never reclaimed. parse error: %v",
db, physical, err)
} else {
t.Fatalf("GC SQL unexpectedly parsed (bug would be gone): %s", gcSQL)
}
// Expected construction (mirror of pkg/frontend/session.go:2150).
okSQL := "drop table if exists " + sqlquote.QualifiedIdent(db, physical)
target, err := loomGcReproParseDrop(t, okSQL)
if err != nil {
t.Fatalf("sqlquote form must parse: %v", err)
}
if target != db+"."+physical {
t.Fatalf("sqlquote form resolved %q, want exactly %q", target, db+"."+physical)
}
t.Logf("contrast: sqlquote form (session.go:2150 shape) parses and resolves exactly the orphan %q", target)
}
// ------------------------------------------------------------------ Run
// From the repo root, with the repo's CGO setup (my workspace used):
CGO_CFLAGS="-I$PWD/cgo -I$PWD/thirdparties/install/include" CGO_LDFLAGS="-Wl,-rpath,$PWD/cgo -Wl,-rpath,$PWD/thirdparties/install/lib -L$PWD/thirdparties/install/lib -L$PWD/cgo -lmo -lusearch_c -lm" \
go test -count=1 -run 'TestLoomTempOrphanGcDropSqlBreaksOnBackticks' -v ./pkg/frontend/
// PASS with: "BUG: GC SQL cannot parse -> ... orphan is never reclaimed. parse error:
// ... syntax error at line 1 column 124 near "b`"" and the sqlquote contrast resolves
// the exact orphan row.Found by Loom bug hunt (module: custom-5ec72c0c38; tests: unit, white-box, end-to-end)
Activity
XuPeng-SH commentedon Oct 22, 2026
This report is now tracked together with #30496. The canonical issue retains explicit acceptance cases for the behavior described here. This is an administrative duplicate/consolidation closure only; the underlying defect is not claimed fixed. The original description and discussion remain available on this issue.