Skip to content

Commit 06a8857

Browse files
authored
[ENH] postgres monitor (#1093)
* wip: adding semantic search * add pghero to docker compose * add missing sql file * replace store_pgsql to store-pgsql * fix configuration * change hardcoding into variables * Revert "wip: adding semantic search" This reverts commit 6b0d6bf.
1 parent 0ab6693 commit 06a8857

File tree

9 files changed

+73
-17
lines changed

9 files changed

+73
-17
lines changed

.github/workflows/deploy-to-staging.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
docker compose up -d
7070
7171
# Wait for PostgreSQL to be ready
72-
until docker compose exec store_pgsql pg_isready; do sleep 5; done
72+
until docker compose exec store-pgsql pg_isready; do sleep 5; done
7373
7474
docker compose exec neurostore flask db migrate
7575
docker compose exec neurostore flask db upgrade

.github/workflows/workflow.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
clean:
1919
strategy:
2020
matrix:
21-
package: ['neurosynth_compose', 'compose_nginx', 'compose_pgsql', 'compose_worker', 'neurostore', 'store_nginx', 'store_pgsql']
21+
package: ['neurosynth_compose', 'compose_nginx', 'compose_pgsql', 'compose_worker', 'neurostore', 'store_nginx', 'store-pgsql']
2222
runs-on: ubuntu-latest
2323

2424
steps:
@@ -126,10 +126,10 @@ jobs:
126126
store_nginx.cache-from=type=registry,ref=ghcr.io/${{ github.repository_owner }}/store_nginx:${{ hashFiles('**/store/nginx/**') }}
127127
store_nginx.cache-from=type=gha,scope=cached-store-nginx
128128
store_nginx.cache-to=type=gha,scope=cached-store-nginx,mode=max,ignore-error=true
129-
store_pgsql.tags=ghcr.io/${{ github.repository_owner }}/store_pgsql:${{ hashFiles('**/store/postgres/**') }}
130-
store_pgsql.cache-from=type=registry,ref=ghcr.io/${{ github.repository_owner }}/store_pgsql:${{ hashFiles('**/store/postgres/**') }}
131-
store_pgsql.cache-from=type=gha,scope=cached-store-pgsql
132-
store_pgsql.cache-to=type=gha,scope=cached-store-pgsql,mode=max,ignore-error=true
129+
store-pgsql.tags=ghcr.io/${{ github.repository_owner }}/store-pgsql:${{ hashFiles('**/store/postgres/**') }}
130+
store-pgsql.cache-from=type=registry,ref=ghcr.io/${{ github.repository_owner }}/store-pgsql:${{ hashFiles('**/store/postgres/**') }}
131+
store-pgsql.cache-from=type=gha,scope=cached-store-pgsql
132+
store-pgsql.cache-to=type=gha,scope=cached-store-pgsql,mode=max,ignore-error=true
133133
134134
135135
neurostore_backend_tests:
@@ -164,7 +164,7 @@ jobs:
164164
set: |
165165
neurostore.cache-from=type=gha,scope=cached-neurostore
166166
store_nginx.cache-from=type=gha,scope=cached-store-nginx
167-
store_pgsql.cache-from=type=gha,scope=cached-store-pgsql
167+
store-pgsql.cache-from=type=gha,scope=cached-store-pgsql
168168
-
169169
name: spin up backend
170170
run: |
@@ -178,7 +178,7 @@ jobs:
178178
name: Create Test Database
179179
run: |
180180
docker compose exec -T \
181-
store_pgsql \
181+
store-pgsql \
182182
psql -U postgres -c "create database test_db"
183183
-
184184
name: Backend Tests
@@ -418,7 +418,7 @@ jobs:
418418
set: |
419419
neurostore.cache-from=type=gha,scope=cached-neurostore
420420
store_nginx.cache-from=type=gha,scope=cached-store-nginx
421-
store_pgsql.cache-from=type=gha,scope=cached-store-pgsql
421+
store-pgsql.cache-from=type=gha,scope=cached-store-pgsql
422422
-
423423
name: Spin up compose backend
424424
run: |
@@ -452,7 +452,7 @@ jobs:
452452
cd store
453453
454454
docker compose exec -T \
455-
store_pgsql \
455+
store-pgsql \
456456
psql -U postgres -c "create database test_db"
457457
-
458458
name: Initialize Compose Database

store/.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ FLASK_APP=manage
33
ACE_DIR=/media
44
FILE_DIR=/tmp
55
COMPOSE_CONVERT_WINDOWS_PATHS=1
6-
POSTGRES_HOST=store_pgsql
6+
POSTGRES_HOST=store-pgsql
7+
POSTGRES_USER=postgres
78
POSTGRES_PASSWORD=example
9+
POSTGRES_DB=neurostore
810
BEARERINFO_FUNC=neurostore.resources.auth.decode_token
911
AUTH0_CLIENT_ID=YOUR_CLIENT_ID
1012
AUTH0_CLIENT_SECRET=YOUR_CLIENT_SECRET

store/backend/README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The server should now be running at http://localhost/
2727

2828
Create the database for neurostore:
2929

30-
docker-compose exec store_pgsql psql -U postgres -c "create database neurostore"
30+
docker-compose exec store-pgsql psql -U postgres -c "create database neurostore"
3131

3232
Next, migrate and upgrade the database migrations.
3333

@@ -64,11 +64,37 @@ If you need to upgrade the db after changing any models:
6464
## Running tests
6565
To run tests, after starting services, create a test database:
6666

67-
docker-compose exec store_pgsql psql -U postgres -c "create database test_db"
67+
docker-compose exec store-pgsql psql -U postgres -c "create database test_db"
6868

6969
**NOTE**: This command will ask you for the postgres password which is defined
7070
in the `.env` file.
7171

7272
and execute:
7373

7474
docker-compose run -e "APP_SETTINGS=neurostore.config.DockerTestConfig" --rm -w /neurostore neurostore python -m pytest neurostore/tests
75+
76+
## pgHero Service
77+
78+
[pgHero](https://github.com/ankane/pghero) is a PostgreSQL monitoring tool that provides insights into database performance and queries through a web UI.
79+
80+
### Starting pgHero with Docker Compose
81+
82+
To start the pgHero service, ensure Docker Compose is set up and run:
83+
84+
```sh
85+
docker-compose up -d pghero
86+
```
87+
88+
### Accessing the pgHero Web UI
89+
90+
Once running, access the pgHero dashboard at: [http://localhost/pghero](http://localhost/pghero) (replace "localhost" with your server's address as needed).
91+
92+
The `/pghero` route is proxied by nginx to the pgHero service, so you do not need to specify a port.
93+
94+
### Required Environment Variables
95+
96+
Set the following environment variables (typically in your `.env` file):
97+
98+
- `PGHERO_DATABASE_URL` — The connection string for your PostgreSQL database (e.g., `postgres://postgres:password@store-pgsql:5432/neurostore`).
99+
100+
Refer to the `docker-compose.yml` for additional configuration options.

store/docker-compose.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
context: ./backend
88
dockerfile: ./Dockerfile
99
depends_on:
10-
store_pgsql:
10+
store-pgsql:
1111
condition: service_healthy
1212
store_redis:
1313
condition: service_healthy
@@ -47,8 +47,8 @@ services:
4747
- VIRTUAL_HOST=${V_HOST}
4848
- LETSENCRYPT_HOST=${V_HOST}
4949

50-
store_pgsql:
51-
image: store_pgsql
50+
store-pgsql:
51+
image: store-pgsql
5252
restart: always
5353
build: ./postgres
5454
# shm_size: '256MB' # if I'm going to increase work_mem in postgres, also increase this
@@ -58,12 +58,26 @@ services:
5858
- '5432'
5959
env_file:
6060
- .env
61-
command: ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"] # Use the custom config
61+
command: ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
6262
healthcheck:
6363
test: ["CMD-SHELL", "pg_isready -U postgres"]
6464
interval: 3s
6565
timeout: 3s
6666
retries: 10
67+
68+
pghero:
69+
image: ankane/pghero
70+
restart: always
71+
ports:
72+
- "8080:8080"
73+
env_file:
74+
- .env
75+
environment:
76+
DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB}"
77+
RAILS_RELATIVE_URL_ROOT: "/pghero"
78+
depends_on:
79+
store-pgsql:
80+
condition: service_healthy
6781

6882
volumes:
6983
postgres_data:

store/nginx/nginx.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,15 @@ http {
9393
return 301 https://neurostuff.github.io/;
9494
}
9595

96+
location /pghero/ {
97+
proxy_pass http://pghero:8080/pghero/;
98+
proxy_set_header Host $host;
99+
proxy_set_header X-Real-IP $remote_addr;
100+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
101+
proxy_set_header X-Forwarded-Proto $scheme;
102+
proxy_http_version 1.1;
103+
proxy_set_header Upgrade $http_upgrade;
104+
proxy_set_header Connection "upgrade";
105+
}
96106
}
97107
}

store/postgres/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ RUN dos2unix /home/pg_dump-to-s3.sh
1111
RUN dos2unix /home/s3-autodelete.sh
1212

1313
COPY postgresql.conf /etc/postgresql/postgresql.conf
14+
COPY pg_stat_statements.sql /docker-entrypoint-initdb.d/
1415

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

store/postgres/postgresql.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
shared_buffers = 128MB
22
work_mem = 8MB
33
listen_addresses = '*'
4+
shared_preload_libraries = 'pg_stat_statements'
5+
pg_stat_statements.track = all

0 commit comments

Comments
 (0)