Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent to Oracle Packages in PostgreSQL

I'm pretty new to PostgreSQL and I wonder if such a thing exists...

I saw some discussions, but nothing concrete..

Thanks!

like image 961
Shvalb Avatar asked Jan 27 '16 17:01

Shvalb


People also ask

Does Postgres have packages like Oracle?

Postgres does not have packages, but, using schema architecture, functions and procedures can be grouped. Use the “orafce” migration tool library, which supports some of the standard packages, or EDB Postgres Advanced Server, which has built-in Packages.

Do we have packages in PostgreSQL?

In PostgreSQL, use type varchar or text instead. Similarly, replace type number with numeric , or use some other numeric data type if there's a more appropriate one. Instead of packages, use schemas to organize your functions into groups. Since there are no packages, there are no package-level variables either.

Can PostgreSQL replace Oracle?

PostgreSQL is by no means a drop-in replacement for Oracle's database, but a developer or DBA that is familiar with Oracle will find PostgreSQL similar.

What is Oracle PostgreSQL?

PostgreSQL is an open source object-relational database management system. It's highly extensible, highly scalable, and has many features. PostgreSQL supports data replication across multiple data centers.


2 Answers

No, there is no equivalent.

The only remotely similar thing would be to create one schema for each "package" and put all functions of one package into that schema. That way you have at least something like the namespace that packages give you.

This of course does not give you package private functions or package wide variables at all.

like image 171
a_horse_with_no_name Avatar answered Sep 25 '22 07:09

a_horse_with_no_name


As written before postgresql has no packages.

Example with namespace. It can be placed in single sql file.

Other methods can be added similarly.

--drop function if exists pack_exmpl.get_name(bigint);
--drop function if exists pack_exmpl.get_abbr(bigint);    
--drop schema if exists pack_exmpl;

drop schema if exists pack_exmpl cascade;
create schema pack_exmpl;

create or replace function pack_exmpl.get_name(p_id bigint)
returns varchar as $$
declare
  l_retval varchar;
begin
   select name into l_retval from example_table where id = p_id;
   return l_retval;
end;
$$ language plpgsql IMMUTABLE;

create or replace function pack_exmpl.get_abbr(p_id bigint)
returns varchar as $$
declare
  l_retval varchar;
begin
  select abbr into l_retval from example_table where id = p_id;
  return l_retval;
end;
$$ language plpgsql IMMUTABLE;
like image 38
Some Avatar answered Sep 25 '22 07:09

Some