```shell
# 1. 安装 PostgreSQL 和 pgbench

sudo apt-get update
sudo apt-get install -y postgresql postgresql-contrib

# 2. 找到 postgresql.conf（Ubuntu 通常在这个目录）

ls /etc/postgresql/*/main/postgresql.conf

# 3. 配置禁用 mmap（编辑 postgresql.conf）

shared_memory_type = sysv
dynamic_shared_memory_type = sysv

# 4. 重启 PostgreSQL

## 清理旧空间
sudo systemctl stop postgresql
pkill -9 -u postgres postgres || true
rm -rf /home/lian/pg/pgdata
rm -rf /zvfs/pg_ts_bench
rm -rf /tmp/pg.log

## 初始化
sudo chown -R postgres:postgres /home/lian/pg
sudo -u postgres mkdir -p /home/lian/pg/pgdata
sudo chown -R postgres:postgres /home/lian/pg/pgdata

sudo -u postgres env LD_PRELOAD=/home/lian/try/zvfs/src/libzvfs.so \
    /usr/lib/postgresql/12/bin/initdb -D /home/lian/pg/pgdata

## 修改配置文件
cp  ./postgresql.conf /home/lian/pg/pgdata/
sudo chown -R postgres:postgres /home/lian/pg/pgdata/postgresql.conf

## 创建测试环境
sudo -u postgres mkdir -p /zvfs/pg_ts_bench
sudo chown -R postgres:postgres /zvfs/pg_ts_bench
sudo chmod 700 /zvfs/pg_ts_bench



## 启动服务
sudo -u postgres env LD_PRELOAD=/home/lian/try/zvfs/src/libzvfs.so \
    /usr/lib/postgresql/12/bin/pg_ctl -D /home/lian/pg/pgdata -l /tmp/pg.log start

sudo -u postgres env \
    LD_PRELOAD=/home/lian/try/zvfs/src/libzvfs.so \
    ZVFS_DEBUG_OPEN=1 \
    ZVFS_DEBUG_OPEN_MATCH=PG_VERSION \
    ZVFS_DEBUG_RW=1 \
    /usr/lib/postgresql/12/bin/pg_ctl -D /home/lian/pg/pgdata -l /tmp/pg.log start

## 打开命令行工具
sudo -u postgres env LD_PRELOAD=/home/lian/try/zvfs/src/libzvfs.so \
    /usr/lib/postgresql/12/bin/psql 
    

sudo -u postgres env LD_PRELOAD=/home/lian/try/zvfs/src/libzvfs.so \
    ZVFS_DEBUG_OPEN=1 \
    ZVFS_DEBUG_OPEN_MATCH=PG_VERSION \
    ZVFS_DEBUG_RW=1 \
    /usr/lib/postgresql/12/bin/psql 

## 重启服务
sudo -u postgres env LD_PRELOAD=/home/lian/try/zvfs/src/libzvfs.so \
    /usr/lib/postgresql/12/bin/pg_ctl -D /home/lian/pg/pgdata -l /tmp/pg.log restart


# 5. 验证配置生效
pid=$(pgrep -u postgres -xo postgres) 
echo "pid=$pid"
sudo grep libzvfs /proc/$pid/maps

show data_directory;
SHOW shared_memory_type;
SHOW dynamic_shared_memory_type;
```

## 简易功能测试
```shell
# 1. 创建 tablespace
DROP TABLESPACE IF EXISTS zvfs_ts;
CREATE TABLESPACE zvfs_ts LOCATION '/zvfs/pg_ts_bench';

# 2. 创建数据库
DROP DATABASE IF EXISTS benchdb;
CREATE DATABASE benchdb TABLESPACE zvfs_ts;

## 3. 连接数据库
\c benchdb

# -- 查看数据库默认 tablespace
SHOW default_tablespace;

# 4. 创建表（默认 tablespace）
DROP TABLE IF EXISTS hook_probe;
CREATE TABLE hook_probe(
    id int,
    val text
);

INSERT INTO hook_probe VALUES (1,'a');
INSERT INTO hook_probe VALUES (2,'b');
INSERT INTO hook_probe VALUES (3,'c');
INSERT INTO hook_probe VALUES (4,'d');

SELECT * FROM hook_probe;

# 5. UPDATE / DELETE 测试
DELETE FROM hook_probe WHERE id = 1;
UPDATE hook_probe SET id = 11 WHERE id = 2;
SELECT * FROM hook_probe;


# 6. 批量 INSERT 测试
INSERT INTO hook_probe
SELECT generate_series(100,10000), 'bulk';

SELECT count(*) FROM hook_probe;

# 7. 创建索引（测试 index file）
CREATE INDEX idx_hook_probe_id ON hook_probe(id);

# 8. 大对象测试（TOAST）
INSERT INTO hook_probe VALUES (99999, repeat('zvfs_test_',1000));

# 9. VACUUM 测试
VACUUM hook_probe;

# 10. ANALYZE
ANALYZE hook_probe;

# 11. 查看物理文件位置
SELECT
    relname,
    pg_relation_filepath(oid)
FROM pg_class
WHERE relname='hook_probe';

# 12. 删表
DROP TABLE IF EXISTS hook_probe;


# 13. 压测
bash /home/lian/try/zvfs/scripts/run_pgbench_no_mmap.sh
```