Repository navigation
Description
Description
For a table column declared DEFAULT nextval('s'), an INSERT INTO owner_db.t issued while a different database is current resolves s in the caller database, not the database that owns t. If the caller database has no s, a valid insert fails. If it has a same-named sequence, the insert silently consumes that unrelated sequence and writes its value into the target table.
Environment
- Branch:
main - Commit:
48dbe277b6d20216c1c513d03c5cbd681380a8a2 - Deployment: isolated local MatrixOne, two CNs (
127.0.0.1:16041,:16042);git_version() = 48dbe277b6 - Date: 2026-10-16
Steps to reproduce
CREATE DATABASE seq_owner;
USE seq_owner;
CREATE SEQUENCE s AS BIGINT INCREMENT BY 1 START WITH 1 NO CYCLE;
CREATE TABLE t(id BIGINT PRIMARY KEY DEFAULT nextval('s'), v INT);
INSERT INTO t(v) VALUES (10); -- id 1, owner sequence 1
CREATE DATABASE seq_caller;
USE seq_caller;
INSERT INTO seq_owner.t(v) VALUES (20);The final statement returns ERROR 1146 (42S02): no such table seq_caller.s. The target table and its sequence both exist, and USE seq_owner; INSERT INTO t(v) VALUES (20) works normally.
For the silent wrong-value variant, create a same-named sequence in seq_caller before the qualified insert:
USE seq_caller;
CREATE SEQUENCE s AS BIGINT INCREMENT BY 1 START WITH 100 NO CYCLE;
INSERT INTO seq_owner.t(v) VALUES (25);
SELECT id,v FROM seq_owner.t ORDER BY id;
SELECT last_seq_num,is_called FROM seq_caller.s;
SELECT last_seq_num,is_called FROM seq_owner.s;Actual behavior
The insert succeeds with row (100,25) in seq_owner.t. seq_caller.s advances to 100 while seq_owner.s remains at 1. Thus a successful write uses and mutates an unrelated database's sequence.
Expected behavior
An unqualified sequence reference stored in a table's default expression should resolve in the table owner's database when the default is evaluated, regardless of the caller's current database. The target sequence should advance and the caller's unrelated sequence should remain unchanged. This follows the documented table-sequence use case: MatrixOne CREATE SEQUENCE documentation.
Stability and controls
- The missing-caller-sequence failure reproduced 3/3 in independent freshly created owner databases (
hb_seq_qual_20261016,hb_seq_qual2_20261016,hb_seq_qual3_20261016) from both CNs; each reported the caller database in the 1146 error. - In each setup, an ordinary same-database default insert succeeded before the cross-database probe. In the first and third, a same-database insert after the failed probe also succeeded, advanced only the owner sequence, and showed the failed probe left no partial row or sequence change.
- The same-named caller sequence variant produced an incorrect but successful value: target rows
(1,10),(100,25), caller sequence 100, owner sequence 1.
Evidence
The complete reproducer, observed rows, error text, and source commit are recorded in /private/tmp/mo-explore-run-20261016/checkpoint.md under the 2026-10-16 Sequence section; the essential output is also quoted above.
Code analysis
pkg/sql/plan/function/seqfunc.go:215 resolves the database from proc.GetSessionInfo().Database unless an internal database argument is present. pkg/sql/plan/base_binder.go:4575-4603 adds that internal argument for view expansion (b.ctx.defaultDatabase), which fixed the related view issue #27581. The table-default path still executes the one-argument NEXTVAL expression and therefore falls back to the caller session database. This is a focused mechanism hypothesis supported by the SQL results; it is not a general claim that all defaults bind incorrectly.
Regression coverage
Add a cross-database table-default test with a qualified target: missing same-name caller sequence (must still succeed), same-name caller sequence (must not consume it), and same-database control. Include a second CN and verify exact sequence states and failure atomicity. No repository test case or PR was added during this exploration.
Related
- [Bug]: Qualified sequence-backed view resolves NEXTVAL in the session database #27581 fixed unqualified
NEXTVALin a qualified view; it is closed. This issue is a different table-default/INSERT binding path still reproducible on the pinned main. - [Bug]: SHOW CREATE TABLE and CLONE fail for tables with DEFAULT nextval(...) #27101 fixed quoted default-expression DDL reconstruction; this is not a DDL parsing failure.
Activity
Ariznawlll commentedon Oct 16, 2026
补充同根边界:SQL
PREPARE/EXECUTE写入跨库的 sequence 默认值表,也会取当前会话库的同名序列,而不是目标表所属库的序列。该现象不是已关闭的 #27581 视图路径。测试版本:
main,48dbe277b6d20216c1c513d03c5cbd681380a8a2;隔离双 CN(16041/16042);2026-10-16。本地实际对象名分别为
hb_seq_qual3_20261016.t/s和hb_seq_qual_caller_20261016.s。三个独立连接分别PREPARE/EXECUTE1 次,结果均成功但新行 ID 依次为101,102,103;caller 序列依次推进到101,102,103,owner 序列始终为2。第三轮完整表行是(1,10),(2,30),(101,77),(102,78),(103,79)。使用目标库作为当前库的普通插入对照正常生成 ID 2。因此问题不限于直接执行的
INSERT,也覆盖 SQL prepared 路径;建议同一修复和回归用例涵盖两者。此处没有新增 issue 或仓库测试文件。Ariznawlll commentedon Oct 16, 2026
再补充二进制协议 prepared statement 路径(与上一条 SQL
PREPARE/EXECUTE不同),仍归入本 issue,不另提单。main/48dbe277b6d20216c1c513d03c5cbd681380a8a2,本地隔离双 CN,2026-10-16。libmysqlclient的mysql_stmt_prepare、mysql_stmt_bind_param、mysql_stmt_execute,真实走二进制协议。当前库为 owner 时,对限定表名INSERT INTO owner.t(v) VALUES(?)prepare,正常生成 ID 1;ALTER SEQUENCE owner.s INCREMENT BY 3后复用同一 statement,正常生成 ID 4。无效 ALTER(MINVALUE 10、MAXVALUE 1)被拒绝,statement 仍可正常执行生成 ID 7。s、起始 100 的序列),复用 statement 插入,成功写入 owner 表的 ID 100。两个独立的新连接再次从 owner 库 prepare、切换 caller 库后执行,写入 ID 101、102。该跨库二进制协议变体复现 3/3;owner.s 始终停在 7,caller.s 依次变为 100、101、102。(1,10),(4,20),(7,30),(100,40),(101,50),(102,50)。这表明即使目标表名已限定且语句在 owner 库 prepare,序列默认值仍在执行时取当前会话库。正常对照(始终保持 owner 为当前库)、失败 ALTER 后继续执行都成功;没有连接失步。复现客户端源码和原始输出保存在本地隔离运行目录
/private/tmp/mo-explore-run-20261016/hb_sequence_binary_prepare.c与checkpoint.md;关键步骤和精确结果已列在上面。建议修复时同时覆盖直接 INSERT、SQL PREPARE 和二进制协议 prepared statement。未新增仓库测试或 PR。Ariznawlll commentedon Oct 16, 2026
补充同根边界:当前会话未选择默认数据库时,表名和来源表名都已完整限定的
INSERT ... SELECT,若目标表的主键为DEFAULT nextval('s'),会报ExpectedEOB。这仍属于本 issue 的默认值序列错误绑定,不是通用INSERT ... SELECT语法问题。测试版本:
main/48dbe277b6d20216c1c513d03c5cbd681380a8a2,隔离双 CN(16041/16042),2026-10-16。三个独立新库分别在 CN1/CN2/CN1 复现,均为 3/3。实际结果:
EXPLAIN正常生成Multi Update -> DEDUP -> Lock -> Project -> Table Scan,但真正 INSERT 返回ERROR at line 1: ExpectedEOB;目标表仍是 0 行,序列仍为(last_seq_num=1,is_called=0)。三轮结果一致。选择 owner 数据库后,INSERT INTO dst(v) SELECT v FROM src全部成功,得到(1,10),(2,20),序列变成(2,1)。排除通用语法/环境问题:另在三个独立新库中,以无默认数据库会话执行相同的完全限定
INSERT INTO db.dst(v) SELECT v FROM db.src,但目标表没有序列默认值,MO 与 MySQL 8.0.45 均 3/3 成功,目标count=2,sum(v)=30。MO 同连接的完全限定INSERT ... VALUES和SELECT也成功。由此可见,失败由默认NEXTVAL的数据库解析触发;请在本 issue 的修复与回归中包含无当前数据库的INSERT ... SELECT。完整输出已保存在隔离运行目录的checkpoint.md,没有新增 issue、仓库测试或 PR。XuPeng-SH commentedon Oct 22, 2026
This report is now tracked together with #30359. 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.
fix(sql): keep sequence ownership and state consistent across DML, DD…
fix(sql): keep sequence ownership and state consistent across DML, DD…
fix(sql): keep sequence ownership and state consistent across DML, DD…
fix(sql): keep sequence ownership and state consistent across DML, DD…