Skip to content

Commit 6817449

Browse files
committed
feat(sql): Enable pg_cron
1 parent e261127 commit 6817449

File tree

8 files changed

+59
-45
lines changed

8 files changed

+59
-45
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ gen.proto.python: ${PROTOC}
197197
.PHONY: run.db # Run an instance of Postgres with the required extensions
198198
run.db:
199199
docker build -f internal/server/postgres/infra/Containerfile internal/server/postgres/infra -t data-platform-pgdb:local
200-
docker run --rm -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p "5400:5432" data-platform-pgdb:local
200+
docker run --rm -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -p "5400:5432" data-platform-pgdb:local postgres -c 'shared_preload_libraries=pg_cron' -c 'cron.database_name=postgres'
201201

202202
.PHONY: run.notebook # Run a python notebook to inspect the API
203203
run.notebook: gen.proto.python

internal/server/postgres/infra/Containerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN apt-get update \
66
&& apt-get install -y --no-install-recommends \
77
postgresql-16-postgis-3 \
88
postgresql-16-postgis-3-scripts \
9+
&& apt-get install -y --no-install-recommends postgresql-16-cron \
910
&& rm -rf /var/lib/apt/lists/*
1011

11-
COPY init_postgis.sh /docker-entrypoint-initdb.d/10_init_postgis.sh
12-
COPY init_partman_bgw.sh /docker-entrypoint-initdb.d/99_init_partman_bgw.sh
12+
COPY init_db.sh /docker-entrypoint-initdb.d/00_init_db.sh
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
#!/bin/bash
2-
# /docker-entrypoint-initdb.d/99_init_partman_bgw.sh
2+
# /docker-entrypoint-initdb.d/10_init_extensions.sh
33

4-
# Exit immediately if a command exits with a non-zero status.
5-
set -e
6-
7-
echo "[+] Configuring pg_partman background worker (BGW) in postgresql.conf"
4+
echo "[+] Configuring shared_preload_libraries in postgresql.conf"
85

96
CONF_FILE="${PGDATA}/postgresql.conf"
10-
LIB_NAME='pg_partman_bgw'
117

12-
# Check if the library is already configured (idempotency)
8+
LIB_NAME='pg_cron'
9+
1310
if grep -Eq "^shared_preload_libraries\s*=\s*'.*${LIB_NAME}" "$CONF_FILE"; then
1411
echo " '${LIB_NAME}' already found in shared_preload_libraries."
15-
# Check if shared_preload_libraries line exists and is commented out
1612
elif grep -q "^#shared_preload_libraries" "$CONF_FILE"; then
13+
# shared_preload_libraries line exists and is commented out
1714
echo " Uncommenting shared_preload_libraries and adding '${LIB_NAME}'"
1815
sed -ri "s!^#shared_preload_libraries\s*=\s*'?(.*?)'?\s*(#.*)?!shared_preload_libraries = '${LIB_NAME}' \2!" "$CONF_FILE"
1916
echo " Successfully uncommented and set '${LIB_NAME}'."
20-
# Check if shared_preload_libraries exists and is uncommented
2117
elif grep -q "^shared_preload_libraries" "$CONF_FILE"; then
18+
# shared_preload_libraries exists and is uncommented
2219
echo " Appending '${LIB_NAME}' to existing shared_preload_libraries..."
23-
# Use the safer method: extract current value, build new value, replace line.
24-
# 1. Extract current value (content inside the quotes)
2520
current_val=$(grep "^shared_preload_libraries" "$CONF_FILE" | sed -n "s/^shared_preload_libraries\s*=\s*'\([^']*\)'.*/\1/p")
26-
# 2. Construct new value
2721
if [ -z "$current_val" ] || [ "$current_val" = '' ]; then
28-
# If current value is empty, just set the new library
22+
# if current value is empty, just set the new library
2923
new_val="'${LIB_NAME}'"
3024
else
31-
# If current value exists, append the new library with a comma
3225
new_val="'${current_val},${LIB_NAME}'"
3326
fi
34-
# 3. Replace the whole line using the new value
3527
sed -ri "s!^shared_preload_libraries\s*=.*!shared_preload_libraries = ${new_val}!" "$CONF_FILE"
3628
echo " Successfully appended '${LIB_NAME}'."
3729
else
@@ -41,13 +33,19 @@ else
4133
echo "shared_preload_libraries = '${LIB_NAME}'" >> "$CONF_FILE"
4234
echo " Successfully added '${LIB_NAME}'."
4335
fi
36+
echo "cron.database_name = '${POSTGRES_DB}'" >> "$CONF_FILE"
37+
4438

