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.

Postgres feature you're not using – CTEs a.k.a. WITH clauses

80 pointsby plaur7829 months ago

13 comments

saurik9 months ago
&gt; CTEs won’t always be quite as performant as optimizing your SQL to be as concise as possible. In most cases I have seen performance differences smaller than a 2X difference, this tradeoff for readability is a nobrainer as far as I’m concerned. And with time the Postgres optimizer should continue to get better about such performance.<p>So, my knowledge here might be out of date, but this behavior isn&#x27;t &quot;the optimizer isn&#x27;t smart enough&quot;: using a CTE in PostgreSQL causes an explicit boundary in the optimizer which prevents some optimizations from being performed. In some cases you really need&#x2F;want this behavior, and it might increase the performance of your query; in other cases, this is the last thing you would want. People keep asking for optimizer hints, which PostgreSQL refuses to add, and yet they leave this quirk in explicitly as an escape hatch.
评论 #41272749 未加载
muhrizqiardi9 months ago
I&#x27;m not using? I&#x27;m using that feature almost all the time!
评论 #41307825 未加载
评论 #41307562 未加载
dveeden29 months ago
The good thing here is that other databases like MySQL, TiDB, SQLite etc also support this. so this. And even recursive CTEs are well supported.<p><a href="https:&#x2F;&#x2F;modern-sql.com&#x2F;caniuse&#x2F;with_(non-recursive,_top_level)" rel="nofollow">https:&#x2F;&#x2F;modern-sql.com&#x2F;caniuse&#x2F;with_(non-recursive,_top_leve...</a>
larodi9 months ago
I sure do use them for many years now, my girlfriend now uses them and loves them, my topcoder friend eventually started using them 10 years ago.<p>So, no, we are using them , but y’all know what ? Unless recursive they are really nothing so special…
评论 #41308698 未加载
hansvm9 months ago
Used judiciously, a macro system in front of your SQL is often a good approach here. You gain the &quot;linguistically common subtable&quot; capabilities of a CTE, the ability to name constants, and the ability to name&#x2F;parameterize common sub-expressions, even in databases not supporting CTEs. With LSP support being what it is, you can even whip up editor integration for your new language in a day or less.<p>Compared to CTEs (assuming you&#x27;re not using recursion and don&#x27;t _want_ the materialization), that extra syntactic sugar helps with readability, and the &quot;macro&quot; nature of the thing ensures you don&#x27;t have any slowdowns from running the thing due to a different query plan.<p>Downsides include the ability for juniors to turn your SQL into an awful mess, the fact that you&#x27;ll still occasionally _want_ materialized CTEs for performance and have to write them anyway, plus if you implement it wrong you&#x27;ll have runtime overhead and very few introspection capabilities (the simplest version I&#x27;ve seen that&#x27;s decent to work with is creating SQL files as artifacts from the templates using the build system, obviously depending on how much of a rube goldberg your particular builds are).
评论 #41308061 未加载
stareatgoats9 months ago
(2013)<p>It was also discussed around that time, 78 comments: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7023907">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7023907</a>
lovasoa9 months ago
It&#x27;s strange to call CTEs a &quot;postgres feature&quot;. It&#x27;s standard SQL, it&#x27;s in all databases.
评论 #41309854 未加载
solatic9 months ago
&gt; CTEs won’t always be quite as performant as optimizing your SQL to be as concise as possible. In most cases I have seen performance differences smaller than a 2X difference, this tradeoff for readability is a nobrainer as far as I’m concerned.<p>This is the key trade-off you need to keep in the back of your head. Pre-mature performance optimization is the root of all evil; prefer readability first. But the fact remains that you should be setting timeouts and other time budgets, tracing calls from API through to the database, seeing what&#x27;s taking the most time. If you get to the point where you need to optimize a CTE-based query, be prepared to rewrite it.
cesarb9 months ago
WITH clauses are a somewhat recent feature, at least on PostgreSQL. If you learned SQL before they became commonly available, it&#x27;s unsurprising if you tend to think of other SQL techniques first.
评论 #41307821 未加载
croes9 months ago
&gt;The general idea is that it allows you to create something somewhat equivilant to a view that only exists during that transaction.<p>I can&#x27;t talk for Postgres but on MS SQL it&#x27;s definitely not like a view.<p>I had a CTE with a filter condition in the where close but got a conversion error in the later use for a value I already filtered out in the CTE.<p>So it may look like some kind of temporary view definition but the actual code the optimizer generates may behave unexpected.
评论 #41308253 未加载
marcus_holmes9 months ago
I use CTEs all the time, find them way more readable than subqueries. I&#x27;ve refactored a few complex queries to use them and had the reaction from the original author of &quot;wtf is this crap?&quot;. It does take some getting used to ;)
beAbU9 months ago
(I&#x27;m not a dev)<p>This feels to me like a &quot;in other news, water is wet&quot; kind of story. Maybe it&#x27;s just me, but based on the other comments here CTEs seem to be common knowledge. I went on a SQL trial by fire this year after landing in a role that required massive amounts of weird querying for ad-hoc reporting purposes. Before this my experience and knowledge stopped at different join types.<p>I naturally discovered CTEs over time after seeking solutions to my problems on the usual online resources. For me the use is two-fold. 1&#x2F; I use it to create ad-hoc lookups on data, where the lookups are not available in the DB in the first place. And 2&#x2F; In complex queries with lots and lots of conditional joins, I can use a CTE to basically build up my temporary base table, getting all my data in one place, then my main `SELECT` becomes a lot cleaner and easier to read.<p>I&#x27;ve not looked at recursive CTEs yet, supposedly they can be quite handy. Not sure for what though.<p>We recently migrated from MySQL to Postgres, and the data reporting went from direct-db querying to Snowflake. And CTEs work on for me on all 3 platforms, so this does not even seem like a Postgres only thing.
Dissori9 months ago
This was posted 4 days ago but now says 7 hours ago, even the comments from back then now show as 30 minutes old?
评论 #41309143 未加载