SlideShare a Scribd company logo
Workshop
Oracle to Postgres Migration
Part 2 - Running Postgres
2016-06-22 @IDM
Chris Mair
http://www.pgtraining.com
2016-06-22OracletoPostgresMigration-part2
The Workshop
very quick walk through for Postgres-DBAs to-be
• installation, getting support, the configuration
files, psql, understanding transactions, the
query-planner and locking, backups, system
tables, streaming replication, hot standbys,
connection pooling, load balancing and even
automatic failover all with life-demos and
condensed into just three hours - will we finish
on time?
2016-06-22OracletoPostgresMigration-part2
Getting Support
• very good community support through mailing
lists: psql.it list / Italian and official list (English)
and many others
• commercial support - in Italy for example from us
at PGtraining (three free lancers) or 2ndQuadrant
(SRL), in Austria from Cypertec (GmbH) et al
• don't forget managed hosting offerings from
Amazon Web Services (PostgreSQL RDS),
Heroku and others
2016-06-22OracletoPostgresMigration-part2
Installing Postgres
• from your distro (note that the second digit is the
major version 9.0 and 9.5 are five years apart
and some distros carry outdated versions)
• from the official repos at www.postgresql.org/
download/ - all major package formats supported
• from source (it is easier than you think:
everything can be compiled in a minute or two)
2016-06-22OracletoPostgresMigration-part2
From Source, You Say?
• yeah, why not?
# Centos 7
yum -y install wget
yum -y install gcc make zlib zlib-devel libxml2 libxml2-devel 
readline readline-devel openssl openssl-libs openssl-devel
useradd -m -s /bin/bash pg95
chmod 755 /home/pg95
su - pg95 -c 'wget https://ftp.postgresql.org/pub/source/v9.5.3/postgresql-9.5.3.tar.gz'
su - pg95 -c 'tar xf postgresql-9.5.3.tar.gz'
su - pg95 -c 'cd postgresql-9.5.3; ./configure --prefix=/home/pg95 --with-libxml 
--with-openssl'
su - pg95 -c 'cd postgresql-9.5.3; make -j 2 && make install'
2016-06-22OracletoPostgresMigration-part2
Sample Setup (v.1)
2016-06-22OracletoPostgresMigration-part2
Configuration
• use initdb to create the "cluster" (as in "instance
of postgres serving a set of databases", not as in
a set of machines)
• configuration is in $PGDATA/postgresql.conf
(at the very least check out listen_addresses,
max_connections, shared_buffers and
work_mem)
• ACLs are in $PGDATA/pg_hba.conf
su - pg95 -c 'bin/initdb -D data'
# instance is fully contained in PGDATA=/home/pg95/data now
2016-06-22OracletoPostgresMigration-part2
Starting and Connecting
• pg_ctl is your friend (put this line in /etc/rc.local
and make it executable):
• psql is the universal client:
su - pg95 -c 'bin/pg_ctl -D data -l log start'
[root@p0-primary ~]# su - pg95
Last login: Wed Jun 22 08:47:36 UTC 2016 on pts/0
[pg95@p0-primary ~]$ bin/psql postgres
psql (9.5.3)
Type "help" for help.
postgres=# q
[pg95@p0-primary ~]$
2016-06-22OracletoPostgresMigration-part2
Psql Sample Session
[root@p0-primary ~]# su - pg95
Last login: Wed Jun 22 08:47:36 UTC 2016 on pts/0
[pg95@p0-primary ~]$ bin/psql postgres
psql (9.5.3)
Type "help" for help.
postgres=# l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
postgres | pg95 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | pg95 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | [...]
template1 | pg95 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | [...]
(3 rows)
postgres=# dn
List of schemas
Name | Owner
--------+-------
public | pg95
(1 row)
postgres=# d
List of relations
Schema | Name | Type | Owner
--------+------------+----------+-------
public | tab | table | pg95
public | tab_id_seq | sequence | pg95
(2 rows)
databases
schemas
tables et.al ?
2016-06-22OracletoPostgresMigration-part2
One Elephant at Work -
understanding transactions
• let's generate a file with single inserts:
• and load it into the database:
• experiments - what happens if:
• you add a begin/commit around the inserts?
• you create an unlogged table?
• you set synchronous_commit to off?
for (( i=0; i < 50000; i++ )) do
echo insert into big values ( $RANDOM ) ;
done
psql postgres -c "drop table big; create table big (x int);"
time psql postgres --quiet < inserts.sql
} outcome will
pretty much depend
on disk type...
2016-06-22OracletoPostgresMigration-part2
One Elephant at Work -
understanding the planner
• let's generate a large table with an index:
• and look at the plans for queries such as:
• experiment - what happens if:
• you switch off auto-analyze (parameter autovacuum
= off in postgresql.conf), restart the server, drop and
recreate the table and repeat the experiment?
select random() as x into big from generate_series(1, 1000000);
create index ix on big(x);
explain select count(*) from big where x < 0.00001;
2016-06-22OracletoPostgresMigration-part2
One Elephant at Work -
understanding MVCC and locking
• thanks to MVCC, "normal" operations such as update/delete/insert
do not need to lock a table, you can do a:
in one session while the table is fully usable on another session.
only if you try to update/delete THE SAME row, will the second
session be blocked.
• there are, however, operations that need locks on whole tables,
typically I've seen:
• truncate
• DDL statements such as ALTER TABLE
• I've seen situations were postgres instances were very "laggy", while
the system load was low due to lock contention
begin;
update person set name = 'Chris' where id = 1;
-- wait
2016-06-22OracletoPostgresMigration-part2
Useful System Tables
• pg_stat_activity - list of sessions and what they're doing:
select pid, usename, state, query from pg_stat_activity;
• pg_locks (beware for example of AccessExclusiveLock locks on
user tables):
select locktype, database, relation, (select relname from pg_class where
oid = relation), pid, mode from pg_locks;
• pg_stat_all_tables - to check among other things auto-analyze is
good:
select relname, last_analyze, last_autoanalyze from pg_stat_user_tables;
• and many more
2016-06-22OracletoPostgresMigration-part2
Backups
• cold backups - just shut the server down and archive the
$PGDATA directory
• online backups - pg_dump or pg_dumpall:
• pg_dump is per database (or table) with options, for example
binary output
• pg_dumpall is needed to backup the cluster-wide info such
as users
• psql and possibly pg_restore (to read the binary format) are
needed to restore the DBs
• demo as time permits
2016-06-22OracletoPostgresMigration-part2
No More Elephants
• Have a look at Josh Berkus' 7 ways to crash Postgres:
• no updates
• out of disk space
• deleting stuff
• out of RAM
• bad hardware
• too many connections
• zombie locks
2016-06-22OracletoPostgresMigration-part2
More Than One Elephant
• the other meaning of the word "cluster" is somewhat vague - here are some
Postgres features that I currently like to use:
• streaming replication: stream database operations to other nodes in real time
(optionally as 2-safe replication - i.e. at least one slave must have ack’ed a
transaction), this can be cascading too
• hot standby: issue queries on any secondary node (this includes doing online
backups on a secondary to save load from the primary)
• instant failover: promote a hot standby node to primary node instantly with a
single operation for high availability setups
• third party software allows much more, including master-master setups
• recent developments have much enhanced the streaming capabilities, for
example pglogical and BDR - eventually these will be merged into Postgres
(see for example my presentation on BDR)
2016-06-22OracletoPostgresMigration-part2
We've Been Doing it the
Whole Time ;)
2016-06-22OracletoPostgresMigration-part2
Setting up Streaming
Replication with a Hot Standby
• 5 minutes instruction by Cybertec
• our setup scripted for reference:
PRIMARY_IP=10.0.1.123
SECONDARY_IP=10.0.1.124
# primary setup
su - pg95 -c 'bin/initdb -D data'
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /home/pg95/data/postgresql.conf
sed -i "s/#wal_level = minimal/wal_level = hot_standby/" /home/pg95/data/postgresql.conf
sed -i "s/#max_wal_senders = 0/max_wal_senders = 3/" /home/pg95/data/postgresql.conf
sed -i "s/#wal_keep_segments = 0/wal_keep_segments = 1024/" /home/pg95/data/postgresql.conf
sed -i "s/#hot_standby = off/hot_standby = on/" /home/pg95/data/postgresql.conf
echo "host replication all $SECONDARY_IP/32 trust" >> /home/pg95/data/pg_hba.conf
su - pg95 -c 'bin/pg_ctl -D data -l log start'
# note: use ssl and don't use trust auth in production, also have a look at the feature "replication slots"
# and if you're doing online backups on the standby see 25.5.2. Handling Query Conflicts in the manual
# secondary setup
su - pg95 -c 'mkdir data && chmod 700 data'
su - pg95 -c "bin/pg_basebackup -h $PRIMARY_IP -D /home/pg95/data --xlog-method=stream"
su - pg95 -c "echo 'standby_mode = on' > data/recovery.conf"
su - pg95 -c "echo "primary_conninfo = 'host=$PRIMARY_IP'" >> data/recovery.conf"
su - pg95 -c "echo "trigger_file = '/tmp/promoteme'" >> data/recovery.conf"
2016-06-22OracletoPostgresMigration-part2
Streaming Experiments
• screenshot from another demo (with machines
africa and asia):
2016-06-22OracletoPostgresMigration-part2
L'Appetito vien mangiando
• from the point of view of the application:
• hey, a connection pool would be handy!
• mmm.... in case of failover to the standby, how
am I notified that I need to change my JDBC
URL?
• come to think of it, it would be cool to off-load
read-only queries to the secondary server(s),
but I don't want to handle that logic by myself...
2016-06-22OracletoPostgresMigration-part2
Enter pgpool-II
• pgpool-II is a middleware that does exactly this:
• it hides Postgres servers behind one port 5432
• it does connection pooling
• it does load balancing with the ability to pre-parse queries and send read-only
once to the standbys
• and much more:
• it can do replication by sending the same queries to multiple servers (this is
master-master replication even, but it is less efficient and more fragile than doing
it with streaming replication)
• it has a built-in watchdog for high availability setups with two pgool-II servers and
virtual IPs
• etc.
2016-06-22OracletoPostgresMigration-part2
pgpool-II
• here is a pgpool-II presentation from the author of the software - this is
what we want to do (from the linked presentation):
2016-06-22OracletoPostgresMigration-part2
The pool is ready!
2016-06-22OracletoPostgresMigration-part2
Experiments
• demo what we have on p2, enable query logging
on p0 and p1 to see the load balancing in action,
see what happens if p0 or p1 goes down!
• our setup for reference:
# note: make a db user nobdody for the monitoring and make a pg_hba.conf entry on p0 and 01 too...
useradd -m -s /bin/bash pgpool
su - pgpool -c 'wget -O pgpool-II-3.5.3.tar.gz http://www.pgpool.net/download.php?f=pgpool-II-3.5.3.tar.gz'
su - pgpool -c 'tar xf pgpool-II-3.5.3.tar.gz'
su - pgpool -c 'cd pgpool-II-3.5.3; ./configure --prefix=/home/pgpool --with-openssl --with-pgsql=/home/pg95'
su - pgpool -c 'cd pgpool-II-3.5.3; make -j 2 && make install'
su - pgpool -c 'cp etc/pgpool.conf.sample-stream etc/pgpool.conf'
su - pgpool -c 'cp etc/pool_hba.conf.sample etc/pool_hba.conf'
su - pgpool -c 'cp etc/pcp.conf.sample etc/pcp.conf'
sed -i "s/^backend_/#backend_/" /home/pgpool/etc/pgpool.conf
sed -i "s/^pid_file_name = '/var/run/pgpool/pgpool.pid'/pid_file_name = '/home/pgpool/pgpool.pid'/" /home/pgpool/etc/pgpool.conf
sed -i "s/^logdir = '/tmp'/logdir = '/home/pgpool'/" /home/pgpool/etc/pgpool.conf
sed -i "s/^health_check_period = 0/health_check_period = 1/" /home/pgpool/etc/pgpool.conf
echo "backend_hostname0 = '$PRIMARY_IP'" >> /home/pgpool/etc/pgpool.conf
echo "backend_port0 = 5432" >> /home/pgpool/etc/pgpool.conf
echo "backend_weight0 = 1" >> /home/pgpool/etc/pgpool.conf
echo "backend_hostname1 = '$SECONDARY_IP'" >> /home/pgpool/etc/pgpool.conf
echo "backend_port1 = 5432" >> /home/pgpool/etc/pgpool.conf
echo "backend_weight1 = 1" >> /home/pgpool/etc/pgpool.conf
echo "pgpool:d41d8cd98f00b204e9800998ecf8427e" >> /home/pgpool/etc/pcp.conf # empty password
su - pgpool -c 'nohup pgpool -n 2> log &'
2016-06-22OracletoPostgresMigration-part2
Failover
• one of the cool features of pgpool-II is that
events from nodes attaching/detaching can be
scripted
• demo (if time permits) how to instruct pgpool-II to
connect to the standby over SSH and touch the
trigger file to trigger a promotion to primary
• however, always be aware that automatic failover
can be tricky (test well!)
2016-06-22OracletoPostgresMigration-part2
A Simpler Pool
• if you don't need load balancing and automatic
failover, I recommend PgBouncer
• PgBouncer is "only" a connection pool, but it
does that job really well
• you can also combine pgpool-II and PgBouncer
2016-06-22OracletoPostgresMigration-part2
'k thx bye ;)

