You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varerrctx=pgzx.err.Context.init();
defererrctx.deinit();
if (errctx.pg_try()) {
// zig code that calls several Postgres C functions.
} else {
returnerrctx.errorValue();
}
pgzx 是一个使用 Zig 编写的 PostgreSQL 扩展库。它提供了一套实用程序(如错误处理、内存分配器、封装器)和一个开发环境,可简化与 PostgreSQL 代码库的集成。
如果浏览一下 Postgres 的源代码,就会发现 PG_TRY / PG_CATCH / PG_FINALLY 宏被用作 C 语言的一种 "异常处理 "形式,用于捕捉 ereport 系列函数引发的错误。这些宏使用 longjump(即跨越函数边界的跳转)来实现 "catch/finally " 效果。
这意味着我们从 Zig 中调用 Postgres 函数时需要小心。例如,如果被调用的 C 函数引发了 ereport 错误,longjmp 可能会跳过清理资源的 Zig 代码(如 errdefer)。pgzx 推荐这么来处理
一个更完整的示例:
实现代码
The text was updated successfully, but these errors were encountered: