From e1011e11fe484c3eeb52d043f9f922cd5d858306 Mon Sep 17 00:00:00 2001 From: Takahiro Itagaki Date: Thu, 16 Apr 2009 06:01:01 +0000 Subject: [PATCH] Modify LOCK to LOCK-NOWAIT-and-loop at swap tables to avoid holding exclusive locks long time. Suggested by Kenny Gorman. --- bin/pg_reorg.c | 43 +++- doc/index-ja.html | 176 +++++++++---- doc/index.html | 176 +++++++++---- doc/pg_reorg-ja.html | 582 +++++++++++++++++++++---------------------- doc/pg_reorg.html | 550 ++++++++++++++++++++-------------------- lib/pg_reorg.sql.in | 2 +- 6 files changed, 849 insertions(+), 680 deletions(-) diff --git a/bin/pg_reorg.c b/bin/pg_reorg.c index 8981690..e73cb31 100755 --- a/bin/pg_reorg.c +++ b/bin/pg_reorg.c @@ -74,7 +74,7 @@ typedef struct reorg_index static void reorg_all_databases(const char *orderby); static bool reorg_one_database(const char *orderby, const char *table); -static void reorg_one_table(const reorg_table *table, const char* orderby); +static void reorg_one_table(const reorg_table *table, const char *orderby); static void reconnect(void); static void disconnect(void); @@ -93,6 +93,13 @@ static void PrintVersion(void); static char *getstr(PGresult *res, int row, int col); static Oid getoid(PGresult *res, int row, int col); +#define SQLSTATE_INVALID_SCHEMA_NAME "3F000" +#define SQLSTATE_LOCK_NOT_AVAILABLE "55P03" + +static bool sqlstate_equals(PGresult *res, const char *state) +{ + return strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), state) == 0; +} static const char *progname = NULL; static bool echo = false; @@ -390,8 +397,7 @@ reorg_one_database(const char *orderby, const char *table) if (PQresultStatus(res) != PGRES_TUPLES_OK) { - const char *state = PQresultErrorField(res, PG_DIAG_SQLSTATE); - if (state && strcmp(state, "3F000") == 0) + if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME)) { /* Schema reorg does not exist. Skip the database. */ ret = false; @@ -657,8 +663,32 @@ reorg_one_table(const reorg_table *table, const char *orderby) if (verbose) fprintf(stderr, "---- swap ----\n"); - command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL); - command(table->lock_table, 0, NULL); + for (;;) + { + command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL); + res = execute_nothrow(table->lock_table, 0, NULL); + if (PQresultStatus(res) == PGRES_COMMAND_OK) + { + PQclear(res); + break; + } + else if (sqlstate_equals(res, SQLSTATE_LOCK_NOT_AVAILABLE)) + { + /* retry if lock conflicted */ + PQclear(res); + command("ROLLBACK", 0, NULL); + sleep(1); + continue; + } + else + { + /* exit otherwise */ + printf("%s", PQerrorMessage(current_conn)); + PQclear(res); + exit_with_cleanup(1); + } + } + apply_log(table, 0); params[0] = utoa(table->target_oid, buffer); command("SELECT reorg.reorg_swap($1)", 1, params); @@ -761,7 +791,7 @@ execute_nothrow(const char *query, int nParams, const char **params) } /* - * execute - Execute a SQL and discard the result, or exit() if failed. + * execute - Execute a SQL and return the result, or exit() if failed. */ static PGresult * execute(const char *query, int nParams, const char **params) @@ -834,7 +864,6 @@ static void PrintVersion(void) { fprintf(stderr, "pg_reorg " REORG_VERSION "\n"); - return; } /* diff --git a/doc/index-ja.html b/doc/index-ja.html index 79ba0ab..e4c17e6 100755 --- a/doc/index-ja.html +++ b/doc/index-ja.html @@ -1,53 +1,123 @@ - - - - - - - - - pg_reorg: Project Home Page - - - -
-
-

pg_reorg ホームページへようこそ

-
-
-

-pg_reorg は PostgreSQL のテーブルを再編成するシェルコマンドです。 -共有ロックや排他ロックを取得しないため、再編成中であっても行の参照や更新を行うことができます。 -このモジュールは CLUSTER や VACUUM FULL コマンドのより良い代替になります。 -

-

この pg_reorg プロジェクトは PostgreSQL コミュニティによる pgFoundry の中のプロジェクトです。

- -
-Here is an English page. -
-
- -

-ドキュメントはこちら。 -

- -
-
-Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION -
- - -
-
- -
- - - + + + + + + + + + pg_reorg: Project Home Page + + + +
+
+

pg_reorg ホームページへようこそ

+
+
+

+pg_reorg は PostgreSQL のテーブルを再編成するシェルコマンドです。 +共有ロックや排他ロックを取得しないため、再編成中であっても行の参照や更新を行うことができます。 +このモジュールは CLUSTER や VACUUM FULL コマンドのより良い代替になります。 +

+

この pg_reorg プロジェクトは PostgreSQL コミュニティによる pgFoundry の中のプロジェクトです。

+ +
+Here is an English page. +
+
+ +

ドキュメント

+

+ドキュメントはこちら。 +

+ +

実行時間

+

+pg_reorg とclusterdb の比較に示します。 +断片化のないソートされた状態 (not fragmented) では clusterdb のほうが高速ですが、完全に断片化した状態 (fully fragmented) では pg_reorg が大幅に高速です。 +一般的に、再編成は断片化が進行した状態で実施されることを考えると、pg_reorg は clusterdb よりも実行時間が短いと言えます。 +

