pllua
Lua 程序语言
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pllua | 2.0.12 | LANG | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 3020 | pllua | 否 | 是 | 否 | 是 | 否 | 否 | pg_catalog |
| 3021 | hstore_pllua | 否 | 是 | 否 | 是 | 否 | 是 | - |
| 3030 | plluau | 否 | 是 | 否 | 是 | 否 | 否 | pg_catalog |
| 3031 | hstore_plluau | 否 | 是 | 否 | 是 | 否 | 是 | pg_catalog |
| 相关扩展 | plperl plpgsql plpython3u pg_tle plv8 pljava plperlu |
|---|---|
| 下游依赖 | hstore_pllua |
missing pg12-15 on el.aarch64
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 2.0.12 | 1817161514 | pllua | - |
| RPM | PGDG | 2.0.12 | 1817161514 | pllua_$v | - |
| DEB | PGDG | 2.0.12 | 1817161514 | postgresql-$v-pllua | - |
| OS / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PGDG MISS | PGDG 2.0.12 el8.x86_64.pg17 : pllua_17 pllua_17-2.0.12-3PGDG.rhel8.x86_64.rpm
| PGDG 2.0.12 el8.x86_64.pg16 : pllua_16 pllua_16-2.0.12-1PGDG.rhel8.x86_64.rpm
| PGDG 2.0.11 | PGDG 2.0.11 |
| el8.aarch64 | PGDG MISS | PGDG 2.0.12 el8.aarch64.pg17 : pllua_17 pllua_17-2.0.12-3PGDG.rhel8.aarch64.rpm
| PGDG 2.0.12 el8.aarch64.pg16 : pllua_16 pllua_16-2.0.12-1PGDG.rhel8.aarch64.rpm
| PGDG MISS | PGDG MISS |
| el9.x86_64 | PGDG MISS | PGDG 2.0.12 el9.x86_64.pg17 : pllua_17 pllua_17-2.0.12-3PGDG.rhel9.x86_64.rpm
| PGDG 2.0.12 el9.x86_64.pg16 : pllua_16 pllua_16-2.0.12-1PGDG.rhel9.x86_64.rpm
| PGDG 2.0.11 | PGDG 2.0.11 el9.x86_64.pg14 : pllua_14 pllua_14-2.0.11-1.rhel9.x86_64.rpm
|
| el9.aarch64 | PGDG MISS | PGDG 2.0.12 el9.aarch64.pg17 : pllua_17 pllua_17-2.0.12-3PGDG.rhel9.aarch64.rpm
| PGDG 2.0.12 el9.aarch64.pg16 : pllua_16 pllua_16-2.0.12-1PGDG.rhel9.aarch64.rpm
| PGDG MISS | PGDG MISS |
| el10.x86_64 | PGDG MISS | PGDG 2.0.12 el10.x86_64.pg17 : pllua_17 pllua_17-2.0.12-4PGDG.rhel10.x86_64.rpm
| PGDG 2.0.12 el10.x86_64.pg16 : pllua_16 pllua_16-2.0.12-4PGDG.rhel10.x86_64.rpm
| PGDG 2.0.12 el10.x86_64.pg15 : pllua_15 pllua_15-2.0.12-4PGDG.rhel10.x86_64.rpm
| PGDG 2.0.12 el10.x86_64.pg14 : pllua_14 pllua_14-2.0.12-4PGDG.rhel10.x86_64.rpm
|
| el10.aarch64 | PGDG MISS | PGDG 2.0.12 el10.aarch64.pg17 : pllua_17 pllua_17-2.0.12-4PGDG.rhel10.aarch64.rpm
| PGDG 2.0.12 el10.aarch64.pg16 : pllua_16 pllua_16-2.0.12-4PGDG.rhel10.aarch64.rpm
| PGDG 2.0.12 el10.aarch64.pg15 : pllua_15 pllua_15-2.0.12-4PGDG.rhel10.aarch64.rpm
| PGDG 2.0.12 el10.aarch64.pg14 : pllua_14 pllua_14-2.0.12-4PGDG.rhel10.aarch64.rpm
|
| d12.x86_64 | PGDG 2.0.12 d12.x86_64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg12+1_amd64.deb
| PGDG 2.0.12 d12.x86_64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg12+1_amd64.deb
| PGDG 2.0.12 d12.x86_64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg12+1_amd64.deb
| PGDG 2.0.12 d12.x86_64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg12+1_amd64.deb
| PGDG 2.0.12 d12.x86_64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg12+1_amd64.deb
|
| d12.aarch64 | PGDG 2.0.12 d12.aarch64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg12+1_arm64.deb
| PGDG 2.0.12 d12.aarch64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg12+1_arm64.deb
| PGDG 2.0.12 d12.aarch64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg12+1_arm64.deb
| PGDG 2.0.12 d12.aarch64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg12+1_arm64.deb
| PGDG 2.0.12 d12.aarch64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg12+1_arm64.deb
|
| d13.x86_64 | PGDG 2.0.12 d13.x86_64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg13+1_amd64.deb
| PGDG 2.0.12 d13.x86_64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg13+1_amd64.deb
| PGDG 2.0.12 d13.x86_64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg13+1_amd64.deb
| PGDG 2.0.12 d13.x86_64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg13+1_amd64.deb
| PGDG 2.0.12 d13.x86_64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg13+1_amd64.deb
|
| d13.aarch64 | PGDG 2.0.12 d13.aarch64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg13+1_arm64.deb
| PGDG 2.0.12 d13.aarch64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg13+1_arm64.deb
| PGDG 2.0.12 d13.aarch64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg13+1_arm64.deb
| PGDG 2.0.12 d13.aarch64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg13+1_arm64.deb
| PGDG 2.0.12 d13.aarch64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg13+1_arm64.deb
|
| u22.x86_64 | PGDG 2.0.12 u22.x86_64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg22.04+1_amd64.deb
| PGDG 2.0.12 u22.x86_64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg22.04+1_amd64.deb
| PGDG 2.0.12 u22.x86_64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg22.04+1_amd64.deb
| PGDG 2.0.12 u22.x86_64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg22.04+1_amd64.deb
| PGDG 2.0.12 u22.x86_64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg22.04+1_amd64.deb
|
| u22.aarch64 | PGDG 2.0.12 u22.aarch64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg22.04+1_arm64.deb
| PGDG 2.0.12 u22.aarch64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg22.04+1_arm64.deb
| PGDG 2.0.12 u22.aarch64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg22.04+1_arm64.deb
| PGDG 2.0.12 u22.aarch64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg22.04+1_arm64.deb
| PGDG 2.0.12 u22.aarch64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg22.04+1_arm64.deb
|
| u24.x86_64 | PGDG 2.0.12 u24.x86_64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg24.04+1_amd64.deb
| PGDG 2.0.12 u24.x86_64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg24.04+1_amd64.deb
| PGDG 2.0.12 u24.x86_64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg24.04+1_amd64.deb
| PGDG 2.0.12 u24.x86_64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg24.04+1_amd64.deb
| PGDG 2.0.12 u24.x86_64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg24.04+1_amd64.deb
|
| u24.aarch64 | PGDG 2.0.12 u24.aarch64.pg18 : postgresql-18-pllua postgresql-18-pllua_2.0.12-7.pgdg24.04+1_arm64.deb
| PGDG 2.0.12 u24.aarch64.pg17 : postgresql-17-pllua postgresql-17-pllua_2.0.12-7.pgdg24.04+1_arm64.deb
| PGDG 2.0.12 u24.aarch64.pg16 : postgresql-16-pllua postgresql-16-pllua_2.0.12-7.pgdg24.04+1_arm64.deb
| PGDG 2.0.12 u24.aarch64.pg15 : postgresql-15-pllua postgresql-15-pllua_2.0.12-7.pgdg24.04+1_arm64.deb
| PGDG 2.0.12 u24.aarch64.pg14 : postgresql-14-pllua postgresql-14-pllua_2.0.12-7.pgdg24.04+1_arm64.deb
|
安装
您可以直接安装 pllua 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:
pig repo add pgdg -u # 添加 PGDG 仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pllua; # 当前活跃 PG 版本安装
pig ext install -y pllua -v 18 # PG 18
pig ext install -y pllua -v 17 # PG 17
pig ext install -y pllua -v 16 # PG 16
pig ext install -y pllua -v 15 # PG 15
pig ext install -y pllua -v 14 # PG 14
dnf install -y pllua_18 # PG 18
dnf install -y pllua_17 # PG 17
dnf install -y pllua_16 # PG 16
dnf install -y pllua_15 # PG 15
dnf install -y pllua_14 # PG 14
apt install -y postgresql-18-pllua # PG 18
apt install -y postgresql-17-pllua # PG 17
apt install -y postgresql-16-pllua # PG 16
apt install -y postgresql-15-pllua # PG 15
apt install -y postgresql-14-pllua # PG 14
创建扩展:
CREATE EXTENSION pllua;
用法
pllua 允许在 PostgreSQL 中使用 Lua(5.3、5.4 或 LuaJIT 2.1)编写函数。
CREATE EXTENSION pllua;
创建函数
CREATE FUNCTION lua_max(a integer, b integer) RETURNS integer LANGUAGE pllua AS $$
if a > b then return a else return b end
$$;
CREATE FUNCTION hello(name text) RETURNS text LANGUAGE pllua AS $$
return "Hello, " .. name .. "!"
$$;
数据类型处理
参数会自动转换:整数/浮点数转为 Lua 数字,text/varchar 转为字符串,布尔值转为 Lua 布尔值,NULL 转为 nil。其他类型保持为 datum 对象。
使用 pgtype 构造类型化的值:
pgtype.numeric(1234)
pgtype.date('2017-12-01')
pgtype.array.integer(1, 2, 3, 4)
pgtype.numrange(1, 2)
复合类型(行)
row.columnname -- 按名称访问
row[3] -- 按属性编号访问
for colname, value, attnum in pairs(row) do ... end
集合返回函数
CREATE FUNCTION generate_n(n integer) RETURNS SETOF integer LANGUAGE pllua AS $$
for i = 1, n do
coroutine.yield(i)
end
$$;
SPI 数据库访问
-- 简单查询
local rows = spi.execute("SELECT * FROM mytable WHERE id = $1", 42)
-- 行迭代器
for row in spi.rows("SELECT * FROM mytable") do
print(row.name)
end
-- 预备语句
local stmt = spi.prepare("SELECT * FROM users WHERE id = $1", {'integer'})
local result = stmt:execute(42)
for row in stmt:rows(42) do ... end
游标
local cursor = spi.newcursor()
cursor:open("SELECT * FROM items")
local rows = cursor:fetch(10)
cursor:move(5)
cursor:close()
触发器函数
CREATE FUNCTION my_trigger() RETURNS trigger LANGUAGE pllua AS $$
function(trigger, old, new)
trigger.row = new
return trigger.row
end
$$;
触发器字段:trigger.event(INSERT/UPDATE/DELETE)、trigger.when(BEFORE/AFTER)、trigger.level(ROW/STATEMENT)、trigger.new、trigger.old、trigger.row。
错误处理
spi.error('division_by_zero', 'Cannot divide by zero')
spi.notice('informational message')
spi.warning('warning message')
-- 使用 pcall 进行子事务
local ok, err = pcall(function()
spi.execute("INSERT INTO mytable VALUES ($1)", val)
end)
日志记录
print("info message")
spi.debug("debug")
spi.notice("notice")
spi.warning("warning")
spi.error("error")