PostgreSQL 18 Injection Point 完整指南:原理、API 与实战


 TL;DR
Injection Point 是 PG 17/18 强化的 运行时测试钩子(runtime test hook)。它让测试代码可以在内核的 指定代码路径 上,触发 任意自定义回调——而且这一切都在生产代码路径中,零侵入、可移除、可控制。
难度 中等

配套源码:~/cwork/postgresql(基于 PG 18 主干)

本文聚焦 PG 18 引入的内核级 Injection Point 框架——一个让测试代码可以在 指定代码路径 上触发任意回调的机制。读完本文你会获得:

  1. Injection Point 的 完整架构原理(共享内存 + 本地缓存 + generation 协议)
  2. API 全集(InjectionPointAttach / Detach / Run / Load / Cached)
  3. PG 内核已有的 18+ 个内置注入点 一览
  4. 3 种调用宏 的适用场景与差异
  5. 实战:如何添加自定义注入点、写异步测试、注入错误路径
  6. lock-free 协议 解读
  7. 统计系统 与监控

一、什么是 Injection Point?

Injection Point 是 PG 17/18 强化的 运行时测试钩子(runtime test hook)。它让测试代码可以在内核的 指定代码路径 上,触发 任意自定义回调——而且这一切都在生产代码路径中,零侵入、可移除、可控制。

flowchart LR P["正常代码路径<br/>(生产逻辑)"] --> I["INJECTION_POINT(name)"] I --> H{"Injection point<br/>是否 attach?"} H -->|否| N["跳过,继续执行"] H -->|是| R["调用 attached callback"] R --> A["任意用户代码<br/>(可执行 SQL / 报错 / 阻塞)"] style I fill:#fef9c3,stroke:#a16207,color:#000 style H fill:#dbeafe,stroke:#1d4ed8,color:#000 style R fill:#dcfce7,stroke:#15803d,color:#000

典型用途:

场景 例子
异步测试 测试 multixact 创建时让另一个进程 wait
错误注入 故意让某条 UPDATE 报错,测试 apply 错误处理
暂停执行 模拟 checkpoint 慢、wal-removal 卡住
并发协调 让两个后端在关键代码点同步
运行时追踪 不修改源码就能打印关键路径数据

📌 不是 gdb 断点,是 生产代码内置的、可远程控制的钩子。


二、构建要求

Injection Point 默认 未启用。需要显式 configure:

cd ~/cwork/postgresql
./configure --enable-injection-points [other options]
make -j$(nproc)

验证是否启用:

# 编译时
grep USE_INJECTION_POINTS src/include/pg_config.h
# 应看到:#define USE_INJECTION_POINTS 1

# 运行时
psql -c "SELECT * FROM pg_available_extensions WHERE name = 'injection_points';"
# 应能看到

源码 src/include/utils/injection_point.h:14:

// src/include/utils/injection_point.h:14-27
/*
 * Injection points require --enable-injection-points.
 */
#ifdef USE_INJECTION_POINTS
#define INJECTION_POINT_LOAD(name) InjectionPointLoad(name)
#define INJECTION_POINT(name, arg) InjectionPointRun(name, arg)
#define INJECTION_POINT_CACHED(name, arg) InjectionPointCached(name, arg)
#define IS_INJECTION_POINT_ATTACHED(name) IsInjectionPointAttached(name)
#else
#define INJECTION_POINT_LOAD(name) ((void) name)
#define INJECTION_POINT(name, arg) ((void) name)
#define INJECTION_POINT_CACHED(name, arg) ((void) name)
#define IS_INJECTION_POINT_ATTACHED(name) (false)
#endif

关键点:

  • 未启用时宏展开为 ((void) name)——生产构建零开销
  • 启用时宏展开为函数调用——开销仅当 attach 时才产生

三、架构原理

3.1 共享内存 + 本地缓存 + lock-free 协议

源码位置:src/backend/utils/misc/injection_point.c(共享内存控制块 InjectionPointsCtl 在 src/backend/utils/misc/injection_point.c:83)。

