Repository navigation
Description
Additional consolidated scope
Track foreign-key integrity across historical database/table restore and SQL dump restore. The cyclic-FK dump report follows a separate ordering path: mo-dump temporarily disables checks and interleaves table creation with row insertion, after which an update can violate a displayed constraint. Keep it as an independent restore mechanism and regression case; the shared invariant is that a successful restore must leave constraints enforced and relationships valid.
Additional acceptance criteria
- Restore a cyclic-FK database from a
mo-dumpSQL file, re-enable foreign-key checks, and verify anON UPDATE RESTRICTmutation that would create an orphan is rejected.SHOW CREATE TABLEalone is insufficient evidence of enforcement. - Retain separate tests for database PITR target-time reconstruction, explicit historical child-table RESTORE/CLONE, and mo-dump cyclic-FK restore.
Additional reports
- #30604 — original report and discussion remain on that issue.
Additional consolidated scope
Track the target-time restoration contract across database-level PITR and explicit historical table RESTORE/CLONE. The linked child-table cases exercise a separate path and must not persist orphan rows. Related reports remain separate reproductions where they exercise distinct paths. Closing a source issue records tracking consolidation only; no fix is claimed.
Additional acceptance criteria
- A database-level PITR restore reconstructs target-time rows and schema, including foreign keys, indexes, and views, or fails clearly when the target is unavailable.
- Explicit child-table SNAPSHOT/PITR restore and historical table CLONE must either reject an operation that would create an orphan FK row or use a documented coordinated restore; they must not return success with an invalid relationship.
- Verify FK enforcement, repeated restore determinism, and the existing baseline-readiness race independently.
Additional reports
- #30388 — original report and discussion remain on that issue.
Consolidated tracking scope
Track exact target-time restoration as one user-visible contract, including the separately isolated baseline-readiness race and schema/data reconstruction paths. The linked reports are retained as separate reproductions where they exercise different code paths. Closing a linked issue is an administrative consolidation only; no fix is claimed here.
Acceptance criteria
- If the target-time baseline is not materialized, RESTORE waits for it or returns a clear error; it never silently returns current or partial state.
- After restore, verify target-time rows and schema objects, including foreign keys, indexes, and views, with each existing reproduction retained as a separate case.
- Repeated restores produce the same target-time state and preserve referential integrity.
Included reports
- #30616 — original report and discussion remain on that issue.
Original canonical report (preserved verbatim)
Is there an existing issue for the same bug?
- I have checked the existing issues. Related but distinct: [Bug]: PITR table restore ignores FK metadata lookup errors and proceeds to DROP TABLE #30393 (closed, fixed by fix(frontend): fail PITR before DROP on FK metadata errors #30448) made PITR fail loudly on FK metadata lookup errors — but FK metadata is still never restored. This issue is the continuation: after the loud-failure fix, the normal path still silently drops FK definitions, and rows deleted under a dropped FK may not come back.
Branch Name
main
Commit ID
b24d1b3 (2026-10-16)
Other Environment Information
- Hardware: macOS arm64 (Apple Silicon)
- Topology: single-node standalone
- OS: macOS
Actual Behavior
RESTORE DATABASE ... FROM PITR silently loses FOREIGN KEY constraints that existed at the restore timestamp — and rows that were deleted from the FK-carrying table after the restore point are unreliably restored.
Reproduced 3/3 (each round a fresh database, same sequence):
create database fkr;
create table fkr.parent (id int primary key);
create table fkr.child (id int primary key, pid int);
insert into fkr.parent values (1),(2);
insert into fkr.child values (100,1),(200,2);
alter table fkr.child add constraint fk1 foreign key (pid) references fkr.parent(id);
-- T = now()
alter table fkr.child drop foreign key fk1;
delete from fkr.child where id = 100;
-- sleep 30s (beyond PITR granularity), then:
restore database fkr from pitr p1 '<T>';Results, 3/3 rounds:
| Check | Expected (state at T) | Round 1 | Rounds 2,3 |
|---|---|---|---|
show create table child |
CONSTRAINT fk1 FOREIGN KEY ... |
no FK at all | no FK at all |
select ... from child |
100,200 | 100,200 ✓ | 200 only (row 100 lost) |
And the FK is not merely missing from display — the semantics are gone too:
insert into fkr.child values (999, 777); -- pid 777 has no parent
-- SUCCEEDS silently (should be rejected by FK)Control experiment — the equivalent SNAPSHOT restore handles the same case correctly: after restore database fks {snapshot="sfk"}, data is 100,200 and CONSTRAINT fk1 FOREIGN KEY is preserved. So the two restore paths again disagree (snapshot restores FK metadata; PITR does not), the same family as #30614 (secondary indexes: snapshot ✓, PITR ✗) and #30610 (publications: PITR ✓, snapshot ✗).
Expected Behavior
After RESTORE DATABASE ... FROM PITR p '<T>', the FK constraints defined at <T> must exist and be enforced (invalid inserts rejected), deleted-after-<T> rows must come back, and FKs created after <T> must not survive — exactly what the snapshot path already does.
Root cause direction
The PITR database-restore path replays table DDL from the historical catalog ({MO_TS} reads in pkg/frontend/pitr.go) but never re-creates mo_catalog.mo_foreign_keys rows for the restore point. #30448 only ensured the restore fails when FK metadata lookups error out; it did not add FK re-creation. The unstable row-restore behavior (rounds 2/3 losing row 100) suggests the drop/recreate of the FK-carrying table races with data replay for rows affected by FK-state changes.
Impact
Referential integrity silently disappears after PITR recovery: a database restored "to a good state" accepts orphan rows without complaint, and may silently lose rows deleted after the restore point. For a feature whose entire purpose is trusted recovery, this is data-integrity grade.
Additional information
- BVT coverage:
pitr/directory has no FK × PITR database-restore scenario at all (onlysnapshot_restore_table_with_fkon the snapshot side).
Activity
Ariznawlll commentedon Oct 19, 2026
Additional evidence: the row-loss symptom is NOT FK-specific
A no-FK plain table loses rows on PITR restore as well, and repeated restores produce states that match neither the restore point nor any intermediate state:
Setup:
jur.twith ids 1..10 inserted,T = now(), thendelete id<=5:1,2,3,4,5,6— a state that never existed (T had 1..10). Partial restore: 7..10 silently missing.now()-90s(a timestamp where the true state was 1..6 post-restore) → result6,7,8,9,10— a chimera: rows 7..10 that did not exist at that point are present, rows 1..5 that did exist are gone.T-60scorrectly errors "database does not exists at timestamp" (create time is respected) — so timestamp selection itself works.This shows the PITR row-restore semantics are non-deterministic across repeated restores of the same database: each restore does not cleanly re-materialize the state at the chosen timestamp; some rows leak from previous states. Combined with the FK metadata loss in the main report, the database-level PITR restore path appears to conflate "drop-and-replay" with the current table contents instead of the selected point-in-time snapshot.
Repro scripts:
rowloss1.sh/rowloss2.sh— will formalize into a systematic repeat-restore matrix next.Ariznawlll commentedon Oct 19, 2026
Cross-reference: the "unstable row restore" symptom has now been isolated and filed as #30616
The row-loss part of this issue (rows deleted after the restore point not coming back, intermittently) is caused by a PITR baseline-materialization readiness race — not by the FK handling itself. See #30616 for the order-dependent reproduction (8-database batch: first 4 restores degrade to current state; reversed order: first restore drops tail rows; 5-second settling delay makes it 5/5 correct).
This issue (#30615) remains valid for its deterministic part: the FK constraint metadata is lost on 3/3 rounds regardless of timing — that is a distinct defect from the race.
[-][Bug]: PITR RESTORE DATABASE silently loses FOREIGN KEY constraints defined at the restore timestamp — FK not enforced after recovery, and rows deleted after the point restore unreliably (snapshot path handles both correctly)[/-][+][Bug]: PITR RESTORE DATABASE fails to reconstruct target-time data and schema metadata (indexes, foreign keys, views)[/+]XuPeng-SH commentedon Oct 22, 2026
Consolidated reports #30614 (secondary indexes) and #30649 (views) into this PITR target-state reconstruction issue. Keep all repros as separate acceptance cases: after RESTORE DATABASE FROM PITR, rows and schema objects that existed at the target timestamp must be present, later-created objects must be absent, and repeated restores must deterministically produce that state. Verify index usability, FK enforcement, view definitions, and base-table rows; the follow-up on this issue already shows row loss can occur without an FK. #30616's baseline-readiness/order race remains a separate issue.
[-][Bug]: PITR RESTORE DATABASE fails to reconstruct target-time data and schema metadata (indexes, foreign keys, views)[/-][+][Bug]: PITR RESTORE DATABASE must reproduce exact target-time data and schema or fail clearly[/+][-][Bug]: PITR RESTORE DATABASE must reproduce exact target-time data and schema or fail clearly[/-][+][Bug]: Historical restore and table CLONE must preserve target-time data and foreign-key integrity[/+][-][Bug]: Historical restore and table CLONE must preserve target-time data and foreign-key integrity[/-][+][Bug]: Restore paths must preserve target-time data and foreign-key integrity[/+]fix(sql): keep foreign-key integrity through restore and deferred FK …
daviszhen commentedon Oct 25, 2026
Fixed by #31105 (merged to main as
20860c7923e051021ae4ed06c221da2f1579fde7).What landed
Scope.RestoreTable(pkg/sql/compile/ddl.go) now runsvalidateClonedTableFKIntegrity(pkg/sql/compile/clone_fk_integrity.go) after the block-level clone in restore sessions and historical clones. A child-table SNAPSHOT/PITR restore or historicalCLONEwhose rows would orphan against the parent visible to the transaction now fails atomically — the error names the constraint, a sample orphan key value, and the parent, and the restore transaction rolls back cleanly. NULL keys are exempt, self-FKs excluded, consistent database/account restores are unaffected (FK topological order within one restore transaction). Verified live: orphan restore rejected withforeign key constraint+ "fk_p" +would leave orphan row(s) (key value 1), state preserved, retry after repairing the parent succeeds,ERROR 1452on subsequent orphan inserts.publishCreateForwardForeignKeys(pkg/sql/compile/create_fk.go) re-reads each child's current constraint at publish time and merges fresh bindings (keyed by name, idempotent), so theRefChildTableDefregistered between bind and publish survives. The mo-dump-style interleave (CREATE b (fwd ref); INSERT b; CREATE a; INSERT aunderforeign_key_checks=0) now keeps parent-side enforcement:UPDATErejected with 1217,DELETErejected, orphanINSERTwith 1452 — identical to schema-first ordering.Tests: 13 new UTs in pkg/sql/compile (validator 100% coverage, publisher 95.3%, full package suite green; vet/gofmt/molint/golangci-lint/license-eye clean); 2 new deterministic BVT goldens (snapshot-restore orphan rejection; cyclic-FK enforcement matrix incl. mo-dump interleave); 26 existing FK/snapshot/restore goldens replayed byte-identical pristine-vs-fixed; CI green; review approved.
Live verification ran at PR tree
b67d58295cd85101b6949d633f4fbe807a31591a(content unchanged by the main-merge commits), single-node Linux build, binary sha2562f484ba623fcd1e3939180fe87ab206b899471c37af2d188c06ff87f3e7eb118.