LoginSignup
5
0

More than 5 years have passed since last update.

psqlの変更(一部だけ)

Last updated at Posted at 2017-12-08

はじめに

にゃーん
この記事は、PostgreSQL 10全部ぬこ Advent Calendar 2017 の9日目のエントリです。

psqlとは

PostgreSQL使いにはお馴染み、PostgreSQLの対話的ターミナルだ。
「今どきキャラクタベースのターミナル?ねーよ」
という人もいそうだが、結構みんな使っている。ていうか自分の身の回りだとpsqlのみを使っている人の方が多い気がする。
そういえば先日のPostgreSQLカンファレンスでも、こんなやりとりがあったなあ。

pg10-09-01.png

ということで(主に開発系の人に)人気のあるpsql。
あと、自分が作った役に立たないスクリプトの一部はpsqlを使わないとうまく表示できないし、pg_reversiはPostgreSQL 9.6のpsql機能を使ってるということで、自分にとってもpsqlは手放すことの出来ない遊び道具です。

PostgreSQL 10の改善項目

単なる対話的ターミナルなのに、未だに改善がされているというのが面白い。

\if, \elseif, \else, \endif

これはタイトルからも想像できるように、psql用のスクリプト内で分岐処理が行えるようになった。
この機能については、betaバージョンの頃に検証した記事(PostgreSQL 10がやってくる!(その7) psql新機能 - \if, \elseif, \else, \endifがあるので、そっちを参照のこと。
(バージョン情報の説明でも簡単な例は使ってますが)

バージョン情報

バージョン情報自体は、PostgreSQLのSQL関数や、SHOWコマンドによるPostgreSQLパラメータの参照でも取得できる。

postgres=# SELECT version();
                                                 version
---------------------------------------------------------------------------------------------------------
 PostgreSQL 10.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16), 64-bit
(1 row)

postgres=# SHOW server_version ;
 server_version
----------------
 10.0
(1 row)

postgres=# SHOW server_version_num;
 server_version_num
--------------------
 100000
(1 row)

PostgreSQL 10からは、psqlのメタコマンド機能でも取得できるようになった。

[nuko@localhost psql]$ more version.sql
\echo Server version is :SERVER_VERSION_NAME
SELECT (setting = '10.0') as v FROM pg_settings WHERE name = 'server_version'
\gset
\if :v
  \echo Script for PostgreSQL 10
\else
  \echo Script for PostgreSQL < 10
\endif

このスクリプトをPostgreSQL 9.6.5(port=5433)に対して実行した場合。

[nuko@localhost psql]$ psql -U postgres -p 5433 -f version.sql
Server version is 9.6.5
Script for PostgreSQL < 10

このスクリプトをPostgreSQL 10.0(port=5432)に対して実行した場合。

[nuko@localhost psql]$ psql -U postgres -p 5432 -f version.sql
Server version is 10.0
Script for PostgreSQL 10

\gxメタコマンド

psqlには\xという拡張表示形式を指定するコマンドがある。
この\gxメタコマンドは、\xの表示形式に対応したものらしいが、正直、良くわからぬ・・・

\dコマンドの出力項目の追加

テーブルの一覧や、指定したテーブルの列情報を表示する\dメタコマンドの出力項目が追加された。追加されたのはcollaion, nullable, defaultの3列の情報だ。

9.6までの表示の例

[nuko@localhost psql]$ ~/pgsql/pgsql-9.6/bin/psql -p 5433 -U postgres
psql (9.6.5)
Type "help" for help.

postgres=# \d pg_database
    Table "pg_catalog.pg_database"
    Column     |   Type    | Modifiers
---------------+-----------+-----------
 datname       | name      | not null
 datdba        | oid       | not null
 encoding      | integer   | not null
 datcollate    | name      | not null
 datctype      | name      | not null
 datistemplate | boolean   | not null
 datallowconn  | boolean   | not null
 datconnlimit  | integer   | not null
 datlastsysoid | oid       | not null
 datfrozenxid  | xid       | not null
 datminmxid    | xid       | not null
 dattablespace | oid       | not null
 datacl        | aclitem[] |
Indexes:
    "pg_database_datname_index" UNIQUE, btree (datname), tablespace "pg_global"
    "pg_database_oid_index" UNIQUE, btree (oid), tablespace "pg_global"
Tablespace: "pg_global"

postgres=# 

10.1での表示例

[nuko@localhost psql]$ psql -p 5432 -U postgres
psql (10.0)
Type "help" for help.

postgres=# \d pg_database
               Table "pg_catalog.pg_database"
    Column     |   Type    | Collation | Nullable | Default
---------------+-----------+-----------+----------+---------
 datname       | name      |           | not null |
 datdba        | oid       |           | not null |
 encoding      | integer   |           | not null |
 datcollate    | name      |           | not null |
 datctype      | name      |           | not null |
 datistemplate | boolean   |           | not null |
 datallowconn  | boolean   |           | not null |
 datconnlimit  | integer   |           | not null |
 datlastsysoid | oid       |           | not null |
 datfrozenxid  | xid       |           | not null |
 datminmxid    | xid       |           | not null |
 dattablespace | oid       |           | not null |
 datacl        | aclitem[] |           |          |
Indexes:
    "pg_database_datname_index" UNIQUE, btree (datname), tablespace "pg_global"
    "pg_database_oid_index" UNIQUE, btree (oid), tablespace "pg_global"
Tablespace: "pg_global"

おわりに

今回はpsql改善のうち、わかりやすそうなもののみを取り上げた。正直、自分にもまだ全ての改善項目が理解しきれているわけではないが・・・

参考:該当するリリースノート

本エントリに関連するPostgreSQL 10リリースノートの記載です。

E.2.3.9.1. psql

  • Add conditional branch support to psql (Corey Huinker)

  • Add psql \gx meta-command to execute (\g) a query in expanded mode (\x) > (Christoph Berg)

  • Expand psql variable references in backtick-executed strings (Tom Lane)

  • Prevent psql's special variables from being set to invalid values (Daniel Vérité, Tom Lane)

  • Add variables showing server version and psql version (Fabien Coelho)

  • Improve psql's \d (display relation) and \dD (display domain) commands to > show collation, nullable, and default properties in separate columns (Peter Eisentraut)

  • Make the various \d commands handle no-matching-object cases more consistently (Daniel Gustafsson)

  • Improve psql's tab completion (Jeff Janes, Ian Barwick, Andreas Karlsson, Sehrope Sarkuni, Thomas Munro, Kevin Grittner, Dagfinn Ilmari Mannsåker)

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