PostgreSQL Oracle 兼容性 之 TABLE、PIPELINED函数

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , 返回表 , 返回复合类型 , 返回游标


背景

Oracle 通过table, pipelined函数,用于格式化返回类型为table的函数的结果。

Table function concepts

There a couple of steps to take when you are working with table functions. Like when you are working with normal tables you have to describe the way the records of the collection are set up.

例子:

-- 定义复合类型  
CREATE TYPE script_line AS OBJECT  
(line NUMBER   ,text VARCHAR2(32767))  
  
-- 定义表类型  
CREATE or replace TYPE script_lines AS TABLE OF script_line;  
  
-- 定义返回表类型的函数  
CREATE OR REPLACE FUNCTION createinsertscriptfor(  
tablename_in IN VARCHAR2 ) RETURN script_lines  
....  
  
-- 通过TABLE格式化返回表类型的函数的结果集  
select * from TABLE(createinsertscriptfor('xx'));  

pipelinedb的功能与table类似,不过它是流式返回,不需要等所有结果接收完后再格式化输出。

Functions can be pipelined. This means the results will become available when they are produced. As soon as a result is available instead of adding it to the nested table it will be piped out of the function.

例子:

CREATE OR REPLACE FUNCTION createinsertscriptfor(  
tablename_in IN VARCHAR2 ) RETURN script_lines PIPELINED  
.....  

PostgreSQL TABLE、PIPELINED兼容性

1、PostgreSQL 建表时,自动建立对应的表类型、表数组类型。

例子

postgres=# create table abc(id int, info text);  
CREATE TABLE  
  
自动建立对应的复合类型以及复合数组类型。  
postgres=# select * from pg_type where typname ~ 'abc';  
 typname | typnamespace | typowner | typlen | typbyval | typtype | typcategory | typispreferred | typisdefined | typdelim | typrelid | typelem | typarray | typinput  | typoutput  | typreceive  |   typsend   | typmodin | typmodout |    ty  
panalyze    | typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typcollation | typdefaultbin | typdefault | typacl   
---------+--------------+----------+--------+----------+---------+-------------+----------------+--------------+----------+----------+---------+----------+-----------+------------+-------------+-------------+----------+-----------+------  
------------+----------+------------+------------+-------------+-----------+----------+--------------+---------------+------------+--------  
 abc     |        34201 |       10 |     -1 | f        | c       | C           | f              | t            | ,        |    34634 |       0 |    34635 | record_in | record_out | record_recv | record_send | -        | -         | -      
            | d        | x          | f          |           0 |        -1 |        0 |            0 |               |            |   
 _abc    |        34201 |       10 |     -1 | f        | b       | A           | f              | t            | ,        |        0 |   34636 |        0 | array_in  | array_out  | array_recv  | array_send  | -        | -         | array  
_typanalyze | d        | x          | f          |           0 |        -1 |        0 |            0 |               |            |   
(2 rows)  
  
将一个字符串转换为复合类型  
  
postgres=# select '(1,hello)'::abc;  
    abc      
-----------  
 (1,hello)  
(1 row)  
  
postgres=# select ('(1,hello)'::abc).id;  
 id   
----  
  1  
(1 row)  
  
postgres=# select ('(1,hello)'::abc).info;  
 info    
-------  
 hello  
(1 row)  
  
删表后自动删除  
  
postgres=# drop table abc;  
DROP TABLE  
postgres=# select * from pg_type where typname ~ 'abc';  
 typname | typnamespace | typowner | typlen | typbyval | typtype | typcategory | typispreferred | typisdefined | typdelim | typrelid | typelem | typarray | typinput | typoutput | typreceive | typsend | typmodin | typmodout | typanalyze |  
 typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typcollation | typdefaultbin | typdefault | typacl   
---------+--------------+----------+--------+----------+---------+-------------+----------------+--------------+----------+----------+---------+----------+----------+-----------+------------+---------+----------+-----------+------------+  
----------+------------+------------+-------------+-----------+----------+--------------+---------------+------------+--------  
(0 rows)  

