TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Creating PostgreSQL Arrays Without A Quadratic Blowup

26 pointsby drobabout 11 years ago

5 comments

danbrucabout 11 years ago
<i>A lot of languages handle this idiom more gracefully.</i><p>Actually they don&#x27;t. You can not resize a (static) array. What you really want is a dynamic array, an array list or whatever you like to call it. In this case you can double the size of the underlying array when you have to reallocate it and get the amortized runtime down but at the price of using up to twice the memory you need.<p>Maybe array_append is to blame because its pure existence somewhat implies that you are working with array lists and not with arrays.
评论 #7482226 未加载
kylloabout 11 years ago
<i>result := array_append(result, (&#x27;num=&gt;&#x27; || i)::HSTORE);</i><p>Yeah, I&#x27;ve never read a line of PLPGSQL before this, but I can tell by the assignment operator := that this is obviously creating a copy of the array every time you add an element to the array. This would also cause a quadratic blowup and&#x2F;or a memory blowup in any other language I&#x27;ve heard of, not just PLPGSQL. Anyone who has taken an introductory algorithms &amp; data structures class really should know better than to write code like this.
评论 #7482963 未加载
ScottBursonabout 11 years ago
I think highly of Postgres, but this is disappointing. It&#x27;s well-known now how to implement functional array-like data structures with log-time concatenation. For a good summary see [0].<p>I think these techniques should be standard practice in programming language implementations.<p>[0] <a href="http://stackoverflow.com/questions/3271256/implement-an-immutable-deque-as-a-balanced-binary-tree/3274355#3274355" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3271256&#x2F;implement-an-immu...</a>
评论 #7484907 未加载
klodolphabout 11 years ago
Seems guilty of trying to do imperative programming in a relational database, then wondering why the performance is not good. Why use an array and not rows in a table?
评论 #7482234 未加载
dsugarmanabout 11 years ago
this is classic OO programmer programming SQL
评论 #7487960 未加载
评论 #7482852 未加载