Fail backup preflight when database index check detects corruption

Open Beginner friendly
#1,012 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@prashantw12 is already working on this.

Since Dec 2, 2026.

  • #1013 by @prashantw12 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ansible, postgresql, python
Domain
backend, databases

Research direction

Start with src/roles/backup/tasks/preflight.yaml and trace how it invokes the check_database_index role; inspect that role's task and rescue block to see how bt_index_check failures are handled. Confirm the check and backup preflight both fail on the synthetic corruption described in the issue, while a healthy database still passes without interrupting services.

Written by the indexing model from the issue text.

Description

Problem

The check_database_index role runs PostgreSQL amcheck (bt_index_check) against each database. If the integrity query fails, the role's rescue block only printed a debug message:

FAILED - indexes may be corrupted

It never failed the play, so the role returned success. The role is used by the backup preflight (src/roles/backup/tasks/preflight.yaml), so foremanctl backup carried on and produced a backup of a database that is already corrupted. The problem would only show up at restore time.

How reproducible

Always (with the synthetic corruption steps below; does not touch real application data).

Steps to Reproduce

Run as the PostgreSQL superuser in the foreman database (adjust the container name if needed):

podman exec -it postgresql psql -U postgres -d foreman
CREATE EXTENSION IF NOT EXISTS amcheck;
CREATE TABLE public.zz_amcheck_demo (id int);
INSERT INTO public.zz_amcheck_demo SELECT generate_series(1, 1000);
CREATE UNIQUE INDEX zz_amcheck_demo_idx ON public.zz_amcheck_demo (id);
GRANT ALL ON public.zz_amcheck_demo TO foreman;

-- rows added while the index is not maintained
UPDATE pg_index SET indisready = false WHERE indexrelid = 'public.zz_amcheck_demo_idx'::regclass;
INSERT INTO public.zz_amcheck_demo SELECT generate_series(1001, 1100);
UPDATE pg_index SET indisready = true  WHERE indexrelid = 'public.zz_amcheck_demo_idx'::regclass;

SELECT bt_index_check(index => 'public.zz_amcheck_demo_idx'::regclass, heapallindexed => true);
-- ERROR: heap tuple (...) from table "zz_amcheck_demo" lacks matching index tuple ...

Then run foremanctl checks and foremanctl backup <dir>.

Cleanup: DROP TABLE public.zz_amcheck_demo; and, if the extension was not installed before, DROP EXTENSION amcheck;.

Note: the role only runs when the amcheck extension is installed in the database, and it only checks btree indexes in the public schema.

Actual results

foremanctl checks / backup preflight rescue only logs debug and returns success; backup continues.

Expected results

Both commands fail with a clear amcheck failure; backup aborts in preflight without creating foreman-backup-* or stopping services.

Test results

Tested manually on Satellite 6.20 with foremanctl-3.3.1-1.el10sat, using the table above in the foreman database.

Scenario Before the fix After the fix
foremanctl checks, corrupted index exit 0, failed=0 rescued=1 exit 2, failed=1
foremanctl backup, corrupted index continues past preflight aborts in preflight, exit 2, no foreman-backup-* directory, foreman.target still active
foremanctl checks, after REINDEX passes passes, exit 0
foremanctl backup --skip-pulp-content, healthy database with the real Satellite indexes not tested preflight reports PASSED, all 8 dumps non-empty, Backup completed successfully, exit 0

Additional info

Dominant language
Python
Stars
26
Forks
59
Avg merge
5d 5h
Merged PRs (30d)
16

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from theforeman/foremanctl

All issues in theforeman/foremanctl

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.