2、PostgreSQL 支持自定义复合类型,定义复合类型时,自动建立对应的复合数组类型。

例子

postgres=# create type typ1 as (id int, c1 int, c2 date);  
CREATE TYPE  
postgres=# select * from pg_type where typname ~ 'typ1';  
 typname | typnamespace | typowner | typlen | typbyval | typtype | typcategory | typispreferred | typisdefined | typdelim | typrelid | typelem | typarray | typinput  | typoutput  | typreceive  |   typsend   | typmodin | typmodout |    ty  
panalyze    | typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typcollation | typdefaultbin | typdefault | typacl   
---------+--------------+----------+--------+----------+---------+-------------+----------------+--------------+----------+----------+---------+----------+-----------+------------+-------------+-------------+----------+-----------+------  
------------+----------+------------+------------+-------------+-----------+----------+--------------+---------------+------------+--------  
 typ1    |        34201 |       10 |     -1 | f        | c       | C           | f              | t            | ,        |    34646 |       0 |    34647 | record_in | record_out | record_recv | record_send | -        | -         | -      
            | d        | x          | f          |           0 |        -1 |        0 |            0 |               |            |   
 _typ1   |        34201 |       10 |     -1 | f        | b       | A           | f              | t            | ,        |        0 |   34648 |        0 | array_in  | array_out  | array_recv  | array_send  | -        | -         | array  
_typanalyze | d        | x          | f          |           0 |        -1 |        0 |            0 |               |            |   
(2 rows)  
  
将一个字符串转换为复合类型  
  
postgres=# select '(1,2,20170901)'::typ1;  
       typ1         
------------------  
 (1,2,2017-09-01)  
(1 row)  
  
postgres=# select ('(1,2,20170901)'::typ1).*;  
 id | c1 |     c2       
----+----+------------  
  1 |  2 | 2017-09-01  
(1 row)  

3、PostgreSQL 函数支持返回不定义结构的类型record,或者定义结构的类型table, type, comp type等。

例子1,返回record,在查询时对其结构化。

create or replace function ftest1() returns record as $$  
declare  
begin  
  return (1,2,3,4);  
end;  
$$ language plpgsql strict;  
  
postgres=# select * from ftest1() as t(c1 int, c2 int, c3 int, c4 int);  
 c1 | c2 | c3 | c4   
----+----+----+----  
  1 |  2 |  3 |  4  
(1 row)  

例子2,返回record,同时定义OUT参数,在查询时无需结构化。

postgres=# DROP FUNCTION ftest1();  
DROP FUNCTION  
postgres=# create or replace function ftest1(OUT C1 INT, OUT C2 TEXT) returns RECORD as $$  
declare  
begin  
  c1:=1;   
  c2:='abcde';   
  return;  
end;  
$$ language plpgsql strict;  
CREATE FUNCTION  
  
postgres=# select * from ftest1();  
 c1 |  c2     
----+-------  
  1 | abcde  
(1 row)  

例子,返回table,在查询时无需结构化

postgres=# create table abcd(id int, info text);  
CREATE TABLE  
  
postgres=# create or replace function ftest2() returns abcd as $$  
declare  
begin  
  return (1, 'hello')::abcd;  
end;  
$$ language plpgsql strict;  
CREATE FUNCTION  
  
postgres=# select * from ftest2();  
 id | info    
----+-------  
  1 | hello  
(1 row)  

4、PostgreSQL 函数支持返回数组。

例子

postgres=# create or replace function ftest3() returns abcd[] as $$  
declare   
  res abcd[];  
begin  
  res := array[(1, 'hello')::abcd];   
  res := array_cat(res, array[(2,'digoal')::abcd]);  
  return res;   
end;  
$$ language plpgsql strict;  
CREATE FUNCTION  
  
postgres=# select * from ftest3();  
           ftest3             
----------------------------  
 {"(1,hello)","(2,digoal)"}  
(1 row)  

5、PostgreSQL 函数支持返回多条记录。

