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.

Debugging random slow writes in PostgreSQL

136 pointsby akbarnamaabout 4 years ago

8 comments

plasmaabout 4 years ago
In case anyone’s an SQL Server user and you have a regular app query suddenly timing out every so often on a large table which otherwise takes 10ms, you’ll probably be experiencing the statistic updates penalty.<p>When MSSQL decides it wants to recheck statistics for a regularly performed query, it picks the Nth “lucky” execution of your otherwise fast query to instead now update table &#x2F;index statistics.<p>To observers, this query is suddenly super slow and may even time out. Very confusing.<p>A workaround is to enable “async statistics update”, this won’t reoccur.
评论 #27164744 未加载
评论 #27163002 未加载
mulanderabout 4 years ago
We have been bitten by the same behavior. I gave a talk with a friend about this exact topic (diagnosing GIN pending list updates) at PGCon 2019 in Ottawa[1][2].<p>What you need to know is that the pending list will be merged with the main b-tree during several operations. Only one of them is so extremely critical for your insert performance - that is during actual insert. Both vacuum and autovacuum (including autovacuum analyze but not direct analyze) will merge the pending list. So frequent autovacuums are the first thing you should tune. Merging on insert happens when you exceed the gin_pending_list_limit. In all cases it is also interesting to know which memory parameter is used to rebuild the index as that inpacts how long it will take: work_mem (when triggered on insert), autovacuum_work_mem (when triggered during autovauum) and maintainance_work_mem (triggered by a call to gin_clean_pending_list()) define how much memory can be used for the rebuild.<p>What you can do is:<p>- tune the size of the pending list (like you did)<p>- make sure vacuum runs frequently<p>- if you have a bulk insert heavy workload (eg. nightly imports), drop the index and create it after inserting rows (not always makes sense business wise, depends on your app)<p>- disable fastupdate, you pay a higher cost per insert but remove the fluctuation when the merge needs to happen<p>The first thing was done in the article. However I believe the author still relies on the list being merged on insert. If vacuums were tuned aggressively along with the limit (vacuums can be tuned per table). Then the list would be merged out of bound of ongoing inserts.<p>I also had the pleasure of speaking with one main authors of GIN indexes (Oleg Bartunov) during the mentioned PGCon. He gave probably the best solution and informed me to &quot;just use RUM indexes&quot;. RUM[3] indexes are like GIN indexes, without the pending list and with faster ranking, faster phrase searches and faster timestamp based ordering. It is however out of the main PostgreSQL release so it might be hard to get it running if you don&#x27;t control the extensions that are loaded to your Postgres instance.<p>[1] - video <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Brt41xnMZqo&amp;t=1s" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Brt41xnMZqo&amp;t=1s</a><p>[2] - slides <a href="https:&#x2F;&#x2F;www.pgcon.org&#x2F;2019&#x2F;schedule&#x2F;attachments&#x2F;541_Let&#x27;s%20(D)Trace%20Postgres%20tracing%20the%20madness.pdf" rel="nofollow">https:&#x2F;&#x2F;www.pgcon.org&#x2F;2019&#x2F;schedule&#x2F;attachments&#x2F;541_Let&#x27;s%20...</a><p>[3] - <a href="https:&#x2F;&#x2F;github.com&#x2F;postgrespro&#x2F;rum" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;postgrespro&#x2F;rum</a>
评论 #27189092 未加载
sa46about 4 years ago
This seems like an instance where BPF could shine. If I had a large magic wand, I’d love to have distributed traces of database query execution with spans or events for significant events like the GIN pending list cleanup described in the article.
评论 #27164617 未加载
评论 #27163526 未加载
superjanabout 4 years ago
As a programmer, the takeaway for me is that the exceptional treatment of this database request should be logged somewhere. Here, the EXPLAIN response would have been a good place.
VWWHFSfQabout 4 years ago
I&#x27;m still surprised it takes &gt; 1 minute to perform 5000 inserts, even after the optimization was put in place. Is there something else wrong?
评论 #27162650 未加载
评论 #27162689 未加载
评论 #27180331 未加载
strictfpabout 4 years ago
I would setup an async replication to another table or data source for the full-text search.
aidosabout 4 years ago
Oooo. That’s a nice performance gotcha I wasn’t aware of. Good article, nicely written.
avinasshabout 4 years ago
I loved the animation! Which tool did you use to create that?
评论 #27189155 未加载