Skip to content

POSTGRES_XLOG_DIR to specify transaction log dir #224

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

Merged
merged 2 commits into from
Mar 20, 2017

Conversation

DanielDent
Copy link
Contributor

Adds support for the POSTGRES_XLOG_DIR environment variable, which specifies where the postgres transaction log is stored.

For some use cases, being able to place the transaction log on a different volume is useful.

(Existing support for providing flags via $POSTGRES_INITDB_ARGS is inadequate because of the need to create and chown/chmod the directory prior to running initdb)

@DanielDent
Copy link
Contributor Author

I just rebased on your latest changes, tests still pass. Please let me know if there is anything I can do to help get this merged.

@tianon
Copy link
Member

tianon commented Nov 28, 2016

When it's used, isn't this directory something the user ought to be persisting as well?

@DanielDent
Copy link
Contributor Author

Definitely. I didn't add another VOLUME because VOLUMEs can't be undefined and not everyone will want multiple VOLUMEs for their data.

Additionally, users don't necessarily need another VOLUME to use this.

A user could define PGDATA to /var/lib/postgresql/data/pg_data, and POSTGRES_XLOG_DIR to /var/lib/postgresql/data/pg_xlog, and both would be persisted within that volume.

But it also gives them the option to attach different volumes to these mount points, or to volumes specified at runtime. The transaction log and the data files have different performance requirements/optimizations, and it's useful to be able to put them on mountpoints which take advantage of the required characteristics.

@tianon
Copy link
Member

tianon commented Nov 28, 2016

So in the case of the xlogdir being a subdirectory of $PGDATA, permissions are already covered by the existing code, right?

I'm kind of inclined to put the permissions responsibility for this in the hands of the user, since it's already possible to do so today without explicit support for this functionality. To put it another way, if we accept this knob for this directory/flag, which initdb or postgres flag is going to come next?

@DanielDent
Copy link
Contributor Author

DanielDent commented Nov 29, 2016

The reason I created this fork/pull request is that I didn't find a way to have this work without modifying the upstream image.

I initially tried having xlogdir as a subdirectory of $PGDATA. Postgres refuses to allow initdb to occur unless the data directory is empty. This creates problems if you are trying to mount a different volume onto that directory, because then the xlogdir directory exists and it's not an empty directory.

I certainly understand the general concern about not wanting to cover every permutation of every use case. That's where things like POSTGRES_INITDB_ARGS allow users to customize the image for themselves at runtime.

Once users start having to manually fiddle with permissions just to launch a container, docker ceases to be something which removes administrative burden in the way it ought to, and I don't see a way around it in this case.

@DanielDent
Copy link
Contributor Author

@tianon I just rebased again.

I understand the desire to not have complicated support for every potential permutation of how Postgres is used, but I'd also point out that creating directories/setting permissions automatically so postgres can start without intervention does seem to be something that is seen as worth doing already - it's just that the current code doesn't work if you are using a custom transaction log directory.

If my commit can be merged as-is, yay! Or if you have alternative suggestions for how I could support this use case, or a more generalized merge-worthy approach I could implement which would cover my use case, I would be pleased to work towards having something which is merge-worthy.

chmod 700 "$POSTGRES_XLOG_DIR"
chown -R postgres "$POSTGRES_XLOG_DIR"
POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_XLOG_DIR"
fi
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for your patience and good-nature, @DanielDent 😞 ❤️

I'm not 100% sold, but I concede that we probably need something for this. Here are my thoughts on the implementation:

  • I'd like a blank space above the comment (so that the /var/run/postgresql lines are separated from these)
  • I'd rather name the variable POSTGRES_INITDB_XLOGDIR so it makes it more clear what it interacts with (and maps directly to the option it enables)
  • the POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS ..." line should start with export,
    ie export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS ..."
  • the four-space indent you've got should be tabs (matching the rest of the file) 👍

Happy to take over for any of this if you'd prefer, but want to give you first dibs (and a chance to converse if what I've proposed doesn't make sense or isn't accurate 😄) ❤️

@DanielDent DanielDent force-pushed the master branch 2 times, most recently from 513274c to 9d0b63f Compare February 28, 2017 21:26
@DanielDent
Copy link
Contributor Author

DanielDent commented Feb 28, 2017

Great. I've incorporated your proposed changes.

Additionally, I reworked things slightly to support secrets (file_env), which did not exist when I originally made these changes. I can revert that if need be - I suspect the desired interface is 'all environment variables can also be provided as file secrets' but I haven't read a style guide on the subject.

Copy link
Member

@tianon tianon left a comment

Choose a reason for hiding this comment

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

Good call on including file_env! 👍

Just a couple minor comments and I think we're good to go!

@@ -47,7 +57,6 @@ if [ "$1" = 'postgres' ]; then

# look specifically for PG_VERSION, as it is expected in the DB dir
if [ ! -s "$PGDATA/PG_VERSION" ]; then
file_env 'POSTGRES_INITDB_ARGS'
Copy link
Member

Choose a reason for hiding this comment

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

This one needs to stay here in case we didn't hit the [ "$(id -u)" = '0' ] block for the above if (it should be harmless to duplicate it -- the function takes care of making sure multi-invocation is safe).

@@ -37,6 +37,16 @@ if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
chown -R postgres /var/run/postgresql
chmod g+s /var/run/postgresql

# Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user
Copy link
Member

Choose a reason for hiding this comment

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

This line appears to still be space-indented. 😄

Adds support for the POSTGRES_INITDB_XLOGDIR environment variable, which specifies where the postgres transaction log is stored.

For some use cases, being able to place the transaction log on a different volume is useful.

Existing support for providing flags via $POSTGRES_INITDB_ARGS is inadequate because of the need to create and chown/chmod the directory prior to running initdb.
@DanielDent
Copy link
Contributor Author

Thanks. Re-pushed.

@DanielDent
Copy link
Contributor Author

@tianon ping

… to where it'll be invoked regardless of our "root" status
@tianon
Copy link
Member

tianon commented Mar 20, 2017

Sorry for the delay @DanielDent; I thought of another edge case (users who use --user rather than our auto-step-down code), so I updated the PR to include that. 👍

@tianon tianon merged commit 123aedc into docker-library:master Mar 20, 2017
@tianon
Copy link
Member

tianon commented Mar 20, 2017

Thanks for your patience!

tianon added a commit to infosiftr/stackbrew that referenced this pull request Mar 20, 2017
- `memcached`: 1.4.36
- `postgres`: add `POSTGRES_INITDB_XLOGDIR` variable for `--xlogdir` during `initdb` (docker-library/postgres#224)
- `pypy`: 5.7.0
DanielDent added a commit to DanielDent/docker-library-docs that referenced this pull request Apr 6, 2017
docker-library/postgres#224 and docker-library/official-images#2762 added the POSTGRES_XLOG_DIR variable. This adds documentation for that variable.
DanielDent added a commit to DanielDent/docker-library-docs that referenced this pull request Apr 6, 2017
docker-library/postgres#224 and docker-library/official-images#2762 added the POSTGRES_INITDB_XLOGDIR variable. This adds documentation for that variable.
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.

3 participants