Add covering indexes for Machine Tracker searches#4140
Open
adobloug wants to merge 1 commit into
Open
Conversation
Machine Tracker filters arp/cam by ip- or mac-range together with end_time and orders by the same leading column. The stock single-column indexes leave end_time as a post-scan Filter and require heap fetches for the remaining columns. Add (ip, end_time) and (mac, end_time) indexes that fold end_time into the index key and INCLUDE the fetched columns, turning these lookups into near index-only scans and providing the ORDER BY prefix for free. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
adobloug
force-pushed
the
chore/machinetracker-covering-indexes
branch
from
July 14, 2026 06:36
dbf3672 to
923fd0e
Compare
|
lunkwill42
self-requested a review
July 14, 2026 08:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Scope and purpose
Adds covering database indexes on
arpandcamto speed up Machine TrackerIP address and MAC address searches.
The Machine Tracker views (
nav.web.machinetracker) queryarp/camfiltering on an ip- or mac-range together with
end_time, ordered by thatsame leading column:
WHERE ip BETWEEN .. AND end_time > .. ORDER BY ip, mac, start_timeWHERE mac BETWEEN .. AND end_time > .. ORDER BY mac, ip, start_timeWHERE mac BETWEEN .. AND end_time > .. ORDER BY mac, sysname, module, portThe stock single-column indexes (
arp_ip_btree,arp_mac_btree,cam_mac_btree) cover only the leading column, soend_timeis applied as apost-scan
Filter— every historical row in the ip/mac range is read and thendiscarded — and for the IP search the planner instead
BitmapAnds the ip andend_timeindexes, scanning the wholeend_timeindex.This PR adds three indexes that fold
end_timeinto the index key so it becomespart of the
Index Cond, and let the leading column supply theORDER BYprefix for free:
arp_ip_end_time_btree (ip, end_time) INCLUDE (mac, start_time)arp_mac_end_time_btree (mac, end_time)cam_mac_end_time_btree (mac, end_time) INCLUDE (netboxid, port, sysname)Verified on a production database with
EXPLAIN (ANALYZE, BUFFERS): the newindexes are selected,
end_timemoves from a post-scanFilterinto theIndex Cond, and the leading column supplies the sort prefix. The figures citedbelow (e.g. cam MAC search 10 197 → 293 buffers; arp IP search
BitmapAndreplaced by a single index scan, 6 598 → 1 593 buffers) come from one production
database and illustrate the potential gain only — absolute numbers depend on
table size, data distribution and cache state. The
INCLUDEd columns additionally let ad-hocip/mac-over-time reporting queries — those selecting only the included columns —
run as near index-only scans; the Machine Tracker views themselves fetch full
rows, so they gain from the
Index Condand sort-prefix improvements ratherthan from covering.
This pull request
Contributor Checklist
master, this is an addition)How to observe
Machine Tracker's IP/MAC searches (
nav.web.machinetracker) filter anarp/camrow on an ip- or mac-range together withend_time, ordered bythat same leading column, all via
BETWEEN(the cam and arp-MAC searches alsojoin
netboxthroughselect_related). The stock single-column indexes coveronly the leading column, so
end_timeis applied as a post-scanFilter. Thenew indexes fold
end_timeinto the index key — it becomes part of theIndex Cond— and the leading column supplies theORDER BYprefix for free.Note
The figures below are from one production database and illustrate the
potential performance gain only. Absolute numbers vary with table size, data
distribution, cache state and hardware.
Run
EXPLAIN (ANALYZE, BUFFERS)before and afternavsyncdb(substitute a real3-octet OUI for
<oui>, e.g.aa:bb:cc). The cam MAC search, measured on aproduction database over one OUI range — 191 active rows out of ~10 000
historical rows in that range (the
netboxjoin these views add is unaffectedby the new indexes and is omitted here for clarity):
Before —
end_timeis a post-scanFilteroncam_mac_btree, so all10 424 rows in the mac range are read and 10 233 are thrown away:
After —
end_timefolds intoIndex Condoncam_mac_end_time_btree;only the 191 matching rows are visited:
Buffers drop from 10 197 to 293 (~35×), execution time from 77 ms to 1.4 ms
(~53×), and the post-scan
Filterdisappears (no moreRows Removed by Filter).The
arpIP search benefits even more, because without the combined index theplanner resorts to a
BitmapAndofarp_ip_btreeandarp_end_time_btree—and the
end_timeside scans hundreds of thousands of entries. Measured over a/22(1 003 active rows):Before — two-index
BitmapAnd(theend_timebitmap alone scans 639 019rows), followed by a full
Sort:After — a single
arp_ip_end_time_btreescan withend_timein theIndex Condandipsupplying the sort prefix (Incremental Sort):Buffers drop from 6 598 to 1 593 and execution time from 321 ms to 9 ms (~35×).
The
arpMAC search improves the same way viaarp_mac_end_time_btree.INCLUDE columns — ad-hoc reporting
The Machine Tracker views fetch full rows (and join
netbox), so they alwaysvisit the heap; the
INCLUDEd columns do not make those queries index-only.Where
INCLUDEpays off is ad-hoc "locate hosts by ip/mac over time" reportingthat selects only the indexed/included columns — such a query runs as a near
Index Only Scan. For example, a subnet lookup expressed with the<<network-contains operator (which Postgres rewrites into the same btree range
bounds, applying exact containment as a recheck
Filter):(The residual
Heap Fetchesare rows on heap pages not yet marked all-visiblein the visibility map, not columns missing from the index.)
Note
Creating these indexes may take a while during
navsyncdb, as thearpandcamtables are typically large.