バージョンごとのドキュメント一覧

8.19. オブジェクト識別子データ型 #

<title>Object Identifier Types</title>

Object identifiers (OIDs) are used internally by <productname>PostgreSQL</productname> as primary keys for various system tables. Type <type>oid</type> represents an object identifier. There are also several alias types for <type>oid</type>, each named <type>reg<replaceable>something</replaceable></type>. <xref linkend="datatype-oid-table"/> shows an overview. オブジェクト識別子(OID)はPostgreSQLの内部で様々なシステムテーブルの主キーとして使用されます。 oidデータ型はオブジェクト識別子を表します。 oidには別名型もいくつかあります。 reg何とかとそれぞれ名付けられたoidの様々なエイリアスの型は表 8.26からその概要を見ることができます。

The <type>oid</type> type is currently implemented as an unsigned four-byte integer. Therefore, it is not large enough to provide database-wide uniqueness in large databases, or even in large individual tables. oidデータ型は現在、符号なし4バイト整数として実装されています。 このため、大きなデータベース内でデータベース単位での一意性や個別の大きなテーブルで一意性を提供するためには十分な大きさではありません。

The <type>oid</type> type itself has few operations beyond comparison. It can be cast to integer, however, and then manipulated using the standard integer operators. (Beware of possible signed-versus-unsigned confusion if you do this.) oidデータ型自体は、比較以外の演算はほとんど行いません。 しかし、整数としてキャストすることもでき、その場合標準の整数演算子を使用して操作することができます。 (これを行うと、符号付きと符号なしの間で混乱が起きかねないことに注意してください。)

The OID alias types have no operations of their own except for specialized input and output routines. These routines are able to accept and display symbolic names for system objects, rather than the raw numeric value that type <type>oid</type> would use. The alias types allow simplified lookup of OID values for objects. For example, to examine the <structname>pg_attribute</structname> rows related to a table <literal>mytable</literal>, one could write: OIDの別名データ型は、専用の入出力ルーチン以外には演算を行いません。 これらのルーチンでは、oid型が使用するような未加工の数値ではなく、システムオブジェクト用のシンボル名を受け入れたり表示したりできます。 別名データ型により、オブジェクトのOID値の検索が簡単になります。 例えば、mytableテーブルに関連したpg_attribute行を確認するには、以下のように記述することができます。

SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;

rather than: 次のように記述する必要はありません。

SELECT * FROM pg_attribute
  WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');

While that doesn't look all that bad by itself, it's still oversimplified. A far more complicated sub-select would be needed to select the right OID if there are multiple tables named <literal>mytable</literal> in different schemas. The <type>regclass</type> input converter handles the table lookup according to the schema path setting, and so it does the <quote>right thing</quote> automatically. Similarly, casting a table's OID to <type>regclass</type> is handy for symbolic display of a numeric OID. 後者もそう悪くないように見えますが、これは過度に単純化されています。 異なるスキーマにmytableテーブルが複数ある場合には、正しいOIDを選択するために、より複雑なsub-SELECTが必要となります。 regclass入力変換ではスキーマパスの設定に従ってテーブル検索を扱いますので、自動的に正しい検索を行います。 同様に、テーブルのOIDをregclassにキャストすることは、数値のOIDのシンボル表示に便利です。

表8.26 オブジェクト識別子データ型

<title>Object Identifier Types</title>
型名参照説明値の例
oidすべて数値オブジェクト識別子564182
regclasspg_classリレーション名pg_type
regcollationpg_collation照合名"POSIX"
regconfigpg_ts_configテキスト検索設定english
regdictionarypg_ts_dictテキスト検索辞書simple
regnamespacepg_namespace名前空間名pg_catalog
regoperpg_operator演算子名+
regoperatorpg_operator引数の型を持つ演算子*(integer,​integer) or -(NONE,​integer)
regprocpg_proc関数名sum
regprocedurepg_proc引数の型を持つ関数sum(int4)
regrolepg_authidロール名smithee
regtypepg_typeデータ型の名前integer

All of the OID alias types for objects that are grouped by namespace accept schema-qualified names, and will display schema-qualified names on output if the object would not be found in the current search path without being qualified. For example, <literal>myschema.mytable</literal> is acceptable input for <type>regclass</type> (if there is such a table). That value might be output as <literal>myschema.mytable</literal>, or just <literal>mytable</literal>, depending on the current search path. The <type>regproc</type> and <type>regoper</type> alias types will only accept input names that are unique (not overloaded), so they are of limited use; for most uses <type>regprocedure</type> or <type>regoperator</type> are more appropriate. For <type>regoperator</type>, unary operators are identified by writing <literal>NONE</literal> for the unused operand. 名前空間でグループ化されたオブジェクトのOID別名型はすべてスキーマ修飾名を受け入れ、出力時にスキーマ修飾名を表示します。 ただし、現在の検索パスでオブジェクトが見つけられなければ、修飾せずに出力します。 例えば、myschema.mytableregclassという入力を(そのようなテーブルがあれば)許容します。 この値の出力は現在の検索パス次第でmyschema.mytableもしくは単にmytableと出力されるでしょう。 regprocregoper別名型は、一意な(オーバーロードしていない)名前のみを入力として受け入れるため、これらの使用には限度があります。 ほとんどの場合、regprocedureまたはregoperatorを使用するのが適切です。 regoperatorの場合、単項演算子は未使用のオペランドをNONEと記述することによって指定されます。