flowchart TB subgraph ShMem["共享内存 (Shared Memory)"] CTL["InjectionPointsCtl"] E1["entry[0]: name=fn1, library=test, function=callback1"]:::entry E2["entry[1]: ..."]:::entry EN["entry[N]: ..."]:::entry CTL --> E1 CTL --> E2 CTL --> EN end subgraph Backend["Backend 进程本地 (TopMemoryContext)"] Cache["InjectionPointCache<br/>(HTAB)"] C1["name=fn1 → slot_idx + generation + callback"]:::cache C2["name=fn2 → ..."]:::cache Cache --> C1 Cache --> C2 end ShMem -.lock-free read<br/>(generation 校验).-> Backend style CTL fill:#fef9c3,stroke:#a16207,color:#000 classDef entry fill:#dcfce7,stroke:#15803d,color:#000 classDef cache fill:#dbeafe,stroke:#1d4ed8,color:#000

每个 entry 的结构(InjectionPointEntry 定义在 src/backend/utils/misc/injection_point.c:41):

// src/backend/utils/misc/injection_point.c:61-72
  pg_atomic_uint64 generation;

  char		name[INJ_NAME_MAXLEN];	/* point name */
  char		library[INJ_LIB_MAXLEN];	/* library */
  char		function[INJ_FUNC_MAXLEN];	/* function */

  /*
   * Opaque data area that modules can use to pass some custom data to
   * callbacks, registered when attached.
   */
  char		private_data[INJ_PRIVATE_MAXLEN];
} InjectionPointEntry;

数组上限 #define MAX_INJECTION_POINTS 128(src/backend/utils/misc/injection_point.c:74)。

关键设计:

  1. 共享内存数组(entries[128],src/backend/utils/misc/injection_point.c:86):所有 backend 可见
  2. 本地缓存(HTAB in TopMemoryContext,src/backend/utils/misc/injection_point.c:136):避免每次查共享内存
  3. lock-free 读:通过 pg_atomic_uint64 generation 计数器(src/backend/utils/misc/injection_point.c:61)
  4. 写需要 LWLock(InjectionPointLock,src/backend/utils/misc/injection_point.c:302)

3.2 lock-free 协议

源码位置:src/backend/utils/misc/injection_point.c:43-61(注释完整)。

// src/backend/utils/misc/injection_point.c:43-61
/*
 * Because injection points need to be usable without LWLocks, we use a
 * generation counter on each entry to allow safe, lock-free reading.
 *
 * To read an entry, first read the current 'generation' value.  If it's
 * even, then the slot is currently unused, and odd means it's in use.
 * When reading the other fields, beware that they may change while
 * reading them, if the entry is released and reused!  After reading the
 * other fields, read 'generation' again: if its value hasn't changed, you
 * can be certain that the other fields you read are valid.  Otherwise,
 * the slot was concurrently recycled, and you should ignore it.
 *
 * When adding an entry, you must store all the other fields first, and
 * then update the generation number, with an appropriate memory barrier
 * in between. In addition to that protocol, you must also hold
 * InjectionPointLock, to prevent two backends from modifying the array at
 * the same time.
 */
pg_atomic_uint64 generation;

读取协议(3 步):

sequenceDiagram participant R as Reader (Backend) participant S as Shared Mem R->>S: 1. 读 generation (= N) Note over R,S: N 是偶数 → 空 Note over R,S: N 是奇数 → 占用 R->>S: 2. 读 entry.name/library/... Note over R: pg_read_barrier() R->>S: 3. 再读 generation alt 还是 N R->>R: 数据有效 else 变成 N+1 或其他 R->>R: 数据无效,丢弃 end

写入协议:

// 写所有字段
strlcpy(entry->name, ...);
strlcpy(entry->library, ...);
strlcpy(entry->function, ...);
memcpy(entry->private_data, ...);

// 写屏障,确保前面都可见
pg_write_barrier();

// generation +1
pg_atomic_write_u64(&entry->generation, generation + 1);

3.3 为什么 lock-free?

核心场景:某些 injection point 会在 临界区(持锁状态)调用。

例如源码位置:src/backend/access/transam/multixact.c:920:

// src/backend/access/transam/multixact.c:920
INJECTION_POINT_CACHED("multixact-create-from-members", NULL);

如果在临界区中 获取 LWLock 查共享内存,会自死锁。所以 InjectionPointRun 路径完全 lock-free——只读共享内存 + 本地缓存。

