-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: allow the 1PC optimization in some cases in the extended protocol
A previous commit (7e2cbf5) fixed our pgwire implementation so that it does not auto-commit a statement executed in the extended protocol until a Sync message is received. That change also had the undesired effect of disabling the 1PC ("insert fast path") optimization that is critical for write-heavy workloads. With this current commit, the 1PC optimization is allowed again, as long as the statement execution is immediately followed by a Sync message. This still has the correct bugfix semantics, but allows the optimization for the common case of how the extended protocol is used. No release note since this only affects unreleased versions. Release note: None
- Loading branch information
Showing
4 changed files
with
128 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
gosql "database/sql" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/settings/cluster" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestInsertFastPathExtendedProtocol verifies that the 1PC "insert fast path" | ||
// optimization is applied when doing a simple INSERT with a prepared statement. | ||
func TestInsertFastPathExtendedProtocol(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
ctx := context.Background() | ||
|
||
var db *gosql.DB | ||
|
||
params, _ := CreateTestServerParams() | ||
params.Settings = cluster.MakeTestingClusterSettings() | ||
|
||
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{ServerArgs: params}) | ||
defer tc.Stopper().Stop(ctx) | ||
db = tc.ServerConn(0) | ||
_, err := db.Exec(`CREATE TABLE fast_path_test(val int);`) | ||
require.NoError(t, err) | ||
|
||
conn, err := db.Conn(ctx) | ||
require.NoError(t, err) | ||
_, err = conn.ExecContext(ctx, "SET tracing = 'on'") | ||
require.NoError(t, err) | ||
// Use placeholders to force usage of extended protocol. | ||
_, err = conn.ExecContext(ctx, "INSERT INTO fast_path_test VALUES($1)", 1) | ||
require.NoError(t, err) | ||
|
||
fastPathEnabled := false | ||
rows, err := conn.QueryContext(ctx, "SELECT message, operation FROM [SHOW TRACE FOR SESSION]") | ||
require.NoError(t, err) | ||
for rows.Next() { | ||
var msg, operation string | ||
err = rows.Scan(&msg, &operation) | ||
require.NoError(t, err) | ||
if msg == "autocommit enabled" && operation == "batch flow coordinator" { | ||
fastPathEnabled = true | ||
} | ||
} | ||
require.NoError(t, rows.Err()) | ||
require.True(t, fastPathEnabled) | ||
_, err = conn.ExecContext(ctx, "SET tracing = 'off'") | ||
require.NoError(t, err) | ||
err = conn.Close() | ||
require.NoError(t, err) | ||
|
||
// Verify that the insert committed successfully. | ||
var c int | ||
err = db.QueryRow("SELECT count(*) FROM fast_path_test").Scan(&c) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, c, "expected 1 row, got %d", c) | ||
} |