+ +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
測定環境
大項目小項目環境
ハードウェアCPU2 × Xeon 5160 3.00GHz (Dual core)
メモリ2GB
ストレージUltra320 SCSI, 15000rpm (220GB)
ソフトウェアOSRHEL 5.2 (64bit) 2.6.18-92.el5
DBPostgreSQL 8.3.3
pg_reorg1.0.0
clusterdbclusterdb (PostgreSQL) 8.3.3
データスキーマ
CREATE TABLE tbl (
+ id bigserial PRIMARY KEY,
+ seqkey timestamp NOT NULL,
+ rndkey timestamp NOT NULL,
+ filler char(75) NOT NULL
+);
+CREATE INDEX idx_seq ON tbl (seqkey);
+CREATE INDEX idx_rnd ON tbl (rndkey);
件数1650万件 (約2GB)
+ +
+ +
+
+Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +
+ + +
+
+ +
+ + + diff --git a/doc/index.html b/doc/index.html index 831a4e0..006718d 100755 --- a/doc/index.html +++ b/doc/index.html @@ -1,53 +1,123 @@ - - - - - - - - - pg_reorg: Project Home Page - - - -
-
-

Welcome to the pg_reorg Project Home Page

-
-
-

-pg_reorg can re-organize tables on a postgres database without any locks so that you can retrieve or update rows in tables being reorganized. -The module is developed to be a better alternative of CLUSTER and VACUUM FULL. -

-

-The pg_reorg project is a PostgreSQL Community project that is a part of the pgFoundry. -

-

-The pgFoundry page for the project is at http://pgfoundry.org/projects/reorg, -where you can find downloads, documentation, bug reports, mailing lists, and a whole lot more. -

-
-日本語のページはこちら。 -
-
- -

-Documentations here. -

- -
-
-Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION -
- - -
-
- -
- - - + + + + + + + + + pg_reorg: Project Home Page + + + +
+
+

Welcome to the pg_reorg Project Home Page

+
+
+

+pg_reorg can re-organize tables on a postgres database without any locks so that you can retrieve or update rows in tables being reorganized. +The module is developed to be a better alternative of CLUSTER and VACUUM FULL. +

+

+The pg_reorg project is a PostgreSQL Community project that is a part of the pgFoundry. +

+

+The pgFoundry page for the project is at http://pgfoundry.org/projects/reorg, +where you can find downloads, documentation, bug reports, mailing lists, and a whole lot more. +

+
+日本語のページはこちら。 +
+
+ +

Documentation

+

+Documentations here. +

+ +

Execution time

+

+Here is a comparison between pg_reorg and clusterdb. +Clusterdb is faster on not fragmented conditions, but pg_reorg is faster on fully fragmented conditions. +Since reorganization is needed only if tables are fragmented, pg_reorg should be faster than clusterdb. +

+ +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Configuration
CategoryItemDetails
HardwareCPU2 * Xeon 5160 3.00GHz (Dual core)
Memory2GB
StorageUltra320 SCSI, 15000rpm (220GB)
SoftwareOSRHEL 5.2 (64bit) 2.6.18-92.el5
DBPostgreSQL 8.3.3
pg_reorg1.0.0
clusterdbclusterdb (PostgreSQL) 8.3.3
DataScheme
CREATE TABLE tbl (
+ id bigserial PRIMARY KEY,
+ seqkey timestamp NOT NULL,
+ rndkey timestamp NOT NULL,
+ filler char(75) NOT NULL
+);
+CREATE INDEX idx_seq ON tbl (seqkey);
+CREATE INDEX idx_rnd ON tbl (rndkey);
Rows16.5 M rows (2GB)
+ +
+ +
+
+Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +
+ + +
+
+ +
+ + + diff --git a/doc/pg_reorg-ja.html b/doc/pg_reorg-ja.html index cb6c4a2..32b73fe 100755 --- a/doc/pg_reorg-ja.html +++ b/doc/pg_reorg-ja.html @@ -1,291 +1,291 @@ - - - -pg_reorg - - - - - - -

pg_reorg

- -
-

名前

-pg_reorg -- PostgreSQLデータベース内のテーブルに対して、参照/更新処理をブロックせずに再編成を行います。 -
- -
- -

概要

-

-pg_reorg [connection-options...] [message-options...] [order-options...] [target-options...] -

-

指定できるオプションには4つのカテゴリがあります。 -詳細はオプションを参照してください。

-
-
connection-options : 接続パラメータ
-
-h [--host] host
-
-p [--port] port
-
-U [--username] username
-
-W [--password]
-
message-options : 出力メッセージ
-
-e [--echo]
-
-q [--quiet]
-
-v [--verbose]
-
order-options : 並び替えの基準
-
-o [--order-by] columns [,...]
-
-n [--no-order]
-
target-options : 処理対象
-
-a [--all]
-
-d [--dbname] dbname
-
-t [--table] table
-
-
- -
-

説明

-

pg_reorg は、PostgreSQLデータベース内のテーブルを再編成(行の並び替え)するユーティリティです。 -clusterdb と異なり、参照/更新処理をブロックしません。 -再編成の方式として、以下のいずれか1つを選択できます。

- -

このユーティリティを使用するためには、以下のことに注意をしてください。

- -
- -
-

-

testというデータベースをオンライン CLUSTER するには、下記のコマンドを実行します。

-
$ pg_reorg test
-

testという名前のデータベースのfooという1つのテーブルに対してオンライン VACUUM FULL を行うには、下記のコマンドを実行します。

-
$ pg_reorg --no-order --table foo -d test

-

-
- -
-

オプション

-

pg_reorg では、下記の4種類のコマンドライン引数を指定できます。

-
-
- -

connection-options

-

PostgreSQLに接続するためのパラメータです。

- -
-
-
-h host
---host host
-
サーバが稼働しているマシンのホスト名を指定します。ホスト名がスラッシュから始まる場合、Unixドメインソケット用のディレクトリとして使用されます。
- -
-p port
---port port
-
サーバが接続を監視するTCPポートもしくはUnixドメインソケットファイルの拡張子を指定します。
- -
-U username
---username username
-
接続するユーザ名を指定します。
- -
-W
--password
-
データベースに接続する前に、pg_reorg は強制的にパスワード入力を促します。
-
サーバがパスワード認証を要求する場合 pg_reorg は自動的にパスワード入力を促しますので、これが重要になることはありません。 -しかし、pg_reorg は、サーバにパスワードが必要かどうかを判断するための接続試行を無駄に行います。 -こうした余計な接続試行を防ぐために-Wの入力が有意となる場合もあります。
-
-
- -

message-options

-

-pg_reorg を実行した際に任意のメッセージを出力するためのパラメータです。 ---quietと他の2つのオプションを同時に指定した場合は、--quietのオプションは無視されます。 -

- -
-
-e
--echo
-
pg_reorg が生成し、サーバに送るコマンドをエコー表示します。
-
-q
--quiet
-
進行メッセージを表示しません。
-
-v
--verbose
-
処理中に詳細な情報を表示します。
-
- -

order-options

-

pg_reorg を実行する際の並び替えの基準を指定するパラメータです。 -何も指定されていない場合は、cluster index順にオンライン CLUSTER を行います。 -この2つを同時に指定することはできません。 -

- -
-
-n
--no-order
-
オンライン VACUUM FULL の処理を行います。
- -
-o columns [,...]
---order-by columns [,...]
-
指定したカラムをキーにオンライン CLUSTER を行います。
-
- -

target-options

-

-pg_reorg を実行する対象を指定するパラメータです。 ---all--dbnameまたは--tableを同時に指定することはできません。 -

- -
-
-a
--all
-
対象となる全てのデータベースに対してオンライン CLUSTER、または、オンラインVACUUM FULLを行います。
- -
--d dbname
---dbname dbname -
-
オンライン CLUSTER、または、オンライン VACUUM FULL を行うデータベース名を指定します。 -データベース名が指定されておらず、-a(または--all)も指定されていない場合、 -データベース名はPGDATABASE環境変数から読み取られます。この変数も設定されていない場合は、接続時に指定したユーザ名が使用されます。 -
- -
--t table
---table table -
-
オンライン CLUSTER 、または、オンライン VACUUM FULL を行うテーブルを指定します。 -このオプションが指定されていない場合は、対象となったデータベースに存在する全ての対象テーブルに対して処理を行います。 -
-
-
- -
-

環境変数

-
-
- PGDATABASE
- PGHOST
- PGPORT
- PGUSER -
-
デフォルトの接続パラメータです。
-
-
-

また、このユーティリティは、他のほとんどの PostgreSQL ユーティリティと同様、 - libpq でサポートされる環境変数を使用します。詳細については、環境変数の項目を参照してください。 -

-
- -
-

トラブルシューティング

-

pg_reorg の実行に失敗した場合にエラーが表示されます。 -想像されるエラー原因と対処を示します。

-

致命的なエラーで終了した場合、手動によるクリーンアップを行う必要があります。 -クリーンアップは、エラーが発生したデータベースに対して、$PGHOME/share/contrib/uninstall_pg_reorg.sql を実行し、その後、$PGHOME/share/contrib/pg_reorg.sql を実行します。

- -
-
pg_reorg : reorg database "template1" ... skipped
-
--allオプションを指定した際に、pg_reorg がインストールされていないデータベースに対して表示されます。
-
pg_reorg スキーマのインストールを行ってください。
- -
ERROR: pg_reorg is not installed
-
--dbnameで指定したデータベースにpg_reorg がインストールされていません。
-
pg_reorg のインストールを行ってください。
- -
ERROR: relation "table" has no primary key
-
指定したテーブルにPRIMARY KEYが存在していません。
-
対象のテーブルにPRIMARY KEYの作成を行ってください。(ALTER TABLE ADD PRIMARY KEY)
- -
ERROR: relation "table" has no cluster key
-
指定したテーブルに CLUSTER KEYが存在していません。
-
対象のテーブルに CLUSTER KEYの作成を行ってください。(ALTER TABLE CLUSTER)
- -
pg_reorg : query failed: ERROR: column "col" does not exist
-
--order-by で指定したカラムが対象のテーブルに存在していません。
-
対象のテーブルに存在するカラムを指定してください。
- -
ERROR: permission denied for schema reorg
-
操作を行おうとした対象に権限がありません。
-
スーパーユーザで操作を行ってください。
- -
pg_reorg : query failed: ERROR: trigger "z_reorg_trigger" for relation "tbl" already exists
-
操作を行おうとした対象にpg_reorg が処理のために作成するトリガと同名のものが存在しています。
-
トリガの改名か削除を行ってください。
- -
pg_reorg : trigger conflicted for tbl
-
操作を行おうとした対象にpg_reorg が処理のために作成するトリガより後に実行されるトリガが存在しています。
-
トリガの改名か削除を行ってください。
-
-
- -
-

制約

-

pg_reorg を使用する際には、以下の制約があります。以下の制約に関する操作を行った場合の動作は保証されません。注意してください。

- -

一時テーブルへの操作

-

pg_reorg では、一時テーブルは操作の対象外です。