PG 内核的做法是:进临界区前先 INJECTION_POINT_LOAD(src/backend/access/transam/multixact.c:904)把回调加载进本地缓存,临界区内只调 INJECTION_POINT_CACHED(src/backend/access/transam/multixact.c:920)。

两种缓存:

宏 调用路径 适用场景
INJECTION_POINT(name, arg) 查共享内存 → 缓存 → 执行 通用路径
INJECTION_POINT_CACHED(name, arg) 直接查本地缓存 → 执行 临界区(不允许分配内存)

四、API 全集

4.1 C API

源码位置:src/include/utils/injection_point.h(C 声明从 src/include/utils/injection_point.h:36 开始)、src/backend/utils/misc/injection_point.c。

函数 用途 源码位置
InjectionPointAttach(name, library, function, private_data, private_data_size) 注册一个新的注入点 src/backend/utils/misc/injection_point.c:273
InjectionPointDetach(name) 移除注入点,返回 bool src/backend/utils/misc/injection_point.c:362
InjectionPointRun(name, arg) 执行注入点(查共享内存 → 缓存 → 回调) src/backend/utils/misc/injection_point.c:545
InjectionPointLoad(name) 仅加载到本地缓存,不执行(预热) src/backend/utils/misc/injection_point.c:532
InjectionPointCached(name, arg) 仅从本地缓存执行(lock-free 路径) src/backend/utils/misc/injection_point.c:562
IsInjectionPointAttached(name) 检查注入点是否已注册 src/backend/utils/misc/injection_point.c:579
InjectionPointShmemSize() 共享内存大小 src/backend/utils/misc/injection_point.c:233
InjectionPointShmemInit() 共享内存初始化 src/backend/utils/misc/injection_point.c:249

4.2 宏(代码路径内调用)

源码位置:src/include/utils/injection_point.h:18。

// src/include/utils/injection_point.h:18-21
#define INJECTION_POINT_LOAD(name) InjectionPointLoad(name)
#define INJECTION_POINT(name, arg) InjectionPointRun(name, arg)
#define INJECTION_POINT_CACHED(name, arg) InjectionPointCached(name, arg)
#define IS_INJECTION_POINT_ATTACHED(name) IsInjectionPointAttached(name)

三种调用宏的差异:

flowchart TB subgraph Cached["INJECTION_POINT_CACHED"] C1["查本地 HTAB 缓存"] --> C2{"找到?"} C2 -->|是| C3["执行 callback"] C2 -->|否| C4["跳过"] end subgraph Normal["INJECTION_POINT"] N1["查本地缓存"] --> N2{"找到?"} N2 -->|是| N3["执行 callback"] N2 -->|否| N4["查共享内存"] N4 --> N5{"找到?"} N5 -->|是| N6["更新本地缓存 + 执行"] N5 -->|否| N7["跳过"] end subgraph Load["INJECTION_POINT_LOAD"] L1["查共享内存"] --> L2{"找到?"} L2 -->|是| L3["仅缓存,不执行"] L2 -->|否| L4["跳过"] end style Cached fill:#dcfce7,stroke:#15803d,color:#000 style Normal fill:#dbeafe,stroke:#1d4ed8,color:#000 style Load fill:#fef9c3,stroke:#a16207,color:#000

选择原则:

场景 推荐宏
通用路径,可能分配内存 INJECTION_POINT
临界区(持锁、内存受限) INJECTION_POINT_CACHED
需要预热,避免首次 hit 时延迟 INJECTION_POINT_LOAD
仅判断是否 attach IS_INJECTION_POINT_ATTACHED

五、源码内置的注入点

PG 18 内核已有 18+ 个内置注入点。下面是按模块整理的全表:

5.1 事务与多事务

注入点 源码位置 用途
multixact-create-from-members src/backend/access/transam/multixact.c:904 / src/backend/access/transam/multixact.c:920 测试 multixact 创建(LOAD + CACHED)
commit-before-clog-update src/backend/access/transam/xact.c:1352 / src/backend/access/transam/xact.c:1512 测试 clog 更新(LOAD + CACHED)
transaction-abort-after-clog src/backend/access/transam/xact.c:1891 测试事务 abort 之后置 clog

源码 src/backend/access/transam/multixact.c:920:

// src/backend/access/transam/multixact.c:920
INJECTION_POINT_CACHED("multixact-create-from-members", NULL);

5.2 WAL 与 checkpoint

