diff --git a/README.md b/README.md index 70f1944..dba1ce3 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,15 @@ Create a dockerized d2-docker: $ bash build-docker-container.sh ``` +## Debug SQL queries + +By default, d2-docker logs all SQL queries executed (one file per weekday). Example: + +``` +$ db_container="d2-docker-docker-eyeseetea-com-samaritans-40-4-0-sp-cpr-test-db-1" +$ docker exec -it "$db_container" tail -f /var/lib/postgresql/data/log/queries-Thu.log +``` + ### API Server Start Flask server in development mode: diff --git a/src/d2_docker/commands/start.py b/src/d2_docker/commands/start.py index 8c899e9..2ff59f4 100644 --- a/src/d2_docker/commands/start.py +++ b/src/d2_docker/commands/start.py @@ -39,6 +39,8 @@ def setup(parser): parser.add_argument("--deploy-path", type=str, help="Set Tomcat context.path") parser.add_argument("--java-opts", type=str, help="Set Tomcat JAVA_OPTS") parser.add_argument("--postgis-version", type=str, help="Set PostGIS database version") + parser.add_argument("--enable-postgres-queries-logging", action="store_true", + help="Enable Postgres queries logging") def run(args): @@ -110,6 +112,7 @@ def start(args): dhis_conf=args.dhis_conf, java_opts=args.java_opts, postgis_version=args.postgis_version, + enable_postgres_queries_logging=args.enable_postgres_queries_logging, ) if args.detach: diff --git a/src/d2_docker/docker-compose.yml b/src/d2_docker/docker-compose.yml index 9f74c06..2cbc68e 100644 --- a/src/d2_docker/docker-compose.yml +++ b/src/d2_docker/docker-compose.yml @@ -41,7 +41,7 @@ services: POSTGRES_DB: dhis2 POSTGRES_USER: dhis POSTGRES_PASSWORD: dhis - command: "postgres -c max_locks_per_transaction=100 -c max_connections=250 -c shared_buffers=3200MB -c work_mem=24MB -c maintenance_work_mem=1024MB -c effective_cache_size=8000MB -c checkpoint_completion_target=0.8 -c synchronous_commit=off -c wal_writer_delay=10000ms -c random_page_cost=1.1 -c max_locks_per_transaction=100 -c temp_buffers=16MB -c track_activity_query_size=8192 -c jit=off" + command: "postgres -c max_locks_per_transaction=100 -c max_connections=250 -c shared_buffers=3200MB -c work_mem=24MB -c maintenance_work_mem=1024MB -c effective_cache_size=8000MB -c checkpoint_completion_target=0.8 -c synchronous_commit=off -c wal_writer_delay=10000ms -c random_page_cost=1.1 -c max_locks_per_transaction=100 -c temp_buffers=16MB -c track_activity_query_size=8192 -c jit=off ${PSQL_ENABLE_QUERY_LOGS--c logging_collector=on -c log_statement=all -c log_filename=queries-%a.log}" restart: unless-stopped ports: - "${DB_PORT}" diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 5c3558e..8d65fb9 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -260,6 +260,7 @@ def run_docker_compose( dhis2_auth=None, tomcat_server=None, postgis_version=None, + enable_postgres_queries_logging=False, **kwargs, ): """ @@ -294,6 +295,7 @@ def run_docker_compose( ("DB_PORT", ("{}:5432".format(db_port) if db_port else "0:1000")), # Add ROOT_PATH from environment (required when run inside a docker) ("ROOT_PATH", ROOT_PATH), + ("PSQL_ENABLE_QUERY_LOGS", "") if not enable_postgres_queries_logging else None, ] env = dict((k, v) for (k, v) in [pair for pair in env_pairs if pair] if v is not None)