SlideShare a Scribd company logo
Oracle to PostgreSQL
Top 10 Mistakes
Who Am I?
●
Jim Mlodgenski
– jimm@openscg.com
– @jim_mlodgenski
●
Co-Chair
– PGConf US
●
Director
– United States PostgreSQL (www.postgresql.us)
●
Co-organizer of
– Philly PUG (www.phlpug.org)
– NYC PUG (www.nycpug.org)
●
CTO, OpenSCG
– www.openscg.com
Why?
●
Project deadline
– Looming Oracle renewal
●
Lack of education
●
Attitude
– Only see the world through an Oracle lens
●
Using migration tools or other short cuts
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
System Tuning
●
When moving to PostgreSQL, many
admins start with configuring values
similar to the Oracle settings
– My SGA was set to 16GB so
shared_buffers is 16GB
– My redo logs are 2GB so max_wal_size
is 2GB
System Tuning
●
In Oracle, I get better performance with
a 32k block size
configure –with-blocksize=32
make
make install
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Uppercase Folding
●
In Oracle, all meta-data folds to uppercase
SQL> DESC USERS
Name Null? Type
---------- ------- ------------------------
FNAME VARCHAR2(100)
MNAME VARCHAR2(100)
LNAME VARCHAR2(100)
Uppercase Folding
●
In PostgreSQL, all meta-data folds to
lowercase
test=# d users
Table "public.users"
Column | Type | Nullable
--------+------------------------+---------
fname | character varying(100) |
mname | character varying(100) |
lname | character varying(100) |
Uppercase Folding
●
Many migration tools carry the uppercase
from Oracle over to PostgreSQL
test=# d "USERS"
Table "public.USERS"
Column | Type | Nullable
--------+------------------------+----------
FNAME | character varying(100) |
MNAME | character varying(100) |
LNAME | character varying(100) |
Uppercase Folding
●
Becomes very tedious needing to double quote everything
test=# SELECT "FNAME", "MNAME", "LNAME" FROM "USERS";
FNAME | MNAME | LNAME
---------+--------+------------
George | | Washington
John | | Adams
Thomas | | Jefferson
James | | Madison
James | | Monroe
Andrew | | Jackson
Martin | | Van Buren
John | | Tyler
John | Quincy | Adams
William | Henry | Harrison
(10 rows)
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Table Spaces
●
In Oracle, table spaces are critical for storing data
●
Generally many table spaces are used for indexes and
tables
CREATE TABLESPACE ts_data1
LOGGING
DATAFILE '/data/ts_data1.dbf'
SIZE 32m
AUTOEXTEND ON
NEXT 32m MAXSIZE 2048m
EXTENT MANAGEMENT local;
Table Spaces
●
In PostgreSQL, table spaces are just
directory locations
●
Provide no real benefit unless the
database spans multiple mount points
CREATE TABLESPACE ts_data1
LOCATION '/data/ts_data1';
Table Spaces
●
Additional table spaces makes operations
more cumbersome like
– Backup and restore
– Replication setup
– Major version upgrades
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Dual Table
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
---------
09-MAY-17
Dual Table
●
In PostgreSQL, the FROM clause is optional
and is unnecessary
●
Don’t mock a DUAL table
test=# SELECT CURRENT_DATE;
current_date
--------------
2017-05-09
(1 row)
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Exceptions
●
Many Oracle procedures use exceptions
as part of standard practice
– Application developers are comfortable
catching exceptions
●
Some applications have exception
handling in every procedure and function
●
Most migration tools simply translate the
code to pl/pgsql
Exceptions
CREATE FUNCTION get_first_name(p_lname varchar2)
RETURN varchar2
IS
l_fname varchar2(100);
BEGIN
SELECT fname
INTO l_fname
FROM users
WHERE lname = p_lname;
RETURN l_fname;
EXCEPTION
WHEN no_data_found THEN
l_fname := null;
RETURN l_fname;
END get_first_name;
Exceptions
CREATE FUNCTION get_first_name(p_lname varchar)
RETURNS varchar
AS $$
DECLARE
l_fname varchar;
BEGIN
SELECT fname
INTO l_fname
FROM users
WHERE lname = p_lname;
RETURN l_fname;
EXCEPTION
WHEN no_data_found THEN
l_fname := null;
RETURN l_fname;
END
$$ LANGUAGE plpgsql;
Exceptions
●
PostgreSQL uses sub transactions to handle
exceptions
CREATE OR REPLACE FUNCTION get_first_name(p_lname varchar)
RETURNS varchar
AS $$
DECLARE
l_fname varchar := null;
BEGIN
SELECT fname
INTO l_fname
FROM users
WHERE lname = p_lname;
RETURN l_fname;
END
$$ LANGUAGE plpgsql;
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Fine Tuning Queries
●
“I added a hint to use an index but
PostgreSQL does not use it”
– PostgreSQL does not have hints
●
It treats Oracle hints as comments
●
PostgreSQL’s optimizer is different than
Oracle so queries are tuned differently
Fine Tuning Queries
●
B-tree
●
Hash
●
GIN
●
GiST
●
SP-GiST
●
BRIN
●
“I didn’t index my column in Oracle, why
would I in PostgreSQL?”
●
PostgreSQL has more and different types
of indexes than Oracle
Fine Tuning Queries
●
PostgreSQL can even use indexes on LIKE queries
CREATE INDEX idx_users_lname
ON users USING gin (lname gin_trgm_ops);
EXPLAIN SELECT * FROM users WHERE lname LIKE '%ing%';
QUERY PLAN
-----------------------------------------------------
Bitmap Heap Scan on users (cost=8.00..12.02 rows=1
width=654)
Recheck Cond: ((lname)::text ~~ '%ing%'::text)
-> Bitmap Index Scan on idx_users_lname
(cost=0.00..8.00 rows=1 width=0)
Index Cond: ((lname)::text ~~ '%ing%'::text)
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Not Using Native Features
●
PostgreSQL is more feature rich for
developers than Oracle
– Stored Procedure Languages
– Foreign Data Wrappers
– Data Types
– Spatial
Not Using Native Features
CREATE OR REPLACE FUNCTION has_valid_keys(doc json)
RETURNS boolean AS
$$
if (!doc.hasOwnProperty('fname'))
return false;
if (!doc.hasOwnProperty('lname'))
return false;
return true;
$$ LANGUAGE plv8 IMMUTABLE;
ALTER TABLE user_collection
ADD CONSTRAINT collection_key_chk
CHECK (has_valid_keys(doc::json));
Not Using Native Features
CREATE TABLE login_history (
user_id bigint,
host inet,
login_ts timestamptz
);
SELECT user_id, count(*)
FROM login_history
WHERE host << '17.0.0.0/8'::inet
AND login_ts > now() - '7 days'::interval
GROUP BY 1;
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Synonyms
“PostgreSQL doesn’t have synonyms so I
can’t migrate my application”
CREATE PUBLIC SYNONYM emp
FOR SCOTT.emp;
●
Synonyms are used to not fully qualify cross
schema objects
●
Mostly a convenience feature
Synonyms
●
In PostgreSQL, search_path can
accomplish many of the same things and is
less tedious to setup
test=# show search_path;
search_path
-----------------
"$user", public
(1 row)
Synonyms
CREATE FUNCTION user1.get_int()
RETURNS int AS
$$
SELECT 1;
$$ LANGUAGE sql;
CREATE FUNCTION user2.get_int()
RETURNS int AS
$$
SELECT 2;
$$ LANGUAGE sql;
CREATE FUNCTION public.get_number()
RETURNS float8 AS
$$
SELECT 3.14::float8;
$$ LANGUAGE sql;
Synonyms
test=# SELECT get_int();
2017-05-08 17:38:50.367 EDT [28855] ERROR: function
get_int() does not exist at character 8
2017-05-08 17:38:50.367 EDT [28855] HINT: No
function matches the given name and argument types.
You might need to add explicit type casts.
2017-05-08 17:38:50.367 EDT [28855] STATEMENT:
SELECT get_int();
ERROR: function get_int() does not exist
LINE 1: SELECT get_int();
^
HINT: No function matches the given name and
argument types. You might need to add explicit type
casts.
Synonyms
test=# SET search_path = user1,
user2, public;
SET
test=# SELECT get_int();
get_int
---------
1
(1 row)
Synonyms
test=# SET search_path = user2,
user1, public;
SET
test=# SELECT get_int();
get_int
---------
2
(1 row)
Synonyms
test=# select get_number();
get_number
------------
3.14
(1 row)
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Nulls
●
PostgreSQL and Oracle handle nulls a bit
differently
– Need to account for them appropriately
– Most often seen with string concatenation
Nulls
CREATE TABLE users (
fname VARCHAR2(100),
mname VARCHAR2(100),
lname VARCHAR2(100)
);
SELECT
fname || ' ' || mname || ' ' || lname
FROM users;
Nulls
SQL> SELECT fname || ' ' || mname || ' ' || lname FROM users;
FNAME||''||MNAME||''||LNAME
---------------------------------------------------------------
George Washington
John Adams
Thomas Jefferson
James Madison
James Monroe
Andrew Jackson
Martin Van Buren
John Tyler
John Quincy Adams
William Henry Harrison
10 rows selected.
Nulls
test=# SELECT fname || ' ' || mname || ' ' || lname FROM
users;
?column?
------------------------
John Quincy Adams
William Henry Harrison
(10 rows)
Nulls
test=# SELECT COALESCE(fname, '') || ' ' || COALESCE(mname,
'') || ' ' || COALESCE(lname, '') FROM users;
?column?
------------------------
George Washington
John Adams
Thomas Jefferson
James Madison
James Monroe
Andrew Jackson
Martin Van Buren
John Tyler
John Quincy Adams
William Henry Harrison
(10 rows)
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Data Types
●
Oracle has a few main data types that are
typically used
– VARCHAR2
– DATE
– NUMBER
●
And a couple Large Object types
– CLOB
– BLOB
Data Types
●
PostgreSQL comes with 64 base types
and can be extended for more
abstime
aclitem
bit
bool
box
bpchar
bytea
char
cid
cidr
circle
date
float4
float8
gtsvector
inet
int2
int2vector
int4
int8
interval
json
jsonb
line
lseg
macaddr
money
name
numeric
oid
oidvector
path
pg_lsn
pg_node_tree
point
polygon
refcursor
regclass
regconfig
regdictionar
y
regnamespace
regoper
regoperator
regproc
regprocedure
regrole
regtype
reltime
smgr
text
tid
time
timestamp
timestamptz
timetz
tinterval
tsquery
tsvector
txid_snapshot
uuid
varbit
varchar
xid
xml
Data Types
●
Don’t assume that the perceived equivalent
in PostgreSQL behaves the same as Oracle
– For example, managing CLOBS
●
Length
●
Substrings
DBMS_LOB.GETLENGTH(x)
Data Types
●
In PostgreSQL, VARCHAR and TEXT are equivalent
and behave the same
CREATE TABLE max_varchar (
a varchar(4001)
);
CREATE TABLE max_varchar (
a varchar(10485760)
);
Data Types
CREATE TABLE max_varchar (
a varchar
);
CREATE TABLE max_varchar (
a text
);
Data Types
test=# INSERT INTO max_varchar SELECT
repeat('x', 1073741800);
INSERT 0 1
test=# SELECT length(a) from
max_varchar ;
length
------------
1073741800
(1 row)
Data Types
Data Types
●
Most migration tools translate an Oracle
NUMBER to a PostgreSQL NUMERIC
– A PostgreSQL NUMERIC can hold
●
131072 before the decimal point
●
16383 after the decimal point
●
It is not the same are NUMBER
SELECT to_number(n, n)
FROM repeat('9', 131071) n;
Summary
●
System Tuning
●
Case Folding
●
Table Spaces
●
Dual Table
●
Exceptions
●
Fine Tuning
●
Native Features
●
Synonyms
●
Nulls
●
Data Types
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Biggest Mistake
●
Paying for a commercial database when it
is not necessary
– Listening to the FUD
– Building new solutions on Oracle
– Not learning PostgreSQL to leverage its great
features and functionality
Questions?
jimm@openscg.com
@jim_mlodgenski

