<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pgrust &#8211; mylogs.cn</title>
	<atom:link href="https://mylogs.cn/tag/pgrust/feed/" rel="self" type="application/rss+xml" />
	<link>https://mylogs.cn</link>
	<description>发现、记录、分享</description>
	<lastBuildDate>Sat, 08 Aug 2026 01:17:09 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>pgrust 让 Postgres 分析查询快 300 倍</title>
		<link>https://mylogs.cn/pgrust-postgres-300x-faster/</link>
					<comments>https://mylogs.cn/pgrust-postgres-300x-faster/#respond</comments>
		
		<dc:creator><![CDATA[steve, zhang]]></dc:creator>
		<pubDate>Sat, 08 Aug 2026 01:16:57 +0000</pubDate>
				<category><![CDATA[科技]]></category>
		<category><![CDATA[ClickHouse]]></category>
		<category><![CDATA[pgrust]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Rust]]></category>
		<category><![CDATA[SIMD]]></category>
		<category><![CDATA[数据库性能]]></category>
		<category><![CDATA[查询引擎]]></category>
		<guid isPermaLink="false">https://mylogs.cn/pgrust-postgres-300x-faster/</guid>

					<description><![CDATA[数据库圈子最近出现一个令人咋舌的基准成绩：用 Rust 写的 pgrust 项目，在分析型查询上比原生 Pos [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">数据库圈子最近出现一个令人咋舌的基准成绩：用 Rust 写的 pgrust 项目，在分析型查询上比原生 PostgreSQL 快了约 300 倍，在 ClickHouse 的 ClickBench 基准上甚至跑赢了 ClickHouse 本身。pgrust 作者在一篇技术长文中拆解了这背后最关键的一环——查询引擎，以及三招把它从「慢」救到「快」的优化。</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a76afad8e482&quot;}" data-wp-interactive="core/image" data-wp-key="6a76afad8e482" class="wp-block-image size-large aligncenter wp-lightbox-container"><img fetchpriority="high" decoding="async" width="1200" height="514" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://mylogs.cn/wp-content/uploads/2026/08/c07ee2367257e282d074be9d16e273ff_header.webp" alt="pgrust 让 Postgres 分析查询快 300 倍" class="wp-image-4031" style="max-width:100%;height:auto;" srcset="https://mylogs.cn/wp-content/uploads/2026/08/c07ee2367257e282d074be9d16e273ff_header.webp 1200w, https://mylogs.cn/wp-content/uploads/2026/08/c07ee2367257e282d074be9d16e273ff_header-300x129.webp 300w, https://mylogs.cn/wp-content/uploads/2026/08/c07ee2367257e282d074be9d16e273ff_header-1024x439.webp 1024w, https://mylogs.cn/wp-content/uploads/2026/08/c07ee2367257e282d074be9d16e273ff_header-768x329.webp 768w" sizes="(max-width: 1200px) 100vw, 1200px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">图片来源：malisper.me</figcaption></figure>





<p class="wp-block-paragraph">PostgreSQL 诞生于磁盘 I/O 是主要瓶颈的年代，而如今很多数据集能直接塞进内存，分析负载的瓶颈早变成了 CPU 和内存带宽。pgrust 的查询引擎单独就贡献了约 10 倍的提速。作者用「求前 5 亿个数的和」这个简单查询来量化差距：同样的活，原生 Postgres 在关掉并行后跑了约 20 秒，而一段朴素的 Rust 循环只要 358 毫秒，快了约 55 倍。</p>



<p class="wp-block-paragraph">差距的根源在 PostgreSQL 使用的「火山模型」（Volcano model）执行器。它要求每个计划节点都实现一个 <code>next()</code> 方法、一次只返回一行。作者照着这个模型写了个迷你版，耗时 1.3 秒——比原生 Postgres 快，但仍远慢于裸循环，因为每行都调一次函数的开销实在太大。</p>



<p class="wp-block-paragraph">第一招是批处理。把 <code>next()</code> 改成一次返回 1024 行，绝大多数函数调用开销被抹掉，耗时从 1.3 秒降到约 480 毫秒（2.7 倍）。关键是批处理缓冲区分配在栈上，运行时不再频繁申请内存。</p>



<p class="wp-block-paragraph">第二招是算子融合。既然顺序扫描和求和总是一前一后出现，就把两个节点合并成一个，省掉中间拷贝，直接回到 358 毫秒（3.6 倍）的裸循环水平。作者坦言这算「作弊」，因为写死了特定查询，但 pgrust 真正的通用解法是 JIT 编译——为每条查询现场生成最优代码，从而对任意查询都能做算子融合。</p>



<p class="wp-block-paragraph">第三招是 SIMD。在 aarch64 架构上用单指令多数据指令一次处理多行，耗时进一步压到 135 毫秒（9.6 倍），比裸循环还快近 3 倍。代价是浮点加法不满足结合律，求和顺序改变会带来微小误差——这也是编译器默认不敢擅自上 SIMD 的原因。</p>



<p class="wp-block-paragraph">整套基准跑在 AWS c8g.4xlarge（Graviton4、16 vCPU）上，使用 PostgreSQL 18.4、关闭并行、数据预热进共享缓冲、取 5 次运行的中位数。作者总结，正是批处理、算子融合、SIMD 这些底层优化，让 pgrust 的分析查询达到数百倍于 Postgres 的速度。</p>



<p class="has-small-font-size wp-block-paragraph">来源：malisper.me</p>

]]></content:encoded>
					
					<wfw:commentRss>https://mylogs.cn/pgrust-postgres-300x-faster/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
