惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
Schneier on Security
The Hacker News
The Hacker News
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
O
OpenAI News
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Latest news
Latest news
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security

RSS feed of yihong0618's gitblog

12 月,姥姥,以及大模型 · Issue #344 · yihong0618/gitblog 不要自动化你的情感链接 · Issue #343 · yihong0618/gitblog 那些幸福的瞬间 · Issue #342 · yihong0618/gitblog 12 年过去了,你过得还好么? · Issue #341 · yihong0618/gitblog Be a human, not a claw. · Issue #339 · yihong0618/gitblog 新年快乐以及一些近况 · Issue #338 · yihong0618/gitblog 偶得的佳句与 AI · Issue #337 · yihong0618/gitblog 确定性的丧失 · Issue #336 · yihong0618/gitblog Something Boring or Interesting · Issue #335 · yihong0618/gitblog 2025 年我提了 500 个 Pull Requests · Issue #334 · yihong0618/gitblog 网友 · Issue #333 · yihong0618/gitblog 如何判断一个项目是个好的开源项目并且想贡献? · Issue #331 · yihong0618/gitblog Things I don’t like · Issue #329 · yihong0618/gitblog Notes on Pycon2025 · Issue #327 · yihong0618/gitblog 从错误中学习 · Issue #326 · yihong0618/gitblog 一次艰难的 cpython issue 排查过程,以及我学到了什么 · Issue #325 · yihong0618/gitblog 八月 · Issue #324 · yihong0618/gitblog 从给 Greptime 贡献代码的过程中,我都学到了什么 · Issue #320 · yihong0618/gitblog Notes on May · Issue #318 · yihong0618/gitblog Things I like · Issue #311 · yihong0618/gitblog 我的 35 岁 · Issue #309 · yihong0618/gitblog 连续跑了 365 天 · Issue #306 · yihong0618/gitblog 最近给开源项目贡献的记录 · Issue #304 · yihong0618/gitblog 2020-2025 最喜欢的 12 篇文章和一张图 · Issue #302 · yihong0618/gitblog 断章 · Issue #301 · yihong0618/gitblog 这两个月 · Issue #299 · yihong0618/gitblog 从给 RisingWave 提 PR 说起,聊聊怎么快速切入一个复杂的开源项目 · Issue #296 · yihong0618/gitblog 能几日,又中秋 · Issue #294 · yihong0618/gitblog 第一次 AI 尝试,完全用 AI(MarsCode) 做个需求 · Issue #293 · yihong0618/gitblog 一件趣事儿 · Issue #292 · yihong0618/gitblog 这玩意不是就__ · Issue #291 · yihong0618/gitblog 六月 · Issue #290 · yihong0618/gitblog 跑了 10000km 花了 13 年 · Issue #289 · yihong0618/gitblog 抉择 · Issue #287 · yihong0618/gitblog 三月 · Issue #283 · yihong0618/gitblog About Me · Issue #282 · yihong0618/gitblog 用小爱加手指机器人实现传统开关语音关灯 · Issue #281 · yihong0618/gitblog 记忆碎片 · Issue #280 · yihong0618/gitblog 开源值得么? · Issue #279 · yihong0618/gitblog 聊聊读书 · Issue #275 · yihong0618/gitblog 旧文,移动端最好的游戏,《Dream Quest》以及其它 · Issue #274 · yihong0618/gitblog 微信好友 · Issue #272 · yihong0618/gitblog 五月病 · Issue #268 · yihong0618/gitblog 杂感 · Issue #262 · yihong0618/gitblog vm.overcommit_memory 学习笔记 · Issue #261 · yihong0618/gitblog 开源四年的一些记录 · Issue #259 · yihong0618/gitblog 不用 root 使用小爱同学和 ChatGPT 交互折腾记 · Issue #258 · yihong0618/gitblog 用 eBPF trace PostgreSQL 的几种姿势 · Issue #257 · yihong0618/gitblog 2022 失落的一年 · Issue #255 · yihong0618/gitblog 写诗吧 · Issue #254 · yihong0618/gitblog 利用 GitHubPoster 和 GitHub Actions 备份任意用户推特 · Issue #252 · yihong0618/gitblog 一杯敬月光 · Issue #251 · yihong0618/gitblog 如何判断一个数字帐号来自中国大陆 · Issue #248 · yihong0618/gitblog Random Thoughts · Issue #242 · yihong0618/gitblog 反思 · Issue #241 · yihong0618/gitblog 今天的跑步电台 · Issue #239 · yihong0618/gitblog LeetCode 是如何运行的--测试篇 · Issue #237 · yihong0618/gitblog 新冠纪元 · Issue #235 · yihong0618/gitblog Postgres 需要学的东西 · Issue #234 · yihong0618/gitblog Postgres 中 GUC 的一些记录 · Issue #233 · yihong0618/gitblog 运交华盖欲何求 · Issue #231 · yihong0618/gitblog 年终岁尾 · Issue #230 · yihong0618/gitblog 二零二二的三个小目标 · Issue #229 · yihong0618/gitblog 经历了人生体验最棒的一次面试 · Issue #228 · yihong0618/gitblog 参加了《开源面对面》《ByteTalk》《枫言枫语》播客的一些记录 · Issue #223 · yihong0618/gitblog 有趣与无趣 · Issue #221 · yihong0618/gitblog running_page 开源一周年的总结 · Issue #220 · yihong0618/gitblog 接下来要写的东西 · Issue #219 · yihong0618/gitblog 一点思考 · Issue #218 · yihong0618/gitblog 友情链接 · Issue #217 · yihong0618/gitblog 朋友圈 · Issue #216 · yihong0618/gitblog 张小龙的饭否 · Issue #215 · yihong0618/gitblog 一件小事 · Issue #214 · yihong0618/gitblog 从 Rich 作者的一个问题说起 · Issue #212 · yihong0618/gitblog 如何用一个仓库记录自己的一年 · Issue #209 · yihong0618/gitblog 逃离 · Issue #208 · yihong0618/gitblog 近况 · Issue #207 · yihong0618/gitblog 年过完了 · Issue #206 · yihong0618/gitblog 力扣的程序是如何运行的 · Issue #205 · yihong0618/gitblog 生存守则 · Issue #204 · yihong0618/gitblog 沮丧 · Issue #203 · yihong0618/gitblog 今年的三个小目标 · Issue #202 · yihong0618/gitblog 新年碎碎念 · Issue #201 · yihong0618/gitblog 二零二一开始 · Issue #200 · yihong0618/gitblog 记住这一刻 · Issue #199 · yihong0618/gitblog 巧妙利用 iOS 的快捷指令配合 GitHub Actions 实现自动化 · Issue #198 · yihong0618/gitblog 如何获取悦跑圈数据 · Issue #197 · yihong0618/gitblog 生的对立面 · Issue #196 · yihong0618/gitblog 推荐一些我一直关注的觉得不错的和 Python 相关的独立博客 · Issue #195 · yihong0618/gitblog 2020 年 10 月总结 11 月计划 · Issue #194 · yihong0618/gitblog 写在 0x20 岁之前 · Issue #193 · yihong0618/gitblog 如何获取 keep 跑步数据 · Issue #191 · yihong0618/gitblog 2020 年 9 月总结 10 月计划 · Issue #190 · yihong0618/gitblog 决定把gitblog和kb结合起来 · Issue #189 · yihong0618/gitblog 感冒了,真是难受 · Issue #188 · yihong0618/gitblog 把running page 变得通用必须提上日程了 · Issue #187 · yihong0618/gitblog 我转行程序员的经历 · Issue #186 · yihong0618/gitblog 普通人和天才之间的巨大鸿沟 · Issue #185 · yihong0618/gitblog 月跑 250 km计划 · Issue #184 · yihong0618/gitblog 2020年八月总结九月月展望 · Issue #182 · yihong0618/gitblog
如何写一个 PostgreSQL Extension · Issue #270 · yihong0618/gitblog
yihong0618 · 2023-07-08 · via RSS feed of yihong0618's gitblog

