This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable synchronous_commit by default for writer connections
Disabling synchronous_commit for the database connections in the writer pool will improve performance. The tradeoff is that some amount of writes may be lost in the event of a crash. This is likely an "okay" tradeoff for monitoring data. By default, we now disable synchronous_commit at the session level on connections in the writer pool. This does not impact other connections/sessions. A new config option has been added to override this default.
- Loading branch information
Showing
5 changed files
with
146 additions
and
48 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
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,77 @@ | ||
package end_to_end_tests | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/pgxpool" | ||
"github.com/stretchr/testify/require" | ||
"github.com/timescale/promscale/pkg/pgclient" | ||
) | ||
|
||
/// TestWriterSynchronousCommit ensures that the database pool for the writer can be configured with synchronous_commit | ||
/// turned on or off and that the effects are limited to that pool | ||
func TestWriterSynchronousCommit(t *testing.T) { | ||
|
||
// creates a new pool of database connections as would be created for the writer path | ||
createWriterPool := func(connStr string, lockerCalled *bool, synchronousCommit bool) *pgxpool.Pool { | ||
pgConfig, err := pgxpool.ParseConfig(connStr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
pgConfig.MaxConns = 2 | ||
pgConfig.MinConns = 1 | ||
*lockerCalled = false | ||
schemaLocker := func(ctx context.Context, conn *pgx.Conn) error { | ||
*lockerCalled = true | ||
return nil | ||
} | ||
pgclient.SetWriterPoolAfterConnect(pgConfig, schemaLocker, synchronousCommit) | ||
writerPool, err := pgxpool.ConnectConfig(context.Background(), pgConfig) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return writerPool | ||
} | ||
|
||
// gets the value of the synchronous_commit setting from the database | ||
getSynchronousCommit := func(writerPool *pgxpool.Pool) string { | ||
var setting string | ||
err := writerPool.QueryRow(context.Background(), "show synchronous_commit").Scan(&setting) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return setting | ||
} | ||
|
||
withDB(t, "writer_sync_commit", func(db *pgxpool.Pool, t testing.TB) { | ||
var lockerCalled bool // used to ensure that the schema locker function is still called | ||
|
||
// create a writer pool with synchronous_commit turned off | ||
writerPool1 := createWriterPool(db.Config().ConnString(), &lockerCalled, false) | ||
setting := getSynchronousCommit(writerPool1) | ||
require.Equal(t, "off", setting, "expected synchronous_commit to be off but it was %s", setting) | ||
require.True(t, lockerCalled, "schemaLocker function should have been called and wasn't") | ||
|
||
// ensure that setting synchronous_commit to off on the writer pool did not impact the setting | ||
// in other database sessions | ||
setting = getSynchronousCommit(db) | ||
require.Equal(t, "on", setting, "expected synchronous_commit to be on but it was %s", setting) | ||
|
||
// make sure the setting stays off after the first transaction finished | ||
setting = getSynchronousCommit(writerPool1) | ||
require.Equal(t, "off", setting, "expected synchronous_commit to be off but it was %s", setting) | ||
writerPool1.Close() | ||
|
||
// now create a writer pool with synchronous_commit turned on | ||
lockerCalled = false | ||
writerPool2 := createWriterPool(db.Config().ConnString(), &lockerCalled, true) | ||
|
||
// make sure the setting is on and the schema locker function was called | ||
setting = getSynchronousCommit(writerPool2) | ||
require.Equal(t, "on", setting, "expected synchronous_commit to be on but it was %s", setting) | ||
require.True(t, lockerCalled, "schemaLocker function should have been called and wasn't") | ||
writerPool2.Close() | ||
}) | ||
} |