Postgres の rule システム

Postgres supports a powerful rule system for the specification of views and ambiguous view updates. Originally the Postgres rule system consisted of two implementations:

Postgresview およびいずれとも解釈される view update の仕様 を決定する強力な rule システムを支援しています。当初から Postgres の rule システムは二つの実装から なっていました。

For information on the syntax and creation of rules in the Postgres system refer to The PostgreSQL User's Guide.

Postgres の構文と rule 作成に関しては The PostgreSQL User's Guide を参照して 下さい。

書き換えシステム

The query rewrite system is a module between the parser stage and the planner/optimizer. It processes the tree handed back by the parser stage (which represents a user query) and if there is a rule present that has to be applied to the query it rewrites the tree to an alternate form.

問合せ書き換えシステムはパーサステージと プランナ/オプティマイザの間に位置するモジュールです。このモジュールは (ユーザの問合せを表す)パーサレベルによって返されるツリーの処理を行い、 その問合せに適用されるべき rule が存在する場合、そのツリーを別の形式に 書き換えます。

view を実装する技術

Now we will sketch the algorithm of the query rewrite system. For better illustration we show how to implement views using rules as an example.

ここで問合せ書き換えシステムのアルゴリズムの概略を見てみることに します。よりわかりやすく図示するための例として rule を用いた view の実装を示します。

Let the following rule be given:

下記の rule が与えられたと仮定します。

  create rule view_rule
  as on select 
  to test_view
  do instead
     select s.sname, p.pname
     from supplier s, sells se, part p
     where s.sno = se.sno and
           p.pno = se.pno;   
      

The given rule will be fired whenever a select against the relation test_view is detected. Instead of selecting the tuples from test_view the select statement given in the action part of the rule is executed.

与えられた rule はリレーション test_view に対する select が検出されたとき起動されます。 test_view からタプルを select するのではなく、rule の アクション部 で与えられた select 文が実行されます。

Let the following user-query against test_view be given:

下記の test_view に対するユーザ問合せが与えられたと 仮定します。

  select sname 
  from test_view
  where sname <> 'Smith';
      

Here is a list of the steps performed by the query rewrite system whenever a user-query against test_view appears. (The following listing is a very informal description of the algorithm just intended for basic understanding. For a detailed description refer to Stonebraker et al, 1989).

ここに test_view に対するユーザ問合せがあった時に 問合せ書き換えシステムが行う一連の作業を示します。(下記のリストは 基本的な理解のためのもの便宜的アルゴリズムです。詳細については Stonebraker et al, 1989 を参照して下さい。

test_view 書き換え

  1. Take the query given in the action part of the rule.

    rule の アクション部で与えられた問合せの取得

  2. Adapt the targetlist to meet the number and order of attributes given in the user-query.

    ユーザ問合せで与えられた属性の数値と優先度に見合うターゲット リストの適用

  3. Add the qualification given in the where clause of the user-query to the qualification of the query given in the action part of the rule.

    ユーザ問合せの where 句による条件の rule のアクション 部で与えられる問合せの条件への追加

Given the rule definition above, the user-query will be rewritten to the following form (Note that the rewriting is done on the internal representation of the user-query handed back by the parser stage but the derived new data structure will represent the following query):

上記で与えられた rule の定義により、ユーザ問い合わせは以下の形式 に書き換えられます。(書き換えはパーサステージによって引き渡される ユーザ問合せの内部表現として行われますが、派生した新規のデータ構造 は以下の問合せとなります。)

  select s.sname
  from supplier s, sells se, part p
  where s.sno = se.sno and
        p.pno = se.pno and
        s.sname <> 'Smith';