Skip to content

Fix #3010: Optimize pgr_separateTouching performance#3081

Open
Mohit242-bit wants to merge 4 commits intopgRouting:developfrom
Mohit242-bit:fix/separateTouching-performance-3010
Open

Fix #3010: Optimize pgr_separateTouching performance#3081
Mohit242-bit wants to merge 4 commits intopgRouting:developfrom
Mohit242-bit:fix/separateTouching-performance-3010

Conversation

@Mohit242-bit
Copy link
Contributor

@Mohit242-bit Mohit242-bit commented Feb 25, 2026

Fixes #3010 .

Problem

pgr_separateTouching calls ST_StartPoint() / ST_EndPoint() inside the WHERE clause of a cross-join, resulting in up to 8 redundant PostGIS function calls per edge pair. On large networks this causes severe performance degradation:

Changes proposed in this pull request:

  • Pre-compute ST_StartPoint(geom) and ST_EndPoint(geom) once per edge as columns in the edges_table CTE
  • Replace repeated function calls with column comparisons in get_touching CTE
  • Replace repeated function calls with column comparisons in touchings CTE
  • Update [docqueries/utilities/separateTouching.result] to reflect the new dryrun output

Performance impact

The optimization is mathematically equivalent (same input → same output) but eliminates millions of redundant function calls on large datasets.
Reported speedup (by issue reporter using this approach):

  • Washington D.C.: 45 min → 2 seconds (~1350x faster)
  • Colorado: 48+ hours → 12 minutes

Testing

-> Manual SQL verification: returns identical results (edge 14 split into 2 segments)
-> dryrun => true confirms optimized query structure
-> All 8 pgTAP tests pass (26 assertions): edge_cases, types_check, issue_280, issue_623, issue_1009, issue_1074, issue_1336, issue_1882
@pgRouting/admins

Summary by CodeRabbit

  • Bug Fixes
    • More accurate touching-edge detection by comparing explicit start/end points to determine adjacency.
    • Performance improvements from precomputing endpoint values for reuse.
  • Documentation
  • Localization
    • Added translation and POT entries to include the new issue note.

Copilot AI review requested due to automatic review settings February 25, 2026 11:33
Copy link

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

Optimizes pgr_separateTouching by reducing redundant PostGIS function calls in hot-path CTEs, improving performance on large edge sets while keeping results equivalent.

Changes:

  • Precomputes ST_StartPoint(geom) / ST_EndPoint(geom) once per edge in edges_table
  • Replaces repeated endpoint function calls with column comparisons in downstream CTEs
  • Updates the dry-run expected output to match the new query shape

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
sql/utilities/separateTouching.sql Precomputes endpoints in a CTE and reuses them to avoid repeated PostGIS calls
docqueries/utilities/separateTouching.result Updates expected dry-run output to reflect the optimized query

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cade7fd and 1c8d3e6.

📒 Files selected for processing (4)
  • NEWS.md
  • doc/src/release_notes.rst
  • locale/en/LC_MESSAGES/pgrouting_doc_strings.po
  • locale/pot/pgrouting_doc_strings.pot

Walkthrough

Precompute and expose edge start/end points (startpoint, endpoint) in the edges table; propagate them as sp1/ep1 in get_touching and replace repeated ST_StartPoint/ST_EndPoint calls with direct column comparisons in touching-filter and intersection logic.

Changes

Cohort / File(s) Summary
SQL Implementation
sql/utilities/separateTouching.sql
Add startpoint/endpoint columns to edges_table, expose as sp1/ep1 in get_touching, and replace ST_StartPoint/ST_EndPoint function calls with direct column comparisons in touching and ST_Intersection checks.
Result/Test File
docqueries/utilities/separateTouching.result
Update expected output to include sp1 and ep1 fields and reflect revised touching-filter logic.
Release & Localization
NEWS.md, doc/src/release_notes.rst, locale/en/LC_MESSAGES/pgrouting_doc_strings.po, locale/pot/pgrouting_doc_strings.pot
Add release-note/translation entries for issue #3010 and update POT/locale entries for the new release note.

Sequence Diagram(s)

(omitted — changes are localized SQL optimization without multi-component sequential interactions needing visualization)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

utility

Suggested reviewers

  • cvvergara
  • robe2
  • iosefa

Poem

🐰 I cached my hops at start and end,

No more repeated calls that bend.
Endpoints held in tidy rows,
Faster runs where the pathway goes.
Hooray — my routes now zip and mend!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main optimization: fixing issue #3010 by optimizing pgr_separateTouching performance through pre-computation of start/end points instead of repeated function calls.
Linked Issues check ✅ Passed The code changes comprehensively address all requirements from issue #3010: pre-compute ST_StartPoint/ST_EndPoint as columns in edges_table CTE, replace repeated function calls with column comparisons in get_touching and touchings CTEs, and preserve functional equivalence while eliminating redundant calls.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the performance issue in pgr_separateTouching. Documentation updates (NEWS.md, release_notes.rst, locale files) and the result file update are appropriate supporting changes, with no extraneous modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@sql/utilities/separateTouching.sql`:
- Around line 78-83: The query is recomputing ST_Intersection(geom, g2) multiple
times; compute it once in the SELECT (e.g., add ST_Intersection(geom, g2) AS
intersection or point) and then reference that alias in the WHERE predicate
instead of calling ST_Intersection again. Update the SELECT (in the get_touching
usage) to produce the computed column (intersection/point) and change the WHERE
clause conditions that compare to sp1/ep1 and the later OR branch to use that
alias (and keep id1, g1, g2 as before).

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4912a28 and ba75d71.

📒 Files selected for processing (2)
  • docqueries/utilities/separateTouching.result
  • sql/utilities/separateTouching.sql

Copy link
Member

@cvvergara cvvergara left a comment

Choose a reason for hiding this comment

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

@Mohit242-bit
Can you update the release notes and news please

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@NEWS.md`:
- Line 37: The NEWS.md entry was edited directly; instead add the release note
for pgr_separateTouching() / issue `#3010` into the generator's source (the
file(s) consumed by tools/release-scripts/notes2news.pl), then run the
notes2news.pl generator to regenerate NEWS.md so the change is preserved;
reference the pgr_separateTouching() / `#3010` text when adding the new source
entry so the generated line matches the intended release note.

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ba75d71 and 391b844.

📒 Files selected for processing (4)
  • NEWS.md
  • doc/src/release_notes.rst
  • locale/en/LC_MESSAGES/pgrouting_doc_strings.po
  • locale/pot/pgrouting_doc_strings.pot

@Mohit242-bit Mohit242-bit force-pushed the fix/separateTouching-performance-3010 branch from 391b844 to f3fe1e8 Compare March 2, 2026 03:48
@Mohit242-bit Mohit242-bit requested a review from cvvergara March 2, 2026 04:11
@Mohit242-bit
Copy link
Contributor Author

I have updated the news and release notes files. I would appreciate a review when you have time.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance issue with pgr_separateTouching()

3 participants