PG 11即将正式发布,本节简单介绍了PG 11的新特性:PL/pgSQL增强和新增的配置参数。
一、PL/pgSQL
Procedure
PG 11新增了过程Procedure对象,类似Oracle的存储过程.
testdb=# create or replace procedure sp_proc1(in p1 text) as testdb-# $$ testdb$# declare testdb$# v1 varchar(10); testdb$# begin testdb$# v1 := 'TEST'; testdb$# raise notice 'Parameter is : %',p1; testdb$# raise notice 'v1 is : %',v1; testdb$# end; testdb$# $$ testdb-# language plpgsql; CREATE PROCEDURE
过程使用CALL调用:
testdb=# call sp_proc1('test');
NOTICE: Parameter is : test
NOTICE: v1 is : TEST
CALL
查看定义信息:
testdb=# \df sp_proc1
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+----------+------------------+---------------------+------
public | sp_proc1 | | p1 text | proc
(1 row)
testdb=# \sf sp_proc1
CREATE OR REPLACE PROCEDURE public.sp_proc1(p1 text)
LANGUAGE plpgsql
AS $procedure$
declare
v1 varchar(10);
begin
v1 := 'TEST';
raise notice 'Parameter is : %',p1;
raise notice 'v1 is : %',v1;
end;
$procedure$
过程中可以对事务进行控制,但如果过程在事务中调用,而过程中有事务控制语句,则"不太好使".
存储过程:
create or replace procedure sp_transaction(in p1 text) as
$$
begin
if lower(p1) = 'commit' then
commit;
elsif lower(p1) = 'rollback' then
rollback;
else
raise notice 'Invalid Parameter!';
end if;
end;
$$
language plpgsql;
测试场景:
testdb=# begin;
BEGIN
testdb=# insert into tt values(1);
INSERT 0 1
testdb=# call sp_transaction('commit');
ERROR: invalid transaction termination
CONTEXT: PL/pgSQL function sp_transaction(text) line 4 at COMMIT
testdb=# commit;
ROLLBACK
testdb=# commit;
WARNING: there is no transaction in progress
COMMIT
PL/pgSQL似乎没有类似于Oracle自治事务的概念(在存储过程中控制事务而与外层事务无关),不建议在过程/函数中使用事务,调用方统一管理事务.变量定义
在函数或过程中,可定义变量为常量(CONSTANT关键字),并设置NOT NULL属性,详细请参照参考资料.
二、配置参数
新增的参数包括并行执行相关的参数如enable_parallel_hash等,详见下表(更详细的信息参照参考资料).
新增参数(1)
新增参数(2))
三、参考资料
PostgreSQL 11 New Features With Examples(Beta 1)
编辑推荐:
- PostgreSQL DBA(6) - PG 11 New Features#303-14
- PostgreSQL 源码解读(42)- 查询语句#27(等价类)03-14
- PostgreSQL 源码解读(43)- 查询语句#28(query_planner函数#5)03-14
- PostgreSQL 源码解读(44)- 查询语句#29(等价类相关数据结构)03-14
- PostgreSQL 源码解读(46)- 查询语句#31(query_planner函数#7)03-14
- PostgreSQL 源码解读(45)- 查询语句#30(query_planner函数#6)03-14
- PostgreSQL 源码解读(48)- 查询语句#33(query_planner函数#9)03-14
- PostgreSQL 源码解读(47)- 查询语句#32(query_planner函数#8)03-14
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- PostgreSQL DBA(6) - PG 11 New Features#3
- PostgreSQL 源码解读(27)- 查询语句#12(查询优化-上拉子链接#2)
- PostgreSQL 源码解读(28)- 查询语句#13(查询优化-上拉子链接#3)
- PostgreSQL DBA(3) - 日志分析工具pgbadger简介
PostgreSQL DBA(3) - 日志分析工具pgbadger简介
26-03-14 - PostgreSQL 源码解读(29)- 查询语句#14(查询优化-上拉子查询)
- PostgreSQL 源码解读(30)- 查询语句#15(查询优化-扁平化处理UNION ALL)
- 数据库减负刻不容缓?多级缓存设计了解一下!
数据库减负刻不容缓?多级缓存设计了解一下!
26-03-14 - PostgreSQL Page页结构解析(6)- B-Tree索引存储结构#2
- PostgreSQL 源码解读(17)- 查询语句#2(查询优化基础)
PostgreSQL 源码解读(17)- 查询语句#2(查询优化基础)
26-03-14 - PostgreSQL 源码解读(22)- 查询语句#7(PlannedStmt结构详解-日志分析)
