planner, executor, sessionctx: reject explicit dml on mview / mlog tables#3
Merged
windtalker merged 1 commit intowindtalker:mv_testfrom Feb 28, 2026
Merged
Conversation
Materialized views and their $mlog$ tables are internal implementation details. Users should not be able to write them directly; only MV maintenance (REFRESH/PURGE) and base-table DML side-effects should touch MV/MV log data. This change introduces a planner-layer guard (CheckMViewUpdatable) as the single authoritative check that rejects explicit DML (INSERT, REPLACE, UPDATE, DELETE, LOAD DATA, IMPORT INTO) targeting MV or MV log tables. The executor build path contains defense-in-depth assertions (intest.AssertNoError) that verify the planner guard in test builds. Bypass rules: - Writes to MV/MV log tables are only allowed when both InMaterializedViewMaintenance and InRestrictedSQL are set, i.e. from internal sessions used by maintenance operations (REFRESH/PURGE) - Base-table DML with mlog side-effects works normally Uses existing ErrNonUpdatableTable for consistency with views and sequences. Scope is intentionally DML-focused (does not gate TRUNCATE/DDL yet).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: ref pingcap#18023
Problem Summary:
Materialized views and their aux tables (
$mlog$...) are internal implementation details, but users can currently write them explicitly, which leaks internal mechanisms and can break MV/MV log invariants. We should reject explicit writes and only allow MV maintenance (REFRESH/PURGE) and base-table DML side-effects to touch MV/MV log data.What changed and how does it work?
Core mechanism
Introduce a
MViewInternalDMLenum onSessionVars/StatementContextto distinguish user-issued DML from internal maintenance operations. The functionCheckMViewUpdatable()checks the target table's metadata and rejects the operation unless the appropriate context flag is set.Planner-layer guard (authoritative)
The planner is the single authoritative guard that rejects DML on MV/mlog tables. It covers all DML paths:
planbuilder.go)logical_plan_builder.go)logical_plan_builder.go)planbuilder.go)planbuilder.go)This catches errors early, including at PREPARE time.
Executor-layer assertions (defense-in-depth)
The executor build phase contains
intest.AssertNoErrorassertions that verify the planner already rejected invalid operations. These are no-ops in production builds and only fire in test builds (via theintestbuild tag), catching developer bugs where a code path bypasses the planner guard.Covered operations: INSERT/REPLACE, UPDATE, DELETE, LOAD DATA, IMPORT INTO (
builder.go).Bypass rules
IsMViewRefreshContext()IsMViewPurgeContext()mlogTablewrapper)INSERT INTO base_table ...Error handling
Uses existing
ErrNonUpdatableTablefor consistency with views and sequences (no new error codes).Tradeoffs / limitations
Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.