The input functions for these types allow whitespace between tokens, and will fold upper-case letters to lower case, except within double quotes; this is done to make the syntax rules similar to the way object names are written in SQL. Conversely, the output functions will use double quotes if needed to make the output be a valid SQL identifier. For example, the OID of a function named <literal>Foo</literal> (with upper case <literal>F</literal>) taking two integer arguments could be entered as <literal>' "Foo" ( int, integer ) '::regprocedure</literal>. The output would look like <literal>"Foo"(integer,integer)</literal>. Both the function name and the argument type names could be schema-qualified, too. これらの型の入力を許容する関数はトークンの間に空白を入れることを許容し、二重引用符で囲まれたものを除き大文字は小文字に折りたたみます。 これはオブジェクト名がSQLで記述される方法と同じような文法のルールとするための動作です。 逆に出力する関数は有効なSQL識別子となるように必要に応じて二重引用符を使用します。 例えば、Foo(Fが大文字)という2つの整数型の引数を持つ関数のOIDは' "Foo" ( int, integer ) '::regprocedureとして入力できます。 出力は"Foo"(integer,integer)のようになります。 関数名も引数の型名も共にスキーマ修飾することもできます。

Many built-in <productname>PostgreSQL</productname> functions accept the OID of a table, or another kind of database object, and for convenience are declared as taking <type>regclass</type> (or the appropriate OID alias type). This means you do not have to look up the object's OID by hand, but can just enter its name as a string literal. For example, the <function>nextval(regclass)</function> function takes a sequence relation's OID, so you could call it like this: PostgreSQLに組み込まれている多くの関数はテーブルやそれ以外の種類のデータベースオブジェクトのOIDを受け入れ、利便性のために regclass(もしくは適切なOIDのエイリアスである型)を取るものとして定義されています。 これはオブジェクトのOIDをわざわざ手動で調べる必要が無く、単にその名前を文字列として入力すれば良いことを意味します。 例えば、 nextval(regclass)関数はシーケンスリレーションのOIDを引数に取りますが、このように呼び出すことができます。


nextval('foo')              <lineannotation>operates on sequence <literal>foo</literal></lineannotation>
nextval('FOO')              <lineannotation>same as above</lineannotation>
nextval('"Foo"')            <lineannotation>operates on sequence <literal>Foo</literal></lineannotation>
nextval('myschema.foo')     <lineannotation>operates on <literal>myschema.foo</literal></lineannotation>
nextval('"myschema".foo')   <lineannotation>same as above</lineannotation>
nextval('foo')              <lineannotation>searches search path for <literal>foo</literal></lineannotation>

nextval('foo')              シーケンスfooへの操作
nextval('FOO')              上と同じ
nextval('"Foo"')            シーケンスFooへの操作
nextval('myschema.foo')     myschema.fooへの操作
nextval('"myschema".foo')   上と同じ
nextval('foo')              fooのサーチパスの検索

注記

When you write the argument of such a function as an unadorned literal string, it becomes a constant of type <type>regclass</type> (or the appropriate type). Since this is really just an OID, it will track the originally identified object despite later renaming, schema reassignment, etc. This <quote>early binding</quote> behavior is usually desirable for object references in column defaults and views. But sometimes you might want <quote>late binding</quote> where the object reference is resolved at run time. To get late-binding behavior, force the constant to be stored as a <type>text</type> constant instead of <type>regclass</type>: そのような関数の引数を装飾のない文字列として記載した場合、それはregclass型(もしくは適切な型)の定数になります。 これは実際には単にOIDなので、スキーマの再割り当てなどで後からリネームされたとしても最初に識別されたオブジェクトを追跡します。 この早期バインディング(early binding)の動作は列のデフォルトを参照する列やビューにとっては望ましい動作です。 しかし、オブジェクトの参照を実行時に行う遅延バインディング(late binding)が望ましいこともあります。 遅延バインディングの動作とするためには、定数はregclassの代わりにtextの定数として配置してください。

nextval('foo'::text)      foo is looked up at runtime

