2000-03-31 05:27:42 +02:00
|
|
|
<!--
|
2002-07-30 19:34:37 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/extend.sgml,v 1.16 2002/07/30 17:34:37 tgl Exp $
|
2000-03-31 05:27:42 +02:00
|
|
|
-->
|
|
|
|
|
1999-07-22 17:11:05 +02:00
|
|
|
<chapter id="extend">
|
|
|
|
<title>Extending <acronym>SQL</acronym>: An Overview</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
In the sections that follow, we will discuss how you
|
2001-11-21 06:53:41 +01:00
|
|
|
can extend the <productname>PostgreSQL</productname>
|
1999-07-22 17:11:05 +02:00
|
|
|
<acronym>SQL</acronym> query language by adding:
|
|
|
|
|
|
|
|
<itemizedlist spacing="compact" mark="bullet">
|
|
|
|
<listitem>
|
|
|
|
<para>
|
1998-03-01 09:16:16 +01:00
|
|
|
functions
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2002-01-07 03:29:15 +01:00
|
|
|
data types
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
1998-03-01 09:16:16 +01:00
|
|
|
operators
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
1998-03-01 09:16:16 +01:00
|
|
|
aggregates
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="extend-how">
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>How Extensibility Works</title>
|
|
|
|
|
|
|
|
<para>
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname> is extensible because its operation is
|
1999-07-22 17:11:05 +02:00
|
|
|
catalog-driven. If you are familiar with standard
|
|
|
|
relational systems, you know that they store information
|
|
|
|
about databases, tables, columns, etc., in what are
|
|
|
|
commonly known as system catalogs. (Some systems call
|
|
|
|
this the data dictionary). The catalogs appear to the
|
2001-01-14 00:58:55 +01:00
|
|
|
user as tables like any other, but the <acronym>DBMS</acronym> stores
|
1999-07-22 17:11:05 +02:00
|
|
|
its internal bookkeeping in them. One key difference
|
2001-11-21 06:53:41 +01:00
|
|
|
between <productname>PostgreSQL</productname> and standard relational systems is
|
|
|
|
that <productname>PostgreSQL</productname> stores much more information in its
|
1999-07-22 17:11:05 +02:00
|
|
|
catalogs -- not only information about tables and columns,
|
|
|
|
but also information about its types, functions, access
|
2001-01-14 00:58:55 +01:00
|
|
|
methods, and so on. These tables can be modified by
|
2001-11-21 06:53:41 +01:00
|
|
|
the user, and since <productname>PostgreSQL</productname> bases its internal operation
|
|
|
|
on these tables, this means that <productname>PostgreSQL</productname> can be
|
1999-07-22 17:11:05 +02:00
|
|
|
extended by users. By comparison, conventional
|
|
|
|
database systems can only be extended by changing hardcoded
|
|
|
|
procedures within the <acronym>DBMS</acronym> or by loading modules
|
2002-01-07 03:29:15 +01:00
|
|
|
specially written by the <acronym>DBMS</acronym> vendor.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname> is also unlike most other data managers in
|
1999-07-22 17:11:05 +02:00
|
|
|
that the server can incorporate user-written code into
|
|
|
|
itself through dynamic loading. That is, the user can
|
2002-01-07 03:29:15 +01:00
|
|
|
specify an object code file (e.g., a shared library) that implements a new type or function
|
2001-11-21 06:53:41 +01:00
|
|
|
and <productname>PostgreSQL</productname> will load it as required. Code written
|
2002-01-07 03:29:15 +01:00
|
|
|
in <acronym>SQL</acronym> is even more trivial to add to the server.
|
2001-09-13 17:55:24 +02:00
|
|
|
This ability to modify its operation <quote>on the fly</quote> makes
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname> uniquely suited for rapid prototyping of new
|
1999-07-22 17:11:05 +02:00
|
|
|
applications and storage structures.
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="type-system">
|
2001-11-21 06:53:41 +01:00
|
|
|
<title>The <productname>PostgreSQL</productname> Type System</title>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<para>
|
2001-11-21 06:53:41 +01:00
|
|
|
The <productname>PostgreSQL</productname> type system
|
1999-07-22 17:11:05 +02:00
|
|
|
can be broken down in several ways.
|
|
|
|
Types are divided into base types and composite types.
|
2002-01-07 03:29:15 +01:00
|
|
|
Base types are those, like <type>int4</type>, that are implemented
|
|
|
|
in a language such as C. They generally correspond to
|
2001-11-21 06:53:41 +01:00
|
|
|
what are often known as <firstterm>abstract data types</firstterm>; <productname>PostgreSQL</productname>
|
1999-07-22 17:11:05 +02:00
|
|
|
can only operate on such types through methods provided
|
|
|
|
by the user and only understands the behavior of such
|
|
|
|
types to the extent that the user describes them.
|
|
|
|
Composite types are created whenever the user creates a
|
2002-01-07 03:29:15 +01:00
|
|
|
table.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname> stores these types
|
1999-07-22 17:11:05 +02:00
|
|
|
in only one way (within the
|
2001-01-14 00:58:55 +01:00
|
|
|
file that stores all rows of a table) but the
|
2001-09-13 17:55:24 +02:00
|
|
|
user can <quote>look inside</quote> at the attributes of these types
|
1999-07-22 17:11:05 +02:00
|
|
|
from the query language and optimize their retrieval by
|
2001-05-17 23:50:18 +02:00
|
|
|
(for example) defining indexes on the attributes.
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</productname> base types are further
|
1999-07-22 17:11:05 +02:00
|
|
|
divided into built-in
|
|
|
|
types and user-defined types. Built-in types (like
|
2002-01-07 03:29:15 +01:00
|
|
|
<type>int4</type>) are those that are compiled
|
1999-07-22 17:11:05 +02:00
|
|
|
into the system.
|
|
|
|
User-defined types are those created by the user in the
|
2002-01-07 03:29:15 +01:00
|
|
|
manner to be described later.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2000-11-29 21:15:59 +01:00
|
|
|
<sect1 id="pg-system-catalogs">
|
2001-11-21 06:53:41 +01:00
|
|
|
<title>About the <productname>PostgreSQL</productname> System Catalogs</title>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Having introduced the basic extensibility concepts, we
|
|
|
|
can now take a look at how the catalogs are actually
|
|
|
|
laid out. You can skip this section for now, but some
|
|
|
|
later sections will be incomprehensible without the
|
|
|
|
information given here, so mark this page for later
|
|
|
|
reference.
|
|
|
|
All system catalogs have names that begin with
|
2002-01-07 03:29:15 +01:00
|
|
|
<literal>pg_</literal>.
|
2001-01-14 00:58:55 +01:00
|
|
|
The following tables contain information that may be
|
1999-07-22 17:11:05 +02:00
|
|
|
useful to the end user. (There are many other system
|
|
|
|
catalogs, but there should rarely be a reason to query
|
|
|
|
them directly.)
|
|
|
|
|
|
|
|
<table tocentry="1">
|
2001-11-21 06:53:41 +01:00
|
|
|
<title>PostgreSQL System Catalogs</title>
|
1999-07-22 17:11:05 +02:00
|
|
|
<titleabbrev>Catalogs</titleabbrev>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>Catalog Name</entry>
|
|
|
|
<entry>Description</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry>pg_database</entry>
|
|
|
|
<entry> databases</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_class</entry>
|
2001-01-14 00:58:55 +01:00
|
|
|
<entry> tables</entry>
|
1999-07-22 17:11:05 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_attribute</entry>
|
2001-01-14 00:58:55 +01:00
|
|
|
<entry> table columns</entry>
|
1999-07-22 17:11:05 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_index</entry>
|
2002-01-07 03:29:15 +01:00
|
|
|
<entry> indexes</entry>
|
1999-07-22 17:11:05 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_proc</entry>
|
2002-01-07 03:29:15 +01:00
|
|
|
<entry> procedures/functions </entry>
|
1999-07-22 17:11:05 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_type</entry>
|
2002-01-07 03:29:15 +01:00
|
|
|
<entry> data types (both base and complex)</entry>
|
1999-07-22 17:11:05 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_operator</entry>
|
|
|
|
<entry> operators</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_aggregate</entry>
|
2002-01-07 03:29:15 +01:00
|
|
|
<entry> aggregate functions</entry>
|
1999-07-22 17:11:05 +02:00
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_am</entry>
|
|
|
|
<entry> access methods</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_amop</entry>
|
|
|
|
<entry> access method operators</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_amproc</entry>
|
|
|
|
<entry> access method support functions</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>pg_opclass</entry>
|
|
|
|
<entry> access method operator classes</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<figure float="1" id="EXTEND-CATALOGS">
|
2001-11-21 06:53:41 +01:00
|
|
|
<title>The major <productname>PostgreSQL</productname> system catalogs</title>
|
2001-09-30 18:05:54 +02:00
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
|
|
|
<imagedata fileref="catalogs" align="center">
|
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
1999-07-22 17:11:05 +02:00
|
|
|
</figure>
|
|
|
|
|
2002-01-07 03:29:15 +01:00
|
|
|
The <citetitle>Developer's Guide</citetitle> gives a more detailed explanation
|
2001-01-14 00:58:55 +01:00
|
|
|
of these catalogs and their columns. However,
|
2000-12-26 01:10:37 +01:00
|
|
|
<xref linkend="EXTEND-CATALOGS">
|
1999-07-22 17:11:05 +02:00
|
|
|
shows the major entities and their relationships
|
2001-01-14 00:58:55 +01:00
|
|
|
in the system catalogs. (Columns that do not refer
|
1999-07-22 17:11:05 +02:00
|
|
|
to other entities are not shown unless they are part of
|
|
|
|
a primary key.)
|
|
|
|
This diagram is more or less incomprehensible until you
|
|
|
|
actually start looking at the contents of the catalogs
|
|
|
|
and see how they relate to each other. For now, the
|
|
|
|
main things to take away from this diagram are as follows:
|
1998-03-01 09:16:16 +01:00
|
|
|
|
1999-07-22 17:11:05 +02:00
|
|
|
<itemizedlist spacing="compact" mark="bullet">
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
In several of the sections that follow, we will
|
|
|
|
present various join queries on the system
|
|
|
|
catalogs that display information we need to extend
|
|
|
|
the system. Looking at this diagram should make
|
|
|
|
some of these join queries (which are often
|
|
|
|
three- or four-way joins) more understandable,
|
|
|
|
because you will be able to see that the
|
2001-01-14 00:58:55 +01:00
|
|
|
columns used in the queries form foreign keys
|
|
|
|
in other tables.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-01-14 00:58:55 +01:00
|
|
|
Many different features (tables, columns,
|
1999-07-22 17:11:05 +02:00
|
|
|
functions, types, access methods, etc.) are
|
|
|
|
tightly integrated in this schema. A simple
|
|
|
|
create command may modify many of these catalogs.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Types and procedures
|
|
|
|
are central to the schema.
|
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
We use the words <firstterm>procedure</firstterm>
|
2001-09-10 23:58:47 +02:00
|
|
|
and <firstterm>function</firstterm> more or less interchangeably.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
|
|
|
|
Nearly every catalog contains some reference to
|
2001-01-14 00:58:55 +01:00
|
|
|
rows in one or both of these tables. For
|
2001-11-21 06:53:41 +01:00
|
|
|
example, <productname>PostgreSQL</productname> frequently uses type
|
1999-07-22 17:11:05 +02:00
|
|
|
signatures (e.g., of functions and operators) to
|
2001-01-14 00:58:55 +01:00
|
|
|
identify unique rows of other catalogs.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2001-01-14 00:58:55 +01:00
|
|
|
There are many columns and relationships that
|
1999-07-22 17:11:05 +02:00
|
|
|
have obvious meanings, but there are many
|
|
|
|
(particularly those that have to do with access
|
2002-07-30 19:34:37 +02:00
|
|
|
methods) that do not.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
2000-03-31 05:27:42 +02:00
|
|
|
mode:sgml
|
1999-07-22 17:11:05 +02:00
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"./reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
2000-03-31 05:27:42 +02:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
1999-07-22 17:11:05 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|