注入点 源码位置 用途
promotion-after-wal-segment-cleanup src/backend/access/transam/xlog.c:6178 测试 promotion 后清理 WAL 段
checkpoint-before-old-wal-removal src/backend/access/transam/xlog.c:7347 测试 checkpoint 慢
create-restart-point src/backend/access/transam/xlog.c:7733 测试 restart point
restartpoint-before-slot-invalidation src/backend/access/transam/xlog.c:7810 测试 restartpoint 时槽失效

5.3 Heap 与索引

注入点 源码位置 用途
heap_update-before-pin src/backend/access/heap/heapam.c:3427 测试 heap update 路径
heap_lock_updated_tuple src/backend/access/heap/heapam.c:6322 测试行锁升级
inplace-before-pin src/backend/access/index/genam.c:854 测试 inplace update
reindex-conc-index-safe src/backend/commands/indexcmds.c:3991 测试并发重建索引(判定安全)
reindex-conc-index-not-safe src/backend/commands/indexcmds.c:3993 测试并发重建索引(判定不安全)
reindex-conc-index-built src/backend/commands/indexcmds.c:4201 测试并发重建索引完成
gin-leave-leaf-split-incomplete src/backend/access/gin/ginbtree.c:688 测试 GIN leaf split 失败
gin-leave-internal-split-incomplete src/backend/access/gin/ginbtree.c:690 测试 GIN internal split 失败
gin-finish-incomplete-split src/backend/access/gin/ginbtree.c:781 测试 GIN 续 split
hash-split-before-relocation src/backend/access/hash/hashpage.c:1110 测试 hash split 重定位
hash-finish-incomplete-split src/backend/access/hash/hashpage.c:1461 测试 hash 续 split

5.4 后台进程与生命周期

注入点 源码位置 用途
autovacuum-worker-start src/backend/postmaster/autovacuum.c:1921 测试 autovacuum 启动
backend-initialize src/backend/tcop/backend_startup.c:252 测试 backend 启动
backend-initialize-v2-error src/backend/tcop/backend_startup.c:253 故意让 backend 启动失败
init-pre-auth src/backend/utils/init/postinit.c:743 测试认证前初始化
backend-ssl-startup src/backend/libpq/be-secure.c:134 测试 SSL 握手
backend-gssapi-startup src/backend/libpq/be-secure-gssapi.c:510 测试 GSSAPI 握手

源码 src/backend/tcop/backend_startup.c:251-263:

// src/backend/tcop/backend_startup.c:251-263
#ifdef USE_INJECTION_POINTS
  INJECTION_POINT("backend-initialize", NULL);
  if (IS_INJECTION_POINT_ATTACHED("backend-initialize-v2-error"))
  {
    /*
     * This simulates an early error from a pre-v14 server, which used the
     * version 2 protocol for any errors that occurred before processing
     * the startup packet.
     */
    FrontendProtocol = PG_PROTOCOL(2, 0);
    elog(FATAL, "protocol version 2 error triggered");
  }
#endif

5.5 超时机制

注入点 源码位置 用途
idle-in-transaction-session-timeout src/backend/tcop/postgres.c:3488 测试 idle in transaction 超时
transaction-timeout src/backend/tcop/postgres.c:3501 测试事务超时
idle-session-timeout src/backend/tcop/postgres.c:3514 测试 idle session 超时

三个注入点分散在 src/backend/tcop/postgres.c 的三处超时分支里,以 idle in transaction 为例:

// src/backend/tcop/postgres.c:3485-3492
    IdleInTransactionSessionTimeoutPending = false;
    if (IdleInTransactionSessionTimeout > 0)
    {
      INJECTION_POINT("idle-in-transaction-session-timeout", NULL);
      ereport(FATAL,
          (errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT),
           errmsg("terminating connection due to idle-in-transaction timeout")));
    }

5.6 复制与槽管理

注入点 源码位置 用途
slot-timeout-inval src/backend/replication/slot.c:1804 测试槽超时失效
logical-replication-slot-advance-segment src/backend/replication/logical/logical.c:1996 测试逻辑复制槽推进
apply-update-before-open-indices src/backend/replication/logical/worker.c:2699 测试 apply worker 打开索引之前

5.7 聚合与执行器

