LoginSignup
2
2

More than 3 years have passed since last update.

pgAdmin4でPL/pgsqlをデバッグする

Posted at

pgAdmin4でPL/pgsqlをデバッグする

pgAdmin4を利用して、PL/pgsqlをデバッグしてみます。
同様の記事は他にもあるのですが、古いバージョン(pgAdmin3)を対象としているものが多いので、新しいバージョンの環境で試してみました(手順はほぼ同じでしたが・・・)
また、pgAdmin3ではエラーとなり使用できないという記事がいくつか見られたのですが、pgAdmin4では(少ししか使っていませんが)問題ないようです。

今回使用する環境は以下のとおりです。

  • PostgreSQL 12(CentOS 7.5)
  • pgAdmin4 4.19(Windows版)

pldebuggerのインストール

$ git clone git://git.postgresql.org/git/pldebugger.git
$ cd pldebugger
$ USE_PGXS=1 make
$ sudo USE_PGXS=1 make install

使用するためにはpostgresql.confを以下のように変更します。変更後、PostgreSQLを再起動します。

shared_preload_libraries = 'plugin_debugger'

モジュールを使用するためには、以下を実行します。

# create extension pldbgapi;
CREATE EXTENSION

実行方法

pgAdmin4で対象のFunctionを選択して、コンテキストメニューから[Debugging] - [Set breakpoint]を選択します。

image.png

以下のようにテスト用のfunctionを実行します。

postgres=# select test_func();

pgAdmin4では以下のようにbreakpointを入れながらデバッグができます。

image.png

functionの実行が終わると以下のように出力されました。

postgres=# select test_func();
INFO:  Current timestamp 1: 2020-04-03 00:32:42.186011
INFO:  Current timestamp 2: 2020-04-03 00:32:51.495337
INFO:  Current timestamp 3: 2020-04-03 00:32:53.966918
 test_func 
-----------

(1 row)

今回使用したfunctionは以下のとおり。

CREATE OR REPLACE FUNCTION public.test_func()
RETURNS void AS $$
DECLARE
BEGIN
  RAISE INFO 'Current timestamp 1: %', timeofday()::TIMESTAMP;
  RAISE INFO 'Current timestamp 2: %', timeofday()::TIMESTAMP;
  RAISE INFO 'Current timestamp 3: %', timeofday()::TIMESTAMP;
END;
$$ LANGUAGE plpgsql;

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2