PostgreSQL Oracle 兼容性之 - DBMS_SQL(存储过程动态SQL中使用绑定变量)

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介:

标签

PostgreSQL , Oracle , pl/sql , 存储过程 , plpgsql , 绑定变量 , DB端绑定变量 , prepare , DEALLOCATE , EXECUTE


背景

Oracle中有一个dbms_sql的包,可以让你在存储过程的动态SQL中使用prepared statement。如果动态SQL的调用频次较高,硬解析可能导致一些性能问题(Oracle硬解析的CPU消耗还是蛮大的)。

https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sql.htm#i1028953

DMBS_SQL里面包含的一些类型

Bulk SQL Types  
  
BFILE_TABLE  
  
BINARY_DOUBLE_TABLE  
  
BLOB_TABLE  
  
CLOB_TABLE  
  
DATE_TABLE  
  
INTERVAL_DAY_TO_SECOND_TABLE  
  
INTERVAL_YEAR_TO_MONTH_TABLE  
  
NUMBER_TABLE  
  
TIME_TABLE  
  
TIME_WITH_TIME_ZONE_TABLE  
  
TIMESTAMP_TABLE  
  
TIMESTAMP_WITH_LTZ_TABLE  
  
UROWID_TABLE  
  
VARCHAR2_TABLE  

其中本文用到的 NUMBER_TABLE

TYPE number_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;  

在PostgreSQL的INLINE CODE或者FUNCTION中如何使用动态SQL,如何使用绑定变量,如何使用BULK类型呢?

PostgreSQL INLINE CODE或函数的动态SQL、绑定变量使用

首先借这个链接,解释一下Oracle dbms_sql的使用

https://stackoverflow.com/questions/21335493/can-somebody-explain-dbms-sql-number-table

Better to understand DBMS_SQL itself to some extent, before understanding NUMBER_TABLE.   
( I do this for My Learning!)  
  
NUMBER_TABLE  
is Actually,  
  
TYPE number_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;  
So, only numbers are allowed!  
  
FlowChart on How DBMS_SQL Works! :  
  
Your interested area comes in bind variable box  
                        -----------  
--                    | open_cursor |  
--                      -----------  
--                           |  
--                           |  
--                           v  
--                         -----  
--          ------------>| parse |  
--          |              -----  
--          |                |  
--          |                | ---------  
--          |                v          |  
--          |           --------------  |  
--          |-------->| bind_variable | |  
--          |     ^     -------------   |  
--          |     |           |         |  
--          |      -----------|         |  
--          |                 |<--------  
--          |                 v  
--          |               query?---------- yes ---------  
--          |                 |                           |  
--          |                no                           |  
--          |                 |                           |  
--          |                 v                           v  
--          |              -------                  -------------  
--          |----------->| execute |            ->| define_column |  
--          |              -------             |    -------------  
--          |                 |------------    |          |  
--          |                 |            |    ----------|  
--          |                 v            |              v  
--          |           --------------     |           -------  
--          |       ->| variable_value |   |  ------>| execute |  
--          |      |    --------------     | |         -------  
--          |      |          |            | |            |  
--          |       ----------|            | |            |  
--          |                 |            | |            v  
--          |                 |            | |        ----------  
--          |                 |<-----------  |----->| fetch_rows |  
--          |                 |              |        ----------  
--          |                 |              |            |  
--          |                 |              |            v  
--          |                 |              |  -----------------  
--          |                 |              | | column_value    |  
--          |                 |              | | variable_value  |  
--          |                 |              |  -----------------  
--          |                 |              |            |  
--          |                 |<--------------------------  
--          |                 |  
--           -----------------|  
--                            |  
--                            v  
--                       ------------  
--                     | close_cursor |  
--                       ------------  

例子

Example: In a DELETE statement, for example, you could bind in an array in the WHERE clause and have the statement be run for each element in the array:  
  
DECLARE  
  stmt VARCHAR2(200);  
  dept_no_array DBMS_SQL.NUMBER_TABLE;  
  c NUMBER;  
  dummy NUMBER;  
begin  
  dept_no_array(1) := 10; dept_no_array(2) := 20; /* Put some values into the array */  
  dept_no_array(3) := 30; dept_no_array(4) := 40;  
  dept_no_array(5) := 30; dept_no_array(6) := 40;  
  stmt := 'delete from emp where deptno = :dept_array'; /* A Dynamic SQL String with a bind variable */  
  c := DBMS_SQL.OPEN_CURSOR; /* Open a Cursor! */  
  DBMS_SQL.PARSE(c, stmt, DBMS_SQL.NATIVE); /* Parse the Dynamic SQL , making it happen on the native database to which is connected! */  
  
  DBMS_SQL.BIND_ARRAY(c, ':dept_array', dept_no_array, 1, 4);  
  /* Bind only elements 1 through 4 to the cursor Happens 4 times */  
  
  dummy := DBMS_SQL.EXECUTE(c);  
  /* Execute the Query, and return number of rows deleted! */  
  
  DBMS_SQL.CLOSE_CURSOR(c);   
  
  EXCEPTION WHEN OTHERS THEN  
    IF DBMS_SQL.IS_OPEN(c) THEN  
      DBMS_SQL.CLOSE_CURSOR(c);  
    END IF;  
    RAISE;  
END;  
/   
  
P.S. Pure rip-off, with some more commments ,from Oracle  

PostgreSQL 服务端绑定变量的用法与之类似

PREPARE,准备DB端绑定变量SQL

EXECUTE,绑定并执行

DEALLOCATE,删除绑定变量

详见:

https://www.postgresql.org/docs/devel/static/sql-prepare.html

因此以上Oracle的代码可以改成如下:

do language plpgsql $$  
DECLARE  
  stmt VARCHAR(200);  
  dept_no_array numeric[];  
  c numeric;  