- -

GiSTインデックスの使用

-

インデックス種別がGiSTとなっているインデックスがクラスタインデックスとなっている -テーブルはpg_reorg コマンドを使用して操作を行うことはできません。 -これは、GiSTインデックスのソート順序は一意ではないため、ORDER BYによる -ソートが行えないためです。

- -

DDLコマンドの発行

-

-pg_reorg の実行中には、VACUUMANALYZE 以外 のDDL操作は行わないでください。 -多くの場合、pg_reorg は失敗しロールバックされます。 -しかし、以下の操作ではデータが破損するため、非常に危険です。 -

- -
-
TRUNCATE
-
TRUNCATEにより削除した行が、pg_reorg 実行後には復元しています。操作結果が消失します。
- -
CREATE INDEX
-
CREATE INDEXは、スワップされない索引が残る可能性があります。データの不整合が生じます。
- -
ADD COLUMN
-
ADD COLUMNは、追加された値が全てNULLに置換されてしまう可能性があります。データが消失します。
- -
ALTER COLUMN TYPE
-
ALTER COLUMN TYPEは、実行するとスキーマで定義された型と実際の格納状態に齟齬をきたします。データの不整合が生じます。
- -
ALTER TABLE SET TABLESPACE
-
ALTER TABLE SET TABLE SPACEは、pg_reorg 実行後にrelfilenodeとの不整合が起こるため、対象のテーブルに対する参照/更新操作時にエラーが発生します。
-
-
- -
-

インストール方法

-

pg_reorg のインストールは、標準のcontribモジュールと同様です。

-

ビルド

-

pg_reorg のフォルダを$PGHOME/contrib/に配置し、make, make installを行ってください。

-

データベースへの登録

-

PostgreSQLを起動し、対象のデータベースに対して $PGHOME/share/contrib にある pg_reorg.sql を実行し、インストールを行ってください。

-
- -
-

動作環境

-
-
PostgreSQLバージョン
PostgreSQL 8.3
-
OS
RHEL 5.2, Windows XP SP3
-
ディスク容量
処理対象のテーブル、インデックスサイズの2倍以上のディスク空き容量
-
-
- -
-

関連項目

-clusterdb, -vacuumdb -
- -
- -

-Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION -

- - - + + + +pg_reorg + + + + + + +

pg_reorg

+ +
+

名前

+pg_reorg -- PostgreSQLデータベース内のテーブルに対して、参照/更新処理をブロックせずに再編成を行います。 +
+ +
+ +

概要

+

+pg_reorg [connection-options...] [message-options...] [order-options...] [target-options...] +

+

指定できるオプションには4つのカテゴリがあります。 +詳細はオプションを参照してください。

+
+
connection-options : 接続パラメータ
+
-h [--host] host
+
-p [--port] port
+
-U [--username] username
+
-W [--password]
+
message-options : 出力メッセージ
+
-e [--echo]
+
-q [--quiet]
+
-v [--verbose]
+
order-options : 並び替えの基準
+
-o [--order-by] columns [,...]
+
-n [--no-order]
+
target-options : 処理対象
+
-a [--all]
+
-d [--dbname] dbname
+
-t [--table] table
+
+
+ +
+

説明

+

pg_reorg は、PostgreSQLデータベース内のテーブルを再編成(行の並び替え)するユーティリティです。 +clusterdb と異なり、参照/更新処理をブロックしません。 +再編成の方式として、以下のいずれか1つを選択できます。

+
    +
  • オンライン CLUSTER (cluster index順に行を並び替える)
  • +
  • ユーザの指定した順に行を並び替える
  • +
  • オンライン VACUUM FULL (行の詰め合わせを行う)
  • +
+

このユーティリティを使用するためには、以下のことに注意をしてください。

+
    +
  • このユーティリティは、スーパーユーザのみが実行することができます。
  • +
  • 対象のテーブルはPRIMARY KEYを持っている必要があります。
  • +
  • pg_reorg 実行後のデータの状態は、統計情報に反映されていません。統計情報を最新化するため、pg_reorg 実行後にANALYZEを実行してください。
  • +
+
+ +
+

+

testというデータベースをオンライン CLUSTER するには、下記のコマンドを実行します。

+
$ pg_reorg test
+

testという名前のデータベースのfooという1つのテーブルに対してオンライン VACUUM FULL を行うには、下記のコマンドを実行します。

+
$ pg_reorg --no-order --table foo -d test

+

+
+ +
+

オプション

+

pg_reorg では、下記の4種類のコマンドライン引数を指定できます。

+
+
+ +

connection-options

+

PostgreSQLに接続するためのパラメータです。

+ +
+
+
-h host
+--host host
+
サーバが稼働しているマシンのホスト名を指定します。ホスト名がスラッシュから始まる場合、Unixドメインソケット用のディレクトリとして使用されます。
+ +
-p port
+--port port
+
サーバが接続を監視するTCPポートもしくはUnixドメインソケットファイルの拡張子を指定します。
+ +
-U username
+--username username
+
接続するユーザ名を指定します。
+ +
-W
--password
+
データベースに接続する前に、pg_reorg は強制的にパスワード入力を促します。
+
サーバがパスワード認証を要求する場合 pg_reorg は自動的にパスワード入力を促しますので、これが重要になることはありません。 +しかし、pg_reorg は、サーバにパスワードが必要かどうかを判断するための接続試行を無駄に行います。 +こうした余計な接続試行を防ぐために-Wの入力が有意となる場合もあります。
+
+
+ +

message-options

+