More Related Content

What's hot (20)

PDF
The Oracle RAC Family of Solutions - Presentation
Markus Michalewicz
 
PPTX
Migration from Oracle to PostgreSQL: NEED vs REALITY
Ashnikbiz
 
PPTX
Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder
 
PPSX
Domain Driven Design
Araf Karsh Hamid
 
PDF
Cloud-Native Integration with Apache Camel on Kubernetes (Copenhagen October ...
Claus Ibsen
 
PDF
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
PDF
Clean Infrastructure as Code
QAware GmbH
 
PDF
Stumbling stones when migrating from Oracle
EDB
 
PPTX
Oracle GoldenGate 21c New Features and Best Practices
Bobby Curtis
 
PDF
What Is Kubernetes | Kubernetes Introduction | Kubernetes Tutorial For Beginn...
Edureka!
 
PDF
DOAG Oracle Unified Audit in Multitenant Environments
Stefan Oehrli
 
PDF
Oracle Database 12c Multitenant for Consolidation
Yudi Herdiana
 
PPTX
What’s New in Oracle Database 19c - Part 1
Satishbabu Gunukula
 
PPT
Oracle GoldenGate
oracleonthebrain
 
PDF
Introduction to Oracle Cloud Infrastructure Services
Knoldus Inc.
 
PDF
PostgreSQL for Oracle Developers and DBA's
Gerger
 
PPTX
What to Expect From Oracle database 19c
Maria Colgan
 
PDF
BigQuery implementation
Simon Su
 
PDF
The Patterns of Distributed Logging and Containers
SATOSHI TAGOMORI
 
PDF
Apache Airflow
Sumit Maheshwari
 
The Oracle RAC Family of Solutions - Presentation
Markus Michalewicz
 
Migration from Oracle to PostgreSQL: NEED vs REALITY
Ashnikbiz
 
Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder
 
Domain Driven Design
Araf Karsh Hamid
 
Cloud-Native Integration with Apache Camel on Kubernetes (Copenhagen October ...
Claus Ibsen
 
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
Clean Infrastructure as Code
QAware GmbH
 
Stumbling stones when migrating from Oracle
EDB
 
Oracle GoldenGate 21c New Features and Best Practices
Bobby Curtis
 
What Is Kubernetes | Kubernetes Introduction | Kubernetes Tutorial For Beginn...
Edureka!
 
DOAG Oracle Unified Audit in Multitenant Environments
Stefan Oehrli
 
Oracle Database 12c Multitenant for Consolidation
Yudi Herdiana
 
What’s New in Oracle Database 19c - Part 1
Satishbabu Gunukula
 
Oracle GoldenGate
oracleonthebrain
 
Introduction to Oracle Cloud Infrastructure Services
Knoldus Inc.
 
PostgreSQL for Oracle Developers and DBA's
Gerger
 
What to Expect From Oracle database 19c
Maria Colgan
 
BigQuery implementation
Simon Su
 
The Patterns of Distributed Logging and Containers
SATOSHI TAGOMORI
 
Apache Airflow
Sumit Maheshwari
 

Viewers also liked (15)

PDF
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Gabriele Bartolini
 
PDF
Key Methodologies for Migrating from Oracle to Postgres
EDB
 
PDF
Love Your Database (ESC 2k16)
PgTraining
 
PPTX
Product Update: EDB Postgres Platform 2017
EDB
 
PPTX
PGADMIN, Aplicaciones
IsabelAlisson
 
PDF
Shaping Optimizer's Search Space
Gerger
 
PDF
Reducing Database Pain & Costs with Postgres
EDB
 
PDF
Ashnik EnterpriseDB PostgreSQL - A real alternative to Oracle
Ashnikbiz
 
PDF
Why use PostgreSQL?
Gabriele Bartolini
 
PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
PDF
Optimizing Your Postgres ROI Through Best Practices
EDB
 
PDF
EDB Postgres DBA Best Practices
EDB
 
PDF
5 Postgres DBA Tips
EDB
 
PDF
Why we love pgpool-II and why we hate it!
PGConf APAC
 
PDF
In-Database Analyticsの必要性と可能性
Satoshi Nagayasu
 
Agile Oracle to PostgreSQL migrations (PGConf.EU 2013)
Gabriele Bartolini
 
Key Methodologies for Migrating from Oracle to Postgres
EDB
 
Love Your Database (ESC 2k16)
PgTraining
 
Product Update: EDB Postgres Platform 2017
EDB
 
PGADMIN, Aplicaciones
IsabelAlisson
 
Shaping Optimizer's Search Space
Gerger
 
Reducing Database Pain & Costs with Postgres
EDB
 
Ashnik EnterpriseDB PostgreSQL - A real alternative to Oracle
Ashnikbiz
 
Why use PostgreSQL?
Gabriele Bartolini
 
10 Reasons to Start Your Analytics Project with PostgreSQL
Satoshi Nagayasu
 
Optimizing Your Postgres ROI Through Best Practices
EDB
 
EDB Postgres DBA Best Practices
EDB
 
5 Postgres DBA Tips
EDB
 
Why we love pgpool-II and why we hate it!
PGConf APAC
 
In-Database Analyticsの必要性と可能性
Satoshi Nagayasu
 
Ad

Similar to Oracle to Postgres Migration - part 2 (20)

PDF
The Accidental DBA
PostgreSQL Experts, Inc.
 
PDF
PERFORMANCE_SCHEMA and sys schema
FromDual GmbH
 
PPTX
Backups
Payal Singh
 
PDF
What’s new in 9.6, by PostgreSQL contributor
Masahiko Sawada
 
PPTX
Developing with the Go client for Apache Kafka
Joe Stein
 
PDF
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
PGDay.Amsterdam
 
PDF
GDG Cloud Iasi - Docker For The Busy Developer.pdf
athlonica
 
PDF
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
Valeriy Kravchuk
 
PDF
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PDF
Demystifying postgres logical replication percona live sc
Emanuel Calvo
 
PPTX
Eko10 Workshop Opensource Database Auditing
Juan Berner
 
PDF
More on gdb for my sql db as (fosdem 2016)
Valeriy Kravchuk
 
PDF
Designing Tracing Tools
Brendan Gregg
 
PPTX
Infrastructure review - Shining a light on the Black Box
Miklos Szel
 
PPTX
How (not) to kill your MySQL infrastructure
Miklos Szel
 
PPTX
Designing Tracing Tools
Sysdig
 
PPTX
Eko10 workshop - OPEN SOURCE DATABASE MONITORING
Pablo Garbossa
 
PPT
Logstash
琛琳 饶
 
The Accidental DBA
PostgreSQL Experts, Inc.
 
PERFORMANCE_SCHEMA and sys schema
FromDual GmbH
 
Backups
Payal Singh
 
What’s new in 9.6, by PostgreSQL contributor
Masahiko Sawada
 
Developing with the Go client for Apache Kafka
Joe Stein
 
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
PGDay.Amsterdam
 
GDG Cloud Iasi - Docker For The Busy Developer.pdf
athlonica
 
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
Valeriy Kravchuk
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
Demystifying postgres logical replication percona live sc
Emanuel Calvo
 
Eko10 Workshop Opensource Database Auditing
Juan Berner
 
More on gdb for my sql db as (fosdem 2016)
Valeriy Kravchuk
 
Designing Tracing Tools
Brendan Gregg
 
Infrastructure review - Shining a light on the Black Box
Miklos Szel
 
How (not) to kill your MySQL infrastructure
Miklos Szel
 
Designing Tracing Tools
Sysdig
 
Eko10 workshop - OPEN SOURCE DATABASE MONITORING
Pablo Garbossa
 
Logstash
琛琳 饶
 
Ad

More from PgTraining (7)

PDF
Webminar del 12.03.2012
PgTraining
 
PDF
Webminar del 12.03.2012
PgTraining
 
PDF
Weminar 12.03.2021 . JIT
PgTraining
 
PDF
Openday - PostgreSQL: primi passi con Json/Jsonb
PgTraining
 
PDF
Pgtraining bdr
PgTraining
 
PDF
Messa in rete
PgTraining
 
PDF
Apcamp
PgTraining
 
Webminar del 12.03.2012
PgTraining
 
Webminar del 12.03.2012
PgTraining
 
Weminar 12.03.2021 . JIT
PgTraining
 
Openday - PostgreSQL: primi passi con Json/Jsonb
PgTraining
 
Pgtraining bdr
PgTraining
 
Messa in rete
PgTraining
 
Apcamp
PgTraining
 

Recently uploaded (20)

PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PPTX
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
PPTX
03_Ariane BERCKMOES_Ethias.pptx_AIBarometer_release_event
FinTech Belgium
 
PDF
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
PPTX
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
PPTX
01_Nico Vincent_Sailpeak.pptx_AI_Barometer_2025
FinTech Belgium
 
PPTX
What Is Data Integration and Transformation?
subhashenia
 
PPTX
big data eco system fundamentals of data science
arivukarasi
 
PDF
Group 5_RMB Final Project on circular economy
pgban24anmola
 
PDF
Business implication of Artificial Intelligence.pdf
VishalChugh12
 
PPTX
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
 
PPTX
Powerful Uses of Data Analytics You Should Know
subhashenia
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
PDF
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
PDF
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
PDF
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
How to Add Columns and Rows in an R Data Frame
subhashenia
 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
03_Ariane BERCKMOES_Ethias.pptx_AIBarometer_release_event
FinTech Belgium
 
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
01_Nico Vincent_Sailpeak.pptx_AI_Barometer_2025
FinTech Belgium
 
What Is Data Integration and Transformation?
subhashenia
 
big data eco system fundamentals of data science
arivukarasi
 
Group 5_RMB Final Project on circular economy
pgban24anmola
 
Business implication of Artificial Intelligence.pdf
VishalChugh12
 
thid ppt defines the ich guridlens and gives the information about the ICH gu...
shaistabegum14
 
Powerful Uses of Data Analytics You Should Know
subhashenia
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
How to Add Columns and Rows in an R Data Frame
subhashenia
 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 

Oracle to Postgres Migration - part 2

  • 1. Workshop Oracle to Postgres Migration Part 2 - Running Postgres 2016-06-22 @IDM Chris Mair http://www.pgtraining.com
  • 2. 2016-06-22OracletoPostgresMigration-part2 The Workshop very quick walk through for Postgres-DBAs to-be • installation, getting support, the configuration files, psql, understanding transactions, the query-planner and locking, backups, system tables, streaming replication, hot standbys, connection pooling, load balancing and even automatic failover all with life-demos and condensed into just three hours - will we finish on time?
  • 3. 2016-06-22OracletoPostgresMigration-part2 Getting Support • very good community support through mailing lists: psql.it list / Italian and official list (English) and many others • commercial support - in Italy for example from us at PGtraining (three free lancers) or 2ndQuadrant (SRL), in Austria from Cypertec (GmbH) et al • don't forget managed hosting offerings from Amazon Web Services (PostgreSQL RDS), Heroku and others
  • 4. 2016-06-22OracletoPostgresMigration-part2 Installing Postgres • from your distro (note that the second digit is the major version 9.0 and 9.5 are five years apart and some distros carry outdated versions) • from the official repos at www.postgresql.org/ download/ - all major package formats supported • from source (it is easier than you think: everything can be compiled in a minute or two)
  • 5. 2016-06-22OracletoPostgresMigration-part2 From Source, You Say? • yeah, why not? # Centos 7 yum -y install wget yum -y install gcc make zlib zlib-devel libxml2 libxml2-devel readline readline-devel openssl openssl-libs openssl-devel useradd -m -s /bin/bash pg95 chmod 755 /home/pg95 su - pg95 -c 'wget https://ftp.postgresql.org/pub/source/v9.5.3/postgresql-9.5.3.tar.gz' su - pg95 -c 'tar xf postgresql-9.5.3.tar.gz' su - pg95 -c 'cd postgresql-9.5.3; ./configure --prefix=/home/pg95 --with-libxml --with-openssl' su - pg95 -c 'cd postgresql-9.5.3; make -j 2 && make install'
  • 7. 2016-06-22OracletoPostgresMigration-part2 Configuration • use initdb to create the "cluster" (as in "instance of postgres serving a set of databases", not as in a set of machines) • configuration is in $PGDATA/postgresql.conf (at the very least check out listen_addresses, max_connections, shared_buffers and work_mem) • ACLs are in $PGDATA/pg_hba.conf su - pg95 -c 'bin/initdb -D data' # instance is fully contained in PGDATA=/home/pg95/data now
  • 8. 2016-06-22OracletoPostgresMigration-part2 Starting and Connecting • pg_ctl is your friend (put this line in /etc/rc.local and make it executable): • psql is the universal client: su - pg95 -c 'bin/pg_ctl -D data -l log start' [root@p0-primary ~]# su - pg95 Last login: Wed Jun 22 08:47:36 UTC 2016 on pts/0 [pg95@p0-primary ~]$ bin/psql postgres psql (9.5.3) Type "help" for help. postgres=# q [pg95@p0-primary ~]$
  • 9. 2016-06-22OracletoPostgresMigration-part2 Psql Sample Session [root@p0-primary ~]# su - pg95 Last login: Wed Jun 22 08:47:36 UTC 2016 on pts/0 [pg95@p0-primary ~]$ bin/psql postgres psql (9.5.3) Type "help" for help. postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+----------+-------------+-------------+------------------- postgres | pg95 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | pg95 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | [...] template1 | pg95 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | [...] (3 rows) postgres=# dn List of schemas Name | Owner --------+------- public | pg95 (1 row) postgres=# d List of relations Schema | Name | Type | Owner --------+------------+----------+------- public | tab | table | pg95 public | tab_id_seq | sequence | pg95 (2 rows) databases schemas tables et.al ?
  • 10. 2016-06-22OracletoPostgresMigration-part2 One Elephant at Work - understanding transactions • let's generate a file with single inserts: • and load it into the database: • experiments - what happens if: • you add a begin/commit around the inserts? • you create an unlogged table? • you set synchronous_commit to off? for (( i=0; i < 50000; i++ )) do echo insert into big values ( $RANDOM ) ; done psql postgres -c "drop table big; create table big (x int);" time psql postgres --quiet < inserts.sql } outcome will pretty much depend on disk type...
  • 11. 2016-06-22OracletoPostgresMigration-part2 One Elephant at Work - understanding the planner • let's generate a large table with an index: • and look at the plans for queries such as: • experiment - what happens if: • you switch off auto-analyze (parameter autovacuum = off in postgresql.conf), restart the server, drop and recreate the table and repeat the experiment? select random() as x into big from generate_series(1, 1000000); create index ix on big(x); explain select count(*) from big where x < 0.00001;
  • 12. 2016-06-22OracletoPostgresMigration-part2 One Elephant at Work - understanding MVCC and locking • thanks to MVCC, "normal" operations such as update/delete/insert do not need to lock a table, you can do a: in one session while the table is fully usable on another session. only if you try to update/delete THE SAME row, will the second session be blocked. • there are, however, operations that need locks on whole tables, typically I've seen: • truncate • DDL statements such as ALTER TABLE • I've seen situations were postgres instances were very "laggy", while the system load was low due to lock contention begin; update person set name = 'Chris' where id = 1; -- wait
  • 13. 2016-06-22OracletoPostgresMigration-part2 Useful System Tables • pg_stat_activity - list of sessions and what they're doing: select pid, usename, state, query from pg_stat_activity; • pg_locks (beware for example of AccessExclusiveLock locks on user tables): select locktype, database, relation, (select relname from pg_class where oid = relation), pid, mode from pg_locks; • pg_stat_all_tables - to check among other things auto-analyze is good: select relname, last_analyze, last_autoanalyze from pg_stat_user_tables; • and many more
  • 14. 2016-06-22OracletoPostgresMigration-part2 Backups • cold backups - just shut the server down and archive the $PGDATA directory • online backups - pg_dump or pg_dumpall: • pg_dump is per database (or table) with options, for example binary output • pg_dumpall is needed to backup the cluster-wide info such as users • psql and possibly pg_restore (to read the binary format) are needed to restore the DBs • demo as time permits
  • 15. 2016-06-22OracletoPostgresMigration-part2 No More Elephants • Have a look at Josh Berkus' 7 ways to crash Postgres: • no updates • out of disk space • deleting stuff • out of RAM • bad hardware • too many connections • zombie locks
  • 16. 2016-06-22OracletoPostgresMigration-part2 More Than One Elephant • the other meaning of the word "cluster" is somewhat vague - here are some Postgres features that I currently like to use: • streaming replication: stream database operations to other nodes in real time (optionally as 2-safe replication - i.e. at least one slave must have ack’ed a transaction), this can be cascading too • hot standby: issue queries on any secondary node (this includes doing online backups on a secondary to save load from the primary) • instant failover: promote a hot standby node to primary node instantly with a single operation for high availability setups • third party software allows much more, including master-master setups • recent developments have much enhanced the streaming capabilities, for example pglogical and BDR - eventually these will be merged into Postgres (see for example my presentation on BDR)
  • 18. 2016-06-22OracletoPostgresMigration-part2 Setting up Streaming Replication with a Hot Standby • 5 minutes instruction by Cybertec • our setup scripted for reference: PRIMARY_IP=10.0.1.123 SECONDARY_IP=10.0.1.124 # primary setup su - pg95 -c 'bin/initdb -D data' sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /home/pg95/data/postgresql.conf sed -i "s/#wal_level = minimal/wal_level = hot_standby/" /home/pg95/data/postgresql.conf sed -i "s/#max_wal_senders = 0/max_wal_senders = 3/" /home/pg95/data/postgresql.conf sed -i "s/#wal_keep_segments = 0/wal_keep_segments = 1024/" /home/pg95/data/postgresql.conf sed -i "s/#hot_standby = off/hot_standby = on/" /home/pg95/data/postgresql.conf echo "host replication all $SECONDARY_IP/32 trust" >> /home/pg95/data/pg_hba.conf su - pg95 -c 'bin/pg_ctl -D data -l log start' # note: use ssl and don't use trust auth in production, also have a look at the feature "replication slots" # and if you're doing online backups on the standby see 25.5.2. Handling Query Conflicts in the manual # secondary setup su - pg95 -c 'mkdir data && chmod 700 data' su - pg95 -c "bin/pg_basebackup -h $PRIMARY_IP -D /home/pg95/data --xlog-method=stream" su - pg95 -c "echo 'standby_mode = on' > data/recovery.conf" su - pg95 -c "echo "primary_conninfo = 'host=$PRIMARY_IP'" >> data/recovery.conf" su - pg95 -c "echo "trigger_file = '/tmp/promoteme'" >> data/recovery.conf"
  • 19. 2016-06-22OracletoPostgresMigration-part2 Streaming Experiments • screenshot from another demo (with machines africa and asia):
  • 20. 2016-06-22OracletoPostgresMigration-part2 L'Appetito vien mangiando • from the point of view of the application: • hey, a connection pool would be handy! • mmm.... in case of failover to the standby, how am I notified that I need to change my JDBC URL? • come to think of it, it would be cool to off-load read-only queries to the secondary server(s), but I don't want to handle that logic by myself...
  • 21. 2016-06-22OracletoPostgresMigration-part2 Enter pgpool-II • pgpool-II is a middleware that does exactly this: • it hides Postgres servers behind one port 5432 • it does connection pooling • it does load balancing with the ability to pre-parse queries and send read-only once to the standbys • and much more: • it can do replication by sending the same queries to multiple servers (this is master-master replication even, but it is less efficient and more fragile than doing it with streaming replication) • it has a built-in watchdog for high availability setups with two pgool-II servers and virtual IPs • etc.
  • 22. 2016-06-22OracletoPostgresMigration-part2 pgpool-II • here is a pgpool-II presentation from the author of the software - this is what we want to do (from the linked presentation):
  • 24. 2016-06-22OracletoPostgresMigration-part2 Experiments • demo what we have on p2, enable query logging on p0 and p1 to see the load balancing in action, see what happens if p0 or p1 goes down! • our setup for reference: # note: make a db user nobdody for the monitoring and make a pg_hba.conf entry on p0 and 01 too... useradd -m -s /bin/bash pgpool su - pgpool -c 'wget -O pgpool-II-3.5.3.tar.gz http://www.pgpool.net/download.php?f=pgpool-II-3.5.3.tar.gz' su - pgpool -c 'tar xf pgpool-II-3.5.3.tar.gz' su - pgpool -c 'cd pgpool-II-3.5.3; ./configure --prefix=/home/pgpool --with-openssl --with-pgsql=/home/pg95' su - pgpool -c 'cd pgpool-II-3.5.3; make -j 2 && make install' su - pgpool -c 'cp etc/pgpool.conf.sample-stream etc/pgpool.conf' su - pgpool -c 'cp etc/pool_hba.conf.sample etc/pool_hba.conf' su - pgpool -c 'cp etc/pcp.conf.sample etc/pcp.conf' sed -i "s/^backend_/#backend_/" /home/pgpool/etc/pgpool.conf sed -i "s/^pid_file_name = '/var/run/pgpool/pgpool.pid'/pid_file_name = '/home/pgpool/pgpool.pid'/" /home/pgpool/etc/pgpool.conf sed -i "s/^logdir = '/tmp'/logdir = '/home/pgpool'/" /home/pgpool/etc/pgpool.conf sed -i "s/^health_check_period = 0/health_check_period = 1/" /home/pgpool/etc/pgpool.conf echo "backend_hostname0 = '$PRIMARY_IP'" >> /home/pgpool/etc/pgpool.conf echo "backend_port0 = 5432" >> /home/pgpool/etc/pgpool.conf echo "backend_weight0 = 1" >> /home/pgpool/etc/pgpool.conf echo "backend_hostname1 = '$SECONDARY_IP'" >> /home/pgpool/etc/pgpool.conf echo "backend_port1 = 5432" >> /home/pgpool/etc/pgpool.conf echo "backend_weight1 = 1" >> /home/pgpool/etc/pgpool.conf echo "pgpool:d41d8cd98f00b204e9800998ecf8427e" >> /home/pgpool/etc/pcp.conf # empty password su - pgpool -c 'nohup pgpool -n 2> log &'
  • 25. 2016-06-22OracletoPostgresMigration-part2 Failover • one of the cool features of pgpool-II is that events from nodes attaching/detaching can be scripted • demo (if time permits) how to instruct pgpool-II to connect to the standby over SSH and touch the trigger file to trigger a promotion to primary • however, always be aware that automatic failover can be tricky (test well!)
  • 26. 2016-06-22OracletoPostgresMigration-part2 A Simpler Pool • if you don't need load balancing and automatic failover, I recommend PgBouncer • PgBouncer is "only" a connection pool, but it does that job really well • you can also combine pgpool-II and PgBouncer