More Related Content

What's hot (20)

PPSX
Oracle Performance Tools of the Trade
Carlos Sierra
 
ODP
Introduction to PostgreSQL
Jim Mlodgenski
 
PDF
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
PPTX
Mongodb basics and architecture
Bishal Khanal
 
PPTX
Getting started with postgresql
botsplash.com
 
PPTX
Explain the explain_plan
Maria Colgan
 
PDF
MongoDB vs. Postgres Benchmarks
EDB
 
PPTX
Oracle sql high performance tuning
Guy Harrison
 
PDF
Oracle statistics by example
Mauro Pagano
 
PPTX
PostgreSQL- An Introduction
Smita Prasad
 
PDF
Get to know PostgreSQL!
Oddbjørn Steffensen
 
PDF
Apache Calcite (a tutorial given at BOSS '21)
Julian Hyde
 
PDF
PostgreSQL Tutorial for Beginners | Edureka
Edureka!
 
PPTX
OLTP+OLAP=HTAP
EDB
 
PDF
The best way to run Elastic on Kubernetes
Elasticsearch
 
PPTX
Part3 Explain the Explain Plan
Maria Colgan
 
PDF
MongoDB Administration 101
MongoDB
 
PDF
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 
PPTX
MySQL Architecture and Engine
Abdul Manaf
 
PDF
What is new in PostgreSQL 14?
Mydbops
 
Oracle Performance Tools of the Trade
Carlos Sierra
 
Introduction to PostgreSQL
Jim Mlodgenski
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Mongodb basics and architecture
Bishal Khanal
 
Getting started with postgresql
botsplash.com
 
Explain the explain_plan
Maria Colgan
 
MongoDB vs. Postgres Benchmarks
EDB
 
Oracle sql high performance tuning
Guy Harrison
 
Oracle statistics by example
Mauro Pagano
 
PostgreSQL- An Introduction
Smita Prasad
 
Get to know PostgreSQL!
Oddbjørn Steffensen
 
Apache Calcite (a tutorial given at BOSS '21)
Julian Hyde
 
PostgreSQL Tutorial for Beginners | Edureka
Edureka!
 
OLTP+OLAP=HTAP
EDB
 
The best way to run Elastic on Kubernetes
Elasticsearch
 
Part3 Explain the Explain Plan
Maria Colgan
 
MongoDB Administration 101
MongoDB
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 
MySQL Architecture and Engine
Abdul Manaf
 
What is new in PostgreSQL 14?
Mydbops
 

Similar to Top 10 Mistakes When Migrating From Oracle to PostgreSQL (20)

KEY
PostgreSQL
Reuven Lerner
 
PDF
Stumbling stones when migrating from Oracle
EDB
 
PDF
PostgreSQL 9.0 & The Future
Aaron Thul
 
PPT
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
PDF
Porting Oracle Applications to PostgreSQL
Peter Eisentraut
 
PDF
Postgres for MySQL (and other database) people
Command Prompt., Inc
 
PDF
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
anandology
 
ODP
Porting Applications From Oracle To PostgreSQL
Peter Eisentraut
 
PPTX
Oracle to Postgres Schema Migration Hustle
EDB
 
PPTX
PostgreSQL - It's kind've a nifty database
Barry Jones
 
PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
PDF
Demystifying PostgreSQL
NOLOH LLC.
 
PDF
Demystifying PostgreSQL (Zendcon 2010)
NOLOH LLC.
 
PDF
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Gabriele Bartolini
 
PDF
Manipulating Data in Style with SQL
Ryan B Harvey, CSDP, CSM
 
PDF
Techday2010 Postgresql9
Dan-Claudiu Dragoș
 
PDF
Postgresql 9.3 overview
Aveic
 
PDF
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
PDF
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
PROIDEA
 
PDF
PostgreSQL Enterprise Class Features and Capabilities
PGConf APAC
 
PostgreSQL
Reuven Lerner
 
Stumbling stones when migrating from Oracle
EDB
 
PostgreSQL 9.0 & The Future
Aaron Thul
 
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
Porting Oracle Applications to PostgreSQL
Peter Eisentraut
 
Postgres for MySQL (and other database) people
Command Prompt., Inc
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
anandology
 
Porting Applications From Oracle To PostgreSQL
Peter Eisentraut
 
Oracle to Postgres Schema Migration Hustle
EDB
 
PostgreSQL - It's kind've a nifty database
Barry Jones
 
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
Demystifying PostgreSQL
NOLOH LLC.
 
Demystifying PostgreSQL (Zendcon 2010)
NOLOH LLC.
 
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Gabriele Bartolini
 
Manipulating Data in Style with SQL
Ryan B Harvey, CSDP, CSM
 
Techday2010 Postgresql9
Dan-Claudiu Dragoș
 
Postgresql 9.3 overview
Aveic
 
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
PROIDEA
 
PostgreSQL Enterprise Class Features and Capabilities
PGConf APAC
 
Ad

More from Jim Mlodgenski (11)

PDF
Strategic autovacuum
Jim Mlodgenski
 
PDF
Oracle postgre sql-mirgration-top-10-mistakes
Jim Mlodgenski
 
PDF
Profiling PL/pgSQL
Jim Mlodgenski
 
PDF
Debugging Your PL/pgSQL Code
Jim Mlodgenski
 
PDF
An Introduction To PostgreSQL Triggers
Jim Mlodgenski
 
PDF
PostgreSQL Procedural Languages: Tips, Tricks and Gotchas
Jim Mlodgenski
 
ODP
Postgresql Federation
Jim Mlodgenski
 
PPT
Leveraging Hadoop in your PostgreSQL Environment
Jim Mlodgenski
 
PDF
Scaling PostreSQL with Stado
Jim Mlodgenski
 
ODP
Multi-Master Replication with Slony
Jim Mlodgenski
 
ODP
Scaling PostgreSQL With GridSQL
Jim Mlodgenski
 
Strategic autovacuum
Jim Mlodgenski
 
Oracle postgre sql-mirgration-top-10-mistakes
Jim Mlodgenski
 
Profiling PL/pgSQL
Jim Mlodgenski
 
Debugging Your PL/pgSQL Code
Jim Mlodgenski
 
An Introduction To PostgreSQL Triggers
Jim Mlodgenski
 
PostgreSQL Procedural Languages: Tips, Tricks and Gotchas
Jim Mlodgenski
 
Postgresql Federation
Jim Mlodgenski
 
Leveraging Hadoop in your PostgreSQL Environment
Jim Mlodgenski
 
Scaling PostreSQL with Stado
Jim Mlodgenski
 
Multi-Master Replication with Slony
Jim Mlodgenski
 
Scaling PostgreSQL With GridSQL
Jim Mlodgenski
 
Ad

Recently uploaded (20)

PDF
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PDF
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PPTX
美国电子毕业证帕克大学电子版成绩单UMCP学费发票办理学历认证
Taqyea
 
PDF
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PDF
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
PPTX
原版一样(LHU毕业证书)英国利物浦希望大学毕业证办理方法
Taqyea
 
PPTX
Academic Debate: Creation vs Evolution.pptx
JOHNPATRICKMARTINEZ5
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PDF
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PDF
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
美国电子毕业证帕克大学电子版成绩单UMCP学费发票办理学历认证
Taqyea
 
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
原版一样(LHU毕业证书)英国利物浦希望大学毕业证办理方法
Taqyea
 
Academic Debate: Creation vs Evolution.pptx
JOHNPATRICKMARTINEZ5
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 

Top 10 Mistakes When Migrating From Oracle to PostgreSQL

  • 2. Who Am I? ● Jim Mlodgenski – [email protected] – @jim_mlodgenski ● Co-Chair – PGConf US ● Director – United States PostgreSQL (www.postgresql.us) ● Co-organizer of – Philly PUG (www.phlpug.org) – NYC PUG (www.nycpug.org) ● CTO, OpenSCG – www.openscg.com
  • 3. Why? ● Project deadline – Looming Oracle renewal ● Lack of education ● Attitude – Only see the world through an Oracle lens ● Using migration tools or other short cuts
  • 5. System Tuning ● When moving to PostgreSQL, many admins start with configuring values similar to the Oracle settings – My SGA was set to 16GB so shared_buffers is 16GB – My redo logs are 2GB so max_wal_size is 2GB
  • 6. System Tuning ● In Oracle, I get better performance with a 32k block size configure –with-blocksize=32 make make install
  • 8. Uppercase Folding ● In Oracle, all meta-data folds to uppercase SQL> DESC USERS Name Null? Type ---------- ------- ------------------------ FNAME VARCHAR2(100) MNAME VARCHAR2(100) LNAME VARCHAR2(100)
  • 9. Uppercase Folding ● In PostgreSQL, all meta-data folds to lowercase test=# d users Table "public.users" Column | Type | Nullable --------+------------------------+--------- fname | character varying(100) | mname | character varying(100) | lname | character varying(100) |
  • 10. Uppercase Folding ● Many migration tools carry the uppercase from Oracle over to PostgreSQL test=# d "USERS" Table "public.USERS" Column | Type | Nullable --------+------------------------+---------- FNAME | character varying(100) | MNAME | character varying(100) | LNAME | character varying(100) |
  • 11. Uppercase Folding ● Becomes very tedious needing to double quote everything test=# SELECT "FNAME", "MNAME", "LNAME" FROM "USERS"; FNAME | MNAME | LNAME ---------+--------+------------ George | | Washington John | | Adams Thomas | | Jefferson James | | Madison James | | Monroe Andrew | | Jackson Martin | | Van Buren John | | Tyler John | Quincy | Adams William | Henry | Harrison (10 rows)
  • 13. Table Spaces ● In Oracle, table spaces are critical for storing data ● Generally many table spaces are used for indexes and tables CREATE TABLESPACE ts_data1 LOGGING DATAFILE '/data/ts_data1.dbf' SIZE 32m AUTOEXTEND ON NEXT 32m MAXSIZE 2048m EXTENT MANAGEMENT local;
  • 14. Table Spaces ● In PostgreSQL, table spaces are just directory locations ● Provide no real benefit unless the database spans multiple mount points CREATE TABLESPACE ts_data1 LOCATION '/data/ts_data1';
  • 15. Table Spaces ● Additional table spaces makes operations more cumbersome like – Backup and restore – Replication setup – Major version upgrades
  • 17. Dual Table SQL> SELECT SYSDATE FROM DUAL; SYSDATE --------- 09-MAY-17
  • 18. Dual Table ● In PostgreSQL, the FROM clause is optional and is unnecessary ● Don’t mock a DUAL table test=# SELECT CURRENT_DATE; current_date -------------- 2017-05-09 (1 row)
  • 20. Exceptions ● Many Oracle procedures use exceptions as part of standard practice – Application developers are comfortable catching exceptions ● Some applications have exception handling in every procedure and function ● Most migration tools simply translate the code to pl/pgsql
  • 21. Exceptions CREATE FUNCTION get_first_name(p_lname varchar2) RETURN varchar2 IS l_fname varchar2(100); BEGIN SELECT fname INTO l_fname FROM users WHERE lname = p_lname; RETURN l_fname; EXCEPTION WHEN no_data_found THEN l_fname := null; RETURN l_fname; END get_first_name;
  • 22. Exceptions CREATE FUNCTION get_first_name(p_lname varchar) RETURNS varchar AS $$ DECLARE l_fname varchar; BEGIN SELECT fname INTO l_fname FROM users WHERE lname = p_lname; RETURN l_fname; EXCEPTION WHEN no_data_found THEN l_fname := null; RETURN l_fname; END $$ LANGUAGE plpgsql;
  • 23. Exceptions ● PostgreSQL uses sub transactions to handle exceptions CREATE OR REPLACE FUNCTION get_first_name(p_lname varchar) RETURNS varchar AS $$ DECLARE l_fname varchar := null; BEGIN SELECT fname INTO l_fname FROM users WHERE lname = p_lname; RETURN l_fname; END $$ LANGUAGE plpgsql;
  • 25. Fine Tuning Queries ● “I added a hint to use an index but PostgreSQL does not use it” – PostgreSQL does not have hints ● It treats Oracle hints as comments ● PostgreSQL’s optimizer is different than Oracle so queries are tuned differently
  • 26. Fine Tuning Queries ● B-tree ● Hash ● GIN ● GiST ● SP-GiST ● BRIN ● “I didn’t index my column in Oracle, why would I in PostgreSQL?” ● PostgreSQL has more and different types of indexes than Oracle
  • 27. Fine Tuning Queries ● PostgreSQL can even use indexes on LIKE queries CREATE INDEX idx_users_lname ON users USING gin (lname gin_trgm_ops); EXPLAIN SELECT * FROM users WHERE lname LIKE '%ing%'; QUERY PLAN ----------------------------------------------------- Bitmap Heap Scan on users (cost=8.00..12.02 rows=1 width=654) Recheck Cond: ((lname)::text ~~ '%ing%'::text) -> Bitmap Index Scan on idx_users_lname (cost=0.00..8.00 rows=1 width=0) Index Cond: ((lname)::text ~~ '%ing%'::text)
  • 29. Not Using Native Features ● PostgreSQL is more feature rich for developers than Oracle – Stored Procedure Languages – Foreign Data Wrappers – Data Types – Spatial
  • 30. Not Using Native Features CREATE OR REPLACE FUNCTION has_valid_keys(doc json) RETURNS boolean AS $$ if (!doc.hasOwnProperty('fname')) return false; if (!doc.hasOwnProperty('lname')) return false; return true; $$ LANGUAGE plv8 IMMUTABLE; ALTER TABLE user_collection ADD CONSTRAINT collection_key_chk CHECK (has_valid_keys(doc::json));
  • 31. Not Using Native Features CREATE TABLE login_history ( user_id bigint, host inet, login_ts timestamptz ); SELECT user_id, count(*) FROM login_history WHERE host << '17.0.0.0/8'::inet AND login_ts > now() - '7 days'::interval GROUP BY 1;
  • 33. Synonyms “PostgreSQL doesn’t have synonyms so I can’t migrate my application” CREATE PUBLIC SYNONYM emp FOR SCOTT.emp; ● Synonyms are used to not fully qualify cross schema objects ● Mostly a convenience feature
  • 34. Synonyms ● In PostgreSQL, search_path can accomplish many of the same things and is less tedious to setup test=# show search_path; search_path ----------------- "$user", public (1 row)
  • 35. Synonyms CREATE FUNCTION user1.get_int() RETURNS int AS $$ SELECT 1; $$ LANGUAGE sql; CREATE FUNCTION user2.get_int() RETURNS int AS $$ SELECT 2; $$ LANGUAGE sql; CREATE FUNCTION public.get_number() RETURNS float8 AS $$ SELECT 3.14::float8; $$ LANGUAGE sql;
  • 36. Synonyms test=# SELECT get_int(); 2017-05-08 17:38:50.367 EDT [28855] ERROR: function get_int() does not exist at character 8 2017-05-08 17:38:50.367 EDT [28855] HINT: No function matches the given name and argument types. You might need to add explicit type casts. 2017-05-08 17:38:50.367 EDT [28855] STATEMENT: SELECT get_int(); ERROR: function get_int() does not exist LINE 1: SELECT get_int(); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
  • 37. Synonyms test=# SET search_path = user1, user2, public; SET test=# SELECT get_int(); get_int --------- 1 (1 row)
  • 38. Synonyms test=# SET search_path = user2, user1, public; SET test=# SELECT get_int(); get_int --------- 2 (1 row)
  • 41. Nulls ● PostgreSQL and Oracle handle nulls a bit differently – Need to account for them appropriately – Most often seen with string concatenation
  • 42. Nulls CREATE TABLE users ( fname VARCHAR2(100), mname VARCHAR2(100), lname VARCHAR2(100) ); SELECT fname || ' ' || mname || ' ' || lname FROM users;
  • 43. Nulls SQL> SELECT fname || ' ' || mname || ' ' || lname FROM users; FNAME||''||MNAME||''||LNAME --------------------------------------------------------------- George Washington John Adams Thomas Jefferson James Madison James Monroe Andrew Jackson Martin Van Buren John Tyler John Quincy Adams William Henry Harrison 10 rows selected.
  • 44. Nulls test=# SELECT fname || ' ' || mname || ' ' || lname FROM users; ?column? ------------------------ John Quincy Adams William Henry Harrison (10 rows)
  • 45. Nulls test=# SELECT COALESCE(fname, '') || ' ' || COALESCE(mname, '') || ' ' || COALESCE(lname, '') FROM users; ?column? ------------------------ George Washington John Adams Thomas Jefferson James Madison James Monroe Andrew Jackson Martin Van Buren John Tyler John Quincy Adams William Henry Harrison (10 rows)
  • 47. Data Types ● Oracle has a few main data types that are typically used – VARCHAR2 – DATE – NUMBER ● And a couple Large Object types – CLOB – BLOB
  • 48. Data Types ● PostgreSQL comes with 64 base types and can be extended for more abstime aclitem bit bool box bpchar bytea char cid cidr circle date float4 float8 gtsvector inet int2 int2vector int4 int8 interval json jsonb line lseg macaddr money name numeric oid oidvector path pg_lsn pg_node_tree point polygon refcursor regclass regconfig regdictionar y regnamespace regoper regoperator regproc regprocedure regrole regtype reltime smgr text tid time timestamp timestamptz timetz tinterval tsquery tsvector txid_snapshot uuid varbit varchar xid xml
  • 49. Data Types ● Don’t assume that the perceived equivalent in PostgreSQL behaves the same as Oracle – For example, managing CLOBS ● Length ● Substrings DBMS_LOB.GETLENGTH(x)
  • 50. Data Types ● In PostgreSQL, VARCHAR and TEXT are equivalent and behave the same CREATE TABLE max_varchar ( a varchar(4001) ); CREATE TABLE max_varchar ( a varchar(10485760) );
  • 51. Data Types CREATE TABLE max_varchar ( a varchar ); CREATE TABLE max_varchar ( a text );
  • 52. Data Types test=# INSERT INTO max_varchar SELECT repeat('x', 1073741800); INSERT 0 1 test=# SELECT length(a) from max_varchar ; length ------------ 1073741800 (1 row)
  • 54. Data Types ● Most migration tools translate an Oracle NUMBER to a PostgreSQL NUMERIC – A PostgreSQL NUMERIC can hold ● 131072 before the decimal point ● 16383 after the decimal point ● It is not the same are NUMBER SELECT to_number(n, n) FROM repeat('9', 131071) n;
  • 55. Summary ● System Tuning ● Case Folding ● Table Spaces ● Dual Table ● Exceptions ● Fine Tuning ● Native Features ● Synonyms ● Nulls ● Data Types
  • 58. Biggest Mistake ● Paying for a commercial database when it is not necessary – Listening to the FUD – Building new solutions on Oracle – Not learning PostgreSQL to leverage its great features and functionality