Objlab
← News
Database

Andrew Atkinson: Test beta di PostgreSQL con Docker

Sintesi redazionale: La comunità Postgres apprezza molto il feedback derivante dai test sulle versioni beta e, grazie a Docker, è diventato più semplice mettere in funzione le versioni pre-release. Con il recente annuncio di PostgreSQL 19 Beta 1, proviamo a installarla e a testare alcune delle nuove funzionalità.. Fonte originale: https://postgr.es/p/9lr

<p>- Pre-Release Versions of Postgres with Docker<br>- Building and Running<br>- Released Versions<br>- Connect via psql<br>- New Feature Testing in 19<br>- Additions to pg_stat_statements<br>- Repack Concurrently for Tables<br>- Wrapping Up</p><p>The Postgres community releases Beta versions, and with Docker it’s been easier than ever to configure pre-release versions to use and test.</p><p>With the recent announcement of PostgreSQL 19 Beta 1, it’s a great time to do that. Let’s get an instance up and running and test some of the new capabilities in Postgres 19.</p><p>## Pre-Release Versions of Postgres with Docker</p><p>First, you’ll need to install Docker for your OS! Grab the version needed for your OS and processor architecture, for example ARM or AMD/Intel/x86.</p><p>On MacOS run `uname -m`</p><p>or `sw_vers`</p><p>in your Terminal to learn more about your hardware details.</p><p>For Windows check Install Docker Desktop on Windows</p><p>## Building and Running</p><p>Official Postgres images for Docker Postgres are limited to fully released versions.</p><p>Fortunately @yosifkit created a PR to add 19 Beta 1 (merged by @tianon) with instructions for how to use `docker buildx`</p><p>to build pre-release versions.</p><p>This command downloads and builds `postgres:19beta1-trixie`</p><p>:</p><p>```<br>docker buildx build -t postgres:19beta1-trixie \<br>&#x27;https://github.com/infosiftr/postgres.git#19-rc:19/trixie&#x27;<br>```</p><p>With that built, I could invoke `docker run`</p><p>with `postgres:19beta1-trixie`</p><p>. I named mine `pg19`</p><p>.</p><p>I also passed the env vars below based on how I run other Docker Postgres containers (these options may not be necessary).</p><p>The final command:</p><p>```<br>docker run \<br>--name pg19 \<br>--env POSTGRES_USER=postgres \<br>--env POSTGRES_PASSWORD=postgres \<br>--detach postgres:19beta1-trixie<br>```</p><p>To check if it’s running, I run `docker ps -a`</p><p>. For logs I’d run: `docker logs -f postgres:19beta1-trixie`</p><p>.</p><p>## Released Versions</p><p>Shortly after writing the initial post on June 5th, the `postgres:beta1`</p><p>image became available for download without any special steps:</p><p>```<br>docker run --detach postgres:19beta1<br>```</p><p>## Connect via psql</p><p>The container is running and the logs have what we want: “database system is ready to accept connections”.</p><p>Let’s connect to the `postgres`</p><p>database using psql on the container:</p><p>```<br>docker container exec -it pg19 psql -U postgres<br>```</p><p>We should see output like showing version 19:</p><p>```<br>psql (19beta1 (Debian 19~beta1-1.pgdg13+1))<br>Type &quot;help&quot; for help.<br>```</p><p>## New Feature Testing in 19</p><p>Great. Let’s try out some things in 19.</p><p>19 Added a new system view for checking out locks. Let’s try it out:</p><p>```<br>postgres=# select * from pg_stat_lock;<br>```</p><p>We get a lot of new data like `waits`</p><p>counts, `wait_time`</p><p>and more.</p><p>What about the new `pg_plan_advice`</p><p>extension? First let’s load it and then create a table `t`</p><p>to experiment with:</p><p>```<br>postgres=# LOAD &#x27;pg_plan_advice&#x27;;<br>postgres=# create table t (id int);<br>```</p><p>With that in place we can show the output via `EXPLAIN`</p><p>with a new `PLAN_ADVICE`</p><p>parameter:</p><p>```<br>postgres=# EXPLAIN (PLAN_ADVICE) SELECT * FROM t;<br>QUERY PLAN<br>-----------------------------------------------------<br>Seq Scan on t (cost=0.00..35.50 rows=2550 width=4)<br>Generated Plan Advice:<br>SEQ_SCAN(t)<br>NO_GATHER(t)<br>(4 rows)<br>```</p><p>I wonder why the rows estimate is 2550 by default? Let’s run `analyze t;`</p><p>.</p><p>After doing that, it looks more sensible with a `rows`</p><p>estimate of 1:</p><p>```<br>postgres=# EXPLAIN (PLAN_ADVICE) SELECT * FROM t;<br>QUERY PLAN<br>-------------------------------------------------<br>Seq Scan on t (cost=0.00..0.00 rows=1 width=4)<br>Generated Plan Advice:<br>SEQ_SCAN(t)<br>NO_GATHER(t)<br>(4 rows)<br>```</p><p>## Additions to pg_stat_statements</p><p>The extension `pg_stat_statements`</p><p>gained new capabilities in 19.</p><p>Let’s try it out:</p><p>```<br>postgres=# select * from pg_stat_statements;<br>ERROR: relation &quot;pg_stat_statements&quot; does not exist<br>```</p><p>Oops, we need to add it to `shared_preload_libraries`</p><p>first. We can see that’s currently not the case:</p><p>```<br>postgres=# show shared_preload_libraries;<br>shared_preload_libraries<br>--------------------------<br>(1 row)<br>```</p><p>One way to do that with `docker run`</p><p>is the `-c`</p><p>parameter as follows:</p><p>```<br>docker run \<br>--name pg19 \<br>--env POSTGRES_USER=postgres \<br>--env POSTGRES_PASSWORD=postgres \<br>--detach postgres:19beta1-trixie \<br>-c shared_preload_libraries=pg_stat_statements<br>```</p><p>Now we see what we want in `shared_preload_libraries`</p><p>:</p><p>```<br>postgres=# show shared_preload_libraries;<br>shared_preload_libraries<br>--------------------------<br>pg_stat_statements<br>(1 row)<br>```</p><p>We have not yet enabled the extension though given `\dx`</p><p>doesn’t list it. Let’s do that:</p><p>```<br>psql&gt; create extension if not exists pg_stat_statements;<br>```</p><p>Now `\dx`</p><p>shows it, and we’re ready to query it.</p><p>One of the additions is tracking the use of prepared statements. Let’s create a basic table and prepared statement.</p><p>Create table again if needed:</p><p>```<br>create table if not exists t (id int);<br>```</p><p>Create a simple prepared statement `get_t`</p><p>and execute it. The goal here is for `pg_stat_statements`</p><p>to increment the `generic_plan_calls`</p><p>field.</p><p>```<br>PREPARE get_t AS<br>SELECT *<br>FROM t;<br>```</p><p>Now let’s execute it:</p><p>```<br>EXECUTE get_t;<br>```</p><p>Did it work?</p><p>```<br>postgres=# select left(query,100),generic_plan_calls from pg_stat_statements limit 1;<br>left | generic_plan_calls<br>------------------+--------------------<br>PREPARE get_t AS+| 1<br>SELECT * +|<br>FROM t |<br>```</p><p>It worked! We see `generic_plan_calls`</p><p>was incremented.</p><p>This looks very useful to monitor the use of prepared statements.</p><p>## Repack Concurrently for Tables</p><p>We’ve had the ability to use `reindex concurrently`</p><p>to rebuild indexes since Postgres 12, but have lacked the ability to rebuild tables.</p><p>That changes in 19, with the introduction of `repack concurrently`</p><p>for tables.</p><p>Let’s try it out quick and bloat a table by updating every row. We’ll compare the size before and after repacking it.</p><p>Run these examples using psql:</p><p>```<br>create table bloat (id int primary key);<br>CREATE TABLE<br>insert into bloat (id) select i from generate_series(1, 1_000_000) as t(i);<br>INSERT 0 1000000<br>SELECT pg_size_pretty(pg_total_relation_size(&#x27;bloat&#x27;));<br>pg_size_pretty<br>----------------<br>56 MB<br>analyze bloat;<br>ANALYZE<br>-- Update every row<br>update bloat set id = id;<br>UPDATE 1000000<br>analyze bloat;<br>ANALYZE<br>-- The table size has doubled<br>SELECT pg_size_pretty(pg_total_relation_size(&#x27;bloat&#x27;));<br>pg_size_pretty<br>----------------<br>112 MB<br>repack (concurrently, verbose) bloat;<br>INFO: repacking &quot;public.bloat&quot; in physical order<br>INFO: &quot;public.bloat&quot;: found 0 removable, 1000000 nonremovable row versions in 8850 pages<br>DETAIL: 0 dead row versions cannot be removed yet.<br>CPU: user: 0.21 s, system: 0.04 s, elapsed: 0.28 s.<br>REPACK<br>-- Returns to original table size<br>SELECT pg_size_pretty(pg_total_relation_size(&#x27;bloat&#x27;));<br>pg_size_pretty<br>----------------<br>56 MB<br>```</p><p>Very useful! Check out the repack documentation for more info.</p><p>## Wrapping Up</p><p>Please give this a shot and experiment with new features in Postgres 19!</p>