317 lines
9.4 KiB
HTML
317 lines
9.4 KiB
HTML
<!doctype html>
|
|
<html lang="fr">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
|
|
<title>Formation Evolix : PostgreSQL</title>
|
|
|
|
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
|
|
<meta name="author" content="Hakim El Hattab">
|
|
|
|
<meta name="apple-mobile-web-app-capable" content="yes">
|
|
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
|
|
<link rel="stylesheet" href="reveal.js/css/reveal.css">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/beige.css" id="theme">
|
|
|
|
<!-- Theme used for syntax highlighting of code -->
|
|
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
|
|
|
|
<!-- Printing and PDF exports -->
|
|
<script>
|
|
var link = document.createElement( 'link' );
|
|
link.rel = 'stylesheet';
|
|
link.type = 'text/css';
|
|
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
|
|
document.getElementsByTagName( 'head' )[0].appendChild( link );
|
|
</script>
|
|
|
|
<!--[if lt IE 9]>
|
|
<script src="reveal.js/lib/js/html5shiv.js"></script>
|
|
<![endif]-->
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="reveal">
|
|
|
|
<!-- Any section element inside of this container is displayed as a slide -->
|
|
<div class="slides">
|
|
|
|
<section>
|
|
<h1>Formation Evolix</h1>
|
|
<h3>PostgreSQL</h3>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>PostgreSQL</h3>
|
|
<ul>
|
|
<li>le respect du standard SQL</li>
|
|
<li>la sécurité des données</li>
|
|
<li>journaux de transaction (WAL) avant persistence</li>
|
|
</ul>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Installation</h3>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight"># vim sources.list</code>
|
|
deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main
|
|
</pre>
|
|
<pre><code data-trim class="hljs nohighlight"># apt install postgresql</code></pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Instances</h3>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight">
|
|
# pg_ctlcluster <version> <cluster> start|stop|restart|reload|status|promote</code>
|
|
<code data-trim class="hljs nohighlight"># pg_lsclusters</code>
|
|
<code data-trim class="hljs nohighlight">
|
|
Ver Cluster Port Status Owner Data directory Log file
|
|
9.4 main 5432 online postgres /var/lib/postgresql/9.4/main /var/log/postgresql/postgresql-9.4-main.log
|
|
</code>
|
|
</pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h4>Attention à la locale</h4>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight">
|
|
# dpkg-reconfigure locales
|
|
$ /usr/bin/pg_createcluster <version> <cluster>
|
|
</code>
|
|
</pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Fichiers</h3>
|
|
<pre>
|
|
<strong>configuration</strong> : /etc/postgresql/<version>/<instance>/
|
|
|
|
<strong>journaux</strong> : /var/log/postgresql/postgresql-<version>-<instance>.log
|
|
|
|
<strong>stockage</strong> : var/lib/postgresql/<version>/<instance>/
|
|
|
|
<strong>binaires et bibliothèques</strong> : /usr/lib/postgresql/<version>/<instance>/
|
|
</pre>
|
|
</section>
|
|
|
|
<section>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight"># sudo -u postgres psql</code>
|
|
</pre>
|
|
<h3>Contenu de pg_hba.conf :</h3>
|
|
<pre><code data-trim class="diff">
|
|
- local all all peer
|
|
+ local all all password
|
|
</code>
|
|
<code data-trim class="hljs nohighlight">
|
|
$ createuser -P <login>
|
|
$ createdb -O <login> <base>
|
|
</code>
|
|
</section>
|
|
|
|
<section>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight">
|
|
$ dropdb <base>
|
|
$ psql -c "ALTER USER <login> WITH PASSWORD 'foo'"
|
|
$ dropuser <login></code>
|
|
<code data-trim class="sql">
|
|
=# SELECT * FROM pg_user;
|
|
=# \du
|
|
</code></pre>
|
|
</section>
|
|
|
|
<section>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight">$ psql -l</code>
|
|
<code data-trim class="sql">
|
|
=# \d
|
|
=# SELECT * FROM pg_database;
|
|
</code>
|
|
|
|
<code data-trim class="sql">
|
|
SELECT pid, datname, usename, client_addr, query_start, waiting, query
|
|
FROM pg_stat_activity
|
|
WHERE state='active'
|
|
ORDER by query_start;</code>
|
|
</pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h4>Outils complémentaires</h4>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight"># aptitude install ptop pgbadger pgadmin3</code>
|
|
<code data-trim class="hljs nohighlight">$ pg_top</code>
|
|
</pre>
|
|
<h4>Faire un VACUUM FULL sur toutes les bases de données</h4>
|
|
<pre><code data-trim class="hljs nohighlight">$ vacuumdb -a -f -v</code>
|
|
</pre>
|
|
<h4>ANALYSE sur toutes les tables d'une base de données</h4>
|
|
<pre><code data-trim class="hljs nohighlight">$ psql mydb</code>
|
|
<code data-trim class="sql">=# ANALYZE;</code>
|
|
</pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Optimisation</h3>
|
|
<ul>
|
|
<li>shared_buffers : quantité de mémoire dédiée à PostgreSQL</li>
|
|
<li>work_mem : quantité de mémoire par process</li>
|
|
<li>max_connections</li>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Sauvegarde</h3>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight">$ pg_dump <base> >dump.sql</code>
|
|
<code data-trim class="hljs nohighlight">$ pg_dumpall > dump.sql</code>
|
|
<code data-trim class="hljs nohighlight">$ psql <base> < dump.sql</code>
|
|
</pre>
|
|
|
|
<h4>Archivage de WAL :</h4>
|
|
<pre>wal_level = 'archive'
|
|
archive_command = 'rsync %p backup.example.net:/backup/…/archives/%f'
|
|
</pre>
|
|
<h4>barman</h4>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Usage</h3>
|
|
<pre><code class="sql">
|
|
CREATE TABLE weather (
|
|
city varchar(80),
|
|
temp_lo int, -- low temperature
|
|
temp_hi int, -- high temperature
|
|
prcp real, -- precipitation
|
|
date date
|
|
);
|
|
|
|
INSERT INTO weather VALUES ('San Francisco', 46,50,0.25, '1994-11-27');
|
|
</code></pre>
|
|
</section>
|
|
|
|
<section>
|
|
<pre>
|
|
<code data-trim class="sql" style="max-height: 600px">
|
|
SELECT * FROM weather;
|
|
|
|
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
|
|
|
|
SELECT * FROM weatherWHERE city = 'San Francisco' AND prcp > 0.0;
|
|
|
|
SELECT DISTINCT city FROM weather ORDER BY city;
|
|
|
|
SELECT * FROM weather, cities WHERE city = name;
|
|
|
|
SELECT weather.city, weather.temp_lo, cities.location
|
|
FROM weather, cities WHERE cities.name = weather.city;
|
|
|
|
SELECT * FROM weather
|
|
INNER JOIN cities ON (weather.city = cities.name);
|
|
|
|
SELECT * FROM weather
|
|
LEFT OUTER JOIN cities ON (weather.city = cities.name);
|
|
|
|
SELECT * FROM weather w, cities c WHERE w.city = c.name;
|
|
</code></pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Pool de connexions :</h3>
|
|
<ul>
|
|
<li>pgpool</li>
|
|
<li>pgbouncer</li>
|
|
</ul>
|
|
</section>
|
|
|
|
<section>
|
|
<h3>Streaming Replication</h3>
|
|
|
|
master / slave
|
|
</section>
|
|
|
|
<section>
|
|
<h4>Slony : réplication des données par trigger</h4>
|
|
<ul>
|
|
<li>Ne réplique pas le schéma : toutes les bases à créer</li>
|
|
<li>Avoir une clé primaire sur chaque table à répliquer *</li>
|
|
<li>Ne pas utiliser "TRUNCATE" (PG < 8.4)</li>
|
|
<pre><code class="sql">SELECT * from pg_stat_replication;</code></pre>
|
|
<small>* : peut être contourné mais fortement conseillé.</small>
|
|
</section>
|
|
|
|
<section>
|
|
<h4>Initialise le cluster Slony</h4>
|
|
<pre>slonik_init_cluster | slonik</pre>
|
|
<h4>Initialise le set de réplication</h4>
|
|
Ensemble de tables à répliquer, y compris les triggers.
|
|
<pre>slonik_create_set set1 | slonik</pre>
|
|
<pre>
|
|
<code data-trim class="hljs nohighlight">
|
|
# zcat /usr/share/doc/slony1-2-bin/examples/slon.conf-sample.gz \
|
|
> /etc/slony1/slon.conf
|
|
</code>
|
|
<code data-trim class="hljs nohighlight"># /etc/init.d/slony1 start</code>
|
|
<code data-trim class="hljs nohighlight">$ slonik_subscribe_set set1 node2 | slonik</code>
|
|
</pre>
|
|
</section>
|
|
|
|
<section>
|
|
<h2>Autres bases de données relationnelles</h4>
|
|
<ul>
|
|
<li>MySQL / MariaDB : base de données très populaires au sein des infrastructures web</li>
|
|
<li>SQLite : faile à installer, idéal pour embarquer</li>
|
|
</ul>
|
|
</section>
|
|
|
|
<section>
|
|
<h2>Autres bases de données dites "NoSQL"</h4>
|
|
<ul>
|
|
<li>Memcached : base de données clés/valeurs volatile et très performante</li>
|
|
<li>Redis : ressemble à Memcache, mais persistent et plus riche en fonctionnalités</li>
|
|
<li>InfluxDB : base de données orientée "métriques"</li>
|
|
<li>Elasticsearch : moteur distribuée d'indexation et de recherche</li>
|
|
<li>Neo4j : base de données orientée "graph"</li>
|
|
<li>Autres : MongoDB, CouchDB, Cassandra, Riak…</li>
|
|
</ul>
|
|
</section>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<script src="reveal.js/lib/js/head.min.js"></script>
|
|
<script src="reveal.js/js/reveal.js"></script>
|
|
|
|
<script>
|
|
|
|
// More info https://github.com/hakimel/reveal.js#configuration
|
|
Reveal.initialize({
|
|
controls: true,
|
|
progress: true,
|
|
history: true,
|
|
center: true,
|
|
|
|
transition: 'slide', // none/fade/slide/convex/concave/zoom
|
|
|
|
// More info https://github.com/hakimel/reveal.js#dependencies
|
|
dependencies: [
|
|
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
|
|
{ src: 'reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
|
|
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
|
|
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
|
|
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true },
|
|
{ src: 'reveal.js/plugin/notes/notes.js', async: true }
|
|
]
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|