BIM Query Deploy Claude Opus 4.7 (1M context) commited on
Commit
42d2a35
·
1 Parent(s): e19c339

fix(deploy): postgres startup robustness on HF Spaces

Browse files

- Move unix socket to /tmp (default /var/run/postgresql is not
writable for uid 1000 on HF Spaces).
- Drop memory tunables: shared_buffers 256MB→64MB,
work_mem 16MB→4MB, max_connections 50→20. /dev/shm in HF
containers is typically ~64MB, which is what was almost
certainly blocking startup.
- huge_pages = off and dynamic_shared_memory_type = posix
(explicit defaults that some restricted container runtimes
override).
- Surface postgres.log on pg_ctl failure so the next iteration
gets a real error message instead of "Examine the log output".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. entrypoint.sh +26 -9
entrypoint.sh CHANGED
@@ -27,19 +27,36 @@ if [ ! -s "$PGDATA/PG_VERSION" ]; then
27
 
28
  # Bind only to localhost — the API and Celery talk to it over the
29
  # loopback inside the same container.
30
- echo "listen_addresses = '127.0.0.1'" >> "$PGDATA/postgresql.conf"
31
- echo "port = 5432" >> "$PGDATA/postgresql.conf"
32
- # Memory tuned for HF's shared 16 GB; conservative so we leave room
33
- # for ifcopenshell + Celery.
34
- echo "shared_buffers = 256MB" >> "$PGDATA/postgresql.conf"
35
- echo "work_mem = 16MB" >> "$PGDATA/postgresql.conf"
36
- echo "maintenance_work_mem = 64MB" >> "$PGDATA/postgresql.conf"
37
- echo "max_connections = 50" >> "$PGDATA/postgresql.conf"
 
 
 
 
 
 
 
 
 
 
 
38
  fi
39
 
40
  # --- 2. Start Postgres in the background ------------------------------------
41
  echo "[entrypoint] starting postgres"
42
- "$PG_BIN/pg_ctl" -D "$PGDATA" -l "$PG_LOG" -w start
 
 
 
 
 
 
43
 
44
  # --- 3. Ensure user / database / extension exist ----------------------------
45
  # Idempotent: safe to run on every boot. On a fresh PGDATA they get created;
 
27
 
28
  # Bind only to localhost — the API and Celery talk to it over the
29
  # loopback inside the same container.
30
+ cat >> "$PGDATA/postgresql.conf" <<'CONF'
31
+ listen_addresses = '127.0.0.1'
32
+ port = 5432
33
+
34
+ # /var/run/postgresql is not writable in HF Spaces; keep the unix socket
35
+ # in /tmp where uid 1000 can write.
36
+ unix_socket_directories = '/tmp'
37
+
38
+ # Container memory tuning. HF /dev/shm is typically small (~64MB), so
39
+ # keep shared_buffers low. The API processes IFC, not OLAP queries.
40
+ shared_buffers = 64MB
41
+ work_mem = 4MB
42
+ maintenance_work_mem = 16MB
43
+ max_connections = 20
44
+
45
+ # Disable kernel features that containers usually don't grant.
46
+ huge_pages = off
47
+ dynamic_shared_memory_type = posix
48
+ CONF
49
  fi
50
 
51
  # --- 2. Start Postgres in the background ------------------------------------
52
  echo "[entrypoint] starting postgres"
53
+ if ! "$PG_BIN/pg_ctl" -D "$PGDATA" -l "$PG_LOG" -w -t 30 start; then
54
+ echo "[entrypoint] pg_ctl start FAILED. Dumping $PG_LOG:" >&2
55
+ echo "---------------- postgres.log ----------------" >&2
56
+ cat "$PG_LOG" >&2 || true
57
+ echo "---------------- end log -------------------" >&2
58
+ exit 1
59
+ fi
60
 
61
  # --- 3. Ensure user / database / extension exist ----------------------------
62
  # Idempotent: safe to run on every boot. On a fresh PGDATA they get created;