ops: stabilize local postgres startup pool
Browse files
start.sh
CHANGED
|
@@ -75,8 +75,8 @@ export LEGACY_LOCAL_DATABASE_DBNAME="${LEGACY_LOCAL_DATABASE_DBNAME:-${legacy_na
|
|
| 75 |
|
| 76 |
# Conservative DB pool profile for HF cpu-basic: keep app-side pool below
|
| 77 |
# PostgreSQL max_connections, leaving headroom for maintenance, backups and psql.
|
| 78 |
-
export DATABASE_MAX_OPEN_CONNS="${DATABASE_MAX_OPEN_CONNS:-
|
| 79 |
-
export DATABASE_MAX_IDLE_CONNS="${DATABASE_MAX_IDLE_CONNS:-
|
| 80 |
export DATABASE_CONN_MAX_LIFETIME_MINUTES="${DATABASE_CONN_MAX_LIFETIME_MINUTES:-30}"
|
| 81 |
export DATABASE_CONN_MAX_IDLE_TIME_MINUTES="${DATABASE_CONN_MAX_IDLE_TIME_MINUTES:-5}"
|
| 82 |
|
|
@@ -282,6 +282,27 @@ start_postgres() {
|
|
| 282 |
fi
|
| 283 |
}
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
promote_if_in_recovery() {
|
| 286 |
if [ "$(su-exec postgres psql -At -h 127.0.0.1 -p "${LOCAL_DATABASE_PORT}" -U postgres -d postgres -c 'SELECT pg_is_in_recovery();')" = "t" ]; then
|
| 287 |
log "promoting restored PostgreSQL out of recovery"
|
|
@@ -493,6 +514,32 @@ cleanup_loops() {
|
|
| 493 |
) &
|
| 494 |
}
|
| 495 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
setup_local_postgres() {
|
| 497 |
[ -n "${LOCAL_DATABASE_PASSWORD}" ] || fail "LOCAL_DATABASE_PASSWORD/DATABASE_PASSWORD is required"
|
| 498 |
[ -n "${BACKUP_ENCRYPTION_KEY}" ] || fail "BACKUP_ENCRYPTION_KEY or DATABASE_PASSWORD is required"
|
|
@@ -515,25 +562,33 @@ setup_local_postgres() {
|
|
| 515 |
su-exec postgres initdb -D "${LOCAL_PGDATA}" --encoding=UTF8 --locale=C
|
| 516 |
configure_pg_for_archive
|
| 517 |
start_postgres
|
|
|
|
| 518 |
promote_if_in_recovery
|
|
|
|
| 519 |
ensure_local_role_db
|
|
|
|
| 520 |
import_source_database
|
| 521 |
make_base_backup
|
| 522 |
make_logical_dump || true
|
| 523 |
force_initial_wal_archive || true
|
| 524 |
-
|
| 525 |
-
|
|
|
|
| 526 |
return 0
|
| 527 |
fi
|
| 528 |
fi
|
| 529 |
|
| 530 |
configure_pg_for_archive
|
| 531 |
start_postgres
|
|
|
|
| 532 |
promote_if_in_recovery
|
|
|
|
| 533 |
ensure_local_role_db
|
|
|
|
| 534 |
force_initial_wal_archive || true
|
| 535 |
-
|
| 536 |
-
|
|
|
|
| 537 |
}
|
| 538 |
|
| 539 |
mkdir -p "${DATA_DIR}" "${DATA_DIR}/redis"
|
|
@@ -595,4 +650,8 @@ fi
|
|
| 595 |
write_runtime_config
|
| 596 |
chown -R app:app "${DATA_DIR}" 2>/dev/null || true
|
| 597 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
exec /app/boot.sh /app/server
|
|
|
|
| 75 |
|
| 76 |
# Conservative DB pool profile for HF cpu-basic: keep app-side pool below
|
| 77 |
# PostgreSQL max_connections, leaving headroom for maintenance, backups and psql.
|
| 78 |
+
export DATABASE_MAX_OPEN_CONNS="${DATABASE_MAX_OPEN_CONNS:-80}"
|
| 79 |
+
export DATABASE_MAX_IDLE_CONNS="${DATABASE_MAX_IDLE_CONNS:-20}"
|
| 80 |
export DATABASE_CONN_MAX_LIFETIME_MINUTES="${DATABASE_CONN_MAX_LIFETIME_MINUTES:-30}"
|
| 81 |
export DATABASE_CONN_MAX_IDLE_TIME_MINUTES="${DATABASE_CONN_MAX_IDLE_TIME_MINUTES:-5}"
|
| 82 |
|
|
|
|
| 282 |
fi
|
| 283 |
}
|
| 284 |
|
| 285 |
+
log_pg_boot_snapshot() {
|
| 286 |
+
label="$1"
|
| 287 |
+
log "pg boot snapshot: ${label}"
|
| 288 |
+
su-exec postgres psql -At -h 127.0.0.1 -p "${LOCAL_DATABASE_PORT}" -U postgres -d postgres <<'EOSQL' || true
|
| 289 |
+
SELECT 'setting max_connections=' || current_setting('max_connections');
|
| 290 |
+
SELECT 'setting superuser_reserved_connections=' || current_setting('superuser_reserved_connections');
|
| 291 |
+
SELECT 'setting reserved_connections=' || coalesce(current_setting('reserved_connections', true), '<missing>');
|
| 292 |
+
SELECT 'activity_total=' || count(*) FROM pg_stat_activity;
|
| 293 |
+
SELECT
|
| 294 |
+
'activity user=' || coalesce(usename, '<null>')
|
| 295 |
+
|| ' state=' || coalesce(state, '<null>')
|
| 296 |
+
|| ' app=' || coalesce(application_name, '<null>')
|
| 297 |
+
|| ' client=' || coalesce(client_addr::text, 'local')
|
| 298 |
+
|| ' count=' || count(*)
|
| 299 |
+
FROM pg_stat_activity
|
| 300 |
+
GROUP BY usename, state, application_name, client_addr
|
| 301 |
+
ORDER BY count(*) DESC
|
| 302 |
+
LIMIT 20;
|
| 303 |
+
EOSQL
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
promote_if_in_recovery() {
|
| 307 |
if [ "$(su-exec postgres psql -At -h 127.0.0.1 -p "${LOCAL_DATABASE_PORT}" -U postgres -d postgres -c 'SELECT pg_is_in_recovery();')" = "t" ]; then
|
| 308 |
log "promoting restored PostgreSQL out of recovery"
|
|
|
|
| 514 |
) &
|
| 515 |
}
|
| 516 |
|
| 517 |
+
backup_loops_delayed() {
|
| 518 |
+
delay="${BACKUP_START_DELAY_SECONDS:-300}"
|
| 519 |
+
if [ "${delay}" = "0" ]; then
|
| 520 |
+
backup_loops
|
| 521 |
+
return 0
|
| 522 |
+
fi
|
| 523 |
+
log "scheduling backup loops after ${delay}s startup delay"
|
| 524 |
+
(
|
| 525 |
+
sleep "${delay}"
|
| 526 |
+
backup_loops
|
| 527 |
+
) &
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
cleanup_loops_delayed() {
|
| 531 |
+
delay="${BACKUP_CLEANUP_START_DELAY_SECONDS:-300}"
|
| 532 |
+
if [ "${delay}" = "0" ]; then
|
| 533 |
+
cleanup_loops
|
| 534 |
+
return 0
|
| 535 |
+
fi
|
| 536 |
+
log "scheduling backup cleanup loop after ${delay}s startup delay"
|
| 537 |
+
(
|
| 538 |
+
sleep "${delay}"
|
| 539 |
+
cleanup_loops
|
| 540 |
+
) &
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
setup_local_postgres() {
|
| 544 |
[ -n "${LOCAL_DATABASE_PASSWORD}" ] || fail "LOCAL_DATABASE_PASSWORD/DATABASE_PASSWORD is required"
|
| 545 |
[ -n "${BACKUP_ENCRYPTION_KEY}" ] || fail "BACKUP_ENCRYPTION_KEY or DATABASE_PASSWORD is required"
|
|
|
|
| 562 |
su-exec postgres initdb -D "${LOCAL_PGDATA}" --encoding=UTF8 --locale=C
|
| 563 |
configure_pg_for_archive
|
| 564 |
start_postgres
|
| 565 |
+
log_pg_boot_snapshot "after start_postgres initial import"
|
| 566 |
promote_if_in_recovery
|
| 567 |
+
log_pg_boot_snapshot "after promote initial import"
|
| 568 |
ensure_local_role_db
|
| 569 |
+
log_pg_boot_snapshot "after ensure_local_role_db initial import"
|
| 570 |
import_source_database
|
| 571 |
make_base_backup
|
| 572 |
make_logical_dump || true
|
| 573 |
force_initial_wal_archive || true
|
| 574 |
+
log_pg_boot_snapshot "after initial WAL archive initial import"
|
| 575 |
+
backup_loops_delayed
|
| 576 |
+
cleanup_loops_delayed
|
| 577 |
return 0
|
| 578 |
fi
|
| 579 |
fi
|
| 580 |
|
| 581 |
configure_pg_for_archive
|
| 582 |
start_postgres
|
| 583 |
+
log_pg_boot_snapshot "after start_postgres"
|
| 584 |
promote_if_in_recovery
|
| 585 |
+
log_pg_boot_snapshot "after promote"
|
| 586 |
ensure_local_role_db
|
| 587 |
+
log_pg_boot_snapshot "after ensure_local_role_db"
|
| 588 |
force_initial_wal_archive || true
|
| 589 |
+
log_pg_boot_snapshot "after initial WAL archive"
|
| 590 |
+
backup_loops_delayed
|
| 591 |
+
cleanup_loops_delayed
|
| 592 |
}
|
| 593 |
|
| 594 |
mkdir -p "${DATA_DIR}" "${DATA_DIR}/redis"
|
|
|
|
| 650 |
write_runtime_config
|
| 651 |
chown -R app:app "${DATA_DIR}" 2>/dev/null || true
|
| 652 |
|
| 653 |
+
if [ "${LOCAL_POSTGRES_ENABLED}" = "true" ]; then
|
| 654 |
+
log_pg_boot_snapshot "before app exec"
|
| 655 |
+
fi
|
| 656 |
+
|
| 657 |
exec /app/boot.sh /app/server
|