例子

postgres=# create or replace function ftest4() returns setof abcd[] as $$  
declare   
  res abcd[];  
begin  
  res := array[(1, 'hello')::abcd];   
  res := array_cat(res, array[(2,'digoal')::abcd]);  
  return next res;   
  return next array_cat(res,res);   
  return;   
end;  
$$ language plpgsql strict;  
CREATE FUNCTION  
  
postgres=# select * from ftest4();  
                       ftest4                          
-----------------------------------------------------  
 {"(1,hello)","(2,digoal)"}  
 {"(1,hello)","(2,digoal)","(1,hello)","(2,digoal)"}  
(2 rows)  

6、PostgreSQL 函数支持返回游标(对应Oracle pipeline流式返回)。

postgres=# create or replace function ftest5() returns refcursor as $$  
declare  
  res refcursor := 'cur_1';  
begin   
  open res for select relname, relkind, relpages from pg_class ;  
  return res;  
end;  
$$ language plpgsql strict;  
CREATE FUNCTION  
  
  
postgres=# begin;  
BEGIN  
postgres=# select * from ftest5();  
 ftest5   
--------  
 cur_1  
(1 row)  
  
postgres=# fetch next from cur_1;  
 relname | relkind | relpages   
---------+---------+----------  
 seq     | S       |        1  
(1 row)  
  
postgres=# fetch next from cur_1;  
      relname      | relkind | relpages   
-------------------+---------+----------  
 idx_train_order_1 | i       |        1  
(1 row)  

参考

http://docs.oracle.com/cd/B19306_01/appdev.102/b14289/dcitblfns.htm#CHDCIEJG

https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:821242100346022602

https://technology.amis.nl/2014/03/31/using-table-functions-2/

《金融风控、公安刑侦、社会关系、人脉分析等需求分析与数据库实现 - PostgreSQL图数据库场景应用》

《PostgreSQL Oracle 兼容性之 - PL/SQL pipelined》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
27天前
|
Oracle 关系型数据库 分布式数据库
PolarDB常见问题之PolarDB(Oracle兼容版) 执行命令报错如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
2月前
|
SQL 存储 缓存
PostgreSQL函数管理接口
学习PostgreSQL服务端开发必须要对函数管理接口有比较深入的了解
141 0
|
1月前
|
Oracle 关系型数据库 数据库
Oracle数据恢复—Oracle数据库误truncate table的数据恢复案例
北京某国企客户Oracle 11g R2数据库误truncate table CM_CHECK_ITEM_HIS,表数据丢失,业务查询到该表时报错,数据库的备份不可用,无法查询表数据。 Oracle数据库执行Truncate命令的原理:在执行Truncate命令后ORACLE会在数据字典和Segment Header中更新表的Data Object ID,但不会修改实际数据部分的块。由于数据字典与段头的DATA_OBJECT_ID与后续的数据块中的并不一致,所以ORACLE服务进程在读取全表数据时不会读取到已经被TRUNCATE的记录,但是实际数据未被覆盖。
Oracle数据恢复—Oracle数据库误truncate table的数据恢复案例
|
1月前
|
关系型数据库 PostgreSQL
postgresql日程排程函数的编写实例
postgresql日程排程函数的编写实例
|
2月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
42 7
|
4月前
|
SQL 关系型数据库 C语言
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
43 0
|
4月前
|
SQL 关系型数据库 数据库
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
47 0
|
4月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
54 0
|
4月前
|
SQL 关系型数据库 编译器
PostgreSQL SQL扩展 ---- C语言函数(二)
可以用C(或者与C兼容,比如C++)语言编写用户自定义函数(User-defined functions)。这些函数被编译到动态可加载目标文件(也称为共享库)中并被守护进程加载到服务中。“C语言函数”与“内部函数”的区别就在于动态加载这个特性,二者的实际编码约定本质上是相同的(因此,标准的内部函数库为用户自定义C语言函数提供了丰富的示例代码)
65 0

相关产品

  • 云原生数据库 PolarDB