Skip to content

Commit

Permalink
Custom Config Values (#117)
Browse files Browse the repository at this point in the history
* Fix #94: Support custom start parameters.

Add a StartParameters config taking configuration settings.

These run-time configuration parameters override those set in the
default postgres.conf, and are passed to the postgres process via the
options flag of pg_ctl.

* Ensure we do run tests for non apple silicon.

---------

Co-authored-by: Josh Giles <jgiles@paxos.com>
  • Loading branch information
fergusstrange and jgiles authored Jun 13, 2023
1 parent c0fbd81 commit 8b137fc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ RuntimePath("/tmp").
BinaryRepositoryURL("https://repo.local/central.proxy").
Port(9876).
StartTimeout(45 * time.Second).
StartParameters(map[string]string{"max_connections": "200"}).
Logger(logger))
err := postgres.Start()

Expand Down
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
dataPath string
binariesPath string
locale string
startParameters map[string]string
binaryRepositoryURL string
startTimeout time.Duration
logger io.Writer
Expand Down Expand Up @@ -101,6 +102,15 @@ func (c Config) Locale(locale string) Config {
return c
}

// StartParameters sets run-time parameters when starting Postgres (passed to Postgres via "-c").
//
// These parameters can be used to override the default configuration values in postgres.conf such
// as max_connections=100. See https://www.postgresql.org/docs/current/runtime-config.html
func (c Config) StartParameters(parameters map[string]string) Config {
c.startParameters = parameters
return c
}

// StartTimeout sets the max timeout that will be used when starting the Postgres process and creating the initial database.
func (c Config) StartTimeout(timeout time.Duration) Config {
c.startTimeout = timeout
Expand Down
11 changes: 10 additions & 1 deletion embedded_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,20 @@ func (ep *EmbeddedPostgres) Stop() error {
return nil
}

func encodeOptions(port uint32, parameters map[string]string) string {
options := []string{fmt.Sprintf("-p %d", port)}
for k, v := range parameters {
// Single-quote parameter values - they may have spaces.
options = append(options, fmt.Sprintf("-c %s='%s'", k, v))
}
return strings.Join(options, " ")
}

func startPostgres(ep *EmbeddedPostgres) error {
postgresBinary := filepath.Join(ep.config.binariesPath, "bin/pg_ctl")
postgresProcess := exec.Command(postgresBinary, "start", "-w",
"-D", ep.config.dataPath,
"-o", fmt.Sprintf(`"-p %d"`, ep.config.port))
"-o", encodeOptions(ep.config.port, ep.config.startParameters))
postgresProcess.Stdout = ep.syncedLogger.file
postgresProcess.Stderr = ep.syncedLogger.file

Expand Down
36 changes: 35 additions & 1 deletion embedded_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func Test_ErrorWhenCannotStartPostgresProcess(t *testing.T) {

err = database.Start()

assert.EqualError(t, err, fmt.Sprintf("could not start postgres using %s/bin/pg_ctl start -w -D %s/data -o \"-p 5432\":\nah it did not work", extractPath, extractPath))
assert.EqualError(t, err, fmt.Sprintf("could not start postgres using %s/bin/pg_ctl start -w -D %s/data -o -p 5432:\nah it did not work", extractPath, extractPath))
}

func Test_CustomConfig(t *testing.T) {
Expand Down Expand Up @@ -416,6 +416,40 @@ func Test_ConcurrentStart(t *testing.T) {
wg.Wait()
}

func Test_CustomStartParameters(t *testing.T) {
database := NewDatabase(DefaultConfig().StartParameters(map[string]string{
"max_connections": "101",
"shared_buffers": "16 MB", // Ensure a parameter with spaces encodes correctly.
}))
if err := database.Start(); err != nil {
shutdownDBAndFail(t, err, database)
}

db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
shutdownDBAndFail(t, err, database)
}

if err := db.Ping(); err != nil {
shutdownDBAndFail(t, err, database)
}

row := db.QueryRow("SHOW max_connections")
var res string
if err := row.Scan(&res); err != nil {
shutdownDBAndFail(t, err, database)
}
assert.Equal(t, "101", res)

if err := db.Close(); err != nil {
shutdownDBAndFail(t, err, database)
}

if err := database.Stop(); err != nil {
shutdownDBAndFail(t, err, database)
}
}

func Test_CanStartAndStopTwice(t *testing.T) {
database := NewDatabase()

Expand Down
4 changes: 3 additions & 1 deletion platform-test/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func Test_AllMajorVersions(t *testing.T) {
embeddedpostgres.V14,
}

if runtime.GOOS != "darwin" && runtime.GOARCH == "arm64" {
isLikelyAppleSilicon := runtime.GOOS == "darwin" && runtime.GOARCH == "arm64"

if !isLikelyAppleSilicon {
allVersions = append(allVersions,
embeddedpostgres.V13,
embeddedpostgres.V12,
Expand Down

0 comments on commit 8b137fc

Please sign in to comment.