+pg_reorg を実行した際に任意のメッセージを出力するためのパラメータです。 +--quietと他の2つのオプションを同時に指定した場合は、--quietのオプションは無視されます。 +

+ +
+
-e
--echo
+
pg_reorg が生成し、サーバに送るコマンドをエコー表示します。
+
-q
--quiet
+
進行メッセージを表示しません。
+
-v
--verbose
+
処理中に詳細な情報を表示します。
+
+ +

order-options

+

pg_reorg を実行する際の並び替えの基準を指定するパラメータです。 +何も指定されていない場合は、cluster index順にオンライン CLUSTER を行います。 +この2つを同時に指定することはできません。 +

+ +
+
-n
--no-order
+
オンライン VACUUM FULL の処理を行います。
+ +
-o columns [,...]
+--order-by columns [,...]
+
指定したカラムをキーにオンライン CLUSTER を行います。
+
+ +

target-options

+

+pg_reorg を実行する対象を指定するパラメータです。 +--all--dbnameまたは--tableを同時に指定することはできません。 +

+ +
+
-a
--all
+
対象となる全てのデータベースに対してオンライン CLUSTER、または、オンラインVACUUM FULLを行います。
+ +
+-d dbname
+--dbname dbname +
+
オンライン CLUSTER、または、オンライン VACUUM FULL を行うデータベース名を指定します。 +データベース名が指定されておらず、-a(または--all)も指定されていない場合、 +データベース名はPGDATABASE環境変数から読み取られます。この変数も設定されていない場合は、接続時に指定したユーザ名が使用されます。 +
+ +
+-t table
+--table table +
+
オンライン CLUSTER 、または、オンライン VACUUM FULL を行うテーブルを指定します。 +このオプションが指定されていない場合は、対象となったデータベースに存在する全ての対象テーブルに対して処理を行います。 +
+
+
+ +
+

環境変数

+
+
+ PGDATABASE
+ PGHOST
+ PGPORT
+ PGUSER +
+
デフォルトの接続パラメータです。
+
+
+

また、このユーティリティは、他のほとんどの PostgreSQL ユーティリティと同様、 + libpq でサポートされる環境変数を使用します。詳細については、環境変数の項目を参照してください。 +

+
+ +
+

トラブルシューティング

+

pg_reorg の実行に失敗した場合にエラーが表示されます。 +想像されるエラー原因と対処を示します。

+

致命的なエラーで終了した場合、手動によるクリーンアップを行う必要があります。 +クリーンアップは、エラーが発生したデータベースに対して、$PGHOME/share/contrib/uninstall_pg_reorg.sql を実行し、その後、$PGHOME/share/contrib/pg_reorg.sql を実行します。

+ +
+
pg_reorg : reorg database "template1" ... skipped
+
--allオプションを指定した際に、pg_reorg がインストールされていないデータベースに対して表示されます。
+
pg_reorg スキーマのインストールを行ってください。
+ +
ERROR: pg_reorg is not installed
+
--dbnameで指定したデータベースにpg_reorg がインストールされていません。
+
pg_reorg のインストールを行ってください。
+ +
ERROR: relation "table" has no primary key
+
指定したテーブルにPRIMARY KEYが存在していません。
+
対象のテーブルにPRIMARY KEYの作成を行ってください。(ALTER TABLE ADD PRIMARY KEY)
+ +
ERROR: relation "table" has no cluster key
+
指定したテーブルに CLUSTER KEYが存在していません。
+
対象のテーブルに CLUSTER KEYの作成を行ってください。(ALTER TABLE CLUSTER)
+ +
pg_reorg : query failed: ERROR: column "col" does not exist
+
--order-by で指定したカラムが対象のテーブルに存在していません。
+
対象のテーブルに存在するカラムを指定してください。
+ +
ERROR: permission denied for schema reorg
+
操作を行おうとした対象に権限がありません。
+
スーパーユーザで操作を行ってください。
+ +
pg_reorg : query failed: ERROR: trigger "z_reorg_trigger" for relation "tbl" already exists
+
操作を行おうとした対象にpg_reorg が処理のために作成するトリガと同名のものが存在しています。
+
トリガの改名か削除を行ってください。
+ +
pg_reorg : trigger conflicted for tbl
+
操作を行おうとした対象にpg_reorg が処理のために作成するトリガより後に実行されるトリガが存在しています。
+
トリガの改名か削除を行ってください。
+
+
+ +
+

制約

+

pg_reorg を使用する際には、以下の制約があります。以下の制約に関する操作を行った場合の動作は保証されません。注意してください。

+ +

一時テーブルへの操作

+

pg_reorg では、一時テーブルは操作の対象外です。

+ +

GiSTインデックスの使用

+

インデックス種別がGiSTとなっているインデックスがクラスタインデックスとなっている +テーブルはpg_reorg コマンドを使用して操作を行うことはできません。 +これは、GiSTインデックスのソート順序は一意ではないため、ORDER BYによる +ソートが行えないためです。

+ +

DDLコマンドの発行

+

+pg_reorg の実行中には、VACUUMANALYZE 以外 のDDL操作は行わないでください。 +多くの場合、pg_reorg は失敗しロールバックされます。 +しかし、以下の操作ではデータが破損するため、非常に危険です。 +

+ +
+
TRUNCATE
+
削除した行が pg_reorg 実行後には復元しています。操作結果が消失します。
+ +
CREATE INDEX
+
スワップされない索引が残る可能性があります。データの不整合が生じます。
+ +
ALTER TABLE ... ADD COLUMN
+
追加された値が全てNULLに置換されてしまう可能性があります。データが消失します。
+ +
ALTER TABLE ... ALTER COLUMN TYPE
+
実行するとスキーマで定義された型と実際の格納状態に齟齬をきたします。データの不整合が生じます。
+ +
ALTER TABLE ... SET TABLESPACE
+
pg_reorg 実行後にrelfilenodeとの不整合が起こるため、対象のテーブルに対する参照/更新操作時にエラーが発生します。
+
+
+ +
+