注入点 源码位置 用途
hash-aggregate-oversize-table src/backend/executor/nodeAgg.c:1492 测试 HashAgg 哈希表超限
hash-aggregate-spill-1000 src/backend/executor/nodeAgg.c:1882 测试 HashAgg 落盘
hash-aggregate-enter-spill-mode src/backend/executor/nodeAgg.c:1913 测试进入落盘模式
hash-aggregate-process-batch src/backend/executor/nodeAgg.c:2742 测试批次处理
hash-aggregate-single-partition src/backend/executor/nodeAgg.c:2994 测试单分区模式
check-exclusion-or-unique-constraint-conflict src/backend/executor/execIndexing.c:930 测试唯一/排他约束冲突

5.8 VACUUM、TOAST 与缓存

注入点 源码位置 用途
vacuum-index-cleanup-auto src/backend/commands/vacuum.c:2221 测试索引清理(自动判定)
vacuum-index-cleanup-disabled src/backend/commands/vacuum.c:2223 测试索引清理(禁用)
vacuum-index-cleanup-enabled src/backend/commands/vacuum.c:2225 测试索引清理(启用)
vacuum-truncate-auto src/backend/commands/vacuum.c:2260 测试 truncate(自动判定)
vacuum-truncate-disabled src/backend/commands/vacuum.c:2262 测试 truncate(禁用)
vacuum-truncate-enabled src/backend/commands/vacuum.c:2264 测试 truncate(启用)
toast-save-datum-indexes-opened src/backend/access/common/toast_internals.c:164 测试 TOAST 索引打开
catcache-list-miss-systable-scan-started src/backend/utils/cache/catcache.c:1940 测试 catalog cache 扫描
transaction-end-process-inval src/backend/utils/cache/inval.c:1210 测试事务结束时的 inval 处理
typecache-before-rel-type-cache-insert src/backend/utils/cache/typcache.c:3075 测试 type cache 插入

5.9 AIO 与备库

注入点 源码位置 用途
aio-process-completion-before-shared src/backend/storage/aio/aio.c:538 测试 AIO 完成回传之前
aio-worker-after-reopen src/backend/storage/aio/method_worker.c:536 测试 AIO worker 重开之后
skip-log-running-xacts src/backend/storage/ipc/standby.c:1292 测试备库跳过 running xacts 日志

六、实战:通过 extension 测试框架使用

源码位置:src/test/modules/injection_points/。

6.1 创建 extension

源码 src/test/modules/injection_points/injection_points.control:1:

// src/test/modules/injection_points/injection_points.control:1-4
comment = 'Test code for injection points'
default_version = '1.0'
module_pathname = '$libdir/injection_points'
relocatable = true

使用:

CREATE EXTENSION injection_points;

这会注册 9 个 SQL 函数(src/test/modules/injection_points/injection_points--1.0.sql:11):

函数 用途 实现位置
injection_points_attach(name, action) attach 一个注入点 src/test/modules/injection_points/injection_points.c:343
injection_points_load(name) 预热缓存 src/test/modules/injection_points/injection_points.c:390
injection_points_run(name, arg) 触发 src/test/modules/injection_points/injection_points.c:408
injection_points_cached(name, arg) 从缓存触发 src/test/modules/injection_points/injection_points.c:431
injection_points_wakeup(name) 唤醒等待的注入点 src/test/modules/injection_points/injection_points.c:454
injection_points_set_local() 标记为本地(进程退出时自动清理) src/test/modules/injection_points/injection_points.c:494
injection_points_detach(name) 移除 src/test/modules/injection_points/injection_points.c:516
injection_points_stats_numcalls(name) 读取统计 src/test/modules/injection_points/injection_stats.c:188
injection_points_stats_fixed() 读取固定计数 src/test/modules/injection_points/injection_stats_fixed.c:173

6.2 SQL 示例:触发一个注入点

源码 injection_points--1.0.sql:

-- 创建一个 ERROR 类型的注入点
SELECT injection_points_attach('my-custom-point', 'error');

-- 触发
SELECT injection_points_run('my-custom-point');
-- ERROR:  error injected for injection point my-custom-point

-- 清理
SELECT injection_points_detach('my-custom-point');

6.3 异步测试:wait + wakeup 协议

