Skip to content

[JIM-349] Parallel import of multiple projects fails with "PG::QueryCancelled" - #27363

Open
ammancilla wants to merge 2 commits into
release/18.0from
bug/jim-349-parallel-import-of-multiple-projects-fails-with-pg-querycancelled
Open

ammancilla wants to merge 2 commits into
release/18.0from
bug/jim-349-parallel-import-of-multiple-projects-fails-with-pg-querycancelled

Conversation

@ammancilla

@ammancilla ammancilla commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Ticket

https://community.openproject.org/wp/JIM-349

What are you trying to accomplish?

Fix statement timeouts (PG::QueryCanceled) when several Jira projects are imported at the same time.

What approach did you choose and why?

The problem: each Jira issue is imported in one transaction. When that transaction writes a row that the whole import shares, the row stays locked until the transaction commits, so issues from other projects being imported in parallel that need the same row have to wait. The import did such writes in two places:

  • Reference rows: every issue upserted the import's rows in jira_open_project_references for its type, status and priority.
  • The workflow: every issue rewrote the workflow of its type, deleting the type's status transitions and inserting them again.

Most issues across projects use the same few types, statuses and priorities, so with many projects the jobs end up waiting for each other until a wait goes past the statement timeout.

The fix keeps the current import structure, but stops writing those import-wide shared rows on every issue:

  • Reference rows: the upsert uses ON CONFLICT DO NOTHING instead of ON CONFLICT DO UPDATE. The first write is final, since an entity is referenced in the same transaction that creates it.
  • The workflow: its creation moves out of the per-issue transaction into one job that runs after the work packages. Each issue only adds its status to its type's workflow when it's missing.

Results

The import was run on a large dataset (364 projects, 36,016 issues) with 20 worker threads and a 90 s statement timeout, before and after applying the fix, with the following results:

Before After
Statement timeouts 3 in the first 4 min 0
Threads waiting on a lock (average) 18.2 0
Work packages per minute ~90 ~830
Stage 8 ~7 h (estimated) 43 min

AI involvement

Collaborative – AI generated a substantial part of the code; I reviewed and understand every line.

Merge checklist

  • Added/updated tests

@github-actions github-actions Bot added the ai: Collaborative 💻 AI generated a substantial part of the code; A human reviewed and understands every line. label Nov 26, 2026
@github-actions

Copy link
Copy Markdown

Warning

Flaky specs

  • rspec ./spec/features/work_packages/details/relations/hierarchy_spec.rb[2:1:2:1:2:1]
🤖 Ask Copilot to investigate

Copy the prompt below into a new comment on this PR to delegate the investigation to GitHub Copilot. It will look into the flakiness and open a separate pull request with you as reviewer.

@copilot The following spec(s) are flaky in CI (first seen on PR #27363, linked for reference only):

- `rspec ./spec/features/work_packages/details/relations/hierarchy_spec.rb[2:1:2:1:2:1]`

Treat this as a standalone task, unrelated to PR #27363. Create a new branch from origin/dev and open a new pull request targeting dev — do not stack it on PR #27363 or reuse that branch.

Follow the playbook in docs/development/testing/handling-flaky-tests/README.md to find the root cause and fix the underlying race — do not skip, delete, or weaken the spec to make it pass; disabling is a last resort per the playbook, and only with a bug ticket. Verify the fix by running the spec(s) repeatedly (e.g. `script/bulk_run_rspec --run-count 10`).

If you cannot reproduce the flake or are not confident in a fix after reasonable investigation, do not fabricate a change or skip the spec to force CI green. Instead, leave the pull request in draft and document what you tried, the suspected cause, and any leads in its description, then assign @ammancilla to take over.

Once the fix is verified, title the PR after the spec(s) it fixes, and use the PR description to explain the root cause, how the change resolves it, and the before/after results. Label the PR `flaky-spec`, assign @ammancilla, and request a review from @ammancilla.
On every commit, set @ammancilla as the sole co-author with a `Co-authored-by:` trailer (use their GitHub no-reply email so it links to their account), so it is traceable who dispatched the fix.

@ammancilla
ammancilla force-pushed the bug/jim-349-parallel-import-of-multiple-projects-fails-with-pg-querycancelled branch 4 times, most recently from e151046 to 436fa37 Compare November 26, 2026 15:56
@ammancilla
ammancilla force-pushed the bug/jim-349-parallel-import-of-multiple-projects-fails-with-pg-querycancelled branch from 436fa37 to 4cad3fd Compare November 27, 2026 08:22
Comment on lines +280 to +290
def ensure_type_has_custom_fields(type, jira_issue)
return if new_custom_fields_in_type(jira_issue, type, @custom_field_registry).empty?

form = type.default_variant.form_configuration
OpenProject::Mutex.with_advisory_lock_transaction(form, WorkPackageTypes::FormConfiguration::LayoutLock::SUFFIX) do
type.default_variant.reload
new_custom_fields = new_custom_fields_in_type(jira_issue, type, @custom_field_registry)
next if new_custom_fields.empty?

update_custom_fields_in_type(type, new_custom_fields)
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a second race condition I found, when importing a large remote Jira intance (363 projects, 42k issues). Added the fix as a drive-by.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai: Collaborative 💻 AI generated a substantial part of the code; A human reviewed and understands every line.

Development

Successfully merging this pull request may close these issues.

1 participant