Skip to content

Commit

Permalink
sql: CREATE TABLE AS in an explicit txn does not flush batched rows
Browse files Browse the repository at this point in the history
Fixes: #81554

Previously, when running a CREATE TABLE AS in explicit
transactions, a single batch inserted all rows. This can
lead to errors running into the KV command size limits. To
address this, this patch will intermittently flush batches
KV, so that a single gigantic batch isn't used.

Release note (bug fix): CREATE TABLE AS in explicit transaction
would fail with an error if the size of the source table exceeded
the raft command size limit.
  • Loading branch information
fqazi committed Jun 15, 2022
1 parent c030b8b commit 42ebed3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ func (n *createTableNode) startExec(params runParams) error {
break
}

// Periodically flush out the batches, so that we don't issue gigantic,
// raft commands.
if ti.currentBatchSize >= ti.maxBatchSize {
if err := tw.flushAndStartNewBatch(params.ctx); err != nil {
return err
}
}

// Populate the buffer.
copy(rowBuffer, n.sourcePlan.Values())

Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/create_as
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,10 @@ query I
SELECT * FROM tab_from_seq
----
2

# Regression test for #81554, where tried to do gigantic batches for CTAS in
# explicit transactions
statement ok
BEGIN;
CREATE TABLE source_tbl_huge AS SELECT 1::CHAR(256) FROM generate_series(1, 500000);
COMMIT;

0 comments on commit 42ebed3

Please sign in to comment.