この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
小ネタです。
Amazon Redshiftでデータ投入周りの作業を行っている際、ふとした事で掲題の情報を知り、実際使ってみたら『ええやん』となったのでご紹介してみたいと思います。
psqlにおける『\x』とは
psqlコマンドに於ける『\x』は、以下の様な形で『拡張テーブル形式モード』の切り替えを行います。
-x --expanded 拡張テーブル形式モードを有効にします。 これは\xコマンドと同じです。
実際どういう風に変わるのでしょうか。試してみましょう。これが通常時。項目が横に並んで表示される形ですね。カラム数及びその内容が表示幅に収まっていれば良いですが、そうでない場合は折り返して表示されてしまいます。(このエントリでは横に見切れて1行で表示されていますが)
# SELECT * FROM public.orders LIMIT 1;
order_id | order_date | priority | quantity | sales | discount_rate | ship_mode | profit | unit_price | ad_expenses | shipping_cost | customer_name | prefecture | city | area | shop_name | customer_segment | product_category | product_sub_category | product_id | product_name | product_description | product_container | base_margin | supplier | deliver_date | ship_date
----------+------------+----------+----------+-------+---------------+------------+--------+------------+-------------+---------------+---------------+------------+------+------+-----------+------------------+------------------+----------------------+------------+--------------+---------------------------------------------------------------+-------------------+-------------+----------+--------------+------------
97 | 2011-12-30 | 中 | 26 | 9446 | 0.03 | 通常航空便 | 2824 | 289 | 244146 | 50 | 仙波 敏男 | 栃木 | 栃木 | 関東 | Willis | 消費者 | 事務用品 | ラベル | P190 | Avery 498 | This is the field which has product description for Avery 498 | 小型ボックス | 0.38 | Argot | 2010-01-30 | 2010-01-30
(1 row)
\xコマンドを実行。切り替わった後に上記SQLを再実行してみます。すると以下のように項目名:値の形で表示されるようになりました。若干見慣れない形なので違和感はありますね。
# \x
Expanded display is on.
# SELECT * FROM public.orders LIMIT 1;
-[ RECORD 1 ]--------+--------------------------------------------------------------
order_id | 97
order_date | 2011-12-30
priority | 中
quantity | 26
sales | 9446
discount_rate | 0.03
ship_mode | 通常航空便
profit | 2824
unit_price | 289
ad_expenses | 244146
shipping_cost | 50
customer_name | 仙波 敏男
prefecture | 栃木
city | 栃木
area | 関東
shop_name | Willis
customer_segment | 消費者
product_category | 事務用品
product_sub_category | ラベル
product_id | P190
product_name | Avery 498
product_description | This is the field which has product description for Avery 498
product_container | 小型ボックス
base_margin | 0.38
supplier | Argot
deliver_date | 2010-01-30
ship_date | 2010-01-30
#
通常のシーンであればあまり使い勝手も無い?ような気がするこのオプションですが、ふと『あれ、これってRedshiftのCOPY処理でエラー出た時の確認で参照するテーブル(stl_load_errors)で使えば良さげじゃね?』と思いたち、試してみました。
通常実行時だとこのような見栄えになります。内容的には分かるといえばわかりますが見づらいですね。
これを拡張テーブル形式モードで表示しています。素敵ですね!
以前のエントリで同じ趣旨の内容を書いていましたが、こちらのSQLを使う事無く、表示切り替えでテーブルの内容をそのまま出すだけで十分わかりやすい、スマートな表示を行う事が出来ました。
# \x
Expanded display is on.
# SELECT * FROM stl_load_errors ORDER BY starttime DESC LIMIT 1;
-[ RECORD 1 ]---+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
userid | 100
slice | 3
tbl | 650906
starttime | 2017-02-14 xx:xx:xx.xxxxxx
session | 24392
query | 480537
filename | s3://xxxxxxxxxxx/xxxxxxxx/sample-xxxxxxx.csv
line_number | 1
colname | userid
type | char
col_length | 5
position | 2
raw_line | 'userid','username','userdata','regdate'
raw_field_value | userid
err_code | 1204
err_reason | Char length exceeds DDL length
まとめ
ちなみにこの表示モードを元に戻す場合は再度『\x』を実行する事で対応出来ます。
# \x
Expanded display is off.
今回のケースではRedshiftの作業に於けるCOPY処理エラーの内容について、更にはpsqlコマンド限定という若干(十分)ニッチな範囲の内容となりますが、この辺りの作業を日々行っている方々であればこの『ええやん!』な気持ち、お分かり頂ける事かと思います。小技ではありますが見づらいデータがあった場合にはこの『\x』コマンドをお試しになってみてはいかがでしょうか。