Skip to content
New issue

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

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

Already on GitHub? # to your account

Shutting down postgres gracefully with docker-compose down to avoid recovery process #544

Closed
csebranek opened this issue Jan 17, 2019 · 8 comments
Labels
question Usability question, not directly related to an error with the image

Comments

@csebranek
Copy link

Sometimes when I shutdown postgres (v9.4.20) with docker-compose down (v1.23.2) , the next time that I run docker-compose up, postgres claims database system was not properly shut down; automatic recovery in progress and takes forever to start back up. Even though when I shut it down the postgres logs say received smart shutdown request. Even trying with docker-compose down -v does not seem to resolve the issue. Here's my docker-compose.yml file.

version: "3.5"
networks:
  network1:
    driver: bridge
    name: sharedservices

services:

  postgres:
    image: postgres:9.4
    volumes:
      - "${PG_CONF_FILE}:/conf/postgresql.conf"
      - "${PG_DATA_DIR}:/var/lib/postgresql/data"
      - "${PG_HBA_FILE}:/conf/pg_hba.conf"
      - "${PG_LOG_DIR}:/logs"
    ports:
      - "${PG_PORT}:5432"
    expose:
      - "5432"
    networks:
      - network1
    environment:
      TZ: America/Chicago
    command: postgres -c log_directory=/logs -c config_file=/conf/postgresql.conf -c hba_file=/conf/pg_hba.conf

Any ideas on how to avoid an "automatic recovery" ?

@wglambert wglambert added the question Usability question, not directly related to an error with the image label Jan 17, 2019
@wglambert
Copy link

Relevant thread #184

Noting #184 (comment)

Smart Shutdown: ... lets existing sessions end their work normally. It shuts down only after all of the sessions terminate.

So if you want to ensure a graceful shutdown of Postgres then do a pg_ctl stop

@tianon
Copy link
Member

tianon commented Jan 18, 2019 via email

@csebranek
Copy link
Author

My feeling is that docker-compose sends the SIGTERM but closes the container after some time, which is what you are talking about with the timeout, but doesn't really ensure that postgres is actually shutdown, and it stops the container anyway before postgres is actually shutdown.

@tianon
Copy link
Member

tianon commented Jan 19, 2019

Right, so if you increase --stop-timeout (on docker run) / stop_grace_period: (on docker-compose.yml; https://docs.docker.com/compose/compose-file/compose-file-v2/#stop_grace_period), it will give PostgreSQL more time before it gets killed and thus increase your chances of PostgreSQL actually finishing up properly and shutting down cleanly.

For my own database instances, I typically use a pretty generous --stop-timeout value of 120 since if I'm using docker stop / SIGTERM, I want to give the database as much time as it needs (if I want a faster shutdown, I use docker rm -f / docker kill directly).

@csebranek
Copy link
Author

Oh nice, it seems like stop_grace_period looks promising. I'll give that a shot and report my findings here.

@csebranek
Copy link
Author

So it seems like stop_grace_period does indeed do what it says. However, it still fails to cut some connections and then the container eventually stops leading to another "automatic recovery" by postgres. In my case, I've narrowed it down to the connection between postgres and pgAdmin not terminating, so I'm thinking of doing some sort of a wrapper script to send a SIGINT after some time... e.g.:

docker-compose exec postgres /bin/bash -c "su - postgres -c '/usr/lib/postgresql/9.4/bin/pg_ctl stop -m fast -D /var/lib/postgresql/data'"

@tianon
Copy link
Member

tianon commented Jan 25, 2019

Yeah, that seems reasonable, but at this point I think we've determined this isn't really an issue with the image (certainly not something we can fix), so I'm going to close. 👍

@tianon tianon closed this as completed Jan 25, 2019
@ducttapecoder-vt

This comment has been minimized.

@docker-library docker-library locked as resolved and limited conversation to collaborators Jun 22, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
question Usability question, not directly related to an error with the image
Projects
None yet
Development

No branches or pull requests

4 participants