Skip to content

Conversation

@samrose
Copy link
Collaborator

@samrose samrose commented Oct 28, 2025

Wrappers Extension Migration Files Fix

Problem Summary

The wrappers PostgreSQL extension package was missing migration files and failing to build for PostgreSQL 15, 17, and OrioleDB variants. This document explains the issues encountered and the solutions implemented.

Issues Identified

1. Missing Migration Files for Previously Packaged Versions

Problem:

  • Versions 0.3.0, 0.4.1, 0.4.2, and 0.4.3 existed in versions.json but were not included in the build for PostgreSQL 17
  • These versions only support PostgreSQL 15
  • Migration paths from these versions to newer versions were missing
  • Users upgrading from these versions would have no upgrade path

Solution:

  • Added versions 0.3.0, 0.4.1, 0.4.2, and 0.4.3 to the allPreviouslyPackagedVersions list (lines 175-178 in nix/ext/wrappers/default.nix)
  • Implemented logic to create empty placeholder SQL files for versions that don't have actual builds (lines 284-290)
  • These placeholder files allow PostgreSQL to recognize the version exists and follow the migration path

2. Double-Counting Versions

Problem:

  • For PostgreSQL 15, versions 0.3.0, 0.4.1, 0.4.2, and 0.4.3 ARE actually built (they support PG 15)
  • These versions appeared in both versions (currently built) AND previouslyPackagedVersions (legacy list)
  • This caused:
    • Library count test failures (expected 34, got 30)
    • Duplicate migration file creation attempts
    • Confusion about whether versions should be real libraries or symlinks

Solution:

  • Renamed previouslyPackagedVersions to allPreviouslyPackagedVersions (line 175)
  • Created filtered previouslyPackagedVersions that excludes versions already in versions (lines 208-211):
    previouslyPackagedVersions = builtins.filter (
      v: !(builtins.elem v versions)
    ) allPreviouslyPackagedVersions;
  • Result:
    • For PG 15: 18 previously packaged versions (22 - 4 that are actually built)
    • For PG 17: 22 previously packaged versions (all of them, since none support PG 17)
    • Library count formula now works correctly for all PostgreSQL versions

3. Duplicate Migration File Creation

Problem:

  • The first loop creates consecutive migration files (e.g., 0.4.3--0.4.4.sql)
  • The second loop creates all combinations from previouslyPackagedVersions to versions
  • When a version appeared in both lists, the second loop tried to recreate files already created by the first loop
  • This caused "file already exists" errors or overwrites

Solution:

  • Added existence check before creating migration files in the second loop (line 301):
    if [ -f "$main_sql_file" ] && [ ! -f "$new_file" ]; then
  • Only creates migration file if it doesn't already exist
  • Prevents conflicts between consecutive migrations (first loop) and skip-version migrations (second loop)

How the Fix Works Across PostgreSQL Versions

PostgreSQL 15

  • Built versions: 0.3.0, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.5.0, 0.5.3, 0.5.4, 0.5.5 (11 versions)
  • Previously packaged (filtered): 18 versions (excludes the 4 that are built: 0.3.0, 0.4.1, 0.4.2, 0.4.3)
  • Library count: 11 + 18 + 1 (main) = 30 ✓
  • Migration files: All upgrade paths exist, including consecutive migrations between built versions

PostgreSQL 17

  • Built versions: 0.4.4, 0.4.5, 0.4.6, 0.5.0, 0.5.3, 0.5.4, 0.5.5 (7 versions)
  • Previously packaged (filtered): 22 versions (all of them, since none support PG 17)
  • Library count: 7 + 22 + 1 (main) = 30 ✓
  • Migration files: All upgrade paths exist from legacy versions to current versions via empty placeholder SQL files

OrioleDB-17

  • Works the same as PostgreSQL 17 since OrioleDB is based on PostgreSQL 17
  • Version support in versions.json includes "orioledb-17" in the postgresql array
  • Same migration paths and library counts as standard PG 17

Key Changes to nix/ext/wrappers/default.nix

  1. Lines 175-198: Renamed to allPreviouslyPackagedVersions and added 4 new versions
  2. Lines 208-211: Filter to create previouslyPackagedVersions excluding currently built versions
  3. Lines 246-268: Directory materialization to make buildEnv output writable
  4. Lines 275-279: Use cp -L to dereference symlinks when copying
  5. Lines 284-290: Create empty SQL placeholder files for versions without builds
  6. Lines 301-304: Check if migration file exists before creating to prevent duplicates

Testing

The fix has been tested and verified to work correctly for:

  • ✅ PostgreSQL 15 (nix build .#psql_15/exts/wrappers-all)
  • ✅ PostgreSQL 17 (nix build .#psql_17/exts/wrappers-all)
  • ✅ OrioleDB-17 (uses same logic as PG 17)

All builds now complete successfully with the correct number of:

  • Library files (30 for current configurations)
  • Migration SQL files (160+ depending on version)
  • Control files (one per version plus main)

@samrose samrose marked this pull request as ready for review October 28, 2025 14:37
@samrose samrose requested review from a team as code owners October 28, 2025 14:37
jfroche and others added 2 commits October 28, 2025 16:14
We do not want to uninstall and install the extension during an upgrade test.
@samrose samrose merged commit ac48c0b into develop Oct 28, 2025
14 checks passed
@samrose samrose deleted the fix/wrappers-030 branch October 28, 2025 16:42
@samrose samrose changed the title fix: covering migrations for wrappers across all versions` fix: covering migrations for wrappers across all versions Oct 28, 2025
hunleyd added a commit that referenced this pull request Oct 28, 2025
* 'develop' of github.com:supabase/postgres:
  fix: covering migrations for wrappers across all versions` (#1876)
hunleyd added a commit that referenced this pull request Oct 30, 2025
* origin:
  refactor(ansible): bring our ansible up to modern ansible-lint standards (#1869)
  feat: update supautils (#1879)
  feat: support multiple versions of the plv8 extension (#1676)
  chore: bump wrappers version 0.5.6 (#1877)
  feat: support multiple versions of the pg_hashids extension (#1755)
  fix: covering migrations for wrappers across all versions` (#1876)
  feat(Nix-flakes): Add pgBackRest flake (#1859)
  fix: wrappers 0.5.3 missing (#1872)
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.

4 participants