-
Notifications
You must be signed in to change notification settings - Fork 0
feat: track restore stage in _pgro.restore_info #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -785,21 +785,33 @@ else | |
| ALTER ROLE ${{ANALYTICS_USERNAME}} WITH SUPERUSER; | ||
| SQLEOF | ||
| fi | ||
| echo "Writing restore metadata..." | ||
| if [ -f /pgdata/needs-reindex ]; then | ||
| PGRO_STAGE=restored | ||
| else | ||
| PGRO_STAGE=ready | ||
| fi | ||
|
|
||
| echo "Writing restore metadata (stage=${{PGRO_STAGE}})..." | ||
| psql -U postgres -d postgres << SQLEOF | ||
| CREATE SCHEMA IF NOT EXISTS _pgro; | ||
| CREATE TABLE IF NOT EXISTS _pgro.restore_info ( | ||
| id integer PRIMARY KEY DEFAULT 1, | ||
| snapshot_id text NOT NULL, | ||
| snapshot_time timestamptz, | ||
| restored_at timestamptz NOT NULL DEFAULT now() | ||
| restored_at timestamptz NOT NULL DEFAULT now(), | ||
| stage text NOT NULL DEFAULT 'restored', | ||
| last_transition_time timestamptz NOT NULL DEFAULT now() | ||
| ); | ||
| INSERT INTO _pgro.restore_info (id, snapshot_id, snapshot_time) | ||
| VALUES (1, '${{PGRO_SNAPSHOT_ID}}', CASE WHEN '${{PGRO_SNAPSHOT_TIME}}' = '' THEN NULL ELSE '${{PGRO_SNAPSHOT_TIME}}'::timestamptz END) | ||
| ALTER TABLE _pgro.restore_info ADD COLUMN IF NOT EXISTS stage text NOT NULL DEFAULT 'restored'; | ||
| ALTER TABLE _pgro.restore_info ADD COLUMN IF NOT EXISTS last_transition_time timestamptz NOT NULL DEFAULT now(); | ||
| INSERT INTO _pgro.restore_info (id, snapshot_id, snapshot_time, stage, last_transition_time) | ||
| VALUES (1, '${{PGRO_SNAPSHOT_ID}}', CASE WHEN '${{PGRO_SNAPSHOT_TIME}}' = '' THEN NULL ELSE '${{PGRO_SNAPSHOT_TIME}}'::timestamptz END, '${{PGRO_STAGE}}', now()) | ||
| ON CONFLICT (id) DO UPDATE | ||
| SET snapshot_id = EXCLUDED.snapshot_id, | ||
| snapshot_time = EXCLUDED.snapshot_time, | ||
| restored_at = now(); | ||
| restored_at = now(), | ||
| stage = EXCLUDED.stage, | ||
| last_transition_time = now(); | ||
| SQLEOF | ||
|
|
||
| echo "Stopping temporary postgres..." | ||
|
|
@@ -951,6 +963,7 @@ if [ -f /pgdata/needs-reindex ]; then | |
| PG_MAJOR=$(cat /pgdata/pgdata/PG_VERSION) | ||
| ( | ||
| while ! pg_isready -q -U postgres -d postgres; do sleep 2; done | ||
| psql -U postgres -d postgres -c "UPDATE _pgro.restore_info SET stage = 'reindexing', last_transition_time = now() WHERE id = 1;" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Bugs & Correctness] The 'reindexing' and 'ready' UPDATE statements in the postgres container silently affect 0 rows if the |
||
| for db in $(psql -U postgres -d postgres -At -c "SELECT datname FROM pg_database WHERE datallowconn AND datname <> 'template0'"); do | ||
| INDEXES=$(psql -U postgres -d "$db" -At -c " | ||
| SELECT DISTINCT indexrelid::regclass::text | ||
|
|
@@ -973,6 +986,7 @@ if [ -f /pgdata/needs-reindex ]; then | |
| done | ||
| done | ||
| rm -f /pgdata/needs-reindex | ||
| psql -U postgres -d postgres -c "UPDATE _pgro.restore_info SET stage = 'ready', last_transition_time = now() WHERE id = 1;" | ||
|
review-hero[bot] marked this conversation as resolved.
|
||
| echo "Background reindex complete" | ||
| ) & | ||
| fi | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Bugs & Correctness]
suggestionThe ON CONFLICT clause always resets
last_transition_time = now()regardless of whetherstageactually changed. If the init container re-runs on a pod restart after a restore has already completed (e.g. the same stage value is upserted),last_transition_timegets bumped to the restart time, making it look like a stage transition just occurred. Fix:last_transition_time = CASE WHEN _pgro.restore_info.stage IS DISTINCT FROM EXCLUDED.stage THEN now() ELSE _pgro.restore_info.last_transition_time END