pgmemcache

为PG提供memcached兼容接口

概览

扩展包名版本分类许可证语言
pgmemcache2.3.0SIMMITC
ID扩展名BinLibLoadCreateTrustReloc模式
9410pgmemcache-
相关扩展redis_fdw redis spat mongo_fdw kafka_fdw documentdb documentdb_core documentdb_distributed

missing pg12-14 on el.aarch64

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG2.3.01817161514pgmemcache-
RPMPGDG2.3.01817161514pgmemcache_$v-
DEBPGDG2.3.01817161514postgresql-$v-pgmemcache-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 2.3.0
PGDG 2.3.0
el8.aarch64PGDG MISSPGDG MISS
el9.x86_64
PGDG 2.3.0
PGDG MISS
el9.aarch64PGDG MISSPGDG MISS
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
d12.aarch64
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
d13.x86_64
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
d13.aarch64
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
u22.x86_64
u22.aarch64
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
u24.x86_64
u24.aarch64
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0
PGDG 2.3.0

安装

您可以直接安装 pgmemcache 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pgmemcache;          # 当前活跃 PG 版本安装
pig ext install -y pgmemcache -v 18  # PG 18
pig ext install -y pgmemcache -v 17  # PG 17
pig ext install -y pgmemcache -v 16  # PG 16
pig ext install -y pgmemcache -v 15  # PG 15
pig ext install -y pgmemcache -v 14  # PG 14
dnf install -y pgmemcache_18       # PG 18
dnf install -y pgmemcache_17       # PG 17
dnf install -y pgmemcache_16       # PG 16
dnf install -y pgmemcache_15       # PG 15
dnf install -y pgmemcache_14       # PG 14
apt install -y postgresql-18-pgmemcache   # PG 18
apt install -y postgresql-17-pgmemcache   # PG 17
apt install -y postgresql-16-pgmemcache   # PG 16
apt install -y postgresql-15-pgmemcache   # PG 15
apt install -y postgresql-14-pgmemcache   # PG 14

创建扩展

CREATE EXTENSION pgmemcache;

用法

pgmemcache: memcached 接口

提供与 memcached 服务器交互的 PostgreSQL 用户自定义函数。

启用

CREATE EXTENSION pgmemcache;

postgresql.conf 中配置默认服务器:

shared_preload_libraries = 'pgmemcache'
pgmemcache.default_servers = 'localhost:11211'
pgmemcache.default_behavior = 'DEAD_TIMEOUT:2'

服务器管理

SELECT memcache_server_add('localhost:11211');
SELECT memcache_server_add('cache-host');  -- 使用默认端口 11211

设置和获取值

-- 设置键(存在则覆盖)
SELECT memcache_set('user:1:name', 'John Doe');
SELECT memcache_set('session:abc', 'data', now() + interval '1 hour');

-- 添加键(存在则失败)
SELECT memcache_add('user:2:name', 'Jane Doe');
SELECT memcache_add('temp_key', 'value', interval '5 minutes');

-- 替换(键不存在则失败)
SELECT memcache_replace('user:1:name', 'John Smith');

-- 获取值
SELECT memcache_get('user:1:name');  -- 返回 text 或 NULL

-- 获取多个值
SELECT key, value FROM memcache_get_multi('{key1,key2,key3}'::text[]);

原子计数器

SELECT memcache_incr('counter');        -- 增加 1
SELECT memcache_incr('counter', 5);     -- 增加 5
SELECT memcache_decr('counter');        -- 减少 1
SELECT memcache_decr('counter', 3);     -- 减少 3

删除和刷新

SELECT memcache_delete('user:1:name');
SELECT memcache_flush_all();  -- 刷新所有服务器

统计信息

SELECT memcache_stats();  -- 返回所有服务器的统计信息

触发器示例

表更新时失效缓存:

CREATE OR REPLACE FUNCTION auth_passwd_upd()
RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
    IF OLD.passwd <> NEW.passwd THEN
        PERFORM memcache_delete('user_id_' || NEW.user_id || '_password');
    END IF;
    RETURN NEW;
END;
$$;

CREATE TRIGGER auth_passwd_upd_trg AFTER UPDATE ON passwd
    FOR EACH ROW EXECUTE PROCEDURE auth_passwd_upd();

最后修改 2026-03-14: update extension metadata (953cbd0)