db: faster ptr() as an inlinable SQL function - #469
Merged
Merged
Conversation
Same body as serveradmin_extras migration 0006_ptr_function: expands the address from the inet_send() bytes instead of a plpgsql regex loop. Returns identical text for every address in server and server_inet_attribute, runs 4.8x faster (465 ms vs 2251 ms over 41845 inet attributes) and is inlined by the planner, which makes the powerdns_records view's PTR branches cheap. The expression indexes below stay valid since the results do not change.
kofrezo
approved these changes
Sep 21, 2026
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.
Faster
public.ptr()as an inlinable SQL functiondb/ptr.sqlshipspublic.ptr(inet), which builds the reverse DNS name(
x.x.x.x.in-addr.arpa/nibbles.ip6.arpa) of an address and backs theexpression indexes
server_ptr_idxandserver_inet_attribute_ptr_idx.It was a plpgsql function that split
host()output with regexes, loopedover the IPv6 groups and re-assembled the labels with
string_agg, whichmade it expensive per row: any query that computes it for many rows, such
as a view exposing PTR records, spent most of its time inside
ptr().Change
Rewrite it as a
LANGUAGE sql IMMUTABLE PARALLEL SAFEfunction that readsthe address bytes from
inet_send()(4 header bytes, then the address):get_byte(), reversed, plus.in-addr.arpa.encode(..., 'hex')gives the 32 fully expanded nibbles, thenreverse()and oneregexp_replaceto insert the dots, plus.ip6.arpa.The body has no
STRICT, no sub-SELECTand only immutable calls, whichis what lets Postgres inline it into the calling query instead of calling a
function per row. The netmask is ignored as before (
host()did the same).ptr(NULL)still returnsNULL.Results
Measured on a staging database with 41,845 rows in
server_inet_attribute(26,602 IPv6, 15,243 IPv4), single worker, everything in shared buffers:
SELECT ptr(value) FROM server_inet_attributeEquivalence was checked over every address in
server.intern_ipandserver_inet_attribute.value:Because the output is byte-identical, the existing expression indexes stay
valid.
CREATE OR REPLACEswaps the body in place on databases thatalready have the function.