Skip to content

Commit

Permalink
feat: Default to disable synchronous postgres commit
Browse files Browse the repository at this point in the history
  • Loading branch information
acaloiaro committed Oct 3, 2023
1 parent f59d0f7 commit 820370e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions backends/postgres/postgres_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func Backend(ctx context.Context, opts ...neoq.ConfigOption) (pb neoq.Neoq, err
// there is no limit to the amount of time a worker's transactions may be idle
query = setIdleInTxSessionTimeout
}

if !p.config.SynchronousCommit {
query = fmt.Sprintf("%s; SET synchronous_commit = 'off';", query)
}
_, err = conn.Exec(ctx, query)
return
}
Expand Down Expand Up @@ -184,6 +188,24 @@ func WithTransactionTimeout(txTimeout int) neoq.ConfigOption {
}
}

// WithSynchronousCommit enables postgres parameter `synchronous_commit`.
//
// By default, neoq runs with synchronous_commit disabled.
//
// Postgres incurrs significant transactional overhead from synchronously committing small transactions. Because
// neoq jobs must be enqueued individually, and payloads are generally quite small, synchronous_commit introduces
// significant overhead, but increases data durability.
//
// See https://www.postgresql.org/docs/current/wal-async-commit.html for details on the implications that this has for
// neoq jobs.
//
// Enabling synchronous commit results in an order of magnitude slowdown in enqueueing and processing jobs.
func WithSynchronousCommit(enabled bool) neoq.ConfigOption {
return func(c *neoq.Config) {
c.SynchronousCommit = enabled
}
}

// txFromContext gets the transaction from a context, if the transaction is already set
func txFromContext(ctx context.Context) (t pgx.Tx, err error) {
var ok bool
Expand Down
3 changes: 2 additions & 1 deletion neoq.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ var ErrBackendNotSpecified = errors.New("a backend must be specified")
// per-handler basis.
type Config struct {
BackendInitializer BackendInitializer
BackendAuthPassword string // password with which to authenticate to the backend's data provider
BackendAuthPassword string // password with which to authenticate to the backend
BackendConcurrency int // total number of backend processes available to process jobs
ConnectionString string // a string containing connection details for the backend
JobCheckInterval time.Duration // the interval of time between checking for new future/retry jobs
FutureJobWindow time.Duration // time duration between current time and job.RunAfter that goroutines schedule for future jobs
IdleTransactionTimeout int // the number of milliseconds PgBackend transaction may idle before the connection is killed
ShutdownTimeout time.Duration // duration to wait for jobs to finish during shutdown
SynchronousCommit bool // Postgres: Enable synchronous commits (increases durability, decreases performance)
LogLevel logging.LogLevel // the log level of the default logger
}

Expand Down

0 comments on commit 820370e

Please sign in to comment.