源码 injection_points.c 中实现的 injection_wait callback(定义在 src/test/modules/injection_points/injection_points.c:272),核心是等 inj_state->wait_counts[] 被 injection_points_wakeup() 改掉:

// src/test/modules/injection_points/injection_points.c:314-336
  /* And sleep.. */
  ConditionVariablePrepareToSleep(&inj_state->wait_point);
  PG_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index));
  {
    for (;;)
    {
      uint32		new_wait_counts;

      SpinLockAcquire(&inj_state->lock);
      new_wait_counts = inj_state->wait_counts[index];
      SpinLockRelease(&inj_state->lock);

      if (old_wait_counts != new_wait_counts)
        break;
      ConditionVariableSleep(&inj_state->wait_point, injection_wait_event);
    }
    ConditionVariableCancelSleep();
  }
  PG_END_ENSURE_ERROR_CLEANUP(injection_wait_cleanup, Int32GetDatum(index));

  /* Remove this injection point from the waiters. */
  injection_wait_cleanup(0, Int32GetDatum(index));
}

测试 SQL 流程:

-- 会话 A:启动注入点,等待
SELECT injection_points_attach('test-wait', 'wait');
SELECT pg_sleep(0);  -- 让出 CPU

-- 会话 B:唤醒会话 A
SELECT injection_points_wakeup('test-wait');

-- 清理
SELECT injection_points_detach('test-wait');

6.4 本地化注入点(并发安全)

源码 injection_points.c::injection_points_set_local()(src/test/modules/injection_points/injection_points.c:494):

// src/test/modules/injection_points/injection_points.c:492-509
PG_FUNCTION_INFO_V1(injection_points_set_local);
Datum
injection_points_set_local(PG_FUNCTION_ARGS)
{
  /* Enable flag to add a runtime condition based on this process ID */
  injection_point_local = true;

  if (inj_state == NULL)
    injection_init_shmem();

  /*
   * Register a before_shmem_exit callback to remove any injection points
   * linked to this process.
   */
  before_shmem_exit(injection_points_cleanup, (Datum) 0);

  PG_RETURN_VOID();
}

SQL:

SELECT injection_points_set_local();
SELECT injection_points_attach('my-point', 'notice');
-- 现在 'my-point' 只在当前 session 有效
-- 进程退出时自动清理

用途:让并行测试套件不互相干扰(每个 backend 的注入点只对自己有效)。

源码 injection_points.c::InjectionPointCondition(定义在 src/test/modules/injection_points/injection_points.h:24,枚举在 src/test/modules/injection_points/injection_points.h:18):

// src/test/modules/injection_points/injection_points.h:24-31
typedef struct InjectionPointCondition
{
  /* Type of the condition */
  InjectionPointConditionType type;

  /* ID of the process where the injection point is allowed to run */
  int			pid;
} InjectionPointCondition;

七、实战:在内核代码中添加自定义注入点

7.1 需求

假设我们在 xlog.c 的某个新代码路径想测试,需要添加:

INJECTION_POINT("my-feature-critical-section", NULL);

7.2 Step 1:添加宏调用

// src/backend/access/transam/xlog.c
void
MyNewFunction(void)
{
    ...
    /* 关键路径开始 */
    INJECTION_POINT("my-feature-critical-section", NULL);
    ...
}

7.3 Step 2:让 attach 生效(开发环境)

# 在测试 SQL 里
SELECT injection_points_attach('my-feature-critical-section', 'error');
-- 此时任何进入 MyNewFunction 的进程都会报错

# 清理
SELECT injection_points_detach('my-feature-critical-section');

7.4 Step 3:提交 commit message 模板

Add injection point 'my-feature-critical-section' in xlog.c

This adds a runtime hook for testing critical section behavior in
MyNewFunction(). The injection point is a no-op unless --enable-injection-points
is configured and a callback is attached via InjectionPointAttach().

This is intended for regression tests only and has no production behavior.

八、实战:用 C 写自定义回调

8.1 注册回调函数

// 在你的测试 module 中
#include "postgres.h"
#include "fmgr.h"
#include "utils/injection_point.h"

PG_MODULE_MAGIC;

PGDLLEXPORT void
my_custom_callback(const char *name,
                   const void *private_data,
                   void *arg)
{
    /* 这里可以任意操作 */
    ereport(WARNING,
            (errmsg(" my custom callback triggered for %s", name),
             errdetail("private_data = %p", private_data)));

    /* 可以调用 ereport(ERROR) 模拟错误 */
    /* 可以调用 ConditionVariableSleep 模拟阻塞 */
}