インストール方法

+

pg_reorg のインストールは、標準のcontribモジュールと同様です。

+

ビルド

+

pg_reorg のフォルダを$PGHOME/contrib/に配置し、make, make installを行ってください。

+

データベースへの登録

+

PostgreSQLを起動し、対象のデータベースに対して $PGHOME/share/contrib にある pg_reorg.sql を実行し、インストールを行ってください。

+
+ +
+

動作環境

+
+
PostgreSQLバージョン
PostgreSQL 8.3
+
OS
RHEL 5.2, Windows XP SP3
+
ディスク容量
処理対象のテーブル、インデックスサイズの2倍以上のディスク空き容量
+
+
+ +
+

関連項目

+clusterdb, +vacuumdb +
+ +
+ +

+Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +

+ + + diff --git a/doc/pg_reorg.html b/doc/pg_reorg.html index e82d44d..09f16b3 100755 --- a/doc/pg_reorg.html +++ b/doc/pg_reorg.html @@ -1,275 +1,275 @@ - - - -pg_reorg - - - - - - -

pg_reorg

- -
-

Name

-pg_reorg -- Reorganize tables in PostgreSQL databases without any locks. -
- -
-

Synopsis

-

-pg_reorg [connection-options...] [message-options...] [order-options...] [target-options...] -

-

There 4 option categories. -See also options for details.

-
-
connection-options
-
-h [--host] host
-
-p [--port] port
-
-U [--username] username
-
-W [--password]
-
message-options
-
-e [--echo]
-
-q [--quiet]
-
-v [--verbose]
-
order-options
-
-o [--order-by] columns [,...]
-
-n [--no-order]
-
target-options
-
-a [--all]
-
-d [--dbname] dbname
-
-t [--table] table
-
-
- -
- -

Description

-

pg_reorg is an utility program to reorganize tables in PostgreSQL databases. -Unlike clusterdb, it doesn't block any selections and updates during reorganization. -You can choose one of the following methods to reorganize.

-
    -
  • Online CLUSTER (ordered by cluster index)
  • -
  • Ordered by specified columns
  • -
  • Online VACUUM FULL (packing rows only)
  • -
-

NOTICE:

-
    -
  • Only superusers can use the utility.
  • -
  • Target table must have PRIMARY KEY.
  • -
  • You'd better to do ANALYZE after pg_reorg is completed.
  • -
-
- -
-

Examples

-

Execute the following command to do online CLUSTER to all tables in test database.

-
$ pg_reorg test
-

Execute the following command to do online VACUUM FULL to foo table in test database.

-
$ pg_reorg --no-order --table foo -d test

-

- -
-

Options

-

pg_reorg has command line options in 4 categolies.

-
-
- -

connection-options

-

Parameters to connect PostgreSQL.

- -
-
-
-h host
---host host
-
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket.
- -
-p port
---port port
-
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.
- -
-U username
---username username
-
User name to connect as.
- -
-W
--password
-
Force pg_reorg to prompt for a password before connecting to a database.
-
This option is never essential, since pg_reorg will automatically prompt for a password if the server demands password authentication. However, vacuumdb will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.
-
-
- -

message-options

-

Specifies message output by pg_reorg. ---quiet is ignored if some of the other options are specified.

- -
-
-e
--echo
-
Echo the commands that pg_reorg generates and sends to the server.
-
-q
--quiet
-
Do not display progress messages.
-
-v
--verbose
-
Print detailed information during processing.
-
- -

order-options

-

Options to order rows. -If not specified, pg_reorg do online CLUSTER using cluster indexes. -Only one option can be specified. -

- -
-
-n
--no-order
-
Do online VACUUM FULL.
- -
-o columns [,...]
---order-by columns [,...]
-
Do online CLUSTER ordered by specified columns.
-
- - -

target-options

-

-Options to specify target tables or databases. -You cannot use --all and --dbname or --table together. -

- -
-
-a
--all
-
Reorganize all databases.
- -
--d dbname
---dbname dbname -
-
Specifies the name of the database to be reorganized. If this is not specified and -a (or --all) is not used, the database name is read from the environment variable PGDATABASE. If that is not set, the user name specified for the connection is used.
- -
--t table
---table table -
-
Reorganize table only. If you don't specify this option, all tables in specified databases are reorganized.
-
-
- -
-

Environment

-
-
- PGDATABASE
- PGHOST
- PGPORT
- PGUSER -
-
Default connection parameters
-
-
-

This utility, like most other PostgreSQL utilities, also uses the environment variables supported by libpq (see Environment Variables).

-
- -
-

Diagnostics

-

Error messages are reported when pg_reorg fails. -The following list shows the cause of errors.

-

You need to cleanup by hand after fatal erros. -To cleanup, execute $PGHOME/share/contrib/uninstall_pg_reorg.sql to the database where the error occured and then execute $PGHOME/share/contrib/pg_reorg.sql. (Do uninstall and reinstall.)

