Skip to content

[Bug]: nextval() cannot address sequences in another database #31452

Open

Description

@Ariznawlll

Description

Sequence functions cannot address a sequence outside the current database, and qualified names are mis-resolved by prefixing the current database again:

CREATE DATABASE seqdb;
CREATE SEQUENCE seqdb.s1 START 100;   -- created fine

USE otherdb;
SELECT nextval('seqdb.s1');
-- ERROR 1146 (42S02): no such table otherdb.seqdb.s1
-- (resolution reshaped the qualified name to <current-db>.<qualified-name-as-singlepart>)

Effectively:

  • nextval('seqdb.s1') from any session whose current database is not seqdb → 1146 with a mangled three-part name (otherdb.seqdb.s1, seqdb.seqdb.s1 etc.). The error message itself reveals the resolver treats the argument as a single identifier.
  • nextval('s1') works — but only for sequences in the current database.
  • NEXT VALUE FOR <qualified> is rejected at parse level entirely (only a single-part name is accepted: syntax error near " seqtest.s1").

Net effect: a sequence is only usable from within its own database — no cross-database nextval, no qualified access, and the failure mode is a misleading "no such table" with a doubled database prefix. MySQL doesn't have sequences (MO follows PostgreSQL-style here), but MO's own docs show nextval('db.seq') qualified usage, and the double-prefix resolution is a resolver defect regardless of the SQL dialect.

Reproduction

CREATE DATABASE seqdb;  CREATE SEQUENCE seqdb.s1 START 100;
CREATE DATABASE otherdb;
-- session A (no default db):   SELECT nextval('seqdb.s1');  -- parse error ExpectedEOB
-- session B (USE seqdb):       SELECT nextval('seqdb.s1');  -- ERROR 1146: no such table seqdb.seqtest.s1-style doubled name
-- session C (USE otherdb):     SELECT nextval('seqdb.s1');  -- ERROR 1146: no such table otherdb.seqdb.s1
-- session C:                   SELECT NEXT VALUE FOR seqdb.s1; -- parser syntax error
-- session C:                   SELECT nextval('s1'); -- ERROR (no sequence in otherdb)

Impact

  • Applications following MO's documented qualified nextval('db.seq') pattern break with confusing doubled-name errors.
  • Schema designs sharing a central sequence across databases (multi-tenant key patterns, ETL watermark sequences) are impossible.
  • The doubled-name error (otherdb.seqdb.s1) makes diagnosis hard — users see a "table" that never existed in their statement.

Expected behavior

nextval('db.seq') (and NEXT VALUE FOR db.seq) should resolve the qualified name correctly from any session context, matching MO's documented usage; until then the resolver should at least not silently prepend the current database to an already-qualified argument.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. Ariznawlll commented on Oct 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    Cross-environment retest found a regression on the 4.2.1 hotfix cloud build that goes beyond this issue's current scope description:

    Same-database, bare-name nextval('sq') also fails on the 4.2.1-hotfix cloud build (works on main):

    CREATE DATABASE mo_seq_test;
    CREATE SEQUENCE mo_seq_test.sqZ;
    -- all three forms below, with the current database = mo_seq_test:
    SELECT NEXTVAL('sqZ');
    Environment Result of the identical operation
    main (Docker 8.0.30-MatrixOne-v4.2.6, image built 2026-10-18) works — returns 100
    MOI cloud 8.0.30-MatrixOne-v4.2.1-hotfix-20261026 ERROR 1146 (HY000): no such table mo_seq_test.sqZ

    So the statement in this issue — "nextval('s1') works — but only for sequences in the current database" — holds on main, but the same-database path is also broken on the 4.2.1-hotfix cloud build (sequence name resolved as a table). This looks like either a fix that was not backported or a regression introduced on the hotfix branch — noting it here for backport triage.

    Additionally (consistent across both environments, within this issue's original scope): with no default database selected, SELECT nextval('db.seq') surfaces a bare ERROR at line 1: ExpectedEOB to the client — the internal OkExpectedEOB error code leaks without an error code / SQLSTATE. Worth normalizing the client-facing error when the resolution is fixed.

  5. Ariznawlll commented on Oct 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    A more severe failure shape of this same resolver bug, reproduced today on main c6683bd2f5 (2026-10-29): in certain states a qualified nextval('db.seq') doesn't even reach the clean 1146 — it leaks the raw internal sentinel to the client:

    CREATE DATABASE srx;
    CREATE SEQUENCE srx.sq START 1 INCREMENT 1 MAXVALUE 1000000;
    CREATE TABLE srx.t(id BIGINT PRIMARY KEY, v INT);
    -- session state: having previously executed INSERT with nextval() in this db (cursor/cache warm)
    SELECT NEXTVAL('srx.sq');
    -- ERROR (0, 'ExpectedEOB')   ← errno 0, SQLSTATE-less, internal snapshot sentinel token

    Observed pattern in one session, in order:

    1. SELECT NEXTVAL('srx.sq') right after the DDL → ERROR (0, 'ExpectedEOB') (raw sentinel, the same shape as previously fixed leaks [Bug]: CREATE TEMPORARY TABLE in a database created in the same transaction leaks raw (0, 'ExpectedEOB') #30506/[Bug]: Temporary-table cleanup emits ExpectedEOB from DROP TABLE IF EXISTS #27463 and my [Bug]: mo_ctl flush bad target leaks errno 0 ExpectedEOB #31663 for mo_ctl flush);
    2. USE srx then retry bare NEXTVAL('sq') → works, sequence advances fine;
    3. INSERT INTO srx.t VALUES (nextval('srx.sq'), 1) → ERROR 1146: no such table srx.srx.sq (the double-prefix mis-resolution already documented in this issue);
    4. INSERT ... nextval('sq') (bare) → OK.

    So the qualified-name path has two user-visible defects — the mangled 1146 name (this issue's main subject) and, on the resolution miss that precedes it, a raw OkExpectedEOB propagation to the client. The ExpectedEOB variant is the more dangerous one for tooling: errno 0 reads as success, and the message is not a real user error.

    Environment: dual-CN launch, reproducible after warm-up sequence usage in the same session; cold sessions mostly give the 1146 shape first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions