Skip to content

Relocate source tablespaces inside shadow's data dir - #322

Open
timmclaughlin wants to merge 1 commit into
ClickHouse:mainfrom
timmclaughlin:fix/bootstrap-tablespaces
Open

timmclaughlin wants to merge 1 commit into
ClickHouse:mainfrom
timmclaughlin:fix/bootstrap-tablespaces

Conversation

@timmclaughlin

Copy link
Copy Markdown
Contributor

Summary

An object-store bootstrap of a source with a user tablespace (only temp files in it) completed, but the first shadow start failed and kept failing.

With tablespace_map in the data dir:

FATAL:  could not remove directory "pg_tblspc/61160": Directory not empty

With the map moved aside, recovery reached consistency and then:

PANIC:  unexpected directory entry "61160" found in pg_tblspc
DETAIL:  All directory entries in pg_tblspc/ should be symbolic links.

Root cause

  • Object store: wal-g-layout parts carry tablespace files as pg_tblspc/<oid>/PG_<ver>_<cat>/…, so extraction writes a real directory there, plus a tablespace_map line <oid> <source location>.
  • Backup recovery: PostgreSQL replaces each mapped pg_tblspc entry with a symlink to the map's location. It can only rmdir the entry, which fails on the populated directory. The location doesn't exist on the walshadow host in any case.
  • Without the map: PG 15+ requires every pg_tblspc entry to be a symlink once recovery is consistent, unless allow_in_place_tablespaces is set.
  • Direct mode: BASE_BACKUP (TABLESPACE_MAP true) sends each tablespace as its own archive, rooted at the tablespace. DirectSource pumped those over the data dir root (<shadow>/PG_18_…/<db>/…), next to a tablespace_map that pointed recovery at the source location.

Fix

  • Relocate after extraction: a new shadow_tablespaces::relocate runs in spawn_greenfield_bootstrap once source.run returns. For each tablespace named by the map or present under pg_tblspc:
    • move pg_tblspc/<oid> to walshadow_tblspc/<oid> inside the shadow data dir;
    • link pg_tblspc/<oid> → ../walshadow_tblspc/<oid>. The link is relative, so the data dir can still be moved or remounted;
    • give a mapped tablespace that shipped no files an empty home, which redo fills.
  • Retire the map: tablespace_map is renamed to tablespace_map.old, which is what recovery itself does after applying it. Links land and are fsynced first.
  • Safe to rerun: an entry already linked to its home is left alone. A symlink pointing anywhere else is refused, since files under it never reached shadow.
  • Direct mode: a tablespace archive now lands under pg_tblspc/<oid>/ (PumpTarget::for_tablespace), the same layout as object store. Both modes then go through the same relocation.
  • Re-bootstrap: keeping the home inside the data dir means discard_partial removes it along with everything else.

Not changed: rows of tables stored in a non-default tablespace are still not page-walked, and replaying a CREATE TABLESPACE issued after bootstrap still targets the source path. docs/limitations.md now states both.

Testing

  • Unit tests (shadow_tablespaces): a landed tablespace moves home and the map retires, and a second pass finds the link already in place; a mapped tablespace with no files gets an empty home; a dir with no tablespaces is left untouched; foreign links and malformed maps are refused.
  • backup_source::tablespace_archive_lands_under_pg_tblspc
  • tests/bootstrap_tablespace_e2e.rs (PG 18): the source has a tablespace holding a table. Bootstrap via DirectSource and via ObjectStoreSource (a wal-rus push). Then stop the source, remove its tablespace location, and recover each shadow from archived WAL. Each reaches hot standby and reads the table from the relocated tablespace. With relocation disabled the test fails with the same could not remove directory "pg_tblspc/<oid>" FATAL.
  • bootstrap_direct_e2e and bootstrap_object_store_e2e still pass.
  • cargo fmt --check, cargo clippy --workspace --all-targets -D warnings and cargo test --lib pass, apart from the two existing macOS-only failures (sync_filesystem_flushes_existing_dir_only, skips_files_with_non_utf8_names).
  • Tested on macOS only; not yet run against a production-size backup.

🤖 Generated with Claude Code

Backups land a user tablespace as a real pg_tblspc/<oid> directory beside
a tablespace_map naming the source location. Shadow recovery then either
fails removing that directory to re-point it, or panics at consistency on
a non-symlink pg_tblspc entry. After extraction, move each tablespace
under walshadow_tblspc/<oid>, link it back by relative path, and retire
the map as tablespace_map.old.

Direct BASE_BACKUP tablespace archives landed over the data dir root;
root them at pg_tblspc/<oid>/ to match the object-store layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@serprex
serprex force-pushed the fix/bootstrap-tablespaces branch from ec12a40 to a4e5d82 Compare November 23, 2026 08:44
@serprex

serprex commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

this is somewhat incompatible with existing plan https://github.com/ClickHouse/walshadow/blob/main/plans/tablespaces.md

this hasn't been prioritized, but does #340 address your needs?

@serprex serprex mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants