Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

デフォルトのプロンプトで (%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

Slide 7

Slide 7 text

デフォルトのプロンプトで (%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

Slide 8

Slide 8 text

デフォルトのプロンプトで (%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 # の左になんかついている!

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

PROMPT2 の桁揃え

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

\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 の例

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

\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 が一目瞭然!

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

演算子クラス / 演算子族の表示 ● 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

Slide 24

Slide 24 text

演算子クラス / 演算子族の表示 ● 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

Slide 25

Slide 25 text

演算子クラス / 演算子族の表示 ● 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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

演算子クラス / 演算子族の表示 ● 自作データ型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=#

Slide 29

Slide 29 text

演算子クラス / 演算子族の表示 ● こんな使い方の例もある。 ● 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 を参照しよう。 にゃーん

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

\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

Slide 32

Slide 32 text

\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 おわかりいただけたであろうか・・・

Slide 33

Slide 33 text

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+ でわかるようになった!

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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 おわかりいただけたであろうか・・・

Slide 38

Slide 38 text

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 表示情報がいろいろ追加された!

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

\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-#

Slide 42

Slide 42 text

\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-#

Slide 43

Slide 43 text

\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 終了時に表示されるようになった!

Slide 44

Slide 44 text

標準エラー出力指定

Slide 45

Slide 45 text

標準エラー出力指定 ● 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

Slide 46

Slide 46 text

標準エラー出力指定 ● 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 おわかりいただけたであろうか・・・

Slide 47

Slide 47 text

標準エラー出力指定 ● 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 ではエラーになる・・・

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

その他

Slide 50

Slide 50 text

--help のメッセージ変更

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

--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 おわかりいただけたであろうか・・・

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

ファイルパスの 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'

Slide 57

Slide 57 text

ファイルパスの 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' おわかりいただけたであろうか・・・

Slide 58

Slide 58 text

ファイルパスの 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 に近いような気がする。 残る!シングルクォート!

Slide 59

Slide 59 text

まとめ

Slide 60

Slide 60 text

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