4539
export PGUSER="$POSTGRES_USER"
46-
# Load pg_partman into $POSTGRES_DB
47-
for DB in "$POSTGRES_DB"; do
48-
echo "Loading pg_partman extensions into $DB"
49-
"${psql[@]}" --dbname="$DB" <<-'EOSQL'
50-
CREATE SCHEMA IF NOT EXISTS partman;
51-
CREATE EXTENSION IF NOT EXISTS pg_partman WITH SCHEMA partman;
40+
41+
echo "[+] Loading extensions into $DB_NAME"
42+
# We still set the session variable to be safe, though CMD usually covers it
43+
"${psql[@]}" --dbname="$DB_NAME" <<-'EOSQL'
44+
CREATE EXTENSION IF NOT EXISTS pg_cron;
45+
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
46+
CREATE SCHEMA IF NOT EXISTS topology;
47+
CREATE EXTENSION IF NOT EXISTS postgis_topology WITH SCHEMA topology;
48+
CREATE SCHEMA IF NOT EXISTS partman;
49+
CREATE EXTENSION IF NOT EXISTS pg_partman WITH SCHEMA partman;
5250
EOSQL
53-
done
51+

internal/server/postgres/infra/init_postgis.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

internal/server/postgres/package_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func setupTestMain(ctx context.Context, m *testing.M) (code int, err error) {
5858
"POSTGRES_PASSWORD": "postgres",
5959
"POSTGRES_DB": "postgres",
6060
},
61-
Cmd: []string{"postgres", "-c", "fsync=off"},
61+
Cmd: []string{"postgres", "-c", "fsync=off", "-c", "shared_preload_libraries=pg_cron"},
6262
ExposedPorts: []string{"5432/tcp"},
6363
WaitingFor: wait.ForAll(
6464
wait.ForLog(

internal/server/postgres/sql/migrations/00002_locations.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
* One geometry can have multiple sources, e.g. the UK nation geometry can have solar, wind, etc.
1717
*/
1818

19-
CREATE SCHEMA loc;
2019
CREATE EXTENSION IF NOT EXISTS btree_gist;
21-
CREATE EXTENSION IF NOT EXISTS postgis;
20+
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
21+
CREATE EXTENSION IF NOT EXISTS postgis_topology WITH SCHEMA topology;
22+
23+
CREATE SCHEMA loc;
2224

2325
/*- Lookups -----------------------------------------------------------------------------------*/
2426

@@ -164,3 +166,6 @@ CREATE INDEX ON loc.sources_mv USING gist (sys_period);
164166

165167
-- +goose Down
166168
DROP SCHEMA loc CASCADE;
169+
DROP EXTENSION IF EXISTS postgis_topology;
170+
DROP EXTENSION IF EXISTS postgis;
171+
DROP EXTENSION IF EXISTS btree_gist;

internal/server/postgres/sql/migrations/00003_observations.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* with these providers provide access to the data in order to test the accuracy of predictions.
99
*/
1010

11+
CREATE SCHEMA IF NOT EXISTS partman;
12+
CREATE EXTENSION IF NOT EXISTS pg_partman WITH SCHEMA partman;
13+
CREATE EXTENSIONS IF NOT EXISTS pg_cron;
14+
1115
CREATE SCHEMA obs;
1216

1317
/*- Tables ----------------------------------------------------------------------------------*/
@@ -84,5 +88,19 @@ SET
8488
WHERE parent_table = 'obs.observed_generation_values';
8589
SELECT partman.run_maintenance('obs.observed_generation_values');
8690

91+
/*
92+
* Schedule regular maintenance for the partitioned observed generation values table.
93+
* This runs every hour.
94+
*/
95+
SELECT cron.schedule('partman-ogv-maintenance', '0 * * * *',
96+
$$SELECT partman.run_maintenance('obs.observed_generation_values');$$
97+
);
98+
99+
87100
-- +goose Down
101+
SELECT cron.unschedule('partman-ogv-maintenance');
88102
DROP SCHEMA obs CASCADE;
103+
104+
DROP EXTENSION IF EXISTS pg_partman CASCADE;
105+
DROP SCHEMA partman CASCADE;
106+
DROP EXTENSION IF EXISTS pg_cron CASCADE;

internal/server/postgres/sql/migrations/00004_predictions.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,15 @@ SET
198198
WHERE parent_table = 'pred.predicted_generation_values';
199199
SELECT partman.run_maintenance('pred.predicted_generation_values');
200200

201+
/*
202+
* Schedule regular maintenance for the partitioned predicted generation values table.
203+
* This runs every hour.
204+
*/
205+
SELECT cron.schedule('partman-pgv-maintenance', '0 * * * *',
206+
$$SELECT partman.run_maintenance('pred.predicted_generation_values');$$
207+
);
208+
201209
-- +goose Down
210+
SELECT cron.unschedule('partman-pgv-maintenance');
202211
DROP SCHEMA pred CASCADE;
212+

0 commit comments

Comments
 (0)