- -
-
pg_reorg : reorg database "template1" ... skipped
-
pg_reorg is not installed in the database when --all option is specified.
-
Do register pg_reorg to the database.
- -
ERROR: pg_reorg is not installed
-
pg_reorg is not installed in the database specified by --dbname.
-
Do register pg_reorg to the database.
- -
ERROR: relation "table" has no primary key
-
The target table doesn't have PRIMARY KEY.
-
Define PRIMARY KEY to the table. (ALTER TABLE ADD PRIMARY KEY)
- -
ERROR: relation "table" has no cluster key
-
The target table doesn't have CLUSTER KEY.
-
Define CLUSTER KEY to the table. (ALTER TABLE CLUSTER)
- -
pg_reorg : query failed: ERROR: column "col" does not exist
-
The target table doesn't have columns specified by --order-by option.
-
Specify existing columns.
- -
ERROR: permission denied for schema reorg
-
Permission error.
-
pg_reorg must be executed by superusers.
- -
pg_reorg : query failed: ERROR: trigger "z_reorg_trigger" for relation "tbl" already exists
-
The target table already has a trigger named "z_reorg_trigger".
-
Delete or rename the trigger.
- -
pg_reorg : trigger conflicted for tbl
-
The target table already has a trigger which follows by "z_reorg_trigger" in alphabetical order.
-
Delete or rename the trigger.
-
-
- -
-

Restrictions

-

pg_reorg has the following restrictions. -Be careful to avoid data corruptions.

- -

Temp tables

-

pg_reorg cannot reorganize temp tables.

- -

GiST indexes

-

pg_reorg cannot reorganize tables using GiST indexes.

- -

DDL commands

-

You cannot do DDL commands except VACUUM and ANALYZE during pg_reorg. -In many case pg_reorg would fail and rollback collectly, but there are some cases ending with data-corruption .

- -
-
TRUNCATE
-
TRUNCATE is lost. Deleted rows still exist after pg_reorg.
- -
CREATE INDEX
-
CREATE INDEX causes index corruptions.
- -
ADD COLUMN
-
ADD COLUMN causes lost of data. Newly added columns are initialized with NULLs.
- -
ALTER COLUMN TYPE
-
ALTER COLUMN TYPE causes data corruptions.
- -
ALTER TABLE SET TABLESPACE
-
ALTER TABLE SET TABLE SPACE causes data corruptions by wrong relfilenode.
-
-
- -
-

Installations

-

pg_reorg can be installed like standard contrib modules.

-

Build from source

-

Place pg_reorg to $PGHOME/contrib/ and input make, make install.

-

Register to database

-

Start PostgreSQL and execute pg_reorg.sql in $PGHOME/share/contrib.

-
- -
-

Requirements

-
-
PostgreSQL version
PostgreSQL 8.3
-
OS
RHEL 5.2, Windows XP SP3
-
Disks
Requires amount of disks twice larger than target table and indexes.
-
-
- -
-

See Also

-clusterdb, -vacuumdb -
- -
- -

-Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION -

- - - + + + +pg_reorg + + + + + + +

pg_reorg

+ +
+

Name

+pg_reorg -- Reorganize tables in PostgreSQL databases without any locks. +
+ +
+

Synopsis

+

+pg_reorg [connection-options...] [message-options...] [order-options...] [target-options...] +

+

There 4 option categories. +See also options for details.

+
+
connection-options
+
-h [--host] host
+
-p [--port] port
+
-U [--username] username
+
-W [--password]
+
message-options
+
-e [--echo]
+
-q [--quiet]
+
-v [--verbose]
+
order-options
+
-o [--order-by] columns [,...]
+
-n [--no-order]
+
target-options
+
-a [--all]
+
-d [--dbname] dbname
+
-t [--table] table
+
+
+ +
+ +

Description

+

pg_reorg is an utility program to reorganize tables in PostgreSQL databases. +Unlike clusterdb, it doesn't block any selections and updates during reorganization. +You can choose one of the following methods to reorganize.

+
    +
  • Online CLUSTER (ordered by cluster index)
  • +
  • Ordered by specified columns
  • +
  • Online VACUUM FULL (packing rows only)
  • +
+

NOTICE:

+
    +
  • Only superusers can use the utility.
  • +
  • Target table must have PRIMARY KEY.
  • +
  • You'd better to do ANALYZE after pg_reorg is completed.
  • +
+
+ +
+

Examples

+

Execute the following command to do online CLUSTER to all tables in test database.

+
$ pg_reorg test
+

Execute the following command to do online VACUUM FULL to foo table in test database.

+
$ pg_reorg --no-order --table foo -d test

+

+ +
+

Options

+

pg_reorg has command line options in 4 categolies.

+
+
+ +

connection-options

+

Parameters to connect PostgreSQL.

+ +
+
+
-h host
+--host host
+
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket.
+ +
-p port
+--port port
+
Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.
+ +
-U username
+--username username
+
User name to connect as.
+ +
-W
--password
+
Force pg_reorg to prompt for a password before connecting to a database.
+
This option is never essential, since pg_reorg will automatically prompt for a password if the server demands password authentication. However, vacuumdb will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.
+
+
+ +

message-options

+

Specifies message output by pg_reorg. +--quiet is ignored if some of the other options are specified.

+ +
+
-e
--echo
+
Echo the commands that pg_reorg generates and sends to the server.
+
-q
--quiet
+
Do not display progress messages.
+
-v
--verbose
+
Print detailed information during processing.
+
+ +

order-options

+

Options to order rows. +If not specified, pg_reorg do online CLUSTER using cluster indexes. +Only one option can be specified. +

+ +
+
-n
--no-order
+
Do online VACUUM FULL.
+ +
-o columns [,...]
+--order-by columns [,...]
+
Do online CLUSTER ordered by specified columns.
+
+ + +

target-options

+

+Options to specify target tables or databases. +You cannot use --all and --dbname or --table together. +