PostgreSQL Extension 是一个可插拔的功能扩展,用于在 PostgreSQL 数据库系统中添加额外的功能和能力。这些扩展可以由第三方开发者开发并加入到 PostgreSQL 中,以满足特定的需求。扩展可以增强数据库功能。
有些非常不错的 Extensions 甚至成了一些公司选型 Postgres 的理由比如:TimescaleDB, PostGIS 等等。

今年开始异常火爆的向量数据库,因为有 pgvector 也让 pg 有了向量计算和存储的能力。

本文会介绍如何编写 extensions 和推荐一些编写 Extensions 的资源。

如何写一个 Extensions

传统的 Extensions 开发一般情况下我们会 c 语言,引入 Postgers.h 写好 Makefile 和 SQL 开发,现在也有了用 Rust 编写 Extensions 的能力 -> pgrx 借助 pgrx 我们能更好的专注 extensions 中算法本身,也可以借助 Rust 强大的生态更容易的编写 extensions 需要的逻辑,进行更快速,更安全的开发。

以下分别介绍 C 和 Rust 两种开发方式。

C 语言开发 Extensions

以 hello_world 为例,需要在 hello_world 文件夹中建下面 4 个文件

hello.control         # 插件名.control
hello.c                   # 插件名.c
hello--1.0.sql        # 插件名--1.0.sql
Makefile                # 用于编译

