Skip to content

Commit 21f9cd5

Browse files
committed
feat: rotate the internal PostgreSQL logs
The internal vector-db PostgreSQL redirected all output to a single "$DATA_DIR/logfile" (pg_ctl -l) that grew without bound and was never rotated, eventually filling the persistent storage on long-running instances. Drop the -l redirect and enable PostgreSQL's built-in logging collector with the documented weekday scheme: one file per day named postgresql-Mon.log through postgresql-Sun.log under "$DATA_DIR/log". Size-based rotation stays off (log_rotation_size=0): with weekday filenames PostgreSQL reopens the same file on a size rotation without truncating, so a size cap cannot bound a day's file anyway. The collector overwrites a weekday file only when it rotates while running across a day boundary, not on startup, so a container that restarts frequently would keep appending to the same-named weekday file week after week. To keep the logs bounded to about a week regardless of restart frequency, remove the legacy single logfile and any weekday log at least six days old before starting the server, so last week's file is always gone before its name is reused. This also reclaims the old unbounded logfile on upgraded installs. The options are passed on the pg_ctl command line, so they also take effect for already-initialised data directories on the next restart and need no edits to persisted configuration. Document the new log location and retention in the README. Fixes #303 Signed-off-by: Cesar <275373127+sanzakicesarr@users.noreply.github.com>
1 parent 5b4c99d commit 21f9cd5

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ volumes:
104104
Logs are stored in the `logs/` directory in the persistent directory. In a docker container, it should be at `/nc_app_context_chat_backend/logs/`. The log file is named `ccb.log` and is set to otate at 20 MB with 10 backups. These logs are in JSONL format, i.e. each line is a valid JSON object.
105105
Now only warning and above logs are printed to the console. All the debug logs are written to the log file if `debug` is set to `true` in the config file.
106106
The logs of the embedding server are written to `logs/embedding_server_[date].log` in the persistent directory, it rotates with date change and is not in JSONL format, just raw stdout and stderr from the embedding server's process.
107+
The internal vector database (PostgreSQL) writes its logs to `vector_db_data/pgsql/log/` in the persistent directory, one file per weekday (e.g. `postgresql-Mon.log`); each is overwritten when that weekday comes round again, so the logs are kept for about a week.
107108

108109
## Configuration
109110
Configuration resides inside the persistent storage as `config.yaml`. The location is `$APP_PERSISTENT_STORAGE`. By default it would be at `/nc_app_context_chat_backend_data/config.yaml` inside the container.

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717
- add gh workflows for docker builds and do separate cpu, cuda and rocm (vulkan) images (#295) @kyteinsky
1818

1919
### Changed
20+
- rotate the internal PostgreSQL logs into one file per weekday instead of a single ever-growing logfile (#303) @sanzakicesarr
2021
- update readme according to the latest changes (#300) @kyteinsky
2122
- bump llama_cpp_python to 0.3.23 (#301) @kyteinsky
2223

dockerfile_scripts/pgsql/setup.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ source "$(dirname $(realpath $0))/env"
1111

1212
if [ x"$1" == "xstop" ]; then
1313
echo -n Stopping PostgreSQL...
14-
sudo -u postgres ${PG_BIN}/pg_ctl -D "$DATA_DIR" -l "${DATA_DIR}/logfile" -o "-p ${PG_PORT}" stop
14+
sudo -u postgres ${PG_BIN}/pg_ctl -D "$DATA_DIR" -o "-p ${PG_PORT}" stop
1515
exit 0
1616
fi
1717

@@ -47,8 +47,19 @@ if [ ! -d "$DATA_DIR/base" ]; then
4747
sudo -u postgres ${PG_BIN}/initdb -D "$DATA_DIR" -E UTF8
4848
fi
4949

50+
# Rotate the internal PostgreSQL logs with the built-in logging collector: one
51+
# file per weekday (postgresql-Mon.log ... postgresql-Sun.log) under
52+
# "$DATA_DIR/log". The collector overwrites a weekday file only on time-based
53+
# rotation, not on startup, so a frequently-restarted container would otherwise
54+
# append to last week's same-named file. Before starting, drop the pre-rotation
55+
# single logfile and any weekday log at least six days old (find -mtime +5), so
56+
# last week's file is always gone before its name is reused. See #303.
57+
rm -f "${DATA_DIR}/logfile"
58+
find "${DATA_DIR}/log" -maxdepth 1 -type f -name 'postgresql-*.log' -mtime +5 -delete 2>/dev/null || true
59+
PG_LOG_OPTS="-c logging_collector=on -c log_directory=log -c log_filename=postgresql-%a.log -c log_rotation_age=1d -c log_rotation_size=0 -c log_truncate_on_rotation=on"
60+
5061
echo "Starting PostgreSQL..."
51-
sudo -u postgres ${PG_BIN}/pg_ctl -D "$DATA_DIR" -l "${DATA_DIR}/logfile" -o "-p ${PG_PORT}" start
62+
sudo -u postgres ${PG_BIN}/pg_ctl -D "$DATA_DIR" -o "-p ${PG_PORT} ${PG_LOG_OPTS}" start
5263

5364
echo "Waiting for PostgreSQL to start..."
5465
until sudo -u postgres ${PG_SQL} -c "SELECT 1" > /dev/null 2>&1; do

0 commit comments

Comments
 (0)