Skip to content

fix(ci): terminate db sessions before DROP DATABASE in hogli#60608

Merged
webjunkie merged 2 commits into
masterfrom
mendral/fix-drop-database-terminate-sessions
May 29, 2026
Merged

fix(ci): terminate db sessions before DROP DATABASE in hogli#60608
webjunkie merged 2 commits into
masterfrom
mendral/fix-drop-database-terminate-sessions

Conversation

@mendral-app
Copy link
Copy Markdown
Contributor

@mendral-app mendral-app Bot commented May 29, 2026

Summary

  • Fixes flaky CI failures where DROP DATABASE fails with database "posthog" is being accessed by other users because idle PostgreSQL connections remain from earlier setup steps.
  • Adds pg_terminate_backend() call before DROP DATABASE in the hogli _recreate_database helper, matching the existing pattern in posthog/ducklake/common.py.

Reported in multiple PRs today — affects Integration Tests and MCP test setup.


Note

Created by Mendral. Tag @mendral-app with feedback or questions.

DROP DATABASE fails with 'database posthog is being accessed by other
users' when idle connections remain from earlier CI steps. Add
pg_terminate_backend() call before DROP to clear lingering sessions.

This mirrors the existing pattern in posthog/ducklake/common.py.
@mendral-app mendral-app Bot requested a review from webjunkie May 29, 2026 07:08
@webjunkie webjunkie marked this pull request as ready for review May 29, 2026 07:13
Copilot AI review requested due to automatic review settings May 29, 2026 07:13
@assign-reviewers-posthog assign-reviewers-posthog Bot requested a review from a team May 29, 2026 07:13
@webjunkie
Copy link
Copy Markdown
Contributor

@mendral-app if this affects certain tests, shouldn't we widen the path filters to include them here?

also fix test_restore_schema_dump_recreate_cleans_up_after_failure

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 29, 2026

Reviews (1): Last reviewed commit: "fix(ci): terminate db sessions before DR..." | Re-trigger Greptile

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 257a1bdb90

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +161 to +162
f"SELECT pg_terminate_backend(pid) FROM pg_stat_activity "
f"WHERE datname = '{target_db}' AND pid <> pg_backend_pid();"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update cleanup expectations for termination queries

When restore_schema_dump(..., recreate=True) fails after starting the restore, the exception handler calls _recreate_database() a second time, so this new admin statement is now emitted twice. The existing tools/hogli-commands/hogli_commands/tests/test_db_schema.py::test_restore_schema_dump_recreate_cleans_up_after_failure still asserts the exact admin SQL sequence is only DROP/CREATE twice, so the hogli command test suite will fail until that expectation includes the termination statements.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes flaky CI DROP DATABASE failures in hogli's _recreate_database by terminating other Postgres sessions on the target DB before dropping, mirroring the existing pattern in posthog/ducklake/common.py.

Changes:

  • Add SELECT pg_terminate_backend(...) call in _recreate_database before DROP DATABASE / CREATE DATABASE.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +160 to +163
_psql_admin(
f"SELECT pg_terminate_backend(pid) FROM pg_stat_activity "
f"WHERE datname = '{target_db}' AND pid <> pg_backend_pid();"
)
@mendral-app
Copy link
Copy Markdown
Contributor Author

mendral-app Bot commented May 29, 2026

Good catch on the test! I've pushed a fix in 341927atest_restore_schema_dump_recreate_cleans_up_after_failure now expects the pg_terminate_backend SQL call before each DROP/CREATE pair.

Regarding path filters: the CI workflow (.github/workflows/ci-python.yml) already includes 'tools/hogli-commands/hogli_commands/**' which covers both the source and tests directory, so no widening is needed here — the hogli tests will run whenever this code changes.

CI is green (the only failure is an unrelated Semgrep Package Managers check).

@webjunkie webjunkie merged commit 1134fee into master May 29, 2026
143 of 144 checks passed
@webjunkie webjunkie deleted the mendral/fix-drop-database-terminate-sessions branch May 29, 2026 07:29
@webjunkie
Copy link
Copy Markdown
Contributor

@mendral-app what I meant was you said "affects Integration Tests and MCP test setup."

But adjusting this hogli command does not have a blast radius of actually testing that. Hence my suggestion to widen path filters. Assess, decide and open follow up PR. This one here has been merged.

@deployment-status-posthog
Copy link
Copy Markdown

deployment-status-posthog Bot commented May 29, 2026

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-05-29 07:52 UTC Run
prod-us ✅ Deployed 2026-05-29 08:11 UTC Run
prod-eu ✅ Deployed 2026-05-29 08:10 UTC Run

@mendral-app
Copy link
Copy Markdown
Contributor Author

mendral-app Bot commented May 29, 2026

Good point — you're right that changes to tools/hogli-commands/ didn't actually trigger the workflows that use those db commands.

I've opened a follow-up PR to widen the path filters: #60612

It adds tools/hogli-commands/hogli_commands/**, tools/hogli/**, and hogli.yaml to the path filters for ci-backend, ci-mcp, and ci-dagster — the three workflows that call hogli db commands (db:restore-test-db / db:restore-schema-fresh).

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