2013年11月16日

RDS for PostgreSQLの中を覗いてみた(追記あり)

皆さんお待ちかねの、RDS for PostgreSQLが昨日リリースされました。
既にインスタンスを立ち上げてみた方もいるようです。
というわけで、デプロイまでの手順は上記を参照いただくとして、今回のエントリでは、実際に「PostgreSQLとして使う」という観点でいくつか見てみたいと思います。


■ログインと通信

まず、PostgreSQLへのログインの方法ですが、通常のpsqlコマンドでログインでき、SSLによる通信の暗号化がサポートされているようです。
[snaga@devsv03 ~]$ psql -U testuser -h testdbinstance.XXXXX.ap-northeast-1.rds.amazonaws.com testdb
Password for user testuser:
psql (9.3.0, server 9.3.1)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

testdb=>
PostgreSQLのSSLサポートは、認証のセッション(ユーザ名とパスワードを送るところ)だけではなく、SQLのやり取りなど全体が暗号化されますので、これによって通信が保護されることになります。

■バージョン


次にPostgreSQLのバージョンを見てみます。
testdb=> select version();
                                                   version
--------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.3.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2), 64-bit
(1 行)

testdb=> 
Red Hatの環境でビルドしたPostgreSQLのようです。最新版の9.3.1がデプロイされています。

■ユーザとユーザ権限


次にユーザ(ロール)と権限を見てみます。
testdb=> \du+
                                         List of roles
   Role name   |                   Attributes                   |    Member of    | Description
---------------+------------------------------------------------+-----------------+-------------
 rds_superuser | Cannot login                                   | {}              |
 rdsadmin      | Superuser, Create role, Create DB, Replication | {}              |
 testuser      | Create role, Create DB                         | {rds_superuser} |

testdb=>
ここでは、インスタンス作成時に指定した「testuser」というユーザで接続していますが、このユーザにはデータベースとユーザの作成権限があるようです。このユーザはスーパーユーザーではありません。

このユーザとは別に、「rdsadmin」というユーザがあり、このユーザが管理者権限を持っているようです。但し、我々AWSユーザはこのrdsadminというユーザは使えません。

■データベースのエンコーディングとロケール


次に、データベースのエンコーディングとロケールを見てみます。
testdb=> \l+
                                                                     List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |   Size    | Tablespace |                Description
-----------+----------+----------+-------------+-------------+-----------------------+-----------+------------+--------------------------------------------
 postgres  | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 6570 kB   | pg_default | default administrative connection database
 rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin | No Access | pg_default |
 template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +| 6457 kB   | pg_default | unmodifiable empty database
           |          |          |             |             | rdsadmin=CTc/rdsadmin |           |            |
 template1 | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/testuser          +| 6570 kB   | pg_default | default template for new databases
           |          |          |             |             | testuser=CTc/testuser |           |            |
 testdb    | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 6570 kB   | pg_default |
(5 rows)

testdb=>
デフォルトではデータベースのロケールは「en_US」となっているようです。

PostgreSQLを日常的に使っている方は、特に日本語を扱う場合にはロケールを「使用しない」または「C」に設定していると思います。そのため、デフォルトのままでは、日本語(というか、en_US以外を扱う時)に問題が生じる可能性があります。

PostgreSQLのロケールの詳細については、以下の記事を参照してください。
ロケールを「C」に設定したデータベースを使いたい場合、少しトリッキーなことをする必要があります。

ロケールを「C」にしたデータベースを新規に作成し、前のRDSで作成した最初のデータベースと入れ替えることで実現することができます。以下では、「testdb2」というデータベースを作成し、「testdb」と入れ替えています。
testdb=> CREATE DATABASE testdb2 WITH template template0 encoding 'utf8' lc_collate 'C' lc_ctype 'C';
CREATE DATABASE
testdb=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
 template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
           |          |          |             |             | rdsadmin=CTc/rdsadmin
 template1 | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/testuser          +
           |          |          |             |             | testuser=CTc/testuser
 testdb    | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)

testdb=> \c postgres
psql (9.3.0, server 9.3.1)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
You are now connected to database "postgres" as user "testuser".
postgres=> DROP DATABASE testdb;
DROP DATABASE
postgres=> ALTER DATABASE testdb2 RENAME TO testdb;
ALTER DATABASE
postgres=> \c testdb
psql (9.3.0, server 9.3.1)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
You are now connected to database "testdb" as user "testuser".
testdb=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
 template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
           |          |          |             |             | rdsadmin=CTc/rdsadmin
 template1 | testuser | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/testuser          +
           |          |          |             |             | testuser=CTc/testuser
 testdb    | testuser | UTF8     | C           | C           |
(5 rows)

testdb=>
これで、ロケールを「C」にしたtestdbを準備することができました。

■EXTENSION(エクステンション、拡張)


PostgreSQLの大きな特徴の一つは、その拡張性です。PostgreSQLの強みの一つとしてよく上げられるGIS拡張「PostGIS」も、EXTENSIONと呼ばれる仕組みで提供されています。

RDS for PostgreSQLで使用できるEXTENSIONの一覧を見てみます。
testdb=> select * FROM pg_available_extensions;
          name          | default_version | installed_version |                               comment
