Sample Page Layout
encode the page size and information on the internal fragmentation of
the page. Page size is stored in each page because frames in the
buffer pool may be subdivided into equal sized pages on a frame by
-frame basis within a class. The internal fragmentation information is
+frame basis within a table. The internal fragmentation information is
used to aid in determining when page reorganization should occur.
-name class%ROWTYPE;
+name table%ROWTYPE;
- Declares a row with the structure of the given class. Class must be
- an existing table- or view name of the database. The fields of the row
+ Declares a row with the structure of the given table. table must be
+ an existing table or view name of the database. The fields of the row
are accessed in the dot notation. Parameters to a function can
be composite types (complete table rows). In that case, the
corresponding identifier $n will be a rowtype, but it
- class.field%TYPE
+ table.field%TYPE
same function, that is visible at this point.
- class is the name of an existing table
+ table is the name of an existing table
or view where field is the name of
an attribute.
- Using the class.field%TYPE
+ Using the table.field%TYPE
causes PL/pgSQL to look up the attributes definitions at the
first call to the function during the lifetime of a backend.
Have a table with a char(20) attribute and some PL/pgSQL functions
char(40) and restores the data. Ha - he forgot about the
functions. The computations inside them will truncate the values
to 20 characters. But if they are defined using the
- class.field%TYPE
+ table.field%TYPE
declarations, they will automagically handle the size change or
if the new table schema defines the attribute as text type.
developed originally in the UC Berkeley Computer Science Department,
pioneered many of the object-relational concepts
now becoming available in some commercial databases.
- It provides SQL92/SQL3 language support,
+ It provides SQL92/SQL99 language support,
transaction integrity, and type extensibility.
open-source descendant of this original Berkeley code.
The
Postgres query language is a variant of
- the
SQL3 draft next-generation standard. It
- has many extensions to
SQL92 such as an
+ has many extensions to
SQL such as an
extensible type system,
inheritance, functions and production rules. These are
features carried over from the original
and
.
You should be aware that some language features
- are extensions to the
ANSI standard.
+ are extensions to the standard.
In the examples that follow, we assume that you have
created the mydb database as described in the previous
subsection and have started
psql.
- Examples in this manual can also be found in
- /usr/local/pgsql/src/tutorial/. Refer to the
+ Examples in this manual can also be found in source distribution
+ in the directory src/tutorial/. Refer to the
README file in that directory for how to use them. To
start the tutorial, do the following:
-% cd /usr/local/pgsql/src/tutorial
-% psql -s mydb
+
+
Welcome to the POSTGRESQL interactive sql monitor:
Please read the file COPYRIGHT for copyright terms of POSTGRESQL
type \q to quit
type \g or terminate with semicolon to execute query
You are currently connected to the database: postgres
+
-mydb=> \i basics.sql
- >
+>
Concepts
- The fundamental notion in
Postgres is that of a class,
- which is a named collection of object instances. Each
- instance has the same collection of named attributes,
- and each attribute is of a specific type. Furthermore,
- each instance has a permanent object identifier
- that is unique throughout the installation. Because
-
SQL syntax refers to tables, we will use the terms
- table and class interchangeably.
- Likewise, an
SQL row is an
- columns
- are attributes.
- As previously discussed, classes are grouped into
- databases, and a collection of databases managed by a
- single
postmaster process constitutes a
- database cluster.
+ The fundamental notion in
Postgres is
+ that of a table, which is a named
+ collection of rows. Each row has the same
+ set of named columns, and each column is of
+ a specific type. Furthermore, each row has a permanent
+
object identifier (
OID)
+ that is unique throughout the database cluster. Historially,
+ tables have been called classes in
+
Postgres, rows are object instances,
+ and columns are attributes. This makes sense if you consider the
+ object-relational aspects of the database system, but in this
+ manual we will use the customary
SQL
+ terminology. As previously discussed,
+ tables are grouped into databases, and a collection of databases
+ managed by a single
postmaster process
+ constitutes a database cluster.
-
Creating a New Class
+
Creating a New Table
- You can create a new class by specifying the class
- name, along with all attribute names and their types:
+ You can create a new table by specifying the table
+ name, along with all column names and their types:
CREATE TABLE weather (
city varchar(80),
temp_lo int, -- low temperature
prcp real, -- precipitation
date date
);
-
+
looks exactly like
the command used to create a table in a traditional
relational system. However, we will presently see that
- classes have properties that are extensions of the
+ tables have properties that are extensions of the
relational model.
-
Populating a Class with Instances
+
Populating a Table with Rows
- The INSERT statement is used to populate a class with
- instances:
+ The INSERT statement is used to populate a table with
+ rows:
-INSERT INTO weather
- VALUES ('San Francisco', 46, 50, 0.25, '11/27/1994');
-
+INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
+
single atomic
transaction directly to or from the target table. An example would be:
-COPY weather FROM '/home/user/weather.txt'
- USING DELIMITERS '|';
-
+COPY weather FROM '/home/user/weather.txt' USING DELIMITERS '|';
+
where the path name for the source file must be available to the
backend server
-
Querying a Class
+
Querying a Table
- The weather class can be queried with normal relational
+ The weather table can be queried with normal relational
selection and projection queries. A
SQL
SELECT
statement is used to do this. The statement is divided into
- a target list (the part that lists the attributes to be
+ a target list (the part that lists the columns to be
returned) and a qualification (the part that specifies
any restrictions). For example, to retrieve all the
rows of weather, type:
SELECT * FROM weather;
-
+
and the output should be:
+--------------+---------+---------+------+------------+
|city | temp_lo | temp_hi | prcp | date |
+--------------+---------+---------+------+------------+
-|San Francisco | 46 | 50 | 0.25 | 11-27-1994 |
+|San Francisco | 46 | 50 | 0.25 | 1994-11-27 |
+--------------+---------+---------+------+------------+
-|San Francisco | 43 | 57 | 0 | 11-29-1994 |
+|San Francisco | 43 | 57 | 0 | 1994-11-29 |
+--------------+---------+---------+------+------------+
-|Hayward | 37 | 54 | | 11-29-1994 |
+|Hayward | 37 | 54 | | 1994-11-29 |
+--------------+---------+---------+------+------------+
-
+
You may specify any arbitrary expressions in the target list. For
example, you can do:
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
-
+
NOT) are
allowed in the qualification of any query. For example,
SELECT * FROM weather
WHERE city = 'San Francisco'
AND prcp > 0.0;
-
+
results in:
+--------------+---------+---------+------+------------+
|city | temp_lo | temp_hi | prcp | date |
+--------------+---------+---------+------+------------+
-|San Francisco | 46 | 50 | 0.25 | 11-27-1994 |
+|San Francisco | 46 | 50 | 0.25 | 1994-11-27 |
+--------------+---------+---------+------+------------+
-
+
As a final note, you can specify that the results of a
select can be returned in a sorted order
- or with duplicate instances removed.
+ or with duplicate rows removed.
SELECT DISTINCT city
FROM weather
ORDER BY city;
-
+
Redirecting SELECT Queries
- Any SELECT query can be redirected to a new class
+ Any SELECT query can be redirected to a new table
SELECT * INTO TABLE temp FROM weather;
-
+
This forms an implicit CREATE command, creating a new
- class temp with the attribute names and types specified
+ table temp with the column names and types specified
in the target list of the SELECT INTO command. We can
then, of course, perform any operations on the resulting
- class that we can perform on other classes.
+ table that we can perform on other tables.
-
Joins Between Classes
+
Joins Between Tables
- Thus far, our queries have only accessed one class at a
- time. Queries can access multiple classes at once, or
- access the same class in such a way that multiple
- instances of the class are being processed at the same
- time. A query that accesses multiple instances of the
- same or different classes at one time is called a join
+ Thus far, our queries have only accessed one table at a
+ time. Queries can access multiple tables at once, or
+ access the same table in such a way that multiple
+ rows of the table are being processed at the same
+ time. A query that accesses multiple rows of the
+ same or different tables at one time is called a join
query.
As an example, say we wish to find all the records that
are in the temperature range of other records. In
effect, we need to compare the temp_lo and temp_hi
- attributes of each WEATHER instance to the temp_lo and
- temp_hi attributes of all other WEATHER instances.
+ columns of each WEATHER row to the temp_lo and
+ temp_hi columns of all other WEATHER columns.
This is only a conceptual model. The actual join may
We can do this with the following query:
SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
W2.city, W2.temp_lo AS low, W2.temp_hi AS high
FROM weather W1, weather W2
+--------------+-----+------+---------------+-----+------+
|San Francisco | 37 | 54 | San Francisco | 46 | 50 |
+--------------+-----+------+---------------+-----+------+
-
+
The semantics of such a join are
that the qualification
is a truth expression defined for the Cartesian product of
- the classes indicated in the query. For those instances in
+ the tables indicated in the query. For those rows in
the Cartesian product for which the qualification is true,
Postgres computes and returns the
values specified in the target list.
In this case, both W1 and
- W2 are surrogates for an
- instance of the class weather, and both range over all
- instances of the class. (In the terminology of most
+ W2 are surrogates for a
+ row of the table weather, and both range over all
+ rows of the table. (In the terminology of most
database systems, W1 and W2
are known as range variables.)
A query can contain an arbitrary number of
- class names and surrogates.
+ table names and surrogates.
Updates
- You can update existing instances using the
+ You can update existing rows using the
UPDATE command.
Suppose you discover the temperature readings are
all off by 2 degrees as of Nov 28, you may update the
data as follow:
UPDATE weather
SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2
- WHERE date > '11/28/1994';
-
+ WHERE date > '1994-11-28';
+
Deletions are performed using the DELETE command:
DELETE FROM weather WHERE city = 'Hayward';
-
+
- All weather recording belongs to Hayward is removed.
+ All weather recording belonging to Hayward are removed.
One should be wary of queries of the form
-DELETE FROM classname;
-
+DELETE FROM tablename;
+
Without a qualification, DELETE will simply
- remove all instances of the given class, leaving it
+ remove all rows from the given table, leaving it
empty. The system will not request confirmation before
doing this.
For example, there are aggregates to compute the
count, sum,
avg (average), max (maximum) and
- min (minimum) over a set of instances.
+ min (minimum) over a set of rows.
- You must own the class in order to change its schema.
+ You must own the table in order to change it.
Renaming any part of the schema of a system
catalog is not permitted.
The PostgreSQL User's Guide has further
1999-07-20
-CLUSTER indexname ON table
+CLUSTER indexname ON tablename
CLUSTER instructs
Postgres
- to cluster the class specified
+ to cluster the table specified
by table approximately
based on the index specified by
indexname. The index must
already have been defined on
- classname.
+ tablename.
- When a class is clustered, it is physically reordered
+ When a table is clustered, it is physically reordered
based on the index information. The clustering is static.
- In other words, as the class is updated, the changes are
+ In other words, as the table is updated, the changes are
not clustered. No attempt is made to keep new instances or
updated tuples clustered. If one wishes, one can
re-cluster manually by issuing the command again.
The following is the same data, output in binary format on a Linux/i586
machine. The data is shown after filtering through
the Unix utility od -c. The table has
- three fields; the first is <classname>char(2)e>,
- the second is <classname>texte>, and the third is
- <classname>int4e>. All the
+ three fields; the first is <type>char(2)e>,
+ the second is <type>texte>, and the third is
+ <type>integere>. All the
rows have a null value in the third field.
In the second syntax shown above, an index is defined
on the result of a user-specified function
func_name applied
- to one or more attributes of a single class.
+ to one or more columns of a single table.
These functional indices
can be used to obtain fast access to data
based on operators that would normally require some
instance variables, the query optimizer must estimate the
size of the resulting join. The function join_proc will
return another floating point number which will be multiplied
- by the cardinalities of the two classes involved to
+ by the cardinalities of the two tables involved to
compute the expected result size.
- You must have rule definition access to a class in order
+ You must have rule definition access to a table in order
to define a rule on it. Use GRANT
and REVOKE to change permissions.
Examples
This command creates the box data type and then uses the
- type in a class definition:
+ type in a table definition:
CREATE TYPE box (INTERNALLENGTH = 8,
INPUT = my_procedure_1, OUTPUT = my_procedure_2);
This command creates a large object type and uses it in
- a class definition:
+ a table definition:
CREATE TYPE bigobj (INPUT = lo_filein, OUTPUT = lo_fileout,
Description
- CREATE VIEW will define a view of a table or
- class. This view is not physically materialized. Specifically, a query
+ CREATE VIEW will define a view of a table.
+ This view is not physically materialized. Specifically, a query
rewrite retrieve rule is automatically generated to support
retrieve operations on views.
Only users with usesuper set in
- the pg_shadow class can create
+ the pg_shadow table can create
By default DELETE will delete tuples in the table specified
- and all its sub-classes. If you wish to only update the
+ and all its sub-tables. If you wish to only update the
specific table mentioned, you should use the ONLY clause.
It is the user's responsibility to remove any operators,
- functions, aggregates, access methods, subtypes, and classes
+ functions, aggregates, access methods, subtypes, and tables
that use a deleted type.
and the databases which that user owned.
Only users with usesuper set in
- the pg_shadow class can destroy
+ the pg_shadow table can destroy
INSERT allows one to insert new rows into a
- class or table. One can insert
+ table. One can insert
a single row at a time or several rows as a result of a query.
The columns in the target list may be listed in any order.
[ HAVING condition [, ...] ]
[ { UNION | INTERSECT | EXCEPT [ ALL ] } select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
- [ FOR UPDATE [ OF class_name [, ...] ] ]
+ [ FOR UPDATE [ OF tablename [, ...] ] ]
[ LIMIT { count | ALL } [ { OFFSET | , } start ]]
where from_item can be:
When a FROM item is a simple table name, it implicitly includes rows
- from subclasses (inheritance children) of the table.
+ from sub-tables (inheritance children) of the table.
ONLY will
- suppress rows from subclasses of the table. Before
+ suppress rows from sub-tables of the table. Before
- this was the default result, and adding subclasses was done
+ this was the default result, and adding sub-tables was done
by appending * to the table name.
This old behaviour is available via the command
SET SQL_Inheritance TO OFF;
[ HAVING condition [, ...] ]
[ { UNION | INTERSECT | EXCEPT [ ALL ] } select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
- [ FOR UPDATE [ OF class_name [, ...] ] ]
+ [ FOR UPDATE [ OF tablename [, ...] ] ]
[ LIMIT { count | ALL } [ { OFFSET | , } start ]]
where from_item can be:
Notes
- classname
+ notifyname
need not be a valid class name but can be any string valid
as a name up to 32 characters long.
Each backend will automatically execute UNLISTEN * when
exiting.
- A restriction in some previous releases of
- classname
- which does not correspond to an actual table must be enclosed in double-quotes
- is no longer present.
-
By default UPDATE will update tuples in the table specified
- and all its sub-classes. If you wish to only update the
+ and all its sub-tables. If you wish to only update the
specific table mentioned, you should use the ONLY clause.
- VACUUM opens every class in the database,
+ VACUUM opens every table in the database,
cleans out records from rolled back transactions, and updates statistics in the
system catalogs. The statistics maintained include the number of
- tuples and number of pages stored in all classes.
+ tuples and number of pages stored in all tables.
We recommend that active production databases be
VACUUM-ed nightly, in order to remove
- expired rows. After copying a large class into
+ expired rows. After copying a large table into
Postgres or after deleting a large number
of records, it may be a good idea to issue a VACUUM
ANALYZE query. This will update the system catalogs with
-
Fields and Columns
-
-
-
Fields
+
Columns
- A field
- is either a user-defined attribute of a given class or one of the
- following system-defined attributes:
+ A column
+ is either a user-defined column of a given table or one of the
+ following system-defined columns:
.
Transaction and command identifiers are 32 bit quantities.
-
-
-
-
Columns
- A column is a construct of the form:
+ A column can be referenced in the form:
-
-instance{.composite_field}.field `['subscript`]'
-
+
+corelation.columnname `['subscript`]'
+
- instance
- identifies a particular class and can be thought of as standing for
- the instances of that class. An instance variable is either a class
- name, an alias for a class defined by means of a FROM clause,
- or the keyword NEW or OLD.
- (NEW and OLD can only appear in the action portion of a rule, while
- other instance variables can be used in any SQL statement.) The
- instance name can be omitted if the first field name is unique
- across all the classes being used in the current query.
- composite_field
- is a field of of one of the Postgres composite types,
- while successive composite fields select attributes in the
- class(s) to which the composite field evaluates. Lastly,
- field
- is a normal (base type) field in the class(s) last addressed. If
- field
- is of an array type,
- then the optional subscript
- selects a specific element in the array. If no subscript is
- provided, then the whole array is selected.
+ corelation is either the name of a
+ table, an alias for a table defined by means of a FROM clause, or
+ the keyword NEW or OLD.
+ (NEW and OLD can only appear in the action portion of a rule,
+ while other corelation names can be used in any SQL statement.)
+ The corelation name can be omitted if the column name is unique
+ across all the tables being used in the current query. If
+ column is of an array type, then the
+ optional subscript selects a specific
+ element in the array. If no subscript is provided, then the
+ whole array is selected. Refer to the description of the
+ particular commands in the PostgreSQL Reference
+ Manual for the allowed syntax in each case.
-
+
The simplest possibility for a from-expression is:
-class_reference [ [ AS ] alias ]
+table_reference [ [ AS ] alias ]
- where class_reference is of the form
+ where table_reference is of the form
[ ONLY ] table_name [ * ]
developed originally in the UC Berkeley Computer Science Department,
pioneered many of the object-relational concepts
now becoming available in some commercial databases.
- It provides SQL92/SQL3 language support,
+ It provides SQL92/SQL99 language support,
transaction integrity, and type extensibility.
PostgreSQL is an open-source descendant
of this original Berkeley code.
developed originally in the UC Berkeley Computer Science Department,
pioneered many of the object-relational concepts
now becoming available in some commercial databases.
- It provides SQL92/SQL3 language support,
+ It provides SQL92/SQL99 language support,
transaction integrity, and type extensibility.
PostgreSQL is an open-source descendant
of this original Berkeley code.
If we define an aggregate that does not use a final function,
we have an aggregate that computes a running function of
- the attribute values from each instance. "Sum" is an
+ the column values from each row. "Sum" is an
example of this kind of aggregate. "Sum" starts at
- zero and always adds the current instance's value to
+ zero and always adds the current row's value to
its running total. For example, if we want to make a Sum
aggregate to work on a datatype for complex numbers,
we only need the addition function for that datatype.
return composite types, we must first introduce the
function notation for projecting attributes. The simple way
to explain this is that we can usually use the
- notations attribute(class) and class.attribute interchangably:
+ notations attribute(table) and table.attribute interchangably:
--
As we shall see, however, this is not always the case.
This function notation is important when we want to use
- a function that returns a single instance. We do this
- by assembling the entire instance within the function,
+ a function that returns a single row. We do this
+ by assembling the entire row within the function,
attribute by attribute. This is an example of a function
- that returns a single EMP instance:
+ that returns a single EMP row:
CREATE FUNCTION new_emp()
- When calling a function that returns an instance, we
- cannot retrieve the entire instance. We must either
- project an attribute out of the instance or pass the
- entire instance into another function.
+ When calling a function that returns a row, we
+ cannot retrieve the entire row. We must either
+ project an attribute out of the row or pass the
+ entire row into another function.
SELECT name(new_emp()) AS nobody;
Therefore,
Postgres provides
a procedural interface for accessing fields of composite types
from C. As
Postgres processes
- a set of instances, each instance will be passed into your
+ a set of rows, each row will be passed into your
function as an opaque structure of type TUPLE.
Suppose we want to write a function to answer the query
#include "executor/executor.h" /* for GetAttributeByName() */
bool
-c_overpaid(TupleTableSlot *t, /* the current instance of EMP */
+c_overpaid(TupleTableSlot *t, /* the current row of EMP */
int32 limit)
{
bool isnull;
GetAttributeByName is the
Postgres system function that
- returns attributes out of the current instance. It has
+ returns attributes out of the current row. It has
three arguments: the argument of type TupleTableSlot* passed into
the function, the name of the desired attribute, and a
return parameter that tells whether the attribute
- While there are ways to construct new instances or modify
- existing instances from within a C function, these
+ While there are ways to construct new rows or modify
+ existing rows from within a C function, these
are far too complex to discuss in this manual.
- The pg_am class contains one instance for every user
+ The pg_am table contains one row for every user
defined access method. Support for the heap access method is built into
Postgres, but every other access method is
described here. The schema is
|
- Attribute
+ Column
Description
|
amowner
- object id of the owner's instance in pg_user
+ user id of the owner
|
amstrategies
...
procedure identifiers for interface routines to the access
method. For example, regproc ids for opening, closing, and
- getting instances from the access method appear here.
+ getting rows from the access method appear here.
- The
object ID of the
instance in
- pg_am is used as a foreign key in lots of other
- classes. You don't need to add a new instance to this class; all
- you
're interested in is the
object ID of the access
- method instance you want to extend:
+ The
object ID of the
row in
+ pg_am is used as a foreign key in a lot of other
+ tables. You do not need to add a new rows to this table; all that
+ you
are interested in is the
object ID of the access
+ method row you want to extend:
SELECT oid FROM pg_am WHERE amname = 'btree';
- The amstrategies attribute exists to standardize
+ The amstrategies column exists to standardize
comparisons across data types. For example,
B-trees
impose a strict ordering on keys, lesser to greater. Since
Postgres allows the user to define operators,
Defining a new set of strategies is beyond the scope of this discussion,
but we'll explain how
B-tree strategies work because
you'll need to know that to add a new operator class. In the
- pg_am class, the amstrategies attribute is the
+ pg_am table, the amstrategies column is the
number of strategies defined for this access method. For
B-trees, this number is 5. These strategies
correspond to
In order to manage diverse support routines consistently across all
- pg_am includes an attribute called
- amsupport. This attribute records the number of
+ pg_am includes a column called
+ amsupport. This column records the number of
support routines used by an access method. For
B-trees,
this number is one -- the routine to take two keys and return -1, 0, or
+1, depending on whether the first key is less than, equal
- The next class of interest is pg_opclass. This class
+ The next table of interest is pg_opclass. This table
exists only to associate an operator class name and perhaps a default type
with an operator class oid. Some existing opclasses are int2_ops,
- int4_ops, and oid_ops. You need to add an
- instance with your opclass name (for example,
+ int4_ops, and oid_ops. You need to add a
+ row with your opclass name (for example,
complex_abs_ops) to
pg_opclass. The oid of
- this instance will be a foreign key in other classes, notably
+ this row will be a foreign key in other tables, notably
pg_amop.
(1 row)
- Note that the oid for your pg_opclass instance will
+ Note that the oid for your pg_opclass row will
be different! Don't worry about this though. We'll get this number
from the system later just like we got the oid of the type here.
hand, the support function returns whatever the particular access method
expects -- in this case, a signed integer.) The final routine in the
file is the "support routine" mentioned when we discussed the amsupport
- attribute of the pg_am class. We will use this
+ column of the pg_am table. We will use this
later on. For now, ignore it.
- Now we're ready to update pg_amop with our new
+ Now we are ready to update pg_amop with our new
operator class. The most important thing in this entire discussion
is that the operators are ordered, from less than through greater
- than, in pg_amop. We add the instances we need:
+ than, in pg_amop. We add the rows we need:
INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy)
The next step is registration of the "support routine" previously
described in our discussion of pg_am. The
oid of this support routine is stored in the
- pg_amproc class, keyed by the access method
+ pg_amproc table, keyed by the access method
oid and the operator class oid.
First, we need to register the function in
Postgres (recall that we put the
(Again, your oid number will probably be different.)
- We can add the new instance as follows:
+ We can add the new row as follows:
INSERT INTO pg_amproc (amid, amopclaid, amproc, amprocnum)
As previously mentioned, there are two kinds of types
in
Postgres: base types (defined in a programming language)
- and composite types (instances).
+ and composite types.
Examples in this section up to interfacing indices can
be found in complex.sql and complex.c. Composite examples
are in funcs.sql.