SlideShare a Scribd company logo
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
@ me
Recovering From A Damaged
PostgreSQL Cluster
Asurion Mobile Applications
http://www.asurion.com/
Robert Bernier
Sr. Database Administrator, Asurion Mobile Applications
robert.bernier@asurion.com
1400 Fashion Island Blvd, San Mateo, CA 94404
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
Just a little bit of
knowledge
Recovering From A Damaged
PostgreSQL Cluster
The secret is...
Recovering From A Damaged
PostgreSQL Cluster
Indexes & Tables are FILES!
Recovering From A Damaged
PostgreSQL Cluster
Recovering From A Damaged
PostgreSQL Cluster
An Example, ver 9.2.4
create table t1 (id serial primary key,x real);
insert into t1 (x) select random() from generate_series(1,1000);
dtis+
List of relations
Schema | Name | Type | Owner | Table | Size | Description
--------+-----------+----------+----------+-------+------------+-------------
public | t1 | table | postgres | | 40 kB |
public | t1_id_seq | sequence | postgres | | 8192 bytes |
public | t1_pkey | index | postgres | t1 | 40 kB |
select relname,relfilenode,relpages,relpages*1024*8 bytes
from pg_class where relname in ('t1','t1_id_seq','t1_pkey');
relname | relfilenode | relpages | bytes
-----------+-------------+----------+-------
t1 | 24628 | 5 | 40960
t1_id_seq | 24626 | 1 | 8192
t1_pkey | 24632 | 5 | 40960
=========================================================================
cd ~/data
ls -l $(find ./|grep 24628) ---> -rw------- 1 rbernier rbernier 40960 2011-03-07 11:53 ./base/11564/24628
-rw------- 1 rbernier rbernier 24576 2011-03-07 11:51 ./base/11564/24628_fsm
ls -l $(find ./|grep 24626) ---> -rw------- 1 rbernier rbernier 8192 2011-03-07 11:53 ./base/11564/24626
ls -l $(find ./|grep 24632) ---> -rw------- 1 rbernier rbernier 40960 2011-03-07 11:53 ./base/11564/24632
Recovering From A Damaged
PostgreSQL Cluster
Damaged files cannot be processed
-- damage the sequence; can't insert records
rm ./base/11564/24626
touch ./base/11564/24626
_______________________________________________________________________
insert into t1(x) values(9990);
ERROR: could not read block 0 of relation base/11564/24626: read only 0 of 8192 bytes
_______________________________________________________________________
ds t1_id_seq
List of relations
Schema | Name | Type | Owner
--------+-----------+----------+----------
public | t1_id_seq | sequence | postgres
_______________________________________________________________________
table t1_id_seq;
sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
---------------+------------+-------------+--------------+-----------+-----------+-------------+---------+-----------+-----------
Recovering From A Damaged
PostgreSQL Cluster
Damaged files cannot be processed
-- damage the index; can't perform a query
rm ./base/11564/24632
touch ./base/11564/24632
-----------------------------
explain select * from t1 where id=100;
QUERY PLAN
------------------------------------------------------------------
Index Scan using t1_pkey on t1 (cost=0.00..8.27 rows=1 width=8)
Index Cond: (id = 100)
postgres=# select * from t1 where id=100;
ERROR: could not read block 0 of relation base/11564/24632: read only 0 of 8192 bytes
Recovering From A Damaged
PostgreSQL Cluster
Damaged files cannot be processed
-- damage the table, can't perform any query
select * from t1;
ERROR: invalid page header in block 0 of relation base/11564/24628
Recovering From A Damaged
PostgreSQL Cluster
Example Invocation
Scenario: Cannot INSERT Due to Damaged Sequence
Solution: DROP and CREATE SEQUENCE
drop sequence t1_id_seq cascade;
create sequence t1_id_seq start with 10001 owned by t1.id;
alter table only t1 alter column id set default nextval('t1_id_seq'::regclass);
select pg_catalog.setval('t1_id_seq', 10002, false);
BEFORE
relname | relfilenode | relpages | bytes
-----------+-------------+----------+-------
t1_id_seq | 24626 | 1 | 8192
AFTER
relname | relfilenode | relpages | bytes
-----------+-------------+----------+-------
t1_id_seq | 189575 | 1 | 8192
Recovering From A Damaged
PostgreSQL Cluster
Example Invocation
Scenario: Rebuild Data Cluster with Corrupted Index
Solution: Rebuild index
reindex index t1_pkey;
BEFORE
relname | relfilenode | relpages | bytes
-----------+-------------+----------+-------
t1_pkey | 24632 | 5 | 40960
AFTER
relname | relfilenode | relpages | bytes
-----------+-------------+----------+-------
t1_pkey | 189578 | 5 | 40960
Recovering From A Damaged
PostgreSQL Cluster
Example Invocation
Scenario: Rebuild Data Cluster with Corrupted System Indexes
Solution: Rebuild indexes under single-user mode
- start up the damaged cluster and reindex with the following invocation
postgres --single postgres -D ~/data <<_eof_
reindex system mydatabase
_eof_
Recovering From A Damaged
PostgreSQL Cluster
About Single UserMode
- Used during bootstrapping by initdb
- Can be used for debugging
- Can be used for disaster recovery
i.e. System indexes can be manipulated
- Can enter queries and the results are printed to the screen
Recovering From A Damaged
PostgreSQL Cluster
About Single UserMode
postgres --single postgres -D ~/data temp
PostgreSQL stand-alone backend 9.2.3
backend> select * from t1 limit 5
1: id (typeid = 23, len = 4, typmod = -1, byval = t)
2: x (typeid = 700, len = 4, typmod = -1, byval = t)
----
1: id = "1" (typeid = 23, len = 4, typmod = -1, byval = t)
2: x = "0.767757" (typeid = 700, len = 4, typmod = -1, byval = t)
----
1: id = "2" (typeid = 23, len = 4, typmod = -1, byval = t)
2: x = "0.253584" (typeid = 700, len = 4, typmod = -1, byval = t)
----
1: id = "3" (typeid = 23, len = 4, typmod = -1, byval = t)
2: x = "0.0912474" (typeid = 700, len = 4, typmod = -1, byval = t)
----
1: id = "4" (typeid = 23, len = 4, typmod = -1, byval = t)
2: x = "0.45276" (typeid = 700, len = 4, typmod = -1, byval = t)
----
1: id = "5" (typeid = 23, len = 4, typmod = -1, byval = t)
2: x = "0.784381" (typeid = 700, len = 4, typmod = -1, byval = t)
----
backend>
Recovering From A Damaged
PostgreSQL Cluster
Example Invocation
Scenario: PostgreSQL Starts Up But Queries To User-Defined Tables Fails
Solution: Generate a data-dump into a newly created data cluster
- start up the damaged cluster with the following invocation
pg_ctl -D ~/data -o '-c zero_damaged_pages=true' -l logfile.txt start
- execute standard data dump with pg_dumpall
Recovering From A Damaged
PostgreSQL Cluster
Example Invocation
Scenario: Rebuild And Reuse An Existing Data Cluster With Corrupted User-Defined Tables
Solution: Rebuild Cluster
- start up the damaged cluster with the following invocation
pg_ctl -D ~/data -o '-c zero_damaged_pages=true' -l logfile.txt start
- login the database and execute the following SQL statement
VACUUM FULL ANALYZE;
REINDEX DATABASE mydatabase;
- restart the newly repaired cluster
pg_ctl -D ~/data -l logfile.txt -m smart restart
Recovering From A Damaged
PostgreSQL Cluster
Caveats
● DROP and CREATE damaged indexes
● Where possible, recreate the cluster on another machine... don't take a chance
● Executing a dump should be the first action i.e. don't VACUUM damaged tables
as you can actually lose data before you reuse/backup/dump
● Damaged system catalogs must be recovered in single user mode
● Tables larger 1GB+ are split
● TOASTED tables add an entirely new level of complexity
Recovering From A Damaged
PostgreSQL Cluster
One last word
Hacking the cluster...
Recovering From A Damaged
PostgreSQL Cluster
References
- http://www.postgresql.org/docs/9.2/static/index.html
- http://www.postgresql.org/docs/9.2/static/app-initdb.html
- http://www.postgresql.org/docs/9.2/static/app-pg-ctl.html
- http://www.postgresql.org/docs/9.2/static/catalog-pg-class.html
- http://www.postgresql.org/docs/9.2/static/runtime-config-developer.html
- http://www.postgresql.org/docs/9.2/static/sql-createindex.html
- http://www.postgresql.org/docs/9.2/static/sql-dropindex.html
- http://www.postgresql.org/docs/9.2/static/sql-reindex.html
- http://www.postgresql.org/docs/9.2/static/sql-vacuum.html
- http://linux.die.net/man/1/find
- http://linux.die.net/man/1/killall
Recovering From A Damaged
PostgreSQL Cluster
Asurion Mobile Applications
http://www.asurion.com/
Robert Bernier
Sr. Database Administrator, Asurion Mobile Applications
robert.bernier@asurion.com
1400 Fashion Island Blvd, San Mateo, CA 94404

More Related Content

PPTX
Replication and Replica Sets
TXT
Oracle ORA Errors
PDF
MySQL Document Store
PPTX
Database administration commands
PDF
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PPTX
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
PDF
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
PPTX
Replication and Replica Sets
Replication and Replica Sets
Oracle ORA Errors
MySQL Document Store
Database administration commands
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
Replication and Replica Sets

What's hot (20)

PDF
[PGDay.Seoul 2020] PostgreSQL 13 New Features
PDF
PostgreSQL 9.6 새 기능 소개
PPTX
Webinar: Replication and Replica Sets
PDF
8 tune tusc
PDF
Lab1-DB-Cassandra
PDF
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
PDF
15 MySQL Basics #burningkeyboards
PDF
Store and Process Big Data with Hadoop and Cassandra
PPTX
MySQLinsanity
PPTX
Sql analytic queries tips
PDF
Advanced pg_stat_statements: Filtering, Regression Testing & more
PDF
Quick reference for Grafana
PDF
Replication
PPTX
Example R usage for oracle DBA UKOUG 2013
PDF
Parallel R in snow (english after 2nd slide)
PDF
Mongodb replication
DOCX
TimesTen in memory database Creation
PDF
Broker otw.pptx
PPTX
MySql:Basics
[PGDay.Seoul 2020] PostgreSQL 13 New Features
PostgreSQL 9.6 새 기능 소개
Webinar: Replication and Replica Sets
8 tune tusc
Lab1-DB-Cassandra
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
15 MySQL Basics #burningkeyboards
Store and Process Big Data with Hadoop and Cassandra
MySQLinsanity
Sql analytic queries tips
Advanced pg_stat_statements: Filtering, Regression Testing & more
Quick reference for Grafana
Replication
Example R usage for oracle DBA UKOUG 2013
Parallel R in snow (english after 2nd slide)
Mongodb replication
TimesTen in memory database Creation
Broker otw.pptx
MySql:Basics
Ad

Similar to Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open (20)

PPTX
MySQL InnoDB Cluster 미리보기 (remote cluster test)
PDF
Introduction to MySQL InnoDB Cluster
PPT
Oracle training in hyderabad
ODP
Drizzle to MySQL, Stress Free Migration
PDF
How To Control IO Usage using Resource Manager
PDF
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
PDF
Lab 2: Classification and Regression Prediction Models, training and testing ...
PPTX
Sql statement,functions and joins
PDF
PerlApp2Postgresql (2)
PPTX
Advanced Replication
PPTX
Replication and replica sets
PPTX
Basic Replication in MongoDB
PPTX
Fatkulin presentation
PPT
03-inheritance.ppt
PDF
Advanced tips of dbms statas
PDF
PDF
12c db upgrade from 11.2.0.4
PDF
Data Wrangling with dplyr and tidyr Cheat Sheet
DOC
Rac nonrac clone
PPTX
From Postgres to Cassandra (Rimas Silkaitis, Heroku) | C* Summit 2016
MySQL InnoDB Cluster 미리보기 (remote cluster test)
Introduction to MySQL InnoDB Cluster
Oracle training in hyderabad
Drizzle to MySQL, Stress Free Migration
How To Control IO Usage using Resource Manager
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Lab 2: Classification and Regression Prediction Models, training and testing ...
Sql statement,functions and joins
PerlApp2Postgresql (2)
Advanced Replication
Replication and replica sets
Basic Replication in MongoDB
Fatkulin presentation
03-inheritance.ppt
Advanced tips of dbms statas
12c db upgrade from 11.2.0.4
Data Wrangling with dplyr and tidyr Cheat Sheet
Rac nonrac clone
From Postgres to Cassandra (Rimas Silkaitis, Heroku) | C* Summit 2016
Ad

