Extending SQL: An Overviewextending SQL
In the sections that follow, we will discuss how you
can extend the PostgreSQL
SQL query language by adding:
functions
data types
operators
aggregates
How Extensibility WorksPostgreSQL is extensible because its operation is
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
user as tables like any other, but the DBMS stores
its internal bookkeeping in them. One key difference
between PostgreSQL and standard relational systems is
that PostgreSQL stores much more information in its
catalogs -- not only information about tables and columns,
but also information about its types, functions, access
methods, and so on. These tables can be modified by
the user, and since PostgreSQL bases its internal operation
on these tables, this means that PostgreSQL can be
extended by users. By comparison, conventional
database systems can only be extended by changing hardcoded
procedures within the DBMS or by loading modules
specially written by the DBMS vendor.
PostgreSQL is also unlike most other data managers in
that the server can incorporate user-written code into
itself through dynamic loading. That is, the user can
specify an object code file (e.g., a shared library) that implements a new type or function
and PostgreSQL will load it as required. Code written
in SQL is even more trivial to add to the server.
This ability to modify its operation on the fly makes
PostgreSQL uniquely suited for rapid prototyping of new
applications and storage structures.
The PostgreSQL Type Systemextending SQLtypesdata types
The PostgreSQL type system
can be broken down in several ways.
Types are divided into base types and composite types.
Base types are those, like int4, that are implemented
in a language such as C. They generally correspond to
what are often known as abstract data types; PostgreSQL
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
table.
PostgreSQL stores these types
in only one way (within the
file that stores all rows of a table) but the
user can look inside at the attributes of these types
from the query language and optimize their retrieval by
(for example) defining indexes on the attributes.
PostgreSQL base types are further
divided into built-in
types and user-defined types. Built-in types (like
int4) are those that are compiled
into the system.
User-defined types are those created by the user in the
manner to be described later.
About the PostgreSQL System Catalogscatalogs
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
pg_.
The following tables contain information that may be
useful to the end user. (There are many other system
catalogs, but there should rarely be a reason to query
them directly.)
PostgreSQL System CatalogsCatalogsCatalog NameDescriptionpg_database> databasespg_class> tablespg_attribute> table columnspg_index> indexespg_proc> procedures/functions pg_type> data types (both base and complex)pg_operator> operatorspg_aggregate> aggregate functionspg_am> access methodspg_amop> access method operatorspg_amproc> access method support functionspg_opclass> access method operator classes
The major PostgreSQL system catalogs
The Developer's Guide gives a more detailed explanation
of these catalogs and their columns. However,
shows the major entities and their relationships
in the system catalogs. (Columns that do not refer
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:
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
columns used in the queries form foreign keys
in other tables.
Many different features (tables, columns,
functions, types, access methods, etc.) are
tightly integrated in this schema. A simple
create command may modify many of these catalogs.
Types and procedures
are central to the schema.
We use the words procedure
and function more or less interchangeably.
Nearly every catalog contains some reference to
rows in one or both of these tables. For
example, PostgreSQL frequently uses type
signatures (e.g., of functions and operators) to
identify unique rows of other catalogs.
There are many columns and relationships that
have obvious meanings, but there are many
(particularly those that have to do with access
methods) that do not.