The <function>to_regclass()</function> function and its siblings can also be used to perform run-time lookups. See <xref linkend="functions-info-catalog-table"/>. to_regclass()関数とその兄弟は実行時に参照させるために使用することもできます。 詳細は表 9.72を参照ください。

Another practical example of use of <type>regclass</type> is to look up the OID of a table listed in the <literal>information_schema</literal> views, which don't supply such OIDs directly. One might for example wish to call the <function>pg_relation_size()</function> function, which requires the table OID. Taking the above rules into account, the correct way to do that is regclassのもう一つの実用的な使用例はそのようなOIDを直接提供しないinformation_schemaビューにリストされたテーブルのOIDを参照することです。 例えば、テーブルのOIDを必要とするpg_relation_size()関数を呼び出したい場合を考えます。 上記のルールを考慮すると、正しい方法は以下のとおりです。

SELECT table_schema, table_name,
       pg_relation_size((quote_ident(table_schema) || '.' ||
                         quote_ident(table_name))::regclass)
FROM information_schema.tables
WHERE ...

The <function>quote_ident()</function> function will take care of double-quoting the identifiers where needed. The seemingly easier quote_ident()関数は必要に応じて二重引用符をつけます。 より簡単そうに思われる

SELECT pg_relation_size(table_name)
FROM information_schema.tables
WHERE ...

is <emphasis>not recommended</emphasis>, because it will fail for tables that are outside your search path or have names that require quoting. という方法は推奨されません。 テーブルがサーチパスの範囲外にあったり、引用符付けを必要とする名前であった場合に失敗するためです。

An additional property of most of the OID alias types is the creation of dependencies. If a constant of one of these types appears in a stored expression (such as a column default expression or view), it creates a dependency on the referenced object. For example, if a column has a default expression <literal>nextval('my_seq'::regclass)</literal>, <productname>PostgreSQL</productname> understands that the default expression depends on the sequence <literal>my_seq</literal>, so the system will not let the sequence be dropped without first removing the default expression. The alternative of <literal>nextval('my_seq'::text)</literal> does not create a dependency. (<type>regrole</type> is an exception to this property. Constants of this type are not allowed in stored expressions.) ほとんどのOID別名型のさらなる属性は依存性の作成です。 これらの型の1つの定数が格納された式内に存在する場合(列のデフォルト式やビューなど)、参照されるオブジェクトへの依存性を生成します。 例えば、列がnextval('my_seq'::regclass)というデフォルト式を持つ場合、PostgreSQLはデフォルト式がmy_seqシーケンスに依存することを理解します。 システムは先にこのデフォルト式が削除されない限り、このシーケンスを削除させません。 代わりにnextval('my_seq'::text)を使用しても依存性は作成されません。 (このプロパティの例外はregroleです。 ストアド式では、この型の定数は使用できません。)

Another identifier type used by the system is <type>xid</type>, or transaction (abbreviated <abbrev>xact</abbrev>) identifier. This is the data type of the system columns <structfield>xmin</structfield> and <structfield>xmax</structfield>. Transaction identifiers are 32-bit quantities. In some contexts, a 64-bit variant <type>xid8</type> is used. Unlike <type>xid</type> values, <type>xid8</type> values increase strictly monotonically and cannot be reused in the lifetime of a database cluster. See <xref linkend="transaction-id"/> for more details. システムが使用するもう1つの識別子の型はxid、すなわちトランザクション(略してxact)識別子です。 これはxminシステム列およびxmaxシステム列のデータ型です。 トランザクション識別子は32ビット長です。 文脈によっては64ビットに変形したxid8が使われます。 xidの値と違い xid8の値は厳密に単調増加し、データベースクラスタのライフタイムの中で再利用されることはありません。 詳細は74.1を参照してください。

A third identifier type used by the system is <type>cid</type>, or command identifier. This is the data type of the system columns <structfield>cmin</structfield> and <structfield>cmax</structfield>. Command identifiers are also 32-bit quantities. システムが使用する3つ目の識別子はcid、すなわちコマンド識別子です。 これはcminシステム列およびcmaxシステム列のデータ型です。 コマンド識別子も32ビット長です。

A final identifier type used by the system is <type>tid</type>, or tuple identifier (row identifier). This is the data type of the system column <structfield>ctid</structfield>. A tuple ID is a pair (block number, tuple index within block) that identifies the physical location of the row within its table. システムが使用する最後の識別子はtid、すなわちタプル識別子(行識別子)です。 これはctidシステム列のデータ型です。 タプルIDはテーブル内の行の物理的位置を識別するための組(ブロック番号、ブロック内のタプルインデックス)です。

(The system columns are further explained in <xref linkend="ddl-system-columns"/>.) (システム列の詳細は5.5で説明します。)