8.2 暴露为 SQL 函数

PG_FUNCTION_INFO_V1(my_attach);
Datum
my_attach(PG_FUNCTION_ARGS)
{
    char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));

    InjectionPointAttach(name,
                        "my_test_module",      // library name
                        "my_custom_callback",  // function name
                        NULL, 0);
    PG_RETURN_VOID();
}

8.3 SQL 文件

-- my_test_module--1.0.sql
CREATE FUNCTION my_attach(name TEXT) RETURNS void
AS 'MODULE_PATHNAME', 'my_attach'
LANGUAGE C STRICT;

-- 使用
SELECT my_attach('heap_lock_updated_tuple');

九、PG 内核测试套件中的实际例子

源码位置:src/test/modules/injection_points/sql/injection_points.sql。

典型测试模式:

-- 1. 创建注入点(让某段代码报错)
SELECT injection_points_attach('my_point', 'error');

-- 2. 执行触发的操作(应当报错)
\set VERBOSITY verbose
DO $$ BEGIN PERFORM my_op(); END $$;
-- ERROR:  error injected for injection point my_point

-- 3. 验证错误确实发生
SELECT ...;

-- 4. 清理
SELECT injection_points_detach('my_point');

十、统计系统

源码位置:src/test/modules/injection_points/injection_stats.c。

flowchart LR A["attach"] --> SA[stats.attachments++] D["detach"] --> SD[stats.detaches++] R["run"] --> SR[stats.run++] C["cached"] --> SC[stats.cached++] L["load"] --> SL[stats.loaded++] SA --> View["injection_points_stats_fixed()"] SD --> View SR --> View SC --> View SL --> View style View fill:#dcfce7,stroke:#15803d,color:#000

SQL 查看:

SELECT * FROM injection_points_stats_fixed();
--  numattach | numdetach | numrun | numcached | numloaded
-- -----------+-----------+--------+-----------+-----------
--         3  |        1  |    5   |         0 |         2

SELECT injection_points_stats_numcalls('my_point');
-- 7

源码 injection_stats.c 的 PgStatShared_InjectionPoint(src/test/modules/injection_points/injection_stats.c:31):

// src/test/modules/injection_points/injection_stats.c:31-35
typedef struct PgStatShared_InjectionPoint
{
  PgStatShared_Common header;
  PgStat_StatInjEntry stats;
} PgStatShared_InjectionPoint;

其中 PgStat_StatInjEntry 只有一个 numcalls 计数器(src/test/modules/injection_points/injection_stats.c:26)。


十一、注意事项

11.1 内存限制

源码 src/backend/utils/misc/injection_point.c:74:

// src/backend/utils/misc/injection_point.c:74
#define MAX_INJECTION_POINTS 128

最多 128 个并发注入点——超出会报 too many injection points(src/backend/utils/misc/injection_point.c:325)。

11.2 名字长度限制

源码 src/backend/utils/misc/injection_point.c:35-38:

// src/backend/utils/misc/injection_point.c:35-38
#define INJ_NAME_MAXLEN 64
#define INJ_LIB_MAXLEN 128
#define INJ_FUNC_MAXLEN 128
#define INJ_PRIVATE_MAXLEN 1024

字段长度超限会报 injection point name too long。

11.3 同一名字的注入点只能 attach 一次

源码 src/backend/utils/misc/injection_point.c:319:

// src/backend/utils/misc/injection_point.c:319-320
    else if (strcmp(entry->name, name) == 0)
      elog(ERROR, "injection point \"%s\" already defined", name);

11.4 二进制协议层无影响

INJECTION_POINT 宏只在内核代码路径生效,不影响 SQL 协议层。

11.5 未启用构建的零开销

源码 src/include/utils/injection_point.h:22:

// src/include/utils/injection_point.h:22-27
#else
#define INJECTION_POINT_LOAD(name) ((void) name)
#define INJECTION_POINT(name, arg) ((void) name)
#define INJECTION_POINT_CACHED(name, arg) ((void) name)
#define IS_INJECTION_POINT_ATTACHED(name) (false)
#endif

