Skip to content

Commit 092ba64

Browse files
authored
to 3.0: fix: force CTAS follow-up insert to run on one CN (#23749)
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue ##23744 ## What this PR does / why we need it: For CTAS, table creation and data population now happen in the same transaction. the newly created target table (including hidden auto-increment/fake-pk metadata), causing pre-insert auto-increment lookup to fail as no such table. ## Fix Before executing the CTAS follow-up internal SQL (createAsSelectSql), mark the current transaction as having DDL: - pkg/sql/compile/ddl.go (normal CTAS path) - pkg/sql/compile/ddl.go (temp table CTAS path) This makes the internal SQL compile path choose one-CN execution, so the follow-up insert runs on the coordinator CN and can access uncommitted metadata in the same transaction. ## Why This Is Safe - Scope is limited to CTAS internal follow-up SQL only. - No user-visible SQL semantics are changed. - This is a correctness-first mitigation; only CTAS internal insert distribution is constrained. ## Impact - Fixes CTAS failures in multi-CN for same-transaction create+insert scenarios. - Potentially reduces parallelism for CTAS follow-up insert (expected and acceptable for correctness).
1 parent e9eeb34 commit 092ba64

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

pkg/sql/compile/ddl.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,9 @@ func (s *Scope) CreateTable(c *Compile) error {
14971497
}
14981498

14991499
if createAsSelectSql := qry.GetCreateAsSelectSql(); createAsSelectSql != "" {
1500+
// Mark current txn as DDL before compiling CTAS follow-up INSERT ... SELECT,
1501+
// so internal SQL stays on one CN and can see uncommitted table metadata.
1502+
c.setHaveDDL(true)
15001503
res, err := func() (executor.Result, error) {
15011504
oldCtx := c.proc.Ctx
15021505
// Force privilege checking for CTAS follow-up INSERT ... SELECT.
@@ -1761,6 +1764,9 @@ func (s *Scope) CreateTempTable(c *Compile) error {
17611764
}
17621765

17631766
if createAsSelectSql := qry.GetCreateAsSelectSql(); createAsSelectSql != "" {
1767+
// Mark current txn as DDL before compiling CTAS follow-up INSERT ... SELECT,
1768+
// so internal SQL stays on one CN and can see uncommitted table metadata.
1769+
c.setHaveDDL(true)
17641770
res, err := func() (executor.Result, error) {
17651771
oldCtx := c.proc.Ctx
17661772
// Force privilege checking for CTAS follow-up INSERT ... SELECT.

0 commit comments

Comments
 (0)