Functions and operators that emulate a subset of functions and packages from the Oracle RDBMS.
This module contains some useful function which can help with porting Oracle application to PostgreSQL or can be useful generally.
Built-in Oracle date functions have been tested against Oracle 10 for comformance. Date ranges from 1960 to 2070 work correctly. Dates before 1100-03-01 cannot be verified due to a bug in Oracle.
All functions are fully compatible with Oracle and respect all known format strings. Detailed description you can find on internet. Find keywords oracle round trunc date iyyy.
Y,YY,YYY,YYYY,SYYY,SYEAR year I,IY,IYY,IYYY iso year Q, quarter WW week, day as first day of year IW week, beginning Monday W week, day as first day of month DAY,DY,D first day of week, sunday MONTH,MON,MM,RM month CC,SCC century DDD,DD,J day HH,HH12,HH24 hour MI minute
Functions round up. That is, a date of July 1st will be rounded to the next year. The 16th of July will be rounded to August.
-
add_months(date, integer) date - Returns date plus n months
add_months(date '2005-05-31',1) -> 2005-06-30
-
last_date(date) date - Returns last day of the month based on a date value
last_day(date '2005-05-24') -> 2005-05-31
-
next_day(date, text) date - Returns the first weekday that is greater than a date value
next_day(date '2005-05-24', 'monday') -> 2005-05-30
-
next_day(date, integer) date - Same as above. The second argument should be 1..7 and interpreted as Sunday..Satday.
next_day(date '2005-05-24', 1) -> 2005-05-30
-
months_between(date, date) float8 - Returns the number of months between date1 and date2. If a fractional month is calculated, the months_between function calculates the fraction based on a 31-day month.
months_between(date '1995-02-02', date '1995-01-01') -> 1.0322580645161
-
trunc(date, text) date - truncate date according to the specified format
trunc(date '2005-07-12', 'iw') -> 2005-07-11
-
round(date, text) date - will round dates according to the specified format
round(date '2005-07-12', 'yyyy') -> 2006-01-01
-
to_date(text) timestamp - will typecast input text to timestamp. The GUC orafce.nls_date_format is used to specify input text format for this function. If the value is left blank or set as DEFAULT then input text format according to PostgreSQL’s datestyle GUC setting.
orafce.nls_date_format value to DEFAULT
to_date('2014-05-19 17:23:53+5:30') -> 2014-05-19 17:23:53
orafce.nls_date_format='YYYY-MMDD HH24:MI:SS'
to_date('2014-0519 17:23:53+5:30') -> 2014-05-19 17:23:53
This module contain implementation of oracle compatible DATE data type "oracle.date" and functions which are using DATE data type like oracle.add_months,oracle.last_day(),oracle.next_day(),oracle.months_between() etc.
Example:
set search_path TO oracle,"$user", public, pg_catalog; create table oracle_date(col1 date); insert into oracle_date values('2014-06-24 12:12:11'::date); select * from oracle_date; col1 --------------------- 2014-06-24 12:12:11 (1 row)
-
oracle.add_months(timestamp with time zone, integer) - Returns date and time plus n months
oracle.add_months(oracle.date'2005-05-31 10:12:12',1) -> 2005-06-30 10:12:12
-
oracle.last_day(timestamp with time zone) - Returns last day of the month based on a date value
oracle.last_day(oracle.date '2005-05-24 11:12:12') -> 2005-05-31 11:12:12
-
oracle.next_day(timestamp with time zone, text) - Returns the first weekday that is greater than a date value
oracle.next_day(oracle.date '2005-05-24 10:12:12', 'monday') -> 2005-05-30 10:12:12
-
oracle.next_day(timestamp with time zone, integer) - Same as above. The second argument should be 1..7 and interpreted as Sunday..Satday.
oracle.next_day(oracle.date '2005-05-24 11:21:12', 1) -> 2005-05-29 11:21:12
-
oracle.months_between(timestamp with time zone, timestamp with time zone) - Returns the number of months between timestamp1 and timestamp2. If a fractional month is calculated, the months_between function calculates the fraction based on a 31-day month.
oracle.months_between(oracle.date '1995-02-02 10:00:00', oracle.date '1995-01-01 10:21:11') -> 1.03225806451613
-
oracle.to_date(text,text) - Returns timestamp without time zone.
oracle.to_date('02/16/09 04:12:12', 'MM/DD/YY HH24:MI:SS') -> 2009-02-16 04:12:12
-
oracle.to_date(text) - Returns oracle.date
oracle.to_date('02/16/09 04:12:12') -> 2009-02-16 04:12:12
-
oracle.to_char(timestamp) - Returns timestamp in nls_date_format.
orafce.nls_date_format='YY-MonDD HH24:MI:SS'
oracle.to_char(to_date('14-Jan08 11:44:49+05:30')) -> 14-Jan08 11:44:49
orafce.nls_date_format='YY-MonDD HH24:MI:SS'
oracle.to_char(oracle.to_date('21052014 12:13:44+05:30','DDMMYYYY HH24:MI:SS')) -> 14-May21 12:13:44
-
oracle.+(oracle.date,smallint) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') + 9::smallint -> 2014-07-11 10:08:55
-
oracle.+(oracle.date,integer) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') + 9::integer -> 2014-07-11 10:08:55
-
oracle.+(oracle.date,bigint) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') + 9::bigint -> 2014-07-11 10:08:55
-
oracle.+(oracle.date,numeric) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') + 9::numeric -> 2014-07-11 10:08:55
-
oracle.-(oracle.date,smallint) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') - 9::smallint -> 2014-06-23 10:08:55
-
oracle.-(oracle.date,integer) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') - 9::integer -> 2014-06-23 10:08:55
-
oracle.-(oracle.date,bigint) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') - 9::bigint -> 2014-06-23 10:08:55
-
oracle.-(oracle.date,numeric) - Returns oracle.date
oracle.to_date('2014-07-02 10:08:55','YYYY-MM-DD HH:MI:SS') - 9::numeric -> 2014-06-23 10:08:55
-
oracle.-(oracle.date,oracle.date) - Returns double precision
oracle.to_date('2014-07-17 11:10:15', 'yyyy-mm-dd hh24:mi:ss') - oracle.to_date('2014-02-01 10:00:00', 'yyyy-mm-dd hh24:mi:ss') -> 166.048785
You need to set search_path TO oracle,"$user", public, pg_catalog because the functions like oracle.add_months,oracle.last_day,oracle.next_day,oracle.months_between is installed side-by-side with pg_catalog.add_months,pg_catalog.last_day,pg_catalog.next_day,pg_catalog.months_between.
PostgreSQL does not need Oracle’s table 'dual', but since it is intensively used by Oracle users, we create it.
PostgreSQL sends information to the client via RAISE NOTICE. Oracle uses dbms_output.put_line(). This works differently to RAISE NOTICE. Oracle has a session queue, put_line() adds a line to the queue and the function get_line() reads from queue. If flag 'serveroutput' is set, then client over all sql statements reads queue. You can use:
select dbms_output.enable(); select dbms_output.put_line('first_line'); select dbms_output.put_line('next_line'); select * from dbms_output.get_lines(0);
or
select dbms_output.enable(); select dbms_output.serveroutput('t'); select dbms_output.put_line('first_line');
This package contains the following functions: enable(), disable(), serveroutput(), put(), put_line(), new_line(), get_line(), get_lines(). The package queue is implemented in the session’s local memory.
This package allows PL/pgSQL prgrams read from and write to any files that are accessible from server. Every session can open maximaly ten files and max line size is 32K. This package contains functions:
-
utl_file.fclose(file utl_file.file_type) - close file
-
utl_file.fclose_all() - close all files
-
utl_file.fcopy(src_location, src_filename, dest_location, dest_filename[, start_line][, end_line]) - copy text file
-
utl_file.fflush(file utl_file.file_type) - flushes all data from buffers
-
utl_file.fgetattr(location, filename) - get file attributes
-
utl_file.fopen(location text, filename text, file_mode text [, maxlinesize int] [, encoding name]) utl_file.file_type - open file
-
utl_file.fremove(location, filename) - remove file
-
utl_file.frename(location, filename, dest_dir, dest_file[, overwrite]) - rename file
-
utl_file.get_line(file utl_file.file_type) text - read one line from file
-
utl_file.get_nextline(file utl_file.file_type) text - read one line from file or returns NULL
-
utl_file.is_open(file utl_file.file_type) bool - returns true, if file is opened
-
utl_file.new_line(file utl_file.file_type [,rows int]) - puts some new line chars to file
-
utl_file.put(file utl_file.file_type, buffer text) - puts buffer to file
-
utl_file.put_line(file utl_file.file_type, buffer text) - puts line to file
-
utl_file.putf(file utl_file.file_type, format buffer [,arg1 text][,arg2 text][..][,arg5 text]) - put formated text into file
-
utl_file.tmpdir() - get path of temp directory
Because PostgreSQL doesn’t support call by reference, some function’s are gently different: fclose and get_line.
declare f utl_file.file_type; begin f := utl_file.fopen('/tmp', 'sample.txt', 'r'); <<readl>> loop begin raise notice '%', utl_file.get_line(f); exception when no_data_found then exit readl; end; end loop; f := fclose(f); end;
or second (with PostgreSQL specific function get_nextline)
declare f utl_file.file_type; line text; begin f := utl_file.fopen('/tmp', 'sample.txt', 'r'); loop line := utl_file.get_nextline(f); exit when line is NULL; raise notice '%', line; exception when others then utl_file.fclose_all(); end;
Before using package you have to set table utl_file.utl_file_dir. This contains all allowed directories without ending symbol ('/' or '\'). On WinNT platform you have to put locality parametr with ending symbol '\' everytime.
This package is an emulation of Oracle’s package dbms_pipe. It provides inter-session comunication. You can send and read any message with or without waiting; list active pipes; set a pipe as private or public; and, use explicit or implicit pipes.
The maximum number of pipes is 50.
Shared memory is used to send messages.
An example follows:
-- Session A select dbms_pipe.create_pipe('my_pipe',10,true); -- explicit pipe creating select dbms_pipe.pack_message('neco je jinak'); select dbms_pipe.pack_message('anything is else'); select dbms_pipe.send_message('my_pipe',20,0); -- change limit and send without waiting select * from dbms_pipe.db_pipes; -- list of current pipes -- Session B select dbms_pipe.receive_message('my_pipe',1); -- wait max 1 sec for message select dbms_pipe.next_item_type(); -- -> 11, text select dbms_pipe.unpack_message_text(); select dbms_pipe.next_item_type(); -- -> 11, text select dbms_pipe.unpack_message_text(); select dbms_pipe.next_item_type(); -- -> 0, no more items select dbms_pipe.remove_pipe('my_pipe');
There are some differences compared to Oracle, however:
-
limit for pipes isn’t in bytes but in elements in pipe
-
you can send message without waiting
-
you can send empty messages
-
next_item_type knows about TIMESTAMP (type 13)
-
PostgreSQL don’t know about the RAW type, use bytea instead
Another means of inter-process communication.
-- Session A select dbms_alert.register('boo'); select * from dbms_alert.waitany(10); -- Session B select dbms_alert.register('boo'); select * from dbms_alert.waitany(10); -- Session C select dbms_alert.signal('boo','Nice day');
This module contains some function for working with bussines days from package PLVdate. Detailed documentation can be found in PLVision library. This package is multicultural, but default configurations are only for european countries (see source code).
You should define your own non-business days (max 50 days) and own holidays (max 30 days). A holiday is any non-business day, which is same every year. For example, Christmas day in Western countries.
-
plvdate.add_bizdays(day date, days int) date - Get the date created by adding <n> business days to a date
-
plvdate.nearest_bizday(day date) date - Get the nearest business date to a given date, user defined
-
plvdate.next_bizday(day date) date - Get the next business date from a given date, user defined
-
plvdate.bizdays_between(day1 date, day2 date) int - Get the number of business days between two dates
-
plvdate.prev_bizday(day date) date - Get the previous business date from a given date
-
plvdate_isbizday(date) bool - Call this function to determine if a date is a business day
-
plvdate.set_nonbizday(dow varchar) - Set day of week as non bussines day
-
plvdate.unset_nonbizday(dow varchar) - Unset day of week as non bussines day
-
plvdate.set_nonbizday(day date) - Set day as non bussines day
-
plvdate.unset_nonbizday(day date) - Unset day as non bussines day
-
plvdate.set_nonbizday(day date, repeat bool) - Set day as non bussines day, if 'repeat' is true, then day is nonbiz every year
-
plvdate.unset_nonbizday(day date, repeat bool) - Unset day as non bussines day, if 'repeat' is true, then day is nonbiz every year
-
plvdate.use_easter() - Easter Sunday and easter monday will be holiday
-
plvdate.unuse_easter();
-
plvdate.use_easter(useit boolean);
-
plvdate.using_easter() bool - If we use easter then returns true
-
plvdate.include_start() - Include starting date in bizdays_between calculation
-
plvdate.noinclude_start();
-
plvdate.include_start(include boolean);
-
plvdate.including_start() bool;
-
plvdate.default_holidays(varchar) - load default configurations. You can use the following configurations: Czech, German, Austria, Poland, Slovakia, Russia, GB and USA at this moment.
-
configuration contains only common holidays for all regions. You can add your own regional holiday with plvdate.set_nonbizday(nonbizday, true)
Example:
postgres=# select plvdate.default_holidays('czech'); default_holidays ----------------- (1 row) postgres=# select to_char(current_date, 'day'), plvdate.next_bizday(current_date), to_char(plvdate.next_bizday(current_date),'day'); to_char | next_bizday | to_char ----------+-------------+----------- saturday | 2006-03-13 | monday (1 row)
Change for non-European environment:
select plvdate.unset_nonbizday('saturday'); select plvdate.unset_nonbizday('sunday'); select plvdate.set_nonbizday('friday'); select plvdate.set_nonbizday('2006-05-19', true); select plvdate.unuse_easter();
This package contains some useful string and character functions. Each function supports positive and negative offsets — i.e., offset from the end of the string. For example:
plvstr.left('abcdef',2) -> ab plvstr.left('abcdef',-2) -> abcd plvstr.substr('abcdef',1,1) -> a plvstr.substr('abcdef',-1,1) -> f plvstr.substr('abcde',-2,1) -> d
List of functions:
-
plvstr.normalize(str text) - Normalize string - Replace white chars by space, replace spaces by space
-
plvstr.is_prefix(str text, prefix text, cs bool) - Returns true, if prefix is prefix of str
-
plvstr.is_prefix(str text, prefix text) - Returns true, if prefix is prefix of str
-
plvstr.is_prefix(str int, prefix int) - Returns true, if prefix is prefix of str
-
plvstr.is_prefix(str bigint, prefix bigint) - Returns true, if prefix is prefix of str
-
plvstr.substr(str text, start int, len int) - Returns substring started on start_in to end
-
plvstr.substr(str text, start int) - Returns substring started on start_in to end
-
plvstr.instr(str text, patt text, start int, nth int) - Search pattern in string
-
plvstr.instr(str text, patt text, start int) - Search pattern in string
-
plvstr.instr(str text, patt text) - Search pattern in string
-
plvstr.lpart(str text, div text, start int, nth int, all_if_notfound bool) - Call this function to return the left part of a string
-
plvstr.lpart(str text, div text, start int, nth int) - Call this function to return the left part of a string
-
plvstr.lpart(str text, div text, start int) - Call this function to return the left part of a string
-
plvstr.lpart(str text, div text) - Call this function to return the left part of a string
-
plvstr.rpart(str text, div text, start int, nth int, all_if_notfound bool) - Call this function to return the right part of a string
-
plvstr.rpart(str text, div text, start int, nth int) - Call this function to return the right part of a string
-
plvstr.rpart(str text, div text, start int) - Call this function to return the right part of a string
-
plvstr.rpart(str text, div text) - Call this function to return the right part of a string
-
plvstr.lstrip(str text, substr text, num int) - Call this function to remove characters from the beginning
-
plvstr.lstrip(str text, substr text) - Call this function to remove characters from the beginning
-
plvstr.rstrip(str text, substr text, num int) - Call this function to remove characters from the end
-
plvstr.rstrip(str text, substr text) - Call this function to remove characters from the end
-
plvstr.rvrs(str text, start int, _end int) - Reverse string or part of string
-
plvstr.rvrs(str text, start int) - Reverse string or part of string
-
plvstr.rvrs(str text) - Reverse string or part of string
-
plvstr.left(str text, n int) - Returns firs num_in charaters. You can use negative num_in
-
plvstr.right(str text, n int) - Returns last num_in charaters. You can use negative num_ni
-
plvstr.swap(str text, replace text, start int, lengh int) - Replace a substring in a string with a specified string
-
plvstr.swap(str text, replace text) - Replace a substring in a string with a specified string
-
plvstr.betwn(str text, start int, _end int, inclusive bool) - Find the Substring Between Start and End Locations
-
plvstr.betwn(str text, start text, _end text, startnth int, endnth int, inclusive bool, gotoend bool) - Find the Substring Between Start and End Locations
-
plvstr.betwn(str text, start text, _end text) - Find the Substring Between Start and End Locations
-
plvstr.betwn(str text, start text, _end text, startnth int, endnth int) - Find the Substring Between Start and End Locations
-
plvchr.nth(str text, n int) - Call this function to return the Nth character in a string
-
plvchr.first(str text) - Call this function to return the first character in a string
-
plvchr.last(str text) - Call this function to return the last character in a string
-
plvchr.is_blank(c int) - Is blank
-
plvchr.is_blank(c text) - Is blank
-
plvchr.is_digit(c int) - Is digit
-
plvchr.is_digit(c text) - Is digit
-
plvchr.is_quote(c int) - Is quote
-
plvchr.is_quote(c text) - Is quote
-
plvchr.is_other(c int) - Is other
-
plvchr.is_other(c text) - Is other
-
plvchr.is_letter(c int) - Is letter
-
plvchr.is_letter(c text) - Is letter
-
plvchr.char_name(c text) - Returns the name of the character to ascii code as a VARCHAR.
-
plvchr.quoted1(str text) - Quoted text between '''
-
plvchr.quoted2(str text) - Quoted text between '"'
-
plvchr.stripped(str text, char_in text) - Strips a string of all instances of the specified characters
The PLVsubst package performs string substitutions based on a substitution keyword.
-
plvsubst.string(template_in text, vals_in text[]) - Scans a string for all instances of the substitution keyword and replace it with the next value in the substitution values list
-
plvsubst.string(template_in text, vals_in text[], subst_in text)
-
plvsubst.string(template_in text, vals_in text, delim_in text)
-
plvsubst.string(template_in text, vals_in text, delim_in text, subst_in text)
-
plvsubst.setsubst(str text) - Set substitution keyword to default '%s'
-
plvsubst.subst() - Retrieve substitution keyword
Examples:
select plvsubst.string('My name is %s %s.', ARRAY['Pavel','Stěhule']); string -------------------------- My name is Pavel Stěhule. (1 row) select plvsubst.string('My name is %s %s.', 'Pavel,Stěhule'); string -------------------------- My name is Pavel Stěhule. (1 row) select plvsubst.string('My name is $$ $$.', 'Pavel|Stěhule','|','$$'); string -------------------------- My name is Pavel Stěhule. (1 row)
-
dms_utility.format_call_stack() — return a formatted string with content of call stack
postgres=# select foo2(); foo2 --------------------------------- ----- Call Stack ----- line object number statement name 1 return function foo 1 return function foo1 1 return function foo2 (1 row)
This package isn’t compatible with original PLVlex.
postgres=# select * from plvlex.tokens('select * from a.b.c join d ON x=y', true, true); pos | token | code | class | separator | mod ----+--------+------+---------+-----------+------ 0 | select | 527 | KEYWORD | | 7 | * | 42 | OTHERS | | self 9 | from | 377 | KEYWORD | | 25 | a.b.c | | IDENT | | 20 | join | 418 | KEYWORD | | 25 | d | | IDENT | | 27 | on | 473 | KEYWORD | | 30 | x | | IDENT | | 31 | = | 61 | OTHERS | | self 32 | y | | IDENT | | (10 rows)
Warning: Keyword’s codes can be changed between PostgreSQL versions! o plvlex.tokens(str text, skip_spaces bool, qualified_names bool) - Returns table of lexical elements in str.
This package protect user input against SQL injection.
-
dbms_assert.enquote_literal(varchar) varchar - Add leading and trailing quotes, verify that all single quotes are paired with adjacent single quotes.
-
dbms_assert.enquote_name(varchar [, boolean]) varchar - Enclose name in double quotes. Optional second parameter ensure loweralize of name. Attention - On Oracle is second parameter capitalize!
-
dbms_assert.noop(varchar) varchar - Returns value without any checking.
-
dbms_assert.qualified_sql_name(varchar) varchar - This function verifies that the input string is qualified SQL name.
-
dbms_assert.schema_name(varchar) varchar - Function verifies that input string is an existing schema name.
-
dbms_assert.simple_sql_name(varchar) varchar -This function verifies that the input string is simple SQL name.
-
dbms_assert.object_name(varchar) varchar - Verifies that input string is qualified SQL identifier of an existing SQL object.
This unit contains some assert functions.
-
plunit.assert_true(bool [, varchar]) - Asserts that the condition is true.
-
plunit.assert_false(bool [, varchar]) - Asserts that the condition is false.
-
plunit.assert_null(anyelement [, varchar]) - Asserts that the actual is null.
-
plunit.assert_not_null(anyelement [, varchar]) - Asserts that the actual isn’t null.
-
plunit.assert_equals(anyelement, anyelement [, double precision] [, varchar]) - Asserts that expected and actual are equal.
-
plunit.assert_not_equals(anyelement, anyelement [, double precision] [, varchar]) - Asserts that expected and actual are equal.
-
plunit.fail([varchar]) - Fail can be used to cause a test procedure to fail immediately using the supplied message.
-
dbms_random.initialize(int) - Initialize package with a seed value.
-
dbms_random.normal() - Returns random numbers in a standard normal distribution.
-
dbms_random.random() - Returns random number from -2^31 .. 2^31.
-
dbms_random.seed(int)
-
dbms_random.seed(text) - Reset seed value.
-
dbms_random.string(opt text(1), len int) - Create random string
-
dbms_random.terminate() - Terminate package (do nothing in Pg)
-
dbms_random.value() - Returns a random number from [0.0 - 1.0)
-
dbms_random.value(low double precision, high double precision) - Returns a random number from [low - high)
This module contains implementation of functions: concat, nvl, nvl2, lnnvl, decode, bitand, nanvl, sinh, cosh, tanh and oracle.substr.
-
oracle.substr(str text, start int, len int) - Oracle compatible substring
-
oracle.substr(str text, start int) - Oracle compatible substring
-
pg_catalog.listagg(str text [, separator text]) - aggregate values to list
-
pg_catalog.median(float4) - calculate a median
-
pg_catalog.median(float8) - calculate a median
You might need to set search_path to 'oracle, pg_catalog, "$user", public' because oracle.substr is installed side-by-side with pg_catalog.substr.
-
better documentation
-
better seralization in dbms_pipe (via _send and _recv functions)
-
alter shared memory structures by temporary tables: only locks are in shmem, (bitmaps), data in tmp tbl
The project was founded in 2008 by Pavel Stehule <[email protected]>.
Other contributors:
-
Peter Eisentraut (petere)
-
Gavin Sherry (swm)
-
Pavel Stehule (okbob)
-
Heikki Linnakangas (hlinnaka)
-
Gabriele Bartolini (gbartolini)
-
Zdenek Kotala (hlipa)
-
Takahiro Itagaki (itagaki)
-
Marco Nenciarini (mnencia)
-
Pavan Deolasee (pavanvd)
-
Jeffrey Cohen (jcohen)
-
Amit Langote (amitlan)
-
Rahila Syed (rahila)
-
Beena Emerson (b-emerson)
-
Vinayak Pokale
-
Fujii Masao