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

Allow CLI flags to be specified by environment variable #152

Merged
merged 4 commits into from
Oct 3, 2023

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Oct 2, 2023

Bind command line arguments to env vars. Allow the flags:

  • --postgres-url
  • --schema
  • --pgroll-schema

To be set via these env vars:

  • PGROLL_PG_URL
  • PGROLL_SCHEMA
  • PGROLL_STATE_SCHEMA

If a flag is specified both via a CLI flag and an env var the CLI flag takes priority.

Allow the flags:

* `--postgres-url`
* `--schema`
* `--pgroll-schema`

To be set via these env vars:

* `PGROLL_PG_URL`
* `PGROLL_SCHEMA`
* `PGROLL_STATE_SCHEMA`
@exekias
Copy link
Member

exekias commented Oct 2, 2023

Change LGTM, can you update the docs to explain the new env vars?

@andrew-farries
Copy link
Collaborator Author

andrew-farries commented Oct 2, 2023

The docs for the env vars are included in the new CLI reference docs section: #156

Set the session `lock_timeout` for the connection used by `pgroll` to
run DDL operations.

The need to set `lock_timeout` for DDL operations to achieve zero
downtime migrations is well documented
[[1](https://xata.io/blog/postgres-schema-changes-pita#locking-gotchas)],
[[2](https://medium.com/paypal-tech/postgresql-at-scale-database-schema-changes-without-downtime-20d3749ed680#00dc)].

When a DDL transaction attempts to take an `ACCESS EXCLUSIVE` lock on a
table while another transaction holds a conflicting lock (such as a
long-running `SELECT` query's `ACCESS SHARE` lock), other queries will
queue up behind the DDL transaction until it is able to obtain the
exclusive lock thus locking out reads from the table and causing
downtime. By setting a `lock_timeout` the DDL transaction will abort if
it is unable to acquire the lock before the timeout, freeing any other
queries that were queued behind it.

The approach taken here is to set the `lock_timeout` at the session
level. This assumes that `pgroll` has a direct connection to the
Postgres instance rather than going through a connection pooler like
PGBouncer. PGBouncer's statement or transaction mode may swap out the
connection between transactions/statements meaning that the lock timeout
may not be applied correctly. See
[[3](https://jpcamara.com/2023/04/12/pgbouncer-is-useful.html)] for a
more detailed discussion of PGBouncer's behaviour here.

The new `--lock-timeout` flag (and `PGROLL_LOCK_TIMEOUT` env var) is
used to control the timeout duration.

Fixes #65
@andrew-farries andrew-farries merged commit 3eae4ec into main Oct 3, 2023
12 checks passed
@andrew-farries andrew-farries deleted the add-viper branch October 3, 2023 06:43
andrew-farries added a commit that referenced this pull request Nov 22, 2023
Fix the `pgroll status` command so that it shows the status for the
correct schema (ie the one set by the `--schema` flag, or `"public"` by
default).

Prior to this the command would show the status for the
`--pgroll-schema` (`"pgroll"` by default).

This appears to have been broken since #152.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants