|
| 1 | +.. _zero_downtime_upgrades: |
| 2 | + |
| 3 | +Zero-Downtime Upgrades |
| 4 | +====================== |
| 5 | + |
| 6 | +On operating systems that support ``SO_REUSEPORT``, PostgREST can start more |
| 7 | +than one process on the same :ref:`server-host` and :ref:`server-port`. This |
| 8 | +allows a new PostgREST process to start and become ready before the old process |
| 9 | +is stopped. |
| 10 | + |
| 11 | +While both processes are running, the operating system distributes new |
| 12 | +connections between them. After the old process exits, the new process receives |
| 13 | +all new connections. |
| 14 | + |
| 15 | +This is useful for upgrades and restarts: |
| 16 | + |
| 17 | +1. Keep the old PostgREST process serving requests. |
| 18 | +2. Start the new PostgREST process on the same host and port. |
| 19 | +3. Wait for the new process to report ``/ready``. |
| 20 | +4. Stop the old process. |
| 21 | + |
| 22 | +Configuration |
| 23 | +------------- |
| 24 | + |
| 25 | +Both processes should use the same public host and port: |
| 26 | + |
| 27 | +.. code-block:: ini |
| 28 | +
|
| 29 | + # /etc/postgrest/postgrest.conf |
| 30 | + server-host = "127.0.0.1" |
| 31 | + server-port = 3000 |
| 32 | +
|
| 33 | + admin-server-host = "127.0.0.1" |
| 34 | + admin-server-port = 3001 |
| 35 | +
|
| 36 | +The second process can use the same configuration file and override only the |
| 37 | +admin server port: |
| 38 | + |
| 39 | +.. code-block:: bash |
| 40 | +
|
| 41 | + PGRST_ADMIN_SERVER_PORT=3002 postgrest /etc/postgrest/postgrest.conf |
| 42 | +
|
| 43 | +.. important:: |
| 44 | + |
| 45 | + Use a different :ref:`admin-server-port` for each PostgREST process during |
| 46 | + the handover. Admin ports are not shared between processes. This keeps |
| 47 | + readiness checks unambiguous: ``/ready`` on the new admin port can only be |
| 48 | + answered by the new process. |
| 49 | + |
| 50 | +Before using this in production, keep these details in mind: |
| 51 | + |
| 52 | +- This works for host and port based servers. It does not apply when |
| 53 | + :ref:`server-unix-socket` is used. |
| 54 | +- If the operating system does not support ``SO_REUSEPORT``, the new process |
| 55 | + will fail to start with an address-in-use error and the old process will keep |
| 56 | + serving requests. |
| 57 | +- If the new process uses the same :ref:`admin-server-port` as the old process, |
| 58 | + it will fail to start because that admin port is already in use. |
| 59 | +- Each PostgREST process has its own :ref:`db-pool`. During the handover, the |
| 60 | + total possible database connections can temporarily double. |
| 61 | +- The old and new processes may both serve requests for a short time. Database |
| 62 | + migrations should be compatible with both versions while they overlap. |
| 63 | + |
| 64 | +Manual Handover |
| 65 | +--------------- |
| 66 | + |
| 67 | +Assuming the old process is already serving on ``127.0.0.1:3000`` and its PID |
| 68 | +is stored in ``OLD_PID``: |
| 69 | + |
| 70 | +.. code-block:: bash |
| 71 | +
|
| 72 | + PGRST_ADMIN_SERVER_PORT=3002 postgrest /etc/postgrest/postgrest.conf & |
| 73 | + NEW_PID=$! |
| 74 | +
|
| 75 | + curl --fail http://127.0.0.1:3002/ready |
| 76 | +
|
| 77 | + kill -TERM "$OLD_PID" |
| 78 | +
|
| 79 | +The ``curl`` request checks the new process through its own admin server port. |
| 80 | +If the new process cannot load its configuration, connect to the database, or |
| 81 | +load the schema cache, ``/ready`` will not return a successful response and the |
| 82 | +old process can keep serving traffic. |
| 83 | + |
| 84 | +Example Script |
| 85 | +-------------- |
| 86 | + |
| 87 | +The following script shows the full sequence for a setup that stores the old |
| 88 | +process PID in a PID file. Adapt the start and stop commands to your process |
| 89 | +manager. |
| 90 | + |
| 91 | +.. code-block:: bash |
| 92 | +
|
| 93 | + #!/usr/bin/env bash |
| 94 | + set -euo pipefail |
| 95 | +
|
| 96 | + POSTGREST=${POSTGREST:-postgrest} |
| 97 | + CONFIG=${CONFIG:-/etc/postgrest/postgrest.conf} |
| 98 | + PID_FILE=${PID_FILE:-/run/postgrest.pid} |
| 99 | +
|
| 100 | + ADMIN_HOST=${ADMIN_HOST:-127.0.0.1} |
| 101 | + NEW_ADMIN_PORT=${NEW_ADMIN_PORT:-3002} |
| 102 | + READY_TIMEOUT=${READY_TIMEOUT:-30} |
| 103 | + STOP_TIMEOUT=${STOP_TIMEOUT:-30} |
| 104 | +
|
| 105 | + if [[ ! -s "$PID_FILE" ]]; then |
| 106 | + echo "PID file not found or empty: $PID_FILE" >&2 |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | +
|
| 110 | + OLD_PID=$(<"$PID_FILE") |
| 111 | +
|
| 112 | + if ! kill -0 "$OLD_PID" 2>/dev/null; then |
| 113 | + echo "Old PostgREST process is not running: $OLD_PID" >&2 |
| 114 | + exit 1 |
| 115 | + fi |
| 116 | +
|
| 117 | + PGRST_ADMIN_SERVER_HOST="$ADMIN_HOST" \ |
| 118 | + PGRST_ADMIN_SERVER_PORT="$NEW_ADMIN_PORT" \ |
| 119 | + "$POSTGREST" "$CONFIG" & |
| 120 | + NEW_PID=$! |
| 121 | +
|
| 122 | + cleanup_new_process() { |
| 123 | + kill "$NEW_PID" 2>/dev/null || true |
| 124 | + } |
| 125 | + trap cleanup_new_process EXIT INT TERM |
| 126 | +
|
| 127 | + READY_URL="http://$ADMIN_HOST:$NEW_ADMIN_PORT/ready" |
| 128 | + READY_DEADLINE=$((SECONDS + READY_TIMEOUT)) |
| 129 | +
|
| 130 | + until curl --fail --silent --show-error --output /dev/null "$READY_URL"; do |
| 131 | + if ! kill -0 "$NEW_PID" 2>/dev/null; then |
| 132 | + echo "New PostgREST process exited before it became ready" >&2 |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | +
|
| 136 | + if (( SECONDS >= READY_DEADLINE )); then |
| 137 | + echo "New PostgREST process did not become ready at $READY_URL" >&2 |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | +
|
| 141 | + sleep 1 |
| 142 | + done |
| 143 | +
|
| 144 | + printf '%s\n' "$NEW_PID" > "$PID_FILE" |
| 145 | +
|
| 146 | + kill -TERM "$OLD_PID" 2>/dev/null || true |
| 147 | +
|
| 148 | + STOP_DEADLINE=$((SECONDS + STOP_TIMEOUT)) |
| 149 | +
|
| 150 | + while kill -0 "$OLD_PID" 2>/dev/null; do |
| 151 | + if (( SECONDS >= STOP_DEADLINE )); then |
| 152 | + echo "Old PostgREST process did not stop after SIGTERM; sending SIGKILL" >&2 |
| 153 | + kill -KILL "$OLD_PID" |
| 154 | + break |
| 155 | + fi |
| 156 | +
|
| 157 | + sleep 1 |
| 158 | + done |
| 159 | +
|
| 160 | + trap - EXIT INT TERM |
| 161 | + echo "PostgREST handover complete: $OLD_PID -> $NEW_PID" |
0 commit comments