Profilage
PL/pgSQL
Plprofiler
Questions ?
Pl Profiler
Sébastien Lardière
Loxodata
mars 2017
slardiere PLPROFILER – mars 2017 1 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Profilage dans PostgreSQL
slardiere PLPROFILER – mars 2017 2 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Profilage dans PostgreSQL
Profilage
Analyse Dynamique d’un programme
Mesure :
fréquence et durée d’un appel
l’usage d’une instruction
la consommation temps et mémoire
slardiere PLPROFILER – mars 2017 3 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Profilage dans PostgreSQL
Profilage dans PostgreSQL
Nombreuses statistiques dans le catalogue (pg_stat_user_tables, ...)
Requêtes lentes dans les logs : log_min_duration_statement
pg_stat_statement
auto_explain.log_nested_statements = on
slardiere PLPROFILER – mars 2017 4 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Profilage dans PostgreSQL
slardiere PLPROFILER – mars 2017 5 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
PL/pgSQL
Langage inspiré de PLSQL, crée par Jan Wieck (fin ’90)
Langage « LOADable »
À l’execution, le moteur est chargé
Le contenu de pg_proc est utilisé
slardiere PLPROFILER – mars 2017 6 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
PL/pgSQL
Au premier appel dans une session :
L’interpréteur « compile » la fonction
Le SQL embarqué n’est qu’une chaine
Résultat : un arbre similaire à celui d’une requête SQL
Les requêtes sont préparées via SPI
Les plans préparées sont utilisées pendant toutes la session
slardiere PLPROFILER – mars 2017 7 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Profilage de PL/pgSQL
Problème : le contenu des fonctions est invisible
L’extension Plprofiler permet d’inspecter l’exécution du code PL/pgSQL
Des hooks de debug ont été ajouté en 2006
slardiere PLPROFILER – mars 2017 8 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Installation
Simple sous GNU/Linux, dépend de Perl et Python
Un fichier plprofiler.so
Une extension plprofiler3.0.sql
Un client (python) plprofiler
slardiere PLPROFILER – mars 2017 9 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Installation
À partir de https://bitbucket.org/openscg/plprofiler/overview
Exemple
cd openscg-plprofiler
sudo make USE_PGXS=1 install
cd python-plprofiler/
sudo python setup.py install
slardiere PLPROFILER – mars 2017 10 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Installation
L’extension doit être créée :
Exemple
create extension plprofiler
slardiere PLPROFILER – mars 2017 11 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
RUN
La commande RUN du client permet de lancer des requêtes :
Exemple
plprofiler run -c select looploop(2,2) 
--name=LoopLoop --title=LoopLoop 
--desc=Loop Loop function call 
--output=looploop.html
slardiere PLPROFILER – mars 2017 12 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
slardiere PLPROFILER – mars 2017 13 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Benchmarks
Pour tester une application globalement, charger le module postgresql.conf :
Exemple
shared_preload_libraries = 'plprofiler'
slardiere PLPROFILER – mars 2017 14 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Benchmarks
Puis, piloter la collecte avec la commande monitor :
Exemple
plprofiler reset
plprofiler monitor --interval 10 --duration 300
plprofiler report --from-shared --output parttest01.html
slardiere PLPROFILER – mars 2017 15 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Benchmarks
Possibilité d’enregistrer des sessions :
Exemple
plprofiler save --name=parttest01
plprofiler report --name=parttest01 --output parttest01.html
slardiere PLPROFILER – mars 2017 16 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
slardiere PLPROFILER – mars 2017 17 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
slardiere PLPROFILER – mars 2017 18 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Benchmarks
Pilotage depuis l’outil de benchmarks, ici, pgbench :
Exemple
SET plprofiler.enabled TO true;
SET plprofiler.collect_interval TO 10;
insert into test.test1jour ( ev_date, label )
values ( now() + interval '1d' *
((random() - 0.5) * 100)::int, 1);
slardiere PLPROFILER – mars 2017 19 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Benchmarks
Pilotage depuis l’outil de benchmarks, ici, pgbench :
Exemple
pgbench -c 10 -j 10 -T 10 -f testpart.sql parttest
slardiere PLPROFILER – mars 2017 20 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
Benchmarks
Pilotage depuis l’outil de benchmarks, ici, pgbench :
Exemple
plprofiler save --name=parttest01
plprofiler report --name=parttest01 --output parttest01.html
slardiere PLPROFILER – mars 2017 21 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Installation
RUN
Benchmarks
pgbench
https://www.loxodata.com/post/plpgsql-profiler/
slardiere PLPROFILER – mars 2017 22 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
slardiere PLPROFILER – mars 2017 23 / 24
Profilage
PL/pgSQL
Plprofiler
Questions ?
Merci !
slardiere PLPROFILER – mars 2017 24 / 24

PostgreSQL plprofiler - Mars 2017 - Loxodata