+ +
+
-a
--all
+
Reorganize all databases.
+ +
+-d dbname
+--dbname dbname +
+
Specifies the name of the database to be reorganized. If this is not specified and -a (or --all) is not used, the database name is read from the environment variable PGDATABASE. If that is not set, the user name specified for the connection is used.
+ +
+-t table
+--table table +
+
Reorganize table only. If you don't specify this option, all tables in specified databases are reorganized.
+
+
+ +
+

Environment

+
+
+ PGDATABASE
+ PGHOST
+ PGPORT
+ PGUSER +
+
Default connection parameters
+
+
+

This utility, like most other PostgreSQL utilities, also uses the environment variables supported by libpq (see Environment Variables).

+
+ +
+

Diagnostics

+

Error messages are reported when pg_reorg fails. +The following list shows the cause of errors.

+

You need to cleanup by hand after fatal erros. +To cleanup, execute $PGHOME/share/contrib/uninstall_pg_reorg.sql to the database where the error occured and then execute $PGHOME/share/contrib/pg_reorg.sql. (Do uninstall and reinstall.)

+ +
+
pg_reorg : reorg database "template1" ... skipped
+
pg_reorg is not installed in the database when --all option is specified.
+
Do register pg_reorg to the database.
+ +
ERROR: pg_reorg is not installed
+
pg_reorg is not installed in the database specified by --dbname.
+
Do register pg_reorg to the database.
+ +
ERROR: relation "table" has no primary key
+
The target table doesn't have PRIMARY KEY.
+
Define PRIMARY KEY to the table. (ALTER TABLE ADD PRIMARY KEY)
+ +
ERROR: relation "table" has no cluster key
+
The target table doesn't have CLUSTER KEY.
+
Define CLUSTER KEY to the table. (ALTER TABLE CLUSTER)
+ +
pg_reorg : query failed: ERROR: column "col" does not exist
+
The target table doesn't have columns specified by --order-by option.
+
Specify existing columns.
+ +
ERROR: permission denied for schema reorg
+
Permission error.
+
pg_reorg must be executed by superusers.
+ +
pg_reorg : query failed: ERROR: trigger "z_reorg_trigger" for relation "tbl" already exists
+
The target table already has a trigger named "z_reorg_trigger".
+
Delete or rename the trigger.
+ +
pg_reorg : trigger conflicted for tbl
+
The target table already has a trigger which follows by "z_reorg_trigger" in alphabetical order.
+
Delete or rename the trigger.
+
+
+ +
+

Restrictions

+

pg_reorg has the following restrictions. +Be careful to avoid data corruptions.

+ +

Temp tables

+

pg_reorg cannot reorganize temp tables.

+ +

GiST indexes

+

pg_reorg cannot reorganize tables using GiST indexes.

+ +

DDL commands

+

You cannot do DDL commands except VACUUM and ANALYZE during pg_reorg. +In many case pg_reorg would fail and rollback collectly, but there are some cases ending with data-corruption .

+ +
+
TRUNCATE
+
TRUNCATE is lost. Deleted rows still exist after pg_reorg.
+ +
CREATE INDEX
+
It causes index corruptions.
+ +
ALTER TABLE ... ADD COLUMN
+
It causes lost of data. Newly added columns are initialized with NULLs.
+ +
ALTER TABLE ... ALTER COLUMN TYPE
+
It causes data corruptions.
+ +
ALTER TABLE ... SET TABLESPACE
+
It causes data corruptions by wrong relfilenode.
+
+
+ +
+

Installations

+

pg_reorg can be installed like standard contrib modules.

+

Build from source

+

Place pg_reorg to $PGHOME/contrib/ and input make, make install.

+

Register to database

+

Start PostgreSQL and execute pg_reorg.sql in $PGHOME/share/contrib.

+
+ +
+

Requirements

+
+
PostgreSQL version
PostgreSQL 8.3
+
OS
RHEL 5.2, Windows XP SP3
+
Disks
Requires amount of disks twice larger than target table and indexes.
+
+
+ +
+

See Also

+clusterdb, +vacuumdb +
+ +
+ +

+Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION +

+ + + diff --git a/lib/pg_reorg.sql.in b/lib/pg_reorg.sql.in index 37b045c..59cb0c5 100755 --- a/lib/pg_reorg.sql.in +++ b/lib/pg_reorg.sql.in @@ -114,7 +114,7 @@ CREATE VIEW reorg.tables AS reorg.get_create_trigger(R.oid, PK.indexrelid) AS create_trigger, 'CREATE TABLE reorg.table_' || R.oid || ' WITH (' || array_to_string(array_append(R.reloptions, 'oids=' || CASE WHEN R.relhasoids THEN 'true' ELSE 'false' END), ',') || ') TABLESPACE ' || coalesce(quote_ident(S.spcname), 'pg_default') || ' AS SELECT * FROM ONLY ' || reorg.oid2text(R.oid) AS create_table, 'DELETE FROM reorg.log_' || R.oid AS delete_log, - 'LOCK TABLE ' || reorg.oid2text(R.oid) || ' IN ACCESS EXCLUSIVE MODE' AS lock_table, + 'LOCK TABLE ' || reorg.oid2text(R.oid) || ' IN ACCESS EXCLUSIVE MODE NOWAIT' AS lock_table, reorg.get_index_keys(CK.indexrelid, R.oid) AS ckey, 'SELECT * FROM reorg.log_' || R.oid || ' ORDER BY id LIMIT $1' AS sql_peek, 'INSERT INTO reorg.table_' || R.oid || ' VALUES ($1.*)' AS sql_insert,