Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
8.19. Object Identifier Types
Prev UpChapter 8. Data TypesHome Next

8.19. Object Identifier Types

Object identifiers (OIDs) are used internally byPostgreSQL as primary keys for various system tables. Typeoid represents an object identifier. There are also several alias types foroid, each namedregsomething.Table 8.26 shows an overview.

Theoid 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.

Theoid 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.)

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 typeoid would use. The alias types allow simplified lookup of OID values for objects. For example, to examine thepg_attribute rows related to a tablemytable, one could write:

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 namedmytable in different schemas. Theregclass input converter handles the table lookup according to the schema path setting, and so it does theright thing automatically. Similarly, casting a table's OID toregclass is handy for symbolic display of a numeric OID.

Table 8.26. Object Identifier Types

NameReferencesDescriptionValue Example
oidanynumeric object identifier564182
regclasspg_classrelation namepg_type
regcollationpg_collationcollation name"POSIX"
regconfigpg_ts_configtext search configurationenglish
regdictionarypg_ts_dicttext search dictionarysimple
regnamespacepg_namespacenamespace namepg_catalog
regoperpg_operatoroperator name+
regoperatorpg_operatoroperator with argument types*(integer,​integer) or-(NONE,​integer)
regprocpg_procfunction namesum
regprocedurepg_procfunction with argument typessum(int4)
regrolepg_authidrole namesmithee
regtypepg_typedata type nameinteger

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,myschema.mytable is acceptable input forregclass (if there is such a table). That value might be output asmyschema.mytable, or justmytable, depending on the current search path. Theregproc andregoper alias types will only accept input names that are unique (not overloaded), so they are of limited use; for most usesregprocedure orregoperator are more appropriate. Forregoperator, unary operators are identified by writingNONE for the unused operand.

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 namedFoo (with upper caseF) taking two integer arguments could be entered as' "Foo" ( int, integer ) '::regprocedure. The output would look like"Foo"(integer,integer). Both the function name and the argument type names could be schema-qualified, too.

Many built-inPostgreSQL functions accept the OID of a table, or another kind of database object, and for convenience are declared as takingregclass (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, thenextval(regclass) function takes a sequence relation's OID, so you could call it like this:

nextval('foo')operates on sequencefoonextval('FOO')same as abovenextval('"Foo"')operates on sequenceFoonextval('myschema.foo')operates onmyschema.foonextval('"myschema".foo')same as abovenextval('foo')searches search path forfoo

Note

When you write the argument of such a function as an unadorned literal string, it becomes a constant of typeregclass (or the appropriate type). Since this is really just an OID, it will track the originally identified object despite later renaming, schema reassignment, etc. Thisearly binding behavior is usually desirable for object references in column defaults and views. But sometimes you might wantlate binding where the object reference is resolved at run time. To get late-binding behavior, force the constant to be stored as atext constant instead ofregclass:

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

Theto_regclass() function and its siblings can also be used to perform run-time lookups. SeeTable 9.70.

Another practical example of use ofregclass is to look up the OID of a table listed in theinformation_schema views, which don't supply such OIDs directly. One might for example wish to call thepg_relation_size() function, which requires the table OID. Taking the above rules into account, the correct way to do that is

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

Thequote_ident() function will take care of double-quoting the identifiers where needed. The seemingly easier

SELECT pg_relation_size(table_name)FROM information_schema.tablesWHERE ...

isnot recommended, 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 expressionnextval('my_seq'::regclass),PostgreSQL understands that the default expression depends on the sequencemy_seq, so the system will not let the sequence be dropped without first removing the default expression. The alternative ofnextval('my_seq'::text) does not create a dependency. (regrole is an exception to this property. Constants of this type are not allowed in stored expressions.)

Another identifier type used by the system isxid, or transaction (abbreviatedxact) identifier. This is the data type of the system columnsxmin andxmax. Transaction identifiers are 32-bit quantities. In some contexts, a 64-bit variantxid8 is used. Unlikexid values,xid8 values increase strictly monotonically and cannot be reused in the lifetime of a database cluster.

A third identifier type used by the system iscid, or command identifier. This is the data type of the system columnscmin andcmax. Command identifiers are also 32-bit quantities.

A final identifier type used by the system istid, or tuple identifier (row identifier). This is the data type of the system columnctid. A tuple ID is a pair (block number, tuple index within block) that identifies the physical location of the row within its table.

(The system columns are further explained inSection 5.5.)


Prev Up Next
8.18. Domain Types Home 8.20. pg_lsn Type
pdfepub
Go to PostgreSQL 14
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp