SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
Agenda Installation Administration Entwicklung Backup




                          PostgreSQL - Troubleshooting

                                             Bernd Helmle


                                           24. August 2008




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Agenda


        1   Installation


        2   Administration


        3   Entwicklung


        4   Backup




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Installation (1)

            Installationspfade, Tablespaces und Mountpoints bestimmen
                   RAID-Level und LVM
                   Separates WAL-Laufwerk?
                       # initdb -X /mnt/wal

                   temp tablespaces
                   Tablespace f¨r Indexe
                               u
            Zu erwartende Gr¨ße von Tabellen und Indexe
                            o
                       tuple header: 24 Bytes
                       item pointer: 4 Bytes
                       data size   : 8 Bytes (2 x Integer)
                       ----------------------
                                     34 Bytes


                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Installation (2)


            Welche Locales, welches Encoding?
                       # initdb -E UTF-8 --locale=de_DE.UTF-8

            Collation bestimmt Indexsortierung
            #= INSERT INTO foo VALUES(’¨’), (’U’), (’¨’), (’¨’), (’¨’), (’¨’);
                                       a      ¨      A      O      o      u

            #= SELECT * FROM foo ORDER BY value DESC;     #= SELECT * FROM foo ORDER BY value DESC;

             value                                         value
            -------                                       -------
             ¨
             u                                             ¨
                                                           U
             ¨
             o                                             ¨
                                                           u
             ¨
             a                                             ¨
                                                           O
             ¨
             U                                             o
                                                           ¨
             ¨
             O                                             ¨
                                                           A
             ¨
             A                                             a
                                                           ¨
            (6 rows)                                      (6 rows)




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

VACUUM (1)



     Tabellen (Heap) fragmentieren
            Erstellen geeigneter VACUUM-Policy, Anwendungsabh¨ngig
                                                             a
            Manuelles VACUUM, Autovacuum, Cost-based VACUUM
            pg autovacuum f¨r spezifische Tabellen
                           u
            Optimizerstatistiken m¨ssen aktuell sein
                                  u
     VACUUM und ANALYZE unbedingt notwendig!




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

VACUUM (2)
     Manuelles VACUUM
     #= VACUUM ANALYZE VERBOSE;
     ...
     INFO: vacuuming "public.foo"
     INFO: "foo": found 0 removable, 6 nonremovable row versions in 1 pages
     DETAIL: 0 dead row versions cannot be removed yet.
     There were 0 unused item pointers.
     1 pages contain useful free space.
     0 pages are entirely empty.
     CPU 0.00s/0.00u sec elapsed 0.00 sec.
     INFO: vacuuming "pg_toast.pg_toast_16385"
     INFO: index "pg_toast_16385_index" now contains 0 row versions in 1 pages
     DETAIL: 0 index row versions were removed.
     0 index pages have been deleted, 0 are currently reusable.
     CPU 0.00s/0.00u sec elapsed 0.00 sec.
     INFO: "pg_toast_16385": found 0 removable, 0 nonremovable row versions in 0 pages
     DETAIL: 0 dead row versions cannot be removed yet.
     There were 0 unused item pointers.
     0 pages contain useful free space.
     0 pages are entirely empty.
     CPU 0.00s/0.00u sec elapsed 0.00 sec.
     INFO: analyzing "public.foo"
     INFO: "foo": scanned 1 of 1 pages, containing 6 live rows and 0 dead rows; 6 rows in sample, 6 estimated
     ...
     INFO: free space map contains 55 pages in 61 relations
     DETAIL: A total of 976 page slots are in use (including overhead).
     976 page slots are required to track all free space.
     Current limits are: 204800 page slots, 1000 relations, using 1305 kB.
     VACUUM
     Time: 16,793 ms

                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

VACUUM (3)
     Autovacuum automatisiert diese Wartungsaufgabe
            Standardm¨ssig aktiviert
                     a
                           autovacuum = on
                           track_counts = on

            Ab Version 8.3 mehrere “Worker” m¨glich
                                             o
                           autovacuum_max_workers = 3
                           autovacuum_naptime = 60s

            Grenzwerte beachten:
            vacuum threshold  = autovacuum_vacuum_threshold
                                + autovacuum_vacuum_scale_factor * number of tuples
            analyze threshold = autovacuum_analyze_threshold
                                + autovacuum_analyze_scale_factor * number of tuples


            ¨
            Uberwachungsm¨glichkeiten in 8.3 erheblich verbessert
                         o

                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

VACUUM (4)


     Cost-based VACUUM hilft, I/O-Ressourcen einzusparen

        vacuum_cost_delay = 0 # disabled
        vacuum_cost_limit = 200 # 200ms Delay

            Verl¨ngerte VACUUM-Laufzeiten
                a

            vacuum cost limit schwierig zu bestimmen

            vacuum cost delay sollte mit Bedacht gew¨hlt werden
                                                    a




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

VACUUM (5)


            Spezielle Konfigurationen f¨r spezifische Tabellen m¨glich
                                      u                       o
            #= d pg_autovacuum
               Table "pg_catalog.pg_autovacuum"
                  Column      | Type    | Modifiers
            ------------------+---------+-----------
             vacrelid         | oid     | not null
             enabled          | boolean | not null
             vac_base_thresh | integer | not null
             vac_scale_factor | real    | not null
             anl_base_thresh | integer | not null
             anl_scale_factor | real    | not null
             vac_cost_delay   | integer | not null
             vac_cost_limit   | integer | not null


            -1 setzt den Systemdefault
            Hinweis f¨r Backups: pg dump ber¨cksichtigt diese
                      u                     u
            Einstellungen nicht!



                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Free Space Map

     Die Free Space Map speichert fragmentierten Bereich einer Tabelle
     oder Index.
            Re-Konfigurierbar nur bei Serverneustart, Shared Memory
                           max_fsm_pages = 204800
                           max_fsm_relations = 1000

            Zu kleine Maps verhindern die Wiederverwendung von
            Speicherplatz innerhalb von Tabellen oder Indexe
            Monitoring durch VACUUM VERBOSE oder
            pg freespacemap (contrib-Modul)
            Vorraussichtlich in 8.4 flexiblere Infrastruktur (Relation Forks)



                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

REINDEX



            #= REINDEX DATABASE db;



            Indexe fragmentieren ebenfalls

            Regelm¨ssiges REINDEX garantiert schlanke Indexe
                  a

            Wartungsfenster erforderlich




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

¨
Uberwachung (1)


            ¨
            Uberwachung von Statistiken uber System-Views
                                        ¨
               Schema   |           Name           | Type    | Owner
            ------------+--------------------------+---------+----------
             pg_catalog | pg_locks                 | Sicht   | postgres
             pg_catalog | pg_stat_activity         | Sicht   | postgres
             pg_catalog | pg_stat_user_indexes     | Sicht   | postgres
             pg_catalog | pg_stat_user_tables      | Sicht   | postgres
             pg_catalog | pg_statio_user_indexes   | Sicht   | postgres
             pg_catalog | pg_statio_user_sequences | Sicht   | postgres
             pg_catalog | pg_statio_user_tables    | Sicht   | postgres

            Statistische Daten wie Anzahl INSERTs oder I/O-Auslastung
            von Tabellen und Indexe in pg stat*- und pg statio*-Views
            pg stat activity und pg locks protokollieren aktuelle
            Tasks auf der Datenbank und assoziierte Sperren




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

¨
Uberwachung (2)


     Aktuelle Abfragen einer Datenbank
     #= x
     Expanded display is on.
     #= SELECT * FROM pg_stat_activity ;
     -[ RECORD 1 ]-+---------------------------------
     datid         | 16384
     datname       | bernd
     procpid       | 16351
     usesysid      | 10
     usename       | bernd
     current_query | SELECT * FROM pg_stat_activity ;
     waiting       | f
     xact_start    | 2008-08-20 00:23:26.905504+02
     query_start   | 2008-08-20 00:23:26.905504+02
     backend_start | 2008-08-19 22:49:02.721159+02
     client_addr   | 127.0.0.1
     client_port   | 54952




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

¨
Uberwachung (3)


     Sind Abfragen als waiting=t markiert, sollte eine Analyse uber
                                                               ¨
     pg locks erfolgen
     #= SELECT * FROM pg_locks WHERE granted = ’f’;
     -[ RECORD 1 ]------+--------------
     locktype           | transactionid
     database           |
     relation           |
     page               |
     tuple              |
     virtualxid         |
     transactionid      | 691
     classid            |
     objid              |
     objsubid           |
     virtualtransaction | 1/926
     pid                | 20908
     mode               | ShareLock
     granted            | f




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

¨
Uberwachung (4)

     Protokollieren problematischer Queries

            log_min_duration_statement = 2min
            log_min_error_statement = ERROR


     Eckdaten der Datenbank erfassen

          log_checkpoints = true
          log_lock_waits = true
          log_temp_files = 32768
          log_autovacuum_min_duration = 10min




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

¨
Uberwachung (5)

     EXPLAIN ANALYZE SELECT * FROM tenk1 t1, tenk2 t2
         WHERE t1.unique1 < 50 AND t1.unique2 = t2.unique2;

                            QUERY PLAN
     -------------------------------------------------------------
      Nested Loop (cost=0.00..327.02 rows=49 width=296)
                   (actual time=1.181..29.822 rows=50 loops=1)
        -> Index Scan using tenk1_unique1 on tenk1 t1
                       (cost=0.00..179.33 rows=49 width=148)
                       (actual time=0.630..8.917 rows=50 loops=1)
              Index Cond: (unique1 < 50)
        -> Index Scan using tenk2_unique2 on tenk2 t2
                       (cost=0.00..3.01 rows=1 width=148)
                       (actual time=0.295..0.324 rows=1 loops=50)
              Index Cond: ("outer".unique2 = t2.unique2)
      Total runtime: 31.604 ms

                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Hausaufgaben



            Identifizieren von problematischen Queries (EXPLAIN,
            Sperren)
            Index-Nutzung
            Anpassen der VACUUM-Policy
            VACUUM FULL vermeiden
            CLUSTER und REINDEX in separate Wartungsfenster
            Gr¨ßen¨berwachung von Tabellen/Indexe, Tablespaces und
              o u
            Festplatten
            Logfile-Auswertungen ggf. notwendig




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Rules vs. Trigger



            Trigger oftmals besser als Rules

            Rules sind “Makros”, urspr¨ngliche Query wird
                                      u
            “umgeschrieben”

            Vorsicht bei Multi-Statements-Rules

            Volatile Funktionen werden u.U doppelt evaluiert




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Server Side Prepared Statements




            Privat f¨r jede Session
                    u

            Kann zu suboptimalen Pl¨nen f¨hren
                                   a     u

            Sessioneigene Prepared Statements k¨nnen uber
                                               o     ¨
            pg prepared statements

            angezeigt werden




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

pl/pgsql



            Leistungsf¨hige prozedurale Sprache f¨r Stored Procedures
                      a                          u

            Parser verbesserungsw¨rdig
                                 u

            Pl¨ne f¨r Abfragen werden innerhalb pl/pgsql gecached
              a    u

            Vor 8.3 keine Plan-Invalidierung:

            relation with OID 1013156 does not exist




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

pg dump

            Drei Formate verf¨gbar: plain (SQL), tar, custom
                             u
            pg dump f¨r Dumps spezifischer Datenbanken
                     u
                   pg_dump -Fc -E LATIN9 -U postgres yourdb
                   pg_dump -Fp -E LATIN9 -U postgres yourdb

            pg dumpall f¨r komplette Dumps (Sicherung kompletter
                         u
            Instanzen, Migration)
                   # globale Objekte
                   pg_dumpall -g -E LATIN9 -U postgres postgres
                   # alles
                   pg_dumpall -E LATIN9 -U postgres

            Problematisch bei sehr großen Datenbanken

                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

WAL-based Backup



            Backups quasi “nebenher”
            Ideal f¨r große Datenbanken mit mehreren hundert GigaByte
                   u
            oder mehr
            Base Backups uber Snapshots (SAN, LVM)
                          ¨
            Wiederherstellung jedoch nur uber Downtime
                                         ¨
            Speicherverbrauch muß streng kontrolliert werden
            Einsatz von Hash Indexe erfordert Spezialbehandlung
            Tablespaces beachten!




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup

Absicherung uber Replikation
            ¨




            DRBD + Heartbeat als Hochverf¨gbarkeitscluster
                                         u
            Slony-I
            WAL-based StandBy
            “Scale-Out” erfordert meist umfangreiche Planung
     “Replikation ist kein Backup”




                                         Bernd Helmle   PostgreSQL - Troubleshooting
Agenda Installation Administration Entwicklung Backup




                                          PQfinish(lecture);




                                         Bernd Helmle   PostgreSQL - Troubleshooting

Contenu connexe

En vedette

ケータイゲームにおけるアイテム課金
ケータイゲームにおけるアイテム課金ケータイゲームにおけるアイテム課金
ケータイゲームにおけるアイテム課金suadd
 
Kebebasan Beragama (Atikel Stulos)
Kebebasan Beragama (Atikel Stulos)Kebebasan Beragama (Atikel Stulos)
Kebebasan Beragama (Atikel Stulos)bhutabarat
 
Updatable Views - What we have and what we want
Updatable Views - What we have and what we wantUpdatable Views - What we have and what we want
Updatable Views - What we have and what we wantpsoo1978
 
Designer As Catalyst (HOW Interactive Design Conf 2014)
Designer As Catalyst (HOW Interactive Design Conf 2014)Designer As Catalyst (HOW Interactive Design Conf 2014)
Designer As Catalyst (HOW Interactive Design Conf 2014)Matthew Muñoz
 
Drugs By 8th Graders
Drugs By 8th GradersDrugs By 8th Graders
Drugs By 8th Gradersson1marc
 
Writing A Foreign Data Wrapper
Writing A Foreign Data WrapperWriting A Foreign Data Wrapper
Writing A Foreign Data Wrapperpsoo1978
 
Logi amiante ais
Logi amiante aisLogi amiante ais
Logi amiante aismarco421
 
Outlook Connector Steps - Salesforce.com
Outlook Connector Steps - Salesforce.comOutlook Connector Steps - Salesforce.com
Outlook Connector Steps - Salesforce.comThe Sailor Group Inc.
 

En vedette (9)

ケータイゲームにおけるアイテム課金
ケータイゲームにおけるアイテム課金ケータイゲームにおけるアイテム課金
ケータイゲームにおけるアイテム課金
 
Kebebasan Beragama (Atikel Stulos)
Kebebasan Beragama (Atikel Stulos)Kebebasan Beragama (Atikel Stulos)
Kebebasan Beragama (Atikel Stulos)
 
Updatable Views - What we have and what we want
Updatable Views - What we have and what we wantUpdatable Views - What we have and what we want
Updatable Views - What we have and what we want
 
Designer As Catalyst (HOW Interactive Design Conf 2014)
Designer As Catalyst (HOW Interactive Design Conf 2014)Designer As Catalyst (HOW Interactive Design Conf 2014)
Designer As Catalyst (HOW Interactive Design Conf 2014)
 
Pollution
PollutionPollution
Pollution
 
Drugs By 8th Graders
Drugs By 8th GradersDrugs By 8th Graders
Drugs By 8th Graders
 
Writing A Foreign Data Wrapper
Writing A Foreign Data WrapperWriting A Foreign Data Wrapper
Writing A Foreign Data Wrapper
 
Logi amiante ais
Logi amiante aisLogi amiante ais
Logi amiante ais
 
Outlook Connector Steps - Salesforce.com
Outlook Connector Steps - Salesforce.comOutlook Connector Steps - Salesforce.com
Outlook Connector Steps - Salesforce.com
 

Similaire à PostgreSQL Troubleshooting

OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...
OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...
OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...NETWAYS
 
Lehmanns Rails Erweitern
Lehmanns Rails ErweiternLehmanns Rails Erweitern
Lehmanns Rails Erweiternjan_mindmatters
 
X$Tabellen und SgaScanner, DOAG 2009
X$Tabellen und SgaScanner, DOAG 2009X$Tabellen und SgaScanner, DOAG 2009
X$Tabellen und SgaScanner, DOAG 2009Frank
 
Ausgewählte PL/SQL Packages (2)
Ausgewählte PL/SQL Packages (2)Ausgewählte PL/SQL Packages (2)
Ausgewählte PL/SQL Packages (2)Ulrike Schwinn
 
Einführung in Puppet und Vagrant
Einführung in Puppet und VagrantEinführung in Puppet und Vagrant
Einführung in Puppet und Vagrants0enke
 
Atmel AVR RISC μC unter GNU/Linux programmieren
Atmel AVR RISC μC unter GNU/Linux programmierenAtmel AVR RISC μC unter GNU/Linux programmieren
Atmel AVR RISC μC unter GNU/Linux programmierenrzbrk
 
Production-ready Infrastruktur in 3 Wochen
Production-ready Infrastruktur in 3 WochenProduction-ready Infrastruktur in 3 Wochen
Production-ready Infrastruktur in 3 WochenAndré Goliath
 
Some Bashing II - Mit der Kommandozeile Abläufe automatisieren
Some Bashing II - Mit der Kommandozeile Abläufe automatisierenSome Bashing II - Mit der Kommandozeile Abläufe automatisieren
Some Bashing II - Mit der Kommandozeile Abläufe automatisierenB1 Systems GmbH
 
Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...
Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...
Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...NETWAYS
 
Einführung in Elasticsearch - August 2014
Einführung in Elasticsearch - August 2014Einführung in Elasticsearch - August 2014
Einführung in Elasticsearch - August 2014inovex GmbH
 
Ausgewählte PL/SQL Packages (3)
Ausgewählte PL/SQL Packages (3)Ausgewählte PL/SQL Packages (3)
Ausgewählte PL/SQL Packages (3)Ulrike Schwinn
 

Similaire à PostgreSQL Troubleshooting (15)

OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...
OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...
OSMC 2008 | Programmierung von Nagios-Plugins für NetApp Speichergeräte by In...
 
Lehmanns Rails Erweitern
Lehmanns Rails ErweiternLehmanns Rails Erweitern
Lehmanns Rails Erweitern
 
X$Tabellen und SgaScanner, DOAG 2009
X$Tabellen und SgaScanner, DOAG 2009X$Tabellen und SgaScanner, DOAG 2009
X$Tabellen und SgaScanner, DOAG 2009
 
Ausgewählte PL/SQL Packages (2)
Ausgewählte PL/SQL Packages (2)Ausgewählte PL/SQL Packages (2)
Ausgewählte PL/SQL Packages (2)
 
Einführung in Puppet und Vagrant
Einführung in Puppet und VagrantEinführung in Puppet und Vagrant
Einführung in Puppet und Vagrant
 
Atmel AVR RISC μC unter GNU/Linux programmieren
Atmel AVR RISC μC unter GNU/Linux programmierenAtmel AVR RISC μC unter GNU/Linux programmieren
Atmel AVR RISC μC unter GNU/Linux programmieren
 
Production-ready Infrastruktur in 3 Wochen
Production-ready Infrastruktur in 3 WochenProduction-ready Infrastruktur in 3 Wochen
Production-ready Infrastruktur in 3 Wochen
 
FLOW3-Workshop F3X12
FLOW3-Workshop F3X12FLOW3-Workshop F3X12
FLOW3-Workshop F3X12
 
NoSQL with MySQL
NoSQL with MySQLNoSQL with MySQL
NoSQL with MySQL
 
Some Bashing II - Mit der Kommandozeile Abläufe automatisieren
Some Bashing II - Mit der Kommandozeile Abläufe automatisierenSome Bashing II - Mit der Kommandozeile Abläufe automatisieren
Some Bashing II - Mit der Kommandozeile Abläufe automatisieren
 
Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...
Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...
Nagios Conference 2007 | Monitoring von Logfiles mit check_logfiles by Gerhar...
 
Einführung in Elasticsearch - August 2014
Einführung in Elasticsearch - August 2014Einführung in Elasticsearch - August 2014
Einführung in Elasticsearch - August 2014
 
check_sap_health
check_sap_healthcheck_sap_health
check_sap_health
 
Testing tools
Testing toolsTesting tools
Testing tools
 
Ausgewählte PL/SQL Packages (3)
Ausgewählte PL/SQL Packages (3)Ausgewählte PL/SQL Packages (3)
Ausgewählte PL/SQL Packages (3)
 

PostgreSQL Troubleshooting

  • 1. Agenda Installation Administration Entwicklung Backup PostgreSQL - Troubleshooting Bernd Helmle 24. August 2008 Bernd Helmle PostgreSQL - Troubleshooting
  • 2. Agenda Installation Administration Entwicklung Backup Agenda 1 Installation 2 Administration 3 Entwicklung 4 Backup Bernd Helmle PostgreSQL - Troubleshooting
  • 3. Agenda Installation Administration Entwicklung Backup Installation (1) Installationspfade, Tablespaces und Mountpoints bestimmen RAID-Level und LVM Separates WAL-Laufwerk? # initdb -X /mnt/wal temp tablespaces Tablespace f¨r Indexe u Zu erwartende Gr¨ße von Tabellen und Indexe o tuple header: 24 Bytes item pointer: 4 Bytes data size : 8 Bytes (2 x Integer) ---------------------- 34 Bytes Bernd Helmle PostgreSQL - Troubleshooting
  • 4. Agenda Installation Administration Entwicklung Backup Installation (2) Welche Locales, welches Encoding? # initdb -E UTF-8 --locale=de_DE.UTF-8 Collation bestimmt Indexsortierung #= INSERT INTO foo VALUES(’¨’), (’U’), (’¨’), (’¨’), (’¨’), (’¨’); a ¨ A O o u #= SELECT * FROM foo ORDER BY value DESC; #= SELECT * FROM foo ORDER BY value DESC; value value ------- ------- ¨ u ¨ U ¨ o ¨ u ¨ a ¨ O ¨ U o ¨ ¨ O ¨ A ¨ A a ¨ (6 rows) (6 rows) Bernd Helmle PostgreSQL - Troubleshooting
  • 5. Agenda Installation Administration Entwicklung Backup VACUUM (1) Tabellen (Heap) fragmentieren Erstellen geeigneter VACUUM-Policy, Anwendungsabh¨ngig a Manuelles VACUUM, Autovacuum, Cost-based VACUUM pg autovacuum f¨r spezifische Tabellen u Optimizerstatistiken m¨ssen aktuell sein u VACUUM und ANALYZE unbedingt notwendig! Bernd Helmle PostgreSQL - Troubleshooting
  • 6. Agenda Installation Administration Entwicklung Backup VACUUM (2) Manuelles VACUUM #= VACUUM ANALYZE VERBOSE; ... INFO: vacuuming "public.foo" INFO: "foo": found 0 removable, 6 nonremovable row versions in 1 pages DETAIL: 0 dead row versions cannot be removed yet. There were 0 unused item pointers. 1 pages contain useful free space. 0 pages are entirely empty. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: vacuuming "pg_toast.pg_toast_16385" INFO: index "pg_toast_16385_index" now contains 0 row versions in 1 pages DETAIL: 0 index row versions were removed. 0 index pages have been deleted, 0 are currently reusable. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: "pg_toast_16385": found 0 removable, 0 nonremovable row versions in 0 pages DETAIL: 0 dead row versions cannot be removed yet. There were 0 unused item pointers. 0 pages contain useful free space. 0 pages are entirely empty. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: analyzing "public.foo" INFO: "foo": scanned 1 of 1 pages, containing 6 live rows and 0 dead rows; 6 rows in sample, 6 estimated ... INFO: free space map contains 55 pages in 61 relations DETAIL: A total of 976 page slots are in use (including overhead). 976 page slots are required to track all free space. Current limits are: 204800 page slots, 1000 relations, using 1305 kB. VACUUM Time: 16,793 ms Bernd Helmle PostgreSQL - Troubleshooting
  • 7. Agenda Installation Administration Entwicklung Backup VACUUM (3) Autovacuum automatisiert diese Wartungsaufgabe Standardm¨ssig aktiviert a autovacuum = on track_counts = on Ab Version 8.3 mehrere “Worker” m¨glich o autovacuum_max_workers = 3 autovacuum_naptime = 60s Grenzwerte beachten: vacuum threshold = autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor * number of tuples analyze threshold = autovacuum_analyze_threshold + autovacuum_analyze_scale_factor * number of tuples ¨ Uberwachungsm¨glichkeiten in 8.3 erheblich verbessert o Bernd Helmle PostgreSQL - Troubleshooting
  • 8. Agenda Installation Administration Entwicklung Backup VACUUM (4) Cost-based VACUUM hilft, I/O-Ressourcen einzusparen vacuum_cost_delay = 0 # disabled vacuum_cost_limit = 200 # 200ms Delay Verl¨ngerte VACUUM-Laufzeiten a vacuum cost limit schwierig zu bestimmen vacuum cost delay sollte mit Bedacht gew¨hlt werden a Bernd Helmle PostgreSQL - Troubleshooting
  • 9. Agenda Installation Administration Entwicklung Backup VACUUM (5) Spezielle Konfigurationen f¨r spezifische Tabellen m¨glich u o #= d pg_autovacuum Table "pg_catalog.pg_autovacuum" Column | Type | Modifiers ------------------+---------+----------- vacrelid | oid | not null enabled | boolean | not null vac_base_thresh | integer | not null vac_scale_factor | real | not null anl_base_thresh | integer | not null anl_scale_factor | real | not null vac_cost_delay | integer | not null vac_cost_limit | integer | not null -1 setzt den Systemdefault Hinweis f¨r Backups: pg dump ber¨cksichtigt diese u u Einstellungen nicht! Bernd Helmle PostgreSQL - Troubleshooting
  • 10. Agenda Installation Administration Entwicklung Backup Free Space Map Die Free Space Map speichert fragmentierten Bereich einer Tabelle oder Index. Re-Konfigurierbar nur bei Serverneustart, Shared Memory max_fsm_pages = 204800 max_fsm_relations = 1000 Zu kleine Maps verhindern die Wiederverwendung von Speicherplatz innerhalb von Tabellen oder Indexe Monitoring durch VACUUM VERBOSE oder pg freespacemap (contrib-Modul) Vorraussichtlich in 8.4 flexiblere Infrastruktur (Relation Forks) Bernd Helmle PostgreSQL - Troubleshooting
  • 11. Agenda Installation Administration Entwicklung Backup REINDEX #= REINDEX DATABASE db; Indexe fragmentieren ebenfalls Regelm¨ssiges REINDEX garantiert schlanke Indexe a Wartungsfenster erforderlich Bernd Helmle PostgreSQL - Troubleshooting
  • 12. Agenda Installation Administration Entwicklung Backup ¨ Uberwachung (1) ¨ Uberwachung von Statistiken uber System-Views ¨ Schema | Name | Type | Owner ------------+--------------------------+---------+---------- pg_catalog | pg_locks | Sicht | postgres pg_catalog | pg_stat_activity | Sicht | postgres pg_catalog | pg_stat_user_indexes | Sicht | postgres pg_catalog | pg_stat_user_tables | Sicht | postgres pg_catalog | pg_statio_user_indexes | Sicht | postgres pg_catalog | pg_statio_user_sequences | Sicht | postgres pg_catalog | pg_statio_user_tables | Sicht | postgres Statistische Daten wie Anzahl INSERTs oder I/O-Auslastung von Tabellen und Indexe in pg stat*- und pg statio*-Views pg stat activity und pg locks protokollieren aktuelle Tasks auf der Datenbank und assoziierte Sperren Bernd Helmle PostgreSQL - Troubleshooting
  • 13. Agenda Installation Administration Entwicklung Backup ¨ Uberwachung (2) Aktuelle Abfragen einer Datenbank #= x Expanded display is on. #= SELECT * FROM pg_stat_activity ; -[ RECORD 1 ]-+--------------------------------- datid | 16384 datname | bernd procpid | 16351 usesysid | 10 usename | bernd current_query | SELECT * FROM pg_stat_activity ; waiting | f xact_start | 2008-08-20 00:23:26.905504+02 query_start | 2008-08-20 00:23:26.905504+02 backend_start | 2008-08-19 22:49:02.721159+02 client_addr | 127.0.0.1 client_port | 54952 Bernd Helmle PostgreSQL - Troubleshooting
  • 14. Agenda Installation Administration Entwicklung Backup ¨ Uberwachung (3) Sind Abfragen als waiting=t markiert, sollte eine Analyse uber ¨ pg locks erfolgen #= SELECT * FROM pg_locks WHERE granted = ’f’; -[ RECORD 1 ]------+-------------- locktype | transactionid database | relation | page | tuple | virtualxid | transactionid | 691 classid | objid | objsubid | virtualtransaction | 1/926 pid | 20908 mode | ShareLock granted | f Bernd Helmle PostgreSQL - Troubleshooting
  • 15. Agenda Installation Administration Entwicklung Backup ¨ Uberwachung (4) Protokollieren problematischer Queries log_min_duration_statement = 2min log_min_error_statement = ERROR Eckdaten der Datenbank erfassen log_checkpoints = true log_lock_waits = true log_temp_files = 32768 log_autovacuum_min_duration = 10min Bernd Helmle PostgreSQL - Troubleshooting
  • 16. Agenda Installation Administration Entwicklung Backup ¨ Uberwachung (5) EXPLAIN ANALYZE SELECT * FROM tenk1 t1, tenk2 t2 WHERE t1.unique1 < 50 AND t1.unique2 = t2.unique2; QUERY PLAN ------------------------------------------------------------- Nested Loop (cost=0.00..327.02 rows=49 width=296) (actual time=1.181..29.822 rows=50 loops=1) -> Index Scan using tenk1_unique1 on tenk1 t1 (cost=0.00..179.33 rows=49 width=148) (actual time=0.630..8.917 rows=50 loops=1) Index Cond: (unique1 < 50) -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.00..3.01 rows=1 width=148) (actual time=0.295..0.324 rows=1 loops=50) Index Cond: ("outer".unique2 = t2.unique2) Total runtime: 31.604 ms Bernd Helmle PostgreSQL - Troubleshooting
  • 17. Agenda Installation Administration Entwicklung Backup Hausaufgaben Identifizieren von problematischen Queries (EXPLAIN, Sperren) Index-Nutzung Anpassen der VACUUM-Policy VACUUM FULL vermeiden CLUSTER und REINDEX in separate Wartungsfenster Gr¨ßen¨berwachung von Tabellen/Indexe, Tablespaces und o u Festplatten Logfile-Auswertungen ggf. notwendig Bernd Helmle PostgreSQL - Troubleshooting
  • 18. Agenda Installation Administration Entwicklung Backup Rules vs. Trigger Trigger oftmals besser als Rules Rules sind “Makros”, urspr¨ngliche Query wird u “umgeschrieben” Vorsicht bei Multi-Statements-Rules Volatile Funktionen werden u.U doppelt evaluiert Bernd Helmle PostgreSQL - Troubleshooting
  • 19. Agenda Installation Administration Entwicklung Backup Server Side Prepared Statements Privat f¨r jede Session u Kann zu suboptimalen Pl¨nen f¨hren a u Sessioneigene Prepared Statements k¨nnen uber o ¨ pg prepared statements angezeigt werden Bernd Helmle PostgreSQL - Troubleshooting
  • 20. Agenda Installation Administration Entwicklung Backup pl/pgsql Leistungsf¨hige prozedurale Sprache f¨r Stored Procedures a u Parser verbesserungsw¨rdig u Pl¨ne f¨r Abfragen werden innerhalb pl/pgsql gecached a u Vor 8.3 keine Plan-Invalidierung: relation with OID 1013156 does not exist Bernd Helmle PostgreSQL - Troubleshooting
  • 21. Agenda Installation Administration Entwicklung Backup pg dump Drei Formate verf¨gbar: plain (SQL), tar, custom u pg dump f¨r Dumps spezifischer Datenbanken u pg_dump -Fc -E LATIN9 -U postgres yourdb pg_dump -Fp -E LATIN9 -U postgres yourdb pg dumpall f¨r komplette Dumps (Sicherung kompletter u Instanzen, Migration) # globale Objekte pg_dumpall -g -E LATIN9 -U postgres postgres # alles pg_dumpall -E LATIN9 -U postgres Problematisch bei sehr großen Datenbanken Bernd Helmle PostgreSQL - Troubleshooting
  • 22. Agenda Installation Administration Entwicklung Backup WAL-based Backup Backups quasi “nebenher” Ideal f¨r große Datenbanken mit mehreren hundert GigaByte u oder mehr Base Backups uber Snapshots (SAN, LVM) ¨ Wiederherstellung jedoch nur uber Downtime ¨ Speicherverbrauch muß streng kontrolliert werden Einsatz von Hash Indexe erfordert Spezialbehandlung Tablespaces beachten! Bernd Helmle PostgreSQL - Troubleshooting
  • 23. Agenda Installation Administration Entwicklung Backup Absicherung uber Replikation ¨ DRBD + Heartbeat als Hochverf¨gbarkeitscluster u Slony-I WAL-based StandBy “Scale-Out” erfordert meist umfangreiche Planung “Replikation ist kein Backup” Bernd Helmle PostgreSQL - Troubleshooting
  • 24. Agenda Installation Administration Entwicklung Backup PQfinish(lecture); Bernd Helmle PostgreSQL - Troubleshooting