$30 off During Our Annual Pro Sale. View Details »

PostgreSQL 13 - Improve psql

PostgreSQL 13 - Improve psql

Improvements to psql in PostgreSQL 13

nuko_yokohama

June 07, 2021
Tweet

More Decks by nuko_yokohama

Other Decks in Technology

Transcript

  1. 細かすぎて伝わらない
    PostgreSQL 13 の psql 改善項目

    View Slide

  2. 自己紹介
    ● ぬこ@横浜 (@nuko_yokohama)
    ● にゃーん
    ● 趣味でポスグレをやってる者だ

    View Slide

  3. 今日のお題
    ● psql のプロンプトに関する改善
    – デフォルトのプロンプトで (%x) 表示
    – PROMPT2 の桁揃え
    ● psql メタコマンドに関する改善
    – \gx, \g のオプション指定追加
    – 演算子クラス / 演算子族の表示
    – \d メタコマンドの Persistence 表示
    – TOAST テーブルの \d 表示の改善
    – \e メタコマンド終了時のクエリ表示
    – 標準エラー出力指定
    ● その他
    – --help のメッセージ変更
    – ファイルパスの TAB 補完改善
    – 結果出力中のエラーをキャッチして報告する
    (今回未検証)
    PostgreSQL 13 では psql の改善もいろいろ入った

    View Slide

  4. psql のプロンプトに関する改善

    View Slide

  5. デフォルトのプロンプトで (%x) 表示

    View Slide

  6. デフォルトのプロンプトで (%x) 表示
    ● PostgreSQL 12 まで
    testdb=# BEGIN;
    BEGIN
    testdb=# INSERT INTO test VALUES (1, 'abc');
    INSERT 0 1
    testdb=# ERROR;
    ERROR: syntax error at or near "ERROR"
    LINE 1: ERROR;
    ^
    testdb=# ROLLBACK;
    ROLLBACK
    testdb=#
    testdb=# BEGIN;
    BEGIN
    testdb=*# INSERT INTO test VALUES (1, 'abc');
    INSERT 0 1
    testdb=*# ERROR;
    ERROR: syntax error at or near "ERROR"
    LINE 1: ERROR;
    ^
    testdb=!# ROLLBACK;
    ROLLBACK
    testdb=#
    ● PostgreSQL 13

    View Slide

  7. デフォルトのプロンプトで (%x) 表示
    ● PostgreSQL 12 まで
    testdb=# BEGIN;
    BEGIN
    testdb=# INSERT INTO test VALUES (1, 'abc');
    INSERT 0 1
    testdb=# ERROR;
    ERROR: syntax error at or near "ERROR"
    LINE 1: ERROR;
    ^
    testdb=# ROLLBACK;
    ROLLBACK
    testdb=#
    おわかりいただけたであろうか・・・
    testdb=# BEGIN;
    BEGIN
    testdb=*# INSERT INTO test VALUES (1, 'abc');
    INSERT 0 1
    testdb=*# ERROR;
    ERROR: syntax error at or near "ERROR"
    LINE 1: ERROR;
    ^
    testdb=!# ROLLBACK;
    ROLLBACK
    testdb=#
    ● PostgreSQL 13

    View Slide

  8. デフォルトのプロンプトで (%x) 表示
    testdb=# BEGIN;
    BEGIN
    testdb=# INSERT INTO test VALUES (1, 'abc');
    INSERT 0 1
    testdb=# ERROR;
    ERROR: syntax error at or near "ERROR"
    LINE 1: ERROR;
    ^
    testdb=# ROLLBACK;
    ROLLBACK
    testdb=#
    testdb=# BEGIN;
    BEGIN
    testdb=*# INSERT INTO test VALUES (1, 'abc');
    INSERT 0 1
    testdb=*# ERROR;
    ERROR: syntax error at or near "ERROR"
    LINE 1: ERROR;
    ^
    testdb=!# ROLLBACK;
    ROLLBACK
    testdb=#
    ● PostgreSQL 12 まで ● PostgreSQL 13
    # の左になんかついている!

    View Slide

  9. デフォルトのプロンプトで (%x) 表示
    ● プロンプトでトランザクション状態がわかるようになった。
    トランザクションの状態 state プロンプト
    トランザクション外
    (セッション開始直後)
    idle #
    トランザクション内 Idel in transaction *#
    トランザクション内で
    異常発生時
    Idle in transaction (abort) !#

    View Slide

  10. デフォルトのプロンプトで (%x) 表示
    ● トランザクション状態をプロンプトに展開する機能自体
    は、 PostgreSQL 7.4 の頃から実装されていたりする。
    ● ただ、デフォルトの PROMPT1, PROMPT2 の設定が、 %x が
    つかない設定値になっているため、この機能自体に気づいてい
    ない人もいるかも(自分も今回、初めて知った)
    ● PostgreSQL 13 では、この PROMPT1, PROMPT2 のデフォル
    ト値に %x が設定された。

    View Slide

  11. PROMPT2 の桁揃え

    View Slide

  12. PROMPT2 の桁揃え
    ● PostgreSQL 12 まで
    testdb=# \set PROMPT1 ' にゃーんにゃーん # '
    にゃーんにゃーん # SELECT 'foo
    testdb'# bar
    testdb'# baz';
    ?column?
    ----------
    foo +
    bar +
    baz
    (1 row)
    にゃーんにゃーん #
    estdb=# \set PROMPT1 ' にゃーんにゃーん # '
    にゃーんにゃーん # \set PROMPT2 %w
    にゃーんにゃーん # SELECT 'foo
    bar
    baz';
    ?column?
    ----------
    foo +
    bar +
    baz
    (1 row)
    にゃーんにゃーん #
    ● PostgreSQL 13

    View Slide

  13. PROMPT2 の桁揃え
    ● PostgreSQL 12 まで
    testdb=# \set PROMPT1 ' にゃーんにゃーん # '
    にゃーんにゃーん # SELECT 'foo
    testdb'# bar
    testdb'# baz';
    ?column?
    ----------
    foo +
    bar +
    baz
    (1 row)
    にゃーんにゃーん #
    おわかりいただけたであろうか・・・
    estdb=# \set PROMPT1 ' にゃーんにゃーん # '
    にゃーんにゃーん # \set PROMPT2 %w
    にゃーんにゃーん # SELECT 'foo
    bar
    baz';
    ?column?
    ----------
    foo +
    bar +
    baz
    (1 row)
    にゃーんにゃーん #
    ● PostgreSQL 13

    View Slide

  14. PROMPT2 の桁揃え
    ● PostgreSQL 12 まで
    testdb=# \set PROMPT1 ' にゃーんにゃーん # '
    にゃーんにゃーん # SELECT 'foo
    testdb'# bar
    testdb'# baz';
    ?column?
    ----------
    foo +
    bar +
    baz
    (1 row)
    にゃーんにゃーん #
    estdb=# \set PROMPT1 ' にゃーんにゃーん # '
    にゃーんにゃーん # \set PROMPT2 %w
    にゃーんにゃーん # SELECT 'foo
    bar
    baz';
    ?column?
    ----------
    foo +
    bar +
    baz
    (1 row)
    にゃーんにゃーん #
    ● PostgreSQL 13

    View Slide

  15. PROMPT2 の桁揃え
    ● PostgreSQL 13 からは、 PROMPT2 ( SQL 入力が継続中のプ
    ロンプト)に %w を設定すると、自動的に継続行先頭の列を
    PROMPT1 のところに合わせてくれるようになった。
    ● この形が見やすい、という人なら、 .psqlrc に
    \set PROMPT2 %w
    を設定しておくと良いかと。
    ● なお、自分の環境では設定してない。

    View Slide

  16. psql メタコマンドに関する改善

    View Slide

  17. \gx, \g のオプション指定追加

    View Slide

  18. \gx, \g のオプション指定追加
    ● psql を普段使ってない人にはこのメタコマンド自体が馴染みな
    いかもしれないので、まずそこから説明。
    ● すごく雑にいうと、 psql 内で直前に実行したコマンドを再度実
    行するというもの。
    ● \g と \gx の違い
    – \g は expanded 設定に従った結果表示をする。
    – \gx は必ず「拡張形式」で結果表示する。

    View Slide

  19. \gx, \g のオプション指定追加
    ● \g の例
    testdb=# VALUES (1,'abc'),(2, NULL);
    column1 | column2
    ---------+---------
    1 | abc
    2 |
    (2 rows)
    testdb=# \g
    column1 | column2
    ---------+---------
    1 | abc
    2 |
    (2 rows)
    testdb=# \x
    Expanded display is on.
    testdb=# \g
    -[ RECORD 1 ]
    column1 | 1
    column2 | abc
    -[ RECORD 2 ]
    column1 | 2
    column2 |
    testdb=#
    同じ操作をくり返したいときに少し便利な機能・・・
    testdb=# VALUES (1,'abc'),(2, NULL);
    column1 | column2
    ---------+---------
    1 | abc
    2 |
    (2 rows)
    testdb=# \x
    Expanded display is on.
    testdb=# \x
    Expanded display is off.
    testdb=# \gx
    -[ RECORD 1 ]
    column1 | 1
    column2 | abc
    -[ RECORD 2 ]
    column1 | 2
    column2 |
    testdb=#
    ● \gx の例

    View Slide

  20. \gx, \g のオプション指定追加
    ● PostgreSQL 13 では、この \g, \gx に (option) が指定でき
    るようになった。
    ● でも、 option って何を指定すればいいんだ?
    →たとえば、 NULL を表す特殊な文字列を指定しておくとか。

    View Slide

  21. \gx, \g のオプション指定追加
    ● option 指定なしの \g
    testdb=# VALUES (1,' '),(2, NULL);
    column1 | column2
    ---------+---------
    1 |
    2 |
    (2 rows)
    testdb=# \g
    column1 | column2
    ---------+---------
    1 |
    2 |
    (2 rows)
    testdb=#
    最初から \pset null ぬるぽ を設定しておけよ!というツッコミはなし!
    testdb=# VALUES (1,' '),(2, NULL);
    column1 | column2
    ---------+---------
    1 |
    2 |
    (2 rows)
    testdb=# \g (null= ぬるぽ )
    column1 | column2
    ---------+---------
    1 |
    2 | ぬるぽ
    (2 rows)
    testdb=#
    ● option 指定ありの \g
    空白文字なのか NULL なのか
    コレガワカラナイ
    NULL が一目瞭然!

    View Slide

  22. 演算子クラス / 演算子族の表示

    View Slide

  23. 演算子クラス / 演算子族の表示
    ● PostgreSQL 12 まで
    testdb=# \?
    (中略)
    Informational
    (options: S = show system objects, + = additional detail)
    \d[S+] list tables, views, and sequences
    \d[S+] NAME describe table, view, sequence, or index
    \da[S] [PATTERN] list aggregates
    \dA[+] [PATTERN] list access methods
    \db[+] [PATTERN] list tablespaces
    testdb-# \?
    (中略)
    Informational
    (options: S = show system objects, + = additional detail)
    \d[S+] list tables, views, and sequences
    \d[S+] NAME describe table, view, sequence, or index
    \da[S] [PATTERN] list aggregates
    \dA[+] [PATTERN] list access methods
    \dAc[+] [AMPTRN [TYPEPTRN]] list operator classes
    \dAf[+] [AMPTRN [TYPEPTRN]] list operator families
    \dAo[+] [AMPTRN [OPFPTRN]] list operators of operator families
    \dAp [AMPTRN [OPFPTRN]] list procedures of operator families
    \db[+] [PATTERN] list tablespaces
    ● PostgreSQL 13

    View Slide

  24. 演算子クラス / 演算子族の表示
    ● PostgreSQL 12 まで
    testdb=# \?
    (中略)
    Informational
    (options: S = show system objects, + = additional detail)
    \d[S+] list tables, views, and sequences
    \d[S+] NAME describe table, view, sequence, or index
    \da[S] [PATTERN] list aggregates
    \dA[+] [PATTERN] list access methods
    \db[+] [PATTERN] list tablespaces
    おわかりいただけたであろうか・・・
    testdb-# \?
    (中略)
    Informational
    (options: S = show system objects, + = additional detail)
    \d[S+] list tables, views, and sequences
    \d[S+] NAME describe table, view, sequence, or index
    \da[S] [PATTERN] list aggregates
    \dA[+] [PATTERN] list access methods
    \dAc[+] [AMPTRN [TYPEPTRN]] list operator classes
    \dAf[+] [AMPTRN [TYPEPTRN]] list operator families
    \dAo[+] [AMPTRN [OPFPTRN]] list operators of operator families
    \dAp [AMPTRN [OPFPTRN]] list procedures of operator families
    \db[+] [PATTERN] list tablespaces
    ● PostgreSQL 13

    View Slide

  25. 演算子クラス / 演算子族の表示
    ● PostgreSQL 12 まで
    testdb=# \?
    (中略)
    Informational
    (options: S = show system objects, + = additional detail)
    \d[S+] list tables, views, and sequences
    \d[S+] NAME describe table, view, sequence, or index
    \da[S] [PATTERN] list aggregates
    \dA[+] [PATTERN] list access methods
    \db[+] [PATTERN] list tablespaces
    なんかめっさ増えてない?
    testdb-# \?
    (中略)
    Informational
    (options: S = show system objects, + = additional detail)
    \d[S+] list tables, views, and sequences
    \d[S+] NAME describe table, view, sequence, or index
    \da[S] [PATTERN] list aggregates
    \dA[+] [PATTERN] list access methods
    \dAc[+] [AMPTRN [TYPEPTRN]] list operator classes
    \dAf[+] [AMPTRN [TYPEPTRN]] list operator families
    \dAo[+] [AMPTRN [OPFPTRN]] list operators of operator families
    \dAp [AMPTRN [OPFPTRN]] list procedures of operator families
    \db[+] [PATTERN] list tablespaces
    ● PostgreSQL 13

    View Slide

  26. 演算子クラス / 演算子族の表示
    ● 演算子クラス / 演算子族 is 何?
    ● 雑に言うと、インデックス組み込みのためのなんか。
    – https://www.postgresql.jp/document/12/html/xindex.html#XINDEX-OPFAMILY
    ● 自作の型をインデックス検索可能にしたり、全文検索用イン
    デックスを使えるようにするためのなんか。

    View Slide

  27. 演算子クラス / 演算子族の表示
    ● PostgreSQL 13 で増えた psql の情報表示メタコマンド
    メタコマンド 内容
    \dAc 演算子クラスを表示する。
    \dAf 演算子族を表示する。
    \dAo 演算族に含まれる演算子を表示する。
    \dAp 演算子族を実装するプロシージャを表示する。
    ● 引数なしだと全てのなんかをリスト表示。
    ● 引数を指定するとパターンマッチするものだけ表示。

    View Slide

  28. 演算子クラス / 演算子族の表示
    ● 自作データ型fractionの演算子クラス / 演算子族の表示例
    testdb=# \dAc btree fraction
    List of operator classes
    AM | Input type | Storage type | Operator class | Default?
    -------+------------+--------------+----------------+----------
    btree | fraction | | fraction_ops | yes
    (1 row)
    testdb=# \dAf btree fraction
    List of operator families
    AM | Operator family | Applicable types
    -------+-----------------+------------------
    btree | fraction_ops | fraction
    (1 row)
    testdb=# \dAo btree fraction_ops
    List of operators of operator families
    AM | Operator family | Operator
    -------+-----------------+-------------------------
    btree | fraction_ops | < (fraction, fraction)
    btree | fraction_ops | <= (fraction, fraction)
    btree | fraction_ops | = (fraction, fraction)
    btree | fraction_ops | >= (fraction, fraction)
    btree | fraction_ops | > (fraction, fraction)
    (5 rows)
    testdb=# \dAp btree fraction_ops
    List of procedures of operator families
    AM | Operator family | Left arg type | Right arg type | Number | Proc name
    -------+-----------------+---------------+----------------+--------+--------------
    btree | fraction_ops | fraction | fraction | 1 | fraction_cmp
    (1 row)
    testdb=#

    View Slide

  29. 演算子クラス / 演算子族の表示
    ● こんな使い方の例もある。
    ● pg_bigm で全文インデックス設定するときに opclass の名前を忘れちまっ
    て設定できない・・・
    testdb=# CREATE INDEX data_bigm_idx ON test USING gin (data);
    ERROR: data type text has no default operator class for access method "gin"
    HINT: You must specify an operator class for the index or define a default operator class for the data type.
    testdb=#
    testdb=# \dAc gin text
    List of operator classes
    AM | Input type | Storage type | Operator class | Default?
    -----+------------+--------------+----------------+----------
    gin | text | | gin_bigm_ops | no
    (1 row)
    testdb=# CREATE INDEX data_bigm_idx ON test USING gin (data gin_bigm_ops);
    CREATE INDEX
    testdb=#
    ● そんなときには \dAc を叩いて Operator class を参照しよう。
    にゃーん

    View Slide

  30. \d メタコマンドの Persistence 表示

    View Slide

  31. \d メタコマンドの Persistence 表示
    ● PostgreSQL 12 まで
    testdb=# \d+
    List of relations
    Schema | Name | Type | Owner | Size | Description
    -----------+----------+-------+----------+------------+-------------
    pg_temp_3 | t_table | table | postgres | 8192 bytes |
    public | l_table | table | postgres | 8192 bytes |
    public | ul_table | table | postgres | 16 kB |
    (3 rows)
    testdb=# \d+
    List of relations
    Schema | Name | Type | Owner | Persistence | Size | Description
    -----------+----------+-------+----------+-------------+------------+-------------
    pg_temp_3 | t_table | table | postgres | temporary | 8192 bytes |
    public | l_table | table | postgres | permanent | 8192 bytes |
    public | ul_table | table | postgres | unlogged | 16 kB |
    (3 rows)
    ● PostgreSQL 13

    View Slide

  32. \d メタコマンドの Persistence 表示
    ● PostgreSQL 12 まで
    testdb=# \d+
    List of relations
    Schema | Name | Type | Owner | Size | Description
    -----------+----------+-------+----------+------------+-------------
    pg_temp_3 | t_table | table | postgres | 8192 bytes |
    public | l_table | table | postgres | 8192 bytes |
    public | ul_table | table | postgres | 16 kB |
    (3 rows)
    testdb=# \d+
    List of relations
    Schema | Name | Type | Owner | Persistence | Size | Description
    -----------+----------+-------+----------+-------------+------------+-------------
    pg_temp_3 | t_table | table | postgres | temporary | 8192 bytes |
    public | l_table | table | postgres | permanent | 8192 bytes |
    public | ul_table | table | postgres | unlogged | 16 kB |
    (3 rows)
    ● PostgreSQL 13
    おわかりいただけたであろうか・・・

    View Slide

  33. testdb=# \d+
    List of relations
    Schema | Name | Type | Owner | Size | Description
    -----------+----------+-------+----------+------------+-------------
    pg_temp_3 | t_table | table | postgres | 8192 bytes |
    public | l_table | table | postgres | 8192 bytes |
    public | ul_table | table | postgres | 16 kB |
    (3 rows)
    testdb=# \d+
    List of relations
    Schema | Name | Type | Owner | Persistence | Size | Description
    -----------+----------+-------+----------+-------------+------------+-------------
    pg_temp_3 | t_table | table | postgres | temporary | 8192 bytes |
    public | l_table | table | postgres | permanent | 8192 bytes |
    public | ul_table | table | postgres | unlogged | 16 kB |
    (3 rows)
    \d メタコマンドの Persistence 表示
    ● PostgreSQL 12 まで
    ● PostgreSQL 13
    UNLOGGED TABLE かどうか \d+ でわかるようになった!

    View Slide

  34. \d メタコマンドの Persistence 表示
    ● (本題じゃないけど) UNLOGGED TABLE って生成直後は 8kB じゃなくて 16kB
    なんだね・・・
    ● PostgreSQL 12 までであっても、 \d テーブル名指定で、指定したテーブルが
    unlogged や temporary かどうかは見分けられたが、一覧表示のときには、それが
    表示されなかった。
    Persistence 列の値 意味
    permanent WAL を出力する通常のテーブルを示す。
    unlogged WAL を出力しない UNLOGGED テーブルを示す。
    temporary 中間テーブルを示す。

    View Slide

  35. TOAST テーブルの \d 表示の改善

    View Slide

  36. TOAST テーブルの \d 表示の改善
    ● PostgreSQL 12 まで
    testdb=# \d pg_toast.pg_toast_1262
    TOAST table "pg_toast.pg_toast_1262"
    Column | Type
    ------------+---------
    chunk_id | oid
    chunk_seq | integer
    chunk_data | bytea
    estdb=# \d pg_toast.pg_toast_1262
    TOAST table "pg_toast.pg_toast_1262"
    Column | Type
    ------------+---------
    chunk_id | oid
    chunk_seq | integer
    chunk_data | bytea
    Owning table: "pg_catalog.pg_database"
    Indexes:
    "pg_toast_1262_index" PRIMARY KEY, btree (chunk_id, chunk_seq), tablespace "pg_global"
    Tablespace: "pg_global"
    ● PostgreSQL 13

    View Slide

  37. TOAST テーブルの \d 表示の改善
    ● PostgreSQL 12 まで
    testdb=# \d pg_toast.pg_toast_1262
    TOAST table "pg_toast.pg_toast_1262"
    Column | Type
    ------------+---------
    chunk_id | oid
    chunk_seq | integer
    chunk_data | bytea
    estdb=# \d pg_toast.pg_toast_1262
    TOAST table "pg_toast.pg_toast_1262"
    Column | Type
    ------------+---------
    chunk_id | oid
    chunk_seq | integer
    chunk_data | bytea
    Owning table: "pg_catalog.pg_database"
    Indexes:
    "pg_toast_1262_index" PRIMARY KEY, btree (chunk_id, chunk_seq), tablespace "pg_global"
    Tablespace: "pg_global"
    ● PostgreSQL 13
    おわかりいただけたであろうか・・・

    View Slide

  38. TOAST テーブルの \d 表示の改善
    ● PostgreSQL 12 まで
    testdb=# \d pg_toast.pg_toast_1262
    TOAST table "pg_toast.pg_toast_1262"
    Column | Type
    ------------+---------
    chunk_id | oid
    chunk_seq | integer
    chunk_data | bytea
    estdb=# \d pg_toast.pg_toast_1262
    TOAST table "pg_toast.pg_toast_1262"
    Column | Type
    ------------+---------
    chunk_id | oid
    chunk_seq | integer
    chunk_data | bytea
    Owning table: "pg_catalog.pg_database"
    Indexes:
    "pg_toast_1262_index" PRIMARY KEY, btree (chunk_id, chunk_seq), tablespace "pg_global"
    Tablespace: "pg_global"
    ● PostgreSQL 13
    pg_toast の \d 表示情報がいろいろ追加された!

    View Slide

  39. TOAST テーブルの \d 表示の改善
    ● PostgreSQL 13 では TOAST テーブル (pg_toast.pg_toast*)
    を \d コマンドで参照するときに、以下の情報が追加された。
    追加された行 内容
    Owning: TOAST 元のテーブルが何か
    Indexes: その TOAST テーブルのインデックス
    Tablespace: どのテーブル空間に割り当てられているか
    ● 普段使う機能ではないだろうけど・・・。

    View Slide

  40. \e メタコマンド終了時のクエリ表示

    View Slide

  41. \e メタコマンド終了時のクエリ表示
    ● PostgreSQL 12 まで
    $ psql testdb
    psql (13beta1)
    Type "help" for help.
    testdb=# \e
    $ psql testdb
    psql (13beta1)
    Type "help" for help.
    testdb=# \e
    ● PostgreSQL 13
    VALUES (1, ' '),
    (2, NULL)
    ~
    :x
    VALUES (1, ' '),
    (2, NULL)
    ~
    :x
    testdb=# \e
    testdb-#
    testdb=# \e
    testdb=# VALUES (1, ' '),
    (2, NULL)
    testdb-#

    View Slide

  42. \e メタコマンド終了時のクエリ表示
    ● PostgreSQL 12 まで
    $ psql testdb
    psql (13beta1)
    Type "help" for help.
    testdb=# \e
    $ psql testdb
    psql (13beta1)
    Type "help" for help.
    testdb=# \e
    ● PostgreSQL 13
    VALUES (1, ' '),
    (2, NULL)
    ~
    :x
    VALUES (1, ' '),
    (2, NULL)
    ~
    :x
    おわかりいただけたであろうか・・・
    testdb=# \e
    testdb-#
    testdb=# \e
    testdb=# VALUES (1, ' '),
    (2, NULL)
    testdb-#

    View Slide

  43. \e メタコマンド終了時のクエリ表示
    ● PostgreSQL 12 まで
    $ psql testdb
    psql (13beta1)
    Type "help" for help.
    testdb=# \e
    $ psql testdb
    psql (13beta1)
    Type "help" for help.
    testdb=# \e
    ● PostgreSQL 13
    VALUES (1, ' '),
    (2, NULL)
    ~
    :x
    VALUES (1, ' '),
    (2, NULL)
    ~
    :x
    testdb=# \e
    testdb-#
    testdb=# \e
    testdb=# VALUES (1, ' '),
    (2, NULL)
    testdb-#
    編集中のコマンドが \e 終了時に表示されるようになった!

    View Slide

  44. 標準エラー出力指定

    View Slide

  45. 標準エラー出力指定
    ● PostgreSQL 12 まで
    $ cat /tmp/q.txt
    \qecho stdout
    \warn stderr
    $ ~/pgsql/pgsql-12/bin/psql -f /tmp/q.txt 1> /tmp/out.txt 2> /tmp/err.txt
    $ cat /tmp/out.txt
    stdout
    $ cat /tmp/err.txt
    psql:/tmp/q.txt:2: error: invalid command \warn
    $
    cat /tmp/q.txt
    \qecho stdout
    \warn stderr
    $ ~/pgsql/pgsql-13beta1/bin/psql -f /tmp/q.txt 1> /tmp/out.txt 2> /tmp/err.txt
    $ cat /tmp/out.txt
    stdout
    $ cat /tmp/err.txt
    stderr
    $
    ● PostgreSQL 13

    View Slide

  46. 標準エラー出力指定
    ● PostgreSQL 12 まで
    $ cat /tmp/q.txt
    \qecho stdout
    \warn stderr
    $ ~/pgsql/pgsql-12/bin/psql -f /tmp/q.txt 1> /tmp/out.txt 2> /tmp/err.txt
    $ cat /tmp/out.txt
    stdout
    $ cat /tmp/err.txt
    psql:/tmp/q.txt:2: error: invalid command \warn
    $
    cat /tmp/q.txt
    \qecho stdout
    \warn stderr
    $ ~/pgsql/pgsql-13beta1/bin/psql -f /tmp/q.txt 1> /tmp/out.txt 2> /tmp/err.txt
    $ cat /tmp/out.txt
    stdout
    $ cat /tmp/err.txt
    stderr
    $
    ● PostgreSQL 13
    おわかりいただけたであろうか・・・

    View Slide

  47. 標準エラー出力指定
    ● PostgreSQL 12 まで
    $ cat /tmp/q.txt
    \qecho stdout
    \warn stderr
    $ ~/pgsql/pgsql-12/bin/psql -f /tmp/q.txt 1> /tmp/out.txt 2> /tmp/err.txt
    $ cat /tmp/out.txt
    stdout
    $ cat /tmp/err.txt
    psql:/tmp/q.txt:2: error: invalid command \warn
    $
    cat /tmp/q.txt
    \qecho stdout
    \warn stderr
    $ ~/pgsql/pgsql-13beta1/bin/psql -f /tmp/q.txt 1> /tmp/out.txt 2> /tmp/err.txt
    $ cat /tmp/out.txt
    stdout
    $ cat /tmp/err.txt
    stderr
    $
    ● PostgreSQL 13
    PostgreSQL 12 ではエラーになる・・・

    View Slide

  48. 標準エラー出力指定
    ● psql には任意のメッセージを出力するメタコマンドがある。
    メタコマンド名 機能 サポートバージョン
    \qecho text [...] 任意のメッセージを標準出力に
    出力する。
    PostgreSQL 7.0
    \warn text [...] 任意のメッセージを標準エラー
    出力に出力する。
    PostgreSQL 13
    ● 標準出力への出力コマンドは遥か昔(西暦 2000 年!)からあったのに、
    標準エラー出力への出力コマンドは、これまで対応していなかったらしい・・・

    View Slide

  49. その他

    View Slide

  50. --help のメッセージ変更

    View Slide

  51. --help のメッセージ変更
    ● PostgreSQL 12 まで
    $ ~/pgsql/pgsql-12/bin/psql --help
    psql is the PostgreSQL interactive terminal.
    (中略)
    Report bugs to .
    $
    $ ~/pgsql/pgsql-13beta1/bin/psql --help
    psql is the PostgreSQL interactive terminal.
    (中略)
    Report bugs to .
    PostgreSQL home page:
    $
    ● PostgreSQL 13

    View Slide

  52. --help のメッセージ変更
    ● PostgreSQL 12 まで
    $ ~/pgsql/pgsql-12/bin/psql --help
    psql is the PostgreSQL interactive terminal.
    (中略)
    Report bugs to .
    $
    $ ~/pgsql/pgsql-13beta1/bin/psql --help
    psql is the PostgreSQL interactive terminal.
    (中略)
    Report bugs to .
    PostgreSQL home page:
    $
    ● PostgreSQL 13
    おわかりいただけたであろうか・・・

    View Slide

  53. --help のメッセージ変更
    ● PostgreSQL 12 まで
    $ ~/pgsql/pgsql-12/bin/psql --help
    psql is the PostgreSQL interactive terminal.
    (中略)
    Report bugs to .
    $
    本家サイトの URL が出力されている!
    $ ~/pgsql/pgsql-13beta1/bin/psql --help
    psql is the PostgreSQL interactive terminal.
    (中略)
    Report bugs to .
    PostgreSQL home page:
    $
    ● PostgreSQL 13

    View Slide

  54. --help のメッセージ変更
    ● 実は他のユーティリティ (createdb, pg_basebackup, initdb
    等)については、 PostgreSQL 12 でも、本家サイトの URL は
    表示されていた。
    ● なぜか psql だけ PostgreSQL 12 までは表示されていなかった
    らしい・・・。

    View Slide

  55. ファイルパスの TAB 補完改善

    View Slide

  56. ファイルパスの TAB 補完改善
    ● PostgreSQL 12 まで
    testdb=# COPY test FROM '/tmp/t testdb=# COPY test FROM '/tmp/t
    ● PostgreSQL 13
    ここで水平タブを入力すると・・・
    testdb=# COPY test FROM /tmp/test.dat testdb=# COPY test FROM '/tmp/test.dat'

    View Slide

  57. ファイルパスの TAB 補完改善
    ● PostgreSQL 12 まで
    testdb=# COPY test FROM '/tmp/t testdb=# COPY test FROM '/tmp/t
    ● PostgreSQL 13
    ここで水平タブを入力すると・・・
    testdb=# COPY test FROM /tmp/test.dat testdb=# COPY test FROM '/tmp/test.dat'
    おわかりいただけたであろうか・・・

    View Slide

  58. ファイルパスの TAB 補完改善
    ● PostgreSQL 12 まで
    testdb=# COPY test FROM '/tmp/t testdb=# COPY test FROM '/tmp/t
    ● PostgreSQL 13
    ここで水平タブを入力すると・・・
    testdb=# COPY test FROM /tmp/test.dat testdb=# COPY test FROM '/tmp/test.dat'
    消える!シングルクォート!
    にゃーん
    改善っていうか Bug-Fix に近いような気がする。
    残る!シングルクォート!

    View Slide

  59. まとめ

    View Slide

  60. 今日のお題(再掲)
    ● psql のプロンプトに関する改善
    – デフォルトのプロンプトで (%x) 表示
    – PROMPT2 の桁揃え
    ● psql メタコマンドに関する改善
    – \gx, \g のオプション指定追加
    – 演算子クラス / 演算子族の表示
    – \d メタコマンドの Persistence 表示
    – TOAST テーブルの \d 表示の改善
    – \e メタコマンド終了時のクエリ表示
    – 標準エラー出力指定
    ● その他
    – --help のメッセージ変更
    – ファイルパスの TAB 補完改善
    – 結果出力中のエラーをキャッチして報告する
    (今回未検証)
    PostgreSQL 13 では psql の改善もいろいろ入った
    やはり
    psql は
    いいぞ

    View Slide