0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PostgreSQLで特定のファンクションをダンプする

Last updated at Posted at 2022-02-15

最近、仕事ではドキュメントを書いてばかりです。
しっかりとドキュメント管理できていない状況で、過去に作ったファンクションの仕様書を作るという、古より伝わる悪いことをしています。
その中作業の中で、ファンクションの定義を出力しくなりました。
いや、ソース管理に登録されてるファンクションのソースが信用できないというか、最新かどうか確認もしたかったので・・・

pg_procテーブルだと、ファンクションの中身しか確認できません。
ググると、pg_dumpでダンプしたファイルから取り出せる例がありましたが、ちょっとわかりづらかったのでまとめておこうかと思います。

やることの概要

やることは以下の2点。

  1. pg_dumpでカスタム形式でスキーマのみをダンプする
  2. pg_restoreでダンプしたいファンクションを指定して実行

実践 引数のないファンクションのダンプ

以下のようなファンクションを登録します。

CREATE FUNCTION testproc() RETURNS text
AS $$
BEGIN
	RETURN 'Hello World';

END; $$
LANGUAGE plpgsql;

スキーマをダンプします。

$ pg_dump -Fc -s -f test.fc testdb
$ ls
test.fc

次にファンクションをリストアします。

$ pg_restore -P "testproc()" -f testproc.sql test.fc
$ cat testproc.sql
--
-- PostgreSQL database dump
--

-- Dumped from database version 12.2 (Ubuntu 12.2-4)
-- Dumped by pg_dump version 12.2 (Ubuntu 12.2-4)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: testproc(); Type: FUNCTION; Schema: public; Owner: krohigewagma
--

CREATE FUNCTION public.testproc() RETURNS text
    LANGUAGE plpgsql
    AS $$
BEGIN
 RETURN 'Hello World';

END; $$;


ALTER FUNCTION public.testproc() OWNER TO krohigewagma;

--
-- PostgreSQL database dump complete
--

このような感じでファンクションの定義を取り出しました。

実践 引数があるファンクションのダンプ

次は以下のようなファンクションを登録して、ダンプします。

CREATE FUNCTION testproc2(name varchar, message varchar) RETURNS text
AS $$
BEGIN
	RETURN 'Hello World ' || name || '!' || message;

END; $$
LANGUAGE plpgsql;

先ほどと同じく、スキーマをダンプします。

$ pg_dump -Fc -s -f test.fc testdb

次にファンクションをリストアします。

$ pg_restore -P "testproc2(character varying, character varying)" -f testproc2.sql test.fc
$ cat testproc2.sql
--
-- PostgreSQL database dump
--

-- Dumped from database version 12.2 (Ubuntu 12.2-4)
-- Dumped by pg_dump version 12.2 (Ubuntu 12.2-4)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: testproc2(character varying, character varying); Type: FUNCTION; Schema: public; Owner: krohigewagma
--

CREATE FUNCTION public.testproc2(name character varying, message character varying) RETURNS text
    LANGUAGE plpgsql
    AS $$
BEGIN
 RETURN 'Hello World ' || name || '!' || message;

END; $$;


ALTER FUNCTION public.testproc2(name character varying, message character varying) OWNER TO krohigewagma;

--
-- PostgreSQL database dump complete
--

注意点1.引数の型名の指定

型を記載する場合、varcharはcharacter varyingというように省略前の名称で記載する必要があります。
ほかにもあると思いますが、調べきれていません。
ちなみに、カスタム形式じゃなくて普通にテキストでスキーマをダンプして、そこに書いてある型名で書けば間違いありません。
(なんか本末転倒な・・・)

注意点2.引数の区切りは", "(カンマスペース)で区切る

testproc2(character varying, character varying)

はOKなんですが

testproc2(character varying,character varying)

は失敗します。
スペースくらい融通利かせて欲しいところです。

一工夫する場合

pg_procテーブルと、pg_typeテーブルを使うことで、DBのファンクションをファンクション別のファイルで出力することも可能です。
ちょっと考えてみるのもよいかと思います。

最後に

たぶん、業務で出力したくなるケースってそんなに多くはないと思います。
しかし、知っておいて損はないと思います。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?