Extending SQL
extending SQL
In the sections that follow, we will discuss how you
can extend the PostgreSQL
SQL query language by adding:
functions (starting in )
data types (starting in )
operators (starting in )
aggregates (starting in )
How Extensibility Works
PostgreSQL is extensible because its operation is
catalog-driven. If you are familiar with standard
relational database 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 database systems is
that PostgreSQL stores much more information in its
catalogs: not only information about tables and columns,
but also information about data types, functions, access
methods, and so on. These tables can be modified by
the user, and since PostgreSQL bases its 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 in the source code or by loading modules
specially written by the DBMS vendor.
The PostgreSQL server can moreover 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 System
extending SQL
types
data types
Data 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. The
user can look inside
at the attributes of these types
from the query language.
&xfunc;
&xtypes;
&xoper;
&xaggr;
&xindex;