Differences between PostgreSQL and Oracle

Differences between Postgresql and Oracle

Through reading materials and experiments, the following compares the differences between Postgresql 9.3 version and Oracle 11g version. Note: The relevant details are still to be verified and improved .

1. Basic syntax differences

1.1. Basic data type differences

Oracle Postgresql
Varchar2 varchar
DATE date/time/timestamp
null null
clob text
blob bytea
number smallint/integer/bigint/numeric/real/double precision
boolean is not supported, can be replaced by 0/1 support boolean

1.2. Basic function differences

Item Oracle Postgresql
string concatenator || concat()
‘a’ || null = ‘a’ null
trunc(time) trunc(date) date_trunc()
Get the current system time SYSDATE localtimestamp, now()
to_char, to_number,
to_date
Automatic format conversion Format required
decode ×
outer join (+) left(right) join
GOTO ×
procedure × needs to be rewritten as function
package × needs to be rewritten as function

1.2.1. Cursor Properties

Oracle Postgresql
%FOUND found
%NOTFOUND not found
%ISOPEN ×
%ROWCOUNT ×

1.2.2. System built-in function package

Oracle Postgresql
DBMS_OUTPUT ×
DBMS_SQL ×
UTIL_FILE ×
UTIL_MAIL ×

1.3. DDL differences

1.3.1. Sequence syntax and usage differences

Item Oracle Postgresql
create create sequence SEQ_TAB_NAME
minvalue 1
maxvalue 9999999999999999
start with 1 increment by 1
cache 20;
create sequence seq_tab_name
minvalue 1
maxvalue 9223372036854775807
start 1
increment 1
cache 20;
query select seq_tab_name.nextval from dual; select next_val(seq_tab_name) from dual;

Note : dual in pgsql needs to be implemented independently. See the Compatibility Settings -> Virtual Table Dual Issues section for details.

1.3.2. constraint differences

Item Oracle Postgresql
constraint alter table tab_name add constraint pk_tab_name primary key(column_id) using index; alter table tab_name add constraint pk_tab_name primary key(column_id);

2. Advanced Syntax Differences

2.1. Transaction Differences

In Oracle, transaction commit or rollback is implemented through commit/rollback. The structure is similar to:

begin
    select ...
    update ...
    delete ...

    commit;
exception
    when others then
    rollback;
end;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

PostgreSQL actually treats each SQL statement as if it were executed within a transaction. If you do not issue the BEGIN command, each individual statement is surrounded by an implicit BEGIN and (if successful) COMMIT. A group of statements enclosed between BEGIN and COMMIT is sometimes called a transaction block.

E.g:

BEGIN;
UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Bob';
-- 呀!加错钱了,应该用 Wally 的账号
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00
    WHERE name = 'Wally';
COMMIT;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

In PL/pgSQL, it also provides Begin, End and ExceptionCode processing mechanisms similar to Oracle. The difference between them is not obvious. In fact, PL/SQL (Oracle Database Operating Language) is highly similar to PL/pgSQL, which makes it extremely convenient to migrate procedures between Oracle and Postgresql.

2.2. Function inheritance and overloading

Oracle does not support inheritance and overloading features, pgsql supports inheritance and function overloading;
(to be improved)

2.3. Type conversion

The type conversion "::" character in pgsql is not supported by Oracle.

2.4. Subqueries

For subqueries, pgsql has stricter requirements and must have an alias;

3. Other differences

3.1. jdbc differences

Oracle's jdbc connection string:db.url=jdbc:oracle:thin:@192.168.1.1:1521:ORCL

Postgresql connection string:db.url=jdbc:postgresql:@192.168.1.1:5432/database

4. Compatibility settings

4.1. String Concatenation Compatibility Solutions

There is no concat function in Postgresql, and because || cannot be used, it needs to be solved by creating a concat function in the public schema.

--在 public schema中创建 concat 函数
create or replace function concat(text, text) returns text as
$body$ select coalesce($1,'') || coalesce($2,'')$body$ language 'sql' volatile;

alter function concat(text, text) owner to postgres;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.2. Virtual table dual problem

There is no dual virtual table in Postgresql. To ensure compatibility, a pseudo view needs to be created instead:

create or replace view dual as
select NULL::"unknown"
where 1=1;

alter table dual owner to postgres;
grant all on table dual to postgres;
grant select on table dual to public;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.3. Data paging problem

There is no limit in Oracle and no rownum in postgresql. The relevant sentences need to be rewritten.

-- Oracle
select * from (
  select * from (
    select * from t1 order by col1, col2
  ) where rownum <=50 order by col3, col4
) where rowmun <=20 order by col5, col6;

-- postgresql
select * from (select * from (select * from t1 order by col1, col2) ta order by col3, col4 limit 50) tb
order by col5, col6 limit 20;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Note: limit must be used after order by!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326543104&siteId=291194637