Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves Postgres write performance #852

Merged
merged 8 commits into from Oct 4, 2022
Merged

Improves Postgres write performance #852

merged 8 commits into from Oct 4, 2022

Conversation

jakedt
Copy link
Member

@jakedt jakedt commented Sep 29, 2022

Previously Postgres was limited to 1 concurrent write request globally. With this change we leverage some of the transaction isolation primitives introduced in Postgres 13 to extend the effective repeatable read lifetime beyond the transaction's lifetime. With this we can leverage internal Postgres transaction IDs and snapshots to greatly improve the performance of our MVCC implementation.

Contains a 4-phase migration!

@jakedt jakedt requested review from vroldanbet and a team as code owners September 29, 2022 13:24
@jakedt jakedt marked this pull request as draft September 29, 2022 13:24
@github-actions github-actions bot added area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools) labels Sep 29, 2022
@github-actions github-actions bot added the area/dependencies Affects dependencies label Sep 29, 2022
@jakedt jakedt force-pushed the postgres-perf branch 3 times, most recently from 01194d1 to 55d16d2 Compare September 30, 2022 01:53
@jakedt jakedt marked this pull request as ready for review September 30, 2022 02:03
internal/datastore/postgres/xid8.go Outdated Show resolved Hide resolved
@@ -32,7 +32,7 @@ func RunPostgresForTesting(t testing.TB, bridgeNetworkName string) RunningEngine
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Name: name,
Repository: "postgres",
Tag: "10.20",
Tag: "13.8",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be worth moving this version into a const in the postgres datastore itself

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the min required version (13.0) and the test version (latest 13.x) aren't necessarily the same? I can see postgres advertising its min required version.

if err := dbpool.
QueryRow(initializationContext, "SHOW track_commit_timestamp;").
Scan(&trackTSOn); err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a more descriptive error here if this feature is not enabled, ideally one indicating the exact documentation on how to enable it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to skip this check if watch is disabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The datastore has no idea if the watch server is enabled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should thread that in then?

internal/datastore/postgres/postgres.go Outdated Show resolved Hide resolved
@@ -74,7 +78,7 @@ func init() {
var (
psql = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)

getRevision = psql.Select("MAX(id)").From(tableTransaction)
getRevision = psql.Select("MAX(xid::text::bigint)").From(tableTransaction)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use a const name for the col name? I know we didn't use it before, but maybe we should now?

This comment was marked as resolved.

internal/datastore/postgres/postgres.go Show resolved Hide resolved
vroldanbet
vroldanbet previously approved these changes Oct 3, 2022
Copy link
Contributor

@vroldanbet vroldanbet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a bit to process all the changes commit by commit, but I think this looks pretty solid to me. I think the fundamental question to clarify is what kind of impact these migrations can have in a production PSQL primary, and describing what the the various manual steps OSS users have to take to migrate their PSQL datastores to the this version are.

Great job @jakedt this is good stuff 👏🏻 👏🏻 👏🏻

internal/datastore/postgres/watch.go Show resolved Hide resolved
internal/datastore/postgres/watch.go Outdated Show resolved Hide resolved
internal/datastore/postgres/watch.go Outdated Show resolved Hide resolved
internal/datastore/postgres/README.md Show resolved Hide resolved
internal/datastore/postgres/watch.go Show resolved Hide resolved
internal/datastore/postgres/revisions.go Show resolved Hide resolved
@@ -44,14 +44,14 @@ const (
// %[4] Inverse of GC window (in seconds)
queryValidTransaction = `
SELECT $1 >= (
SELECT MIN(%[1]s) FROM %[2]s WHERE %[3]s >= NOW() - INTERVAL '%[4]f seconds'
SELECT MIN(%[1]s::text::bigint) FROM %[2]s WHERE %[3]s >= NOW() - INTERVAL '%[4]f seconds'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: perhaps we could have turned this type cast into a PSQL function for readability

internal/datastore/postgres/postgres.go Show resolved Hide resolved
vroldanbet
vroldanbet previously approved these changes Oct 4, 2022
josephschorr
josephschorr previously approved these changes Oct 4, 2022
Copy link
Member

@josephschorr josephschorr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jakedt jakedt merged commit 42d2555 into main Oct 4, 2022
@jakedt jakedt deleted the postgres-perf branch October 4, 2022 16:37
@github-actions github-actions bot locked and limited conversation to collaborators Oct 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/datastore Affects the storage system area/dependencies Affects dependencies area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants