plprql

在PostgreSQL使用PRQL——管线式关系查询语言

概览

扩展包名版本分类许可证语言
plprql18.0.1LANGApache-2.0Rust
ID扩展名BinLibLoadCreateTrustReloc模式
3040plprql-
相关扩展pg_tle plpgsql plv8 plperl plpython3u pllua hstore_pllua plluau

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY18.0.11817161514plprql-
RPMPIGSTY18.0.11817161514plprql_$v-
DEBPIGSTY18.0.11817161514postgresql-$v-plprql-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
u22.x86_64
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
u22.aarch64
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
u24.x86_64
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
u24.aarch64
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1
PIGSTY 18.0.1

构建

您可以使用 pig build 命令构建 plprql 扩展的 RPM / DEB 包:

pig build pkg plprql         # 构建 RPM / DEB 包

安装

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

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

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

pig install plprql;          # 当前活跃 PG 版本安装
pig ext install -y plprql -v 18  # PG 18
pig ext install -y plprql -v 17  # PG 17
pig ext install -y plprql -v 16  # PG 16
pig ext install -y plprql -v 15  # PG 15
pig ext install -y plprql -v 14  # PG 14
dnf install -y plprql_18       # PG 18
dnf install -y plprql_17       # PG 17
dnf install -y plprql_16       # PG 16
dnf install -y plprql_15       # PG 15
dnf install -y plprql_14       # PG 14
apt install -y postgresql-18-plprql   # PG 18
apt install -y postgresql-17-plprql   # PG 17
apt install -y postgresql-16-plprql   # PG 16
apt install -y postgresql-15-plprql   # PG 15
apt install -y postgresql-14-plprql   # PG 14

创建扩展

CREATE EXTENSION plprql;

用法

plprql: 在 PostgreSQL 中使用 PRQL —— 管道式关系查询语言

plprql 允许使用 PRQL(管道式关系查询语言)编写 PostgreSQL 函数,这是一种编译为 SQL 的现代语言,采用管道语法。

CREATE EXTENSION plprql;

使用 PRQL 创建函数

CREATE FUNCTION match_stats(int)
RETURNS TABLE(player text, kd_ratio float) AS $$
  from matches
  filter match_id == $1
  group player (
    aggregate {
      total_kills = sum kills,
      total_deaths = sum deaths
    }
  )
  filter total_deaths > 0
  derive kd_ratio = total_kills / total_deaths
  select { player, kd_ratio }
$$ LANGUAGE plprql;

SELECT * FROM match_stats(42);

直接执行 PRQL

SELECT prql('from matches | filter player == "Player1"')
  AS (id int, match_id int, player text, kills int)
  LIMIT 2;

使用游标处理大结果集

SELECT prql('from matches | filter player == "Player1"', 'cursor_name');
FETCH 2 FROM cursor_name;

查看编译后的 SQL

SELECT prql_to_sql('from matches | filter player == "Player1"');

PRQL 语法概览

PRQL 使用管道式转换:

from employees                    # 数据源
filter department == "Engineering" # 行过滤
derive monthly_salary = salary / 12 # 计算列
sort {-monthly_salary}            # 排序(- 表示降序)
select {name, monthly_salary}     # 列投影
take 10                           # 限制行数

限制

PRQL 仅支持 SELECT 语句。对于 INSERTUPDATEDELETE,请使用 SQL 或 PL/pgSQL。


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