Skip to content

Conversation

@7ttp
Copy link
Contributor

@7ttp 7ttp commented Nov 19, 2025

ERROR: relation "pgmq.q_<queue>_msg_id_seq" does not exist

Related issue: #4492


🐛 Root cause

Preview-branch resets can leave the pgmq extension in a partial / inconsistent state, where some extension-managed objects (tables or sequences) are missing.
When migrations later execute pgmq.create(...), the extension expects these objects to exist and fails with missing-relation errors.


✅ What this change does

During remote resets only, this PR:

  • Checks whether the pgmq extension exists
  • If present, runs:
    DROP EXTENSION IF EXISTS pgmq CASCADE
  • Recreates it with:
    CREATE EXTENSION IF NOT EXISTS pgmq
  • Uses a short bounded retry to tolerate transient lock or catalog-contention issues
  • Then continues with the standard down.ResetAll(...) migration flow

This ensures the PGMQ extension is always in a known-good, consistent state before migrations run, eliminating missing-sequence errors.


📌 Notes

  • Change is scoped only to the remote / preview-branch reset path
  • Local development resets are unaffected
  • If the remote Postgres instance disallows extension creation or removal, the reset will fail with a clear error — this is expected and indicates a platform-side configuration issue

🔧 What kind of change does this PR introduce?

Bug fix


🚨 Current behavior

Preview-branch resets may leave the PGMQ extension in a partial state, causing migrations such as:

SELECT pgmq.create('my_queue');
SELECT pgmq.create('my_queue_dlq');

to fail with missing-relation errors.

More details: #4492


🎉 New behavior

Preview-branch resets now consistently rebuild the PGMQ extension, ensuring all pgmq.create(...) migrations run successfully without errors.


📎 Additional context

  • Issue occurs only on preview-branch resets (first builds and new commits work normally)
  • Verified locally using a disposable Postgres container

…ing-sequence errors

On preview branch resets, migrations that call pgmq.create(...) could fail with:
  ERROR: relation "pgmq.q_<queue>_msg_id_seq" does not exist

Root cause: reset left the DB in a partial extension state. This change ensures that,
during remote resets, we drop and recreate the pgmq extension (DROP ... CASCADE and
CREATE ... IF NOT EXISTS) with a bounded retry before applying migrations. This
puts extension-managed objects into a consistent state and prevents the sequence
race.

Notes:
- This is scoped to the remote reset flow.
- If the remote Postgres does not allow CREATE/DROP EXTENSION or lacks pgmq files,
  CREATE EXTENSION will fail and the reset will abort; this is expected and requires
  platform intervention.
@7ttp 7ttp requested a review from a team as a code owner November 19, 2025 06:54
@7ttp 7ttp changed the title fix(reset): recreate pgmq extension during remote reset to avoid miss… fix(reset): ensure pgmq extension is fully rebuilt during remote resets Nov 19, 2025
@coveralls
Copy link

coveralls commented Nov 19, 2025

Pull Request Test Coverage Report for Build 19625585658

Details

  • 1 of 24 (4.17%) changed or added relevant lines in 1 file are covered.
  • 6 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.1%) to 54.979%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/db/reset/reset.go 1 24 4.17%
Files with Coverage Reduction New Missed Lines %
internal/db/reset/reset.go 1 63.85%
internal/gen/keys/keys.go 5 12.9%
Totals Coverage Status
Change from base Build 19620063801: -0.1%
Covered Lines: 6531
Relevant Lines: 11879

💛 - Coveralls

@7ttp 7ttp marked this pull request as draft November 19, 2025 07:07
@sweatybridge
Copy link
Contributor

sweatybridge commented Nov 19, 2025

You should enable the pgmq extension from your migration files. For eg. supabase/migrations/<timestamp>_name.sql

create extension if not exists pgmq;

select pgmq.create('my_queue');

@7ttp
Copy link
Contributor Author

7ttp commented Nov 19, 2025

thanks @sweatybridge for the heads-up! I haven’t added the migration yet but I’ll include the pgmq extension setup:

CREATE EXTENSION IF NOT EXISTS pgmq;
SELECT pgmq.create('my_queue');

I’ll push the update shortly.

@sweatybridge
Copy link
Contributor

Cool, all extension related setup should be enabled through user created migration files. I will probably close this PR if you can confirm that the above comment works.

@ToJen
Copy link

ToJen commented Nov 20, 2025

@sweatybridge unfortunately adding create extension in the migrations is not enough. it will still fail after a reset. @7ttp what was the outcome for you?

@7ttp
Copy link
Contributor Author

7ttp commented Nov 20, 2025

i tested it again just to be sure, adding create extension if not exists pgmq in the migrations wasn’t enough on my side either. after a remote/preview reset the extension objects were still in a partial state, so the next pgmq.create(...) call failed with the same missing-relation error. @ToJen @sweatybridge

@ToJen
Copy link

ToJen commented Nov 21, 2025

@sweatybridge

@7ttp
Copy link
Contributor Author

7ttp commented Nov 22, 2025

@ToJen @sweatybridge I’m working on the fixes now. I’ll clean up the gofmt formatting, add the missing migration so the recommendation is covered, and update the PR to align with the feedback. I’ll push the updates shortly.

@7ttp 7ttp marked this pull request as ready for review November 22, 2025 12:21
@7ttp
Copy link
Contributor Author

7ttp commented Nov 22, 2025

@sweatybridge Could you take a look at this when you have a moment?

@sweatybridge
Copy link
Contributor

I don't think it's the CLI's responsibility to customise user's migrations for any specific extensions. So I will go ahead and close this PR.

I will follow up on the original issue to resolve any errors you are facing #4492

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