cat hello.control

comment = 'hello:'
default_version = '1.0'
module_pathname = '$libdir/hello'
relocatable = false
superuser = true

剩下的可以参考这个 pg 的 hello_world 项目 https://github.com/magnusp/pg_hello

cat pg_hello.c

#include "postgres.h"

#include "fmgr.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

Datum hello( PG_FUNCTION_ARGS );

PG_FUNCTION_INFO_V1( hello );
Datum
hello( PG_FUNCTION_ARGS )
{
   // variable declarations
   char greet[] = "Hello, ";
   text *towhom;
   int greetlen;
   int towhomlen;
   text *greeting;

   // Get arguments.  If we declare our function as STRICT, then
   // this check is superfluous.
   if( PG_ARGISNULL(0) ) {
      PG_RETURN_NULL();
   }
   towhom = PG_GETARG_TEXT_P(0);

   // Calculate string sizes.
   greetlen = strlen(greet);
   towhomlen = VARSIZE(towhom) - VARHDRSZ;

   // Allocate memory and set data structure size.
   greeting = (text *)palloc( greetlen + towhomlen );
   SET_VARSIZE(greeting, greetlen + towhomlen  + VARHDRSZ);

   // Construct greeting string.
   strncpy( VARDATA(greeting), greet, greetlen );
   strncpy( VARDATA(greeting) + greetlen,
            VARDATA(towhom),
            towhomlen );

   PG_RETURN_TEXT_P( greeting );
}

代码的一些解释:

  1. #include "postgres.h" 包含了大部分编写 postgres 相关程序需要的东西,每个 extensions 必须包含这个
  2. #include "fmgr.h" 则包含了PG_GETARG_XXX、PG_RETURN_XXX和PG_ARGISNULL 等编写 extensions 必要的宏
  3. Datum 是 data 的单数是在 pg 中最重要的数据类型之一,它肩负着在PG 内核与用户代码之间传递数据的责任
  4. PG_MODULE_MAGIC 这个宏是编写 extensions 的一个必要的宏为了后面编译生成的库才可以被 postgresql 加载

在这之后就可以编写好 Makefile 把 pg 的 PATH 加入到环境变量中之后 make && make install

在这之后进入到 psql, create extension hello; select hello('hello'); 就可以了

当然后续可以写一些测试,需要建一个文件夹名为 sql 把相关的测试写在里面,再建一个文件夹名为 expected 把测试跑出的结果写在里面,在 Makefile 加上这句 REGRESS = hello, 就可以利用 pg 的 installcheck 了

cat sql/hello.sql

CREATE EXTENSION hello;
select hello_hello();

**如果不知道如何写插件可以参考 pg 核心开发者这个项目,里面有各种各样的 extensions **

Rust 语言开发 Extensions

当然现在是 2023 年我们完全可以借助 Rust 来开发 Extensions, 这特别得益于 pgrx 这个项目它的前身也是一个非常棒的 pg 插件 zombodb

来看看借助 pgrx 开发的优秀的 pg 插件

  • pgvecto.rs -> pgvector 的 Rust 版
  • postgresml -> postgres ml 第二版用 Rust 重写,速度大幅提升
  • plrust -> 在 pg 中使用 Rust 作为 Procedural Language

编写起来就比 C 简单多了,只需要

  1. 安装 pgrx
  2. cargo pgrx init
  3. cargo pgrx new hello
  4. cargo pgrx install or cargo prgx run

一个简单的 hello 程序就做好了,其中 cargo pgrx new hello 帮助我们生成了所有需要的文件,其中 pgrx-examples 文件夹中包含了很多如何使用 pgrx 编写的例子。

当然大家可以参考这个项目 pg_slugify,利用了 Rust 的生态,几行代码做了一个非常有用 extension, 大家的很多简单的脚本完全可以利用 Rust 编写成插件方便自己开发。

推荐一些资源

补充

2024.11.07 我用 pgrx 写了两个 extensions
https://github.com/yihong0618/pg_polyline
https://github.com/yihong0618/pg_geohash