Home / Companies / Heap / Blog / Post Details
Content Deep Dive

Creating PostgreSQL Arrays Without A Quadratic Blowup

Blog post from Heap

Post Details
Company
Date Published
Author
Dan Robinson
Word Count
561
Company Posts That Month
2
Language
English
Hacker News Points
-
Post removed?
No
Summary

Heap relies on PostgreSQL for backend processing, storing events as hstore blobs and maintaining arrays of user events to enhance performance in funnel queries. The company discovered inefficiencies in a PostgreSQL function that used array_append in loops, leading to quadratic runtime increases, as evidenced by a function that took 36 seconds to generate a 100,000-element array. The inefficiency arises because PostgreSQL creates a new array and copies data with each append operation, unlike other languages that handle such operations more efficiently. The solution involves using the array_agg function, which aggregates a set into an array in linear time, drastically reducing the runtime to 300 milliseconds for the same task. This experience emphasizes the need to avoid procedural idioms in relational databases and highlights the importance of understanding expected runtimes for operations. Heap encourages feedback and further PostgreSQL discussions, hinting at future content about their use of Citus Data.

Trends Found in this Post

No tracked trend matches for this post yet.

Use This Data

Use this post, company, and trend context to find content marketing opportunities, perform competitive analysis, or address product feature gaps via the Plushcap MCP server or the Plushcap API.