Repository navigation
Tune Postgres settings and drop unused episode embeddings - #254
Merged
Merged
Conversation
- Postgres: shared_buffers 1536MB, effective_cache_size 6GB, work_mem 16MB, random_page_cost 1.1, effective_io_concurrency 200, jit off, wal_compression lz4, shm 1 GB, 60 s stop grace. Remove auto_explain: it instrumented every statement while the container's log driver discarded its output. - Deploy recreates Postgres when its compose configuration hash changes, stopping the app first; otherwise it leaves the running database alone. - Episodes are searched by text only. Stop embedding them and drop the unused HNSW index (migration 202) and the embedding column (203).
|
Owner
Author
|
Danger warnings: the description now has a migration compatibility section (no-tx single-statement index drop, metadata-only column drop, down behaviour and the data that is not restorable, old-binary ordering) and names the SQLx checks; the deployment-risk note and post-deploy plan were already there. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Postgres ran on stock settings (128 MB shared buffers, 4 MB work_mem, HDD cost model), so most reads missed the buffer cache and sorts spilled to temporary files. Episode inserts also paid for a vector index that no query used.
Postgres settings (production compose)
shared_buffers128MB → 1536MB,effective_cache_size6GB,work_mem4MB → 16MB.random_page_cost4 → 1.1 andeffective_io_concurrency1 → 200 (SSD-backed volume).jit=off: statements are short OLTP queries; JIT only added compile time on large estimates.wal_compression=lz4(the image is built--with-lz4).auto_explainremoved. It ranlog_analyzewith per-node timing on every statement, while the container's log driver isnone, so its plans were never kept.shm_size: 1gbfor parallel-query shared memory,stop_grace_period: 60sso a recreate shuts down cleanly instead of being killed after 10 s.synchronous_commitstays on.Deploy applies Postgres configuration changes
deploy-production.shstarted Postgres with--no-recreate, so new settings never reached a running database. It now comparesdocker compose config --hash postgresqlwith the running container'scom.docker.compose.config-hashlabel. Only when they differ does it stop the app and recreate Postgres; an unchanged configuration leaves the database running.Drop episode embeddings
Episodes are retrieved by full-text search only (
SQL_RETRIEVE_MEMORY_EPISODES); no query readsmemory_episodes.embedding. The memory pipeline no longer embeds episodes, and the episode insert has one code path without the vector.memory_episodes_embedding_hnsw_idxconcurrently.embeddingcolumn. Its TOAST space returns on the next table rewrite.Migration compatibility
-- no-transaction, one statement):DROP INDEX CONCURRENTLY IF EXISTS, so it does not block writes and is a no-op when rerun. Down recreates the HNSW index on the column 203's down restores.DROP COLUMN IF EXISTS, a metadata-only change. Down re-addsembedding vector(512), the type migration 100 created. Data is not restorable: rollback gives an empty column, and the reverted code would embed new episodes again.embeddingon insert, so it must not run after 203 applies; the deploy stops the old app before the new one migrates.concurrent_index_migrations_are_single_statement_no_tx_filesandsqlx_up_migration_versions_are_uniquepass; the CI Postgres job applies all migrations (run_migrations_on) against pgvector/pg17 before the live storage tests.Deployment risk and verification plan
SHOWreturns the new settings; confirmauto_explainis not loaded; confirm migrations 202/203 applied and the index is gone; compare buffer cache hit ratio, temp-file growth and episode insert time frompg_stat_statementsbefore and after; check host memory and dialog failures for 30 minutes.Verification
cargo fmt --all,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace.docker compose config --hash postgresqlon the production host matches the running container label for the current compose file, so an unchanged configuration is a no-op.bash -n deploy/production/deploy-production.sh.