宏直接展开成 (void) name,编译器会消除。

11.6 临界区的限制

INJECTION_POINT_CACHED 只能在 已 load 过 的注入点用,否则本地缓存找不到,直接跳过。源码 src/backend/utils/misc/injection_point.c:562:

// src/backend/utils/misc/injection_point.c:561-573
void
InjectionPointCached(const char *name, void *arg)
{
#ifdef USE_INJECTION_POINTS
  InjectionPointCacheEntry *cache_entry;

  cache_entry = injection_point_cache_get(name);
  if (cache_entry)
    cache_entry->callback(name, cache_entry->private_data, arg);
#else
  elog(ERROR, "Injection points are not supported by this build");
#endif
}

对策:在临界区前调用 INJECTION_POINT_LOAD(name) 预热(src/backend/utils/misc/injection_point.c:532)。

11.7 跨进程 attach

源码 src/backend/utils/misc/injection_point.c:288(参数校验):

// src/backend/utils/misc/injection_point.c:288-290
  if (strlen(library) >= INJ_LIB_MAXLEN)
    elog(ERROR, "injection point library %s too long (maximum of %u characters)",
       library, INJ_LIB_MAXLEN - 1);

library 是 PG 共享库名(不是文件路径),比如 injection_points。attach 时通过 load_external_function() 动态加载并解析 function 符号。


十二、与传统测试钩子的对比

flowchart TB subgraph Old["传统方式"] O1["#ifdef FOOBAR_TEST<br/>test_foobar();<br/>#endif"] end subgraph New["Injection Point"] N1["INJECTION_POINT('foobar-test')"] N2["运行时:<br/>attach 回调"] N3["运行时:<br/>不 attach = noop"] end O1 -->|"需要重新编译<br/>不能远程控制"| A[❌] N1 -->|"无需重新编译<br/>SQL 级别 attach"| B[✅] N2 --> B N3 --> B style A fill:#fee2e2,stroke:#dc2626,color:#000 style B fill:#dcfce7,stroke:#15803d,color:#000

核心优势:

维度 传统 #ifdef Injection Point
重新编译 需要 不需要
远程控制 不行 SQL 即可
异步测试 困难 wait/wakeup 协议
错误注入 不便 callback 内 ereport
并发安全 困难 本地化 + PID 过滤
性能影响 编译期 运行时,attach 时才有

十三、源码速查

关注点 源码位置
头文件(宏与 API 声明) src/include/utils/injection_point.h
实现 src/backend/utils/misc/injection_point.c
InjectionPointEntry 结构 src/backend/utils/misc/injection_point.c:41
InjectionPointsCtl 结构 src/backend/utils/misc/injection_point.c:83
本地缓存 HTAB src/backend/utils/misc/injection_point.c:110
InjectionPointAttach src/backend/utils/misc/injection_point.c:273
InjectionPointDetach src/backend/utils/misc/injection_point.c:362
InjectionPointCacheRefresh src/backend/utils/misc/injection_point.c:422
InjectionPointLoad src/backend/utils/misc/injection_point.c:532
InjectionPointRun src/backend/utils/misc/injection_point.c:545
InjectionPointCached src/backend/utils/misc/injection_point.c:562
IsInjectionPointAttached src/backend/utils/misc/injection_point.c:579
测试 extension src/test/modules/injection_points/
9 个 SQL 函数 src/test/modules/injection_points/injection_points--1.0.sql:11
多事务注入点 src/backend/access/transam/multixact.c:904、src/backend/access/transam/multixact.c:920
Checkpoint 注入点 src/backend/access/transam/xlog.c:7347、src/backend/access/transam/xlog.c:7733、src/backend/access/transam/xlog.c:7810
Heap 注入点 src/backend/access/heap/heapam.c:3427、src/backend/access/heap/heapam.c:6322
GIN 注入点 src/backend/access/gin/ginbtree.c:688、src/backend/access/gin/ginbtree.c:781
Autovacuum 注入点 src/backend/postmaster/autovacuum.c:1921
Backend 启动注入点 src/backend/tcop/backend_startup.c:252、src/backend/tcop/backend_startup.c:253
逻辑复制注入点 src/backend/replication/logical/logical.c:1996

十四、参考


文章作者: growdu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 growdu !
这篇还发到了
今日头条 drafted
  目录