More from PostgresOpen (18)

PDF
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PDF
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
PDF
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
PPTX
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
PDF
Keith Paskett - Postgres on ZFS @ Postgres Open
PDF
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
PDF
Craig Kerstiens - Scalable Uniques in Postgres @ Postgres Open
PDF
Henrietta Dombrovskaya - A New Approach to Resolve Object-Relational Impedanc...
PDF
Steve Singer - Managing PostgreSQL with Puppet @ Postgres Open
PDF
John Melesky - Federating Queries Using Postgres FDW @ Postgres Open
PDF
Koichi Suzuki - Postgres-XC Dynamic Cluster Management @ Postgres Open
PDF
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PDF
Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open
PDF
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
PDF
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
PDF
Robert Haas Query Planning Gone Wrong Presentation @ Postgres Open
PDF
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
PDF
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
Keith Paskett - Postgres on ZFS @ Postgres Open
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
Craig Kerstiens - Scalable Uniques in Postgres @ Postgres Open
Henrietta Dombrovskaya - A New Approach to Resolve Object-Relational Impedanc...
Steve Singer - Managing PostgreSQL with Puppet @ Postgres Open
John Melesky - Federating Queries Using Postgres FDW @ Postgres Open
Koichi Suzuki - Postgres-XC Dynamic Cluster Management @ Postgres Open
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
Robert Haas Query Planning Gone Wrong Presentation @ Postgres Open
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Understanding_Digital_Forensics_Presentation.pptx
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectral efficient network and resource selection model in 5G networks
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.pdf

Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open

  • 1. Recovering From A Damaged PostgreSQL Cluster
  • 2. Recovering From A Damaged PostgreSQL Cluster @ me
  • 3. Recovering From A Damaged PostgreSQL Cluster Asurion Mobile Applications http://www.asurion.com/ Robert Bernier Sr. Database Administrator, Asurion Mobile Applications robert.bernier@asurion.com 1400 Fashion Island Blvd, San Mateo, CA 94404
  • 4. Recovering From A Damaged PostgreSQL Cluster
  • 5. Recovering From A Damaged PostgreSQL Cluster
  • 6. Recovering From A Damaged PostgreSQL Cluster
  • 7. Recovering From A Damaged PostgreSQL Cluster
  • 8. Recovering From A Damaged PostgreSQL Cluster
  • 9. Recovering From A Damaged PostgreSQL Cluster
  • 10. Recovering From A Damaged PostgreSQL Cluster Just a little bit of knowledge
  • 11. Recovering From A Damaged PostgreSQL Cluster The secret is...
  • 12. Recovering From A Damaged PostgreSQL Cluster Indexes & Tables are FILES!
  • 13. Recovering From A Damaged PostgreSQL Cluster
  • 14. Recovering From A Damaged PostgreSQL Cluster An Example, ver 9.2.4 create table t1 (id serial primary key,x real); insert into t1 (x) select random() from generate_series(1,1000); dtis+ List of relations Schema | Name | Type | Owner | Table | Size | Description --------+-----------+----------+----------+-------+------------+------------- public | t1 | table | postgres | | 40 kB | public | t1_id_seq | sequence | postgres | | 8192 bytes | public | t1_pkey | index | postgres | t1 | 40 kB | select relname,relfilenode,relpages,relpages*1024*8 bytes from pg_class where relname in ('t1','t1_id_seq','t1_pkey'); relname | relfilenode | relpages | bytes -----------+-------------+----------+------- t1 | 24628 | 5 | 40960 t1_id_seq | 24626 | 1 | 8192 t1_pkey | 24632 | 5 | 40960 ========================================================================= cd ~/data ls -l $(find ./|grep 24628) ---> -rw------- 1 rbernier rbernier 40960 2011-03-07 11:53 ./base/11564/24628 -rw------- 1 rbernier rbernier 24576 2011-03-07 11:51 ./base/11564/24628_fsm ls -l $(find ./|grep 24626) ---> -rw------- 1 rbernier rbernier 8192 2011-03-07 11:53 ./base/11564/24626 ls -l $(find ./|grep 24632) ---> -rw------- 1 rbernier rbernier 40960 2011-03-07 11:53 ./base/11564/24632
  • 15. Recovering From A Damaged PostgreSQL Cluster Damaged files cannot be processed -- damage the sequence; can't insert records rm ./base/11564/24626 touch ./base/11564/24626 _______________________________________________________________________ insert into t1(x) values(9990); ERROR: could not read block 0 of relation base/11564/24626: read only 0 of 8192 bytes _______________________________________________________________________ ds t1_id_seq List of relations Schema | Name | Type | Owner --------+-----------+----------+---------- public | t1_id_seq | sequence | postgres _______________________________________________________________________ table t1_id_seq; sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called ---------------+------------+-------------+--------------+-----------+-----------+-------------+---------+-----------+-----------
  • 16. Recovering From A Damaged PostgreSQL Cluster Damaged files cannot be processed -- damage the index; can't perform a query rm ./base/11564/24632 touch ./base/11564/24632 ----------------------------- explain select * from t1 where id=100; QUERY PLAN ------------------------------------------------------------------ Index Scan using t1_pkey on t1 (cost=0.00..8.27 rows=1 width=8) Index Cond: (id = 100) postgres=# select * from t1 where id=100; ERROR: could not read block 0 of relation base/11564/24632: read only 0 of 8192 bytes
  • 17. Recovering From A Damaged PostgreSQL Cluster Damaged files cannot be processed -- damage the table, can't perform any query select * from t1; ERROR: invalid page header in block 0 of relation base/11564/24628
  • 18. Recovering From A Damaged PostgreSQL Cluster Example Invocation Scenario: Cannot INSERT Due to Damaged Sequence Solution: DROP and CREATE SEQUENCE drop sequence t1_id_seq cascade; create sequence t1_id_seq start with 10001 owned by t1.id; alter table only t1 alter column id set default nextval('t1_id_seq'::regclass); select pg_catalog.setval('t1_id_seq', 10002, false); BEFORE relname | relfilenode | relpages | bytes -----------+-------------+----------+------- t1_id_seq | 24626 | 1 | 8192 AFTER relname | relfilenode | relpages | bytes -----------+-------------+----------+------- t1_id_seq | 189575 | 1 | 8192
  • 19. Recovering From A Damaged PostgreSQL Cluster Example Invocation Scenario: Rebuild Data Cluster with Corrupted Index Solution: Rebuild index reindex index t1_pkey; BEFORE relname | relfilenode | relpages | bytes -----------+-------------+----------+------- t1_pkey | 24632 | 5 | 40960 AFTER relname | relfilenode | relpages | bytes -----------+-------------+----------+------- t1_pkey | 189578 | 5 | 40960
  • 20. Recovering From A Damaged PostgreSQL Cluster Example Invocation Scenario: Rebuild Data Cluster with Corrupted System Indexes Solution: Rebuild indexes under single-user mode - start up the damaged cluster and reindex with the following invocation postgres --single postgres -D ~/data <<_eof_ reindex system mydatabase _eof_
  • 21. Recovering From A Damaged PostgreSQL Cluster About Single UserMode - Used during bootstrapping by initdb - Can be used for debugging - Can be used for disaster recovery i.e. System indexes can be manipulated - Can enter queries and the results are printed to the screen
  • 22. Recovering From A Damaged PostgreSQL Cluster About Single UserMode postgres --single postgres -D ~/data temp PostgreSQL stand-alone backend 9.2.3 backend> select * from t1 limit 5 1: id (typeid = 23, len = 4, typmod = -1, byval = t) 2: x (typeid = 700, len = 4, typmod = -1, byval = t) ---- 1: id = "1" (typeid = 23, len = 4, typmod = -1, byval = t) 2: x = "0.767757" (typeid = 700, len = 4, typmod = -1, byval = t) ---- 1: id = "2" (typeid = 23, len = 4, typmod = -1, byval = t) 2: x = "0.253584" (typeid = 700, len = 4, typmod = -1, byval = t) ---- 1: id = "3" (typeid = 23, len = 4, typmod = -1, byval = t) 2: x = "0.0912474" (typeid = 700, len = 4, typmod = -1, byval = t) ---- 1: id = "4" (typeid = 23, len = 4, typmod = -1, byval = t) 2: x = "0.45276" (typeid = 700, len = 4, typmod = -1, byval = t) ---- 1: id = "5" (typeid = 23, len = 4, typmod = -1, byval = t) 2: x = "0.784381" (typeid = 700, len = 4, typmod = -1, byval = t) ---- backend>
  • 23. Recovering From A Damaged PostgreSQL Cluster Example Invocation Scenario: PostgreSQL Starts Up But Queries To User-Defined Tables Fails Solution: Generate a data-dump into a newly created data cluster - start up the damaged cluster with the following invocation pg_ctl -D ~/data -o '-c zero_damaged_pages=true' -l logfile.txt start - execute standard data dump with pg_dumpall
  • 24. Recovering From A Damaged PostgreSQL Cluster Example Invocation Scenario: Rebuild And Reuse An Existing Data Cluster With Corrupted User-Defined Tables Solution: Rebuild Cluster - start up the damaged cluster with the following invocation pg_ctl -D ~/data -o '-c zero_damaged_pages=true' -l logfile.txt start - login the database and execute the following SQL statement VACUUM FULL ANALYZE; REINDEX DATABASE mydatabase; - restart the newly repaired cluster pg_ctl -D ~/data -l logfile.txt -m smart restart
  • 25. Recovering From A Damaged PostgreSQL Cluster Caveats ● DROP and CREATE damaged indexes ● Where possible, recreate the cluster on another machine... don't take a chance ● Executing a dump should be the first action i.e. don't VACUUM damaged tables as you can actually lose data before you reuse/backup/dump ● Damaged system catalogs must be recovered in single user mode ● Tables larger 1GB+ are split ● TOASTED tables add an entirely new level of complexity
  • 26. Recovering From A Damaged PostgreSQL Cluster One last word Hacking the cluster...
  • 27. Recovering From A Damaged PostgreSQL Cluster References - http://www.postgresql.org/docs/9.2/static/index.html - http://www.postgresql.org/docs/9.2/static/app-initdb.html - http://www.postgresql.org/docs/9.2/static/app-pg-ctl.html - http://www.postgresql.org/docs/9.2/static/catalog-pg-class.html - http://www.postgresql.org/docs/9.2/static/runtime-config-developer.html - http://www.postgresql.org/docs/9.2/static/sql-createindex.html - http://www.postgresql.org/docs/9.2/static/sql-dropindex.html - http://www.postgresql.org/docs/9.2/static/sql-reindex.html - http://www.postgresql.org/docs/9.2/static/sql-vacuum.html - http://linux.die.net/man/1/find - http://linux.die.net/man/1/killall
  • 28. Recovering From A Damaged PostgreSQL Cluster Asurion Mobile Applications http://www.asurion.com/ Robert Bernier Sr. Database Administrator, Asurion Mobile Applications robert.bernier@asurion.com 1400 Fashion Island Blvd, San Mateo, CA 94404