begin  
  dept_no_array[1] := 10; dept_no_array[2] := 20; /* Put some values into the array */  
  dept_no_array[3] := 30; dept_no_array[4] := 40;  
  dept_no_array[5] := 30; dept_no_array[6] := 40;  
  execute format('prepare stmt(numeric) as delete from emp where deptno = $1');    /* A Dynamic SQL String with a bind variable */  
  
  foreach c in array dept_no_array[1:4]  
  loop  
    execute format('execute stmt(%s)', c);   -- 执行绑定SQL  
  end loop;  
  
  DEALLOCATE stmt;  
  
  EXCEPTION WHEN OTHERS THEN  
    DEALLOCATE stmt;  
    RAISE;  
END;  
$$;  

如果不需要用到绑定变量,那就更简单了。

do language plpgsql $$  
DECLARE  
  dept_no_array numeric[];  
  c numeric;  
begin  
  dept_no_array[1] := 10; dept_no_array[2] := 20; /* Put some values into the array */  
  dept_no_array[3] := 30; dept_no_array[4] := 40;  
  dept_no_array[5] := 30; dept_no_array[6] := 40;  
  
  foreach c in array dept_no_array[1:4]  
  loop  
    delete from emp where deptno = c;  
  end loop;  
  
  DEALLOCATE stmt;  
  
  EXCEPTION WHEN OTHERS THEN  
    DEALLOCATE stmt;  
    RAISE;  
END;  
$$;  

例子

建表

do language plpgsql $$
declare
begin
  execute 'drop table if exists test';
  execute 'create table test(id int primary key, info text, crt_time timestamp)';
  
  for i in 0..1023 loop
    execute format('drop table if exists test%s', i);
    execute format('create table test%s (like test including all)', i);
  end loop;
end;
$$;

使用动态SQL,写入目标子表

create or replace function dyn_pre(int) returns void as $$
declare
  suffix int := mod($1,1024);
begin
  execute format('insert into test%s values(%s, md5(random()::text), now()) on conflict(id) do update set info=excluded.info,crt_time=excluded.crt_time', suffix, $1);
end;
$$ language plpgsql strict;

使用绑定变量,写入目标子表

create or replace function dyn_pre(int) returns void as $$
declare
  suffix int := mod($1,1024);
begin
  execute format('execute p%s(%s)', suffix, $1);
  exception when others then
    execute format('prepare p%s(int) as insert into test%s values($1, md5(random()::text), now()) on conflict(id) do update set info=excluded.info,crt_time=excluded.crt_time', suffix, suffix);
    execute format('execute p%s(%s)', suffix, $1);
end;
$$ language plpgsql strict;

性能对比

vi test.sql

\set id random(1,1000000000)
select dyn_pre(:id);

pgbench -M prepared -n -r -P 1 -f ./test.sql -c 56 -j 56 -T 120

1、使用动态SQL,TPS约5.2万。

2、使用函数内绑定变量,TPS约13.4万。

参考

https://www.postgresql.org/docs/devel/static/sql-prepare.html

https://stackoverflow.com/questions/21335493/can-somebody-explain-dbms-sql-number-table

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
SQL Java 编译器
SQL 语言:嵌入式 SQL 和动态 SQL
SQL 语言:嵌入式 SQL 和动态 SQL
415 4
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
12月前
|
Oracle 关系型数据库 数据库
【赵渝强老师】在PostgreSQL中访问Oracle
本文介绍了如何在PostgreSQL中使用oracle_fdw扩展访问Oracle数据库数据。首先需从Oracle官网下载三个Instance Client安装包并解压,设置Oracle环境变量。接着从GitHub下载oracle_fdw扩展,配置pg_config环境变量后编译安装。之后启动PostgreSQL服务器,在数据库中创建oracle_fdw扩展及外部数据库服务,建立用户映射。最后通过创建外部表实现对Oracle数据的访问。文末附有具体操作步骤与示例代码。
1125 6
【赵渝强老师】在PostgreSQL中访问Oracle
|
存储 SQL 编译器
【YashanDB知识库】列与存储过程中重名变量/别名问题
在SQL查询或存储过程中,若出现重复别名或变量名与别名相同,会导致报错。此问题已在多个客户现场发生,风险在于报错难以定位。截止2024年4月,最新版本仍存在该问题。原因是变量未分层处理。解决方法是修改变量名称以避免重名。建议编写SQL时区分变量名,防止重名导致的问题。
|
SQL Oracle 关系型数据库
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
我们经常发现,部分国产数据库声称与 Oracle兼容性高达90%,但在实际迁移过程中,仍需要频繁地修改业务应用的代码。为何实现与Oracle高兼容度的数据库产品如此困难?其中一个重要原因是Oracle兼容性不仅是模仿,而是一个非常复杂和工程量庞大的逆向工程。其技术实现的复杂性以及多如牛毛的细节,足以让多数“年轻”的数据库团队望洋兴叹。YashanDB作为一款从核心理论到关键系统均为原创的数据库产品,从构建初期就具备了技术优势,在Oracle兼容性实现上,敢于亮剑并充分发挥工匠精神,不断打磨,努力构筑一个真正形神兼备的数据库产品。以下将从YashanDB SQL引擎技术、Oracle兼容性的开发
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
Oracle兼容性是目前国产数据库的关键任务之一,其直接影响到商业迁移的成本和竞争力。
294 8
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
3237 3
|
人工智能 Oracle 关系型数据库
一篇文章弄懂Oracle和PostgreSQL的Database Link
一篇文章弄懂Oracle和PostgreSQL的Database Link
|
SQL 缓存 Oracle
SQL调优之绑定变量用法简介
SQL调优之绑定变量用法简介

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多