------------------------+-----------------+-------------------+---------------------------------------------------------------------
 cube                   | 1.0             |                   | data type for multidimensional cubes
 earthdistance          | 1.0             |                   | calculate great-circle distances on the surface of the Earth
 btree_gin              | 1.0             |                   | support for indexing common datatypes in GIN
 postgres_fdw           | 1.0             |                   | foreign-data wrapper for remote PostgreSQL servers
 pg_buffercache         | 1.0             |                   | examine the shared buffer cache
 pg_freespacemap        | 1.0             |                   | examine the free space map (FSM)
 intagg                 | 1.0             |                   | integer aggregator and enumerator (obsolete)
 unaccent               | 1.0             |                   | text search dictionary that removes accents
 postgis_topology       | 2.1.0           |                   | PostGIS topology spatial types and functions
 chkpass                | 1.0             |                   | data type for auto-encrypted passwords
 citext                 | 1.0             |                   | data type for case-insensitive character strings
 dict_xsyn              | 1.0             |                   | text search dictionary template for extended synonym processing
 dict_int               | 1.0             |                   | text search dictionary template for integers
 postgis_tiger_geocoder | 2.1.0           |                   | PostGIS tiger geocoder and reverse geocoder
 fuzzystrmatch          | 1.0             |                   | determine similarities and distance between strings
 pg_stat_statements     | 1.1             |                   | track execution statistics of all SQL statements executed
 pg_trgm                | 1.1             |                   | text similarity measurement and index searching based on trigrams
 hstore                 | 1.2             |                   | data type for storing sets of (key, value) pairs
 tsearch2               | 1.0             |                   | compatibility package for pre-8.3 text search functions
 intarray               | 1.0             |                   | functions, operators, and index support for 1-D arrays of integers
 uuid-ossp              | 1.0             |                   | generate universally unique identifiers (UUIDs)
 pgcrypto               | 1.0             |                   | cryptographic functions
 pgstattuple            | 1.1             |                   | show tuple-level statistics
 sslinfo                | 1.0             |                   | information about SSL certificates
 ltree                  | 1.0             |                   | data type for hierarchical tree-like structures
 pltcl                  | 1.0             |                   | PL/Tcl procedural language
 plpgsql                | 1.0             | 1.0               | PL/pgSQL procedural language
 btree_gist             | 1.0             |                   | support for indexing common datatypes in GiST
 plperl                 | 1.0             |                   | PL/Perl procedural language
 isn                    | 1.0             |                   | data types for international product numbering standards
 pgrowlocks             | 1.1             |                   | show row-level locking information
 tablefunc              | 1.0             |                   | functions that manipulate whole tables, including crosstab
 dblink                 | 1.1             |                   | connect to other PostgreSQL databases from within a database
 postgis                | 2.1.0           |                   | PostGIS geometry, geography, and raster spatial types and functions
(34 rows)

testdb=>
PostgreSQLのcontribにあるすべてのモジュールではないようですが、多くのEXTENSIONを使用できるようになっています(デフォルトでインストールされているのはplpgsqlのみ)。これらを使いたい場合には CREATE EXTENSION コマンドでインストールします。
testdb=> CREATE EXTENSION postgis;
CREATE EXTENSION
testdb=> \d
               List of relations
 Schema |       Name        | Type  |  Owner
--------+-------------------+-------+----------
 public | geography_columns | view  | rdsadmin
 public | geometry_columns  | view  | rdsadmin
 public | raster_columns    | view  | rdsadmin
 public | raster_overviews  | view  | rdsadmin
 public | spatial_ref_sys   | table | rdsadmin
(5 rows)

testdb=>

■レプリケーション


今回はマルチAZ構成でデプロイしたので、レプリケーションの設定を見てみます。レプリケーション関連の統計情報は pg_stat_replication で取得します。
testdb=> SELECT * FROM pg_stat_replication;
 pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | state | sent_location | write_location | flush_location | replay_location | sync_priority | sync_state
-----+----------+---------+------------------+-------------+-----------------+-------------+---------------+-------+-------------
--+----------------+----------------+-----------------+---------------+------------
(0 rows)

testdb=>
上記の通り、レプリケーションの統計情報が存在していませんでした。RDSのマルチAZ構成では、PostgreSQLのレプリケーション機能は使っていないようです。

ここからは想像ですが、以下のログアーカイブの設定やタイムアウト値から考えると、RDS for PostgreSQLのマルチAZ構成では、アーカイブログを介したホットスタンバイで構成しているのかもしれません。

■設定パラメータ(postgresql.conf)


最後に postgresql.conf の設定(pg_settings)を見てみます。 これは長くなるので、以下のGistに張り付けておきます。
特に変わったところは見られませんでしたが、マルチAZ構成でレプリケーションを使っていると想像していたものの、実際にはアーカイブログによるホットスタンバイ構成っぽい、ということくらいでしょうか。

■まとめ

というわけで、実際に使う上で若干気になるところもありましたが、まずは使い始めてみる、という点ではさすがによくできたサービスだと思います。興味のある方はぜひ試してみてください。私も少しずつ使ってみようと思います。

では、また。

(追記 11/18 16:22)
上記で「ホットスタンバイのHAかも」と書いたのですが、よく考えたら wal_level が archive なので、ホットスタンバイでもないと思われます。

ただ、Amazon RDS for PostgreSQLのドキュメント

"This allows you to restore your DB Instance to any second during your retention period, up to the last five minutes."

とあることと、archive_timeoutの300秒の設定を併せて考えると、おそらくアーカイブログの機能を使って可用性を確保しているのではないかと思います。(もしかしたら全然違うAWSの独自機能を使っているかもしれませんが)

10 件のコメント:

  1. Nice work, your blog is concept-oriented, kindly share more blogs like this
    AWS Online Course

    返信削除
  2. Appreciate it for this post, I am a big fan of this internet site would like to keep updated.온라인경마

    返信削除
  3. You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent.
    스포츠토토

    返信削除
  4. Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates. 파칭코사이트인포

    返信削除