Refer back to the queries in .
Suppose the combined listing of weather records and city location
- is of particular interest to your application, but you don't want
+ is of particular interest to your application, but you do not want
to type the query each time you need it. You can create a
view over the query, which gives a name to
the query that you can refer to like an ordinary table.
- If, partway through the transaction, we decide we don't want to
+ If, partway through the transaction, we decide we do not want to
commit (perhaps we just noticed that Alice's balance went negative),
we can issue the command ROLLBACK> instead of
COMMIT>, and all our updates so far will be canceled.
PostgreSQL> actually treats every SQL statement as being
- executed within a transaction. If you don't issue a BEGIN>
+ executed within a transaction. If you do not issue a BEGIN>
command,
then each individual statement has an implicit BEGIN> and
(if successful) COMMIT> wrapped around it. A group of
This chapter gives an overview of the internal structure of the
- After having read the following sections you
- should have an idea of how a query is processed. Don't expect a
- detailed description here (I think such a description dealing with
- all data structures and functions used within
PostgreSQL
- would exceed 1000
- pages!). This chapter is intended to help understanding the general
- control and data flow within the backend from receiving a query to
- sending the results.
+ backend of
PostgreSQL.
After having
+ read the following sections you should have an idea of how a query
+ is processed. This chapter does not aim to provide a detailed
+ description of the internal operation of
+
PostgreSQL, as such a document would be
+ very extensive. Rather, this chapter is intended to help the reader
+ understand the general sequence of operations that occur within the
+ backend from the point at which a query is received, to the point
+ when the results are returned to the client.
How Connections are Established
-
PostgreSQL is implemented using a simple "process per-user"
- client/server model. In this model there is one client process
- connected to exactly one server process.
- As we don't know per se
- how many connections will be made, we have to use a master process
- that spawns a new server process every time a connection is
- requested. This master process is called postmaster and
- listens at a specified TCP/IP port for incoming connections. Whenever
- a request for a connection is detected the postmaster process
- spawns a new server process called postgres. The server
- tasks (postgres processes) communicate with each other using
- semaphores and shared memory
- to ensure data integrity
- throughout concurrent data access. Figure
- \ref{connection} illustrates the interaction of the master process
- postmaster the server process postgres and a client
- application.
+
PostgreSQL is implemented using a
+ simple "process per-user" client/server model. In this model
+ there is one client process connected to
+ exactly one server process. As we do not
+ know ahead of time how many connections will be made, we have to
+ use a master process that spawns a new
+ server process every time a connection is requested. This master
+ process is called postmaster and listens at a
+ specified TCP/IP port for incoming connections. Whenever a request
+ for a connection is detected the postmaster
+ process spawns a new server process called
+ postgres. The server tasks
+ (postgres processes) communicate with each
+ other using semaphores and
+ shared memory to ensure data integrity
+ throughout concurrent data access. Figure \ref{connection}
+ illustrates the interaction of the master process
+ postmaster the server process
+ postgres and a client application.
for the varno fields in the
VAR nodes appearing in the mergeclauses list (and also in the
targetlist) mean that not the tuples of the current node should be
- considered but the tuples of the next "deeper" nodes (i.e. the top
+ considered but the tuples of the next deeper
nodes (i.e. the top
nodes of the subplans) should be used instead.
actual string, and in case of character plus the
padding. Long strings are compressed by the system automatically, so
the physical requirement on disk may be less. Long values are also
- stored in background tables so they don't interfere with rapid
+ stored in background tables so they do not interfere with rapid
access to the shorter column values. In any case, the longest
possible character string that can be stored is about 1 GB. (The
maximum value that will be allowed for n> in the data
-
+
Data Definition
- If you don't need a table anymore, you can remove it using the
- <literal>DROP TABLE> command. For example:
+ If you no longer need a table, you can remove it using the
+ <command>DROP TABLE> command. For example:
DROP TABLE my_first_table;
DROP TABLE products;
We know that the foreign keys disallow creation of orders that
- don't relate to any products. But what if a product is removed
+ do not relate to any products. But what if a product is removed
after an order is created that references it? SQL allows you to
specify that as well. Intuitively, we have a few options:
Restricting and cascading deletes are the two most common options.
RESTRICT can also be written as NO
- ACTION and it's also the default if you don't specify
+ ACTION and it's also the default if you do not specify
anything. There are two other options for what should happen with
the foreign key columns when a primary key is deleted:
SET NULL and SET DEFAULT.
In the SQL standard, the notion of objects in the same schema
being owned by different users does not exist. Moreover, some
- implementations don't allow you to create schemas that have a
+ implementations do not allow you to create schemas that have a
different name than their owner. In fact, the concepts of schema
and user are nearly equivalent in a database system that
implements only the basic schema support specified in the
ERROR: Cannot drop table products because other objects depend on it
Use DROP ... CASCADE to drop the dependent objects too
- The error message contains a useful hint: If you don't want to
+ The error message contains a useful hint: if you do not want to
bother deleting all the dependent objects individually, you can run
DROP TABLE products CASCADE;
-
+
Documentation
If the program uses 0 for success and non-zero for failure,
- then you don't need to document it. If there is a meaning
+ then you do not need to document it. If there is a meaning
behind the different non-zero exit codes, list them here.
- The main difference between multiversion and lock models is that
- in
MVCC locks acquired for querying (reading)
- data don't conflict with locks acquired for writing data, and so
+ The main advantage to using the
MVCC model of
+ concurrency control rather than locking is that in
+
MVCC locks acquired for querying (reading) data
+ do not conflict with locks acquired for writing data, and so
reading never blocks writing and writing never blocks reading.
- The SELECT command acquires a
- lock of this mode on referenced tables. In general, any query
- that only reads a table and does not modify it will acquire
- this lock mode.
+ The commands SELECT and
+ ANALYZE acquire a lock of this mode on
+ referenced tables. In general, any query that only reads a table
+ and does not modify it will acquire this lock mode.
Acquired by the ALTER TABLE, DROP
- TABLE, and VACUUM FULL commands.
- This is also the default lock mode for LOCK TABLE
- statements that do not specify a mode explicitly.
+ TABLE, REINDEX,
+ CLUSTER, and VACUUM FULL
+ commands. This is also the default lock mode for LOCK
+ TABLE statements that do not specify a mode explicitly.
A row-level lock on a specific row is automatically acquired when the
row is updated (or deleted or marked for update). The lock is held
until the transaction commits or rolls back.
- Row-level locks don't affect data
+ Row-level locks do not affect data
querying; they block writers to the same row
only. To acquire a row-level lock on a row without actually
modifying the row, select the row with SELECT FOR
Because readers in
PostgreSQL
- don't lock data, regardless of
+ do not lock data, regardless of
transaction isolation level, data read by one transaction can be
overwritten by another concurrent transaction. In other words,
if a row is returned by SELECT it doesn't mean that
- The final section is the "special section" which may contain anything the
- access method wishes to store. Ordinary tables do not use this at all
- (indicated by setting pd_special> to equal the pagesize).
+ The final section is the special section
which may
+ contain anything the access method wishes to store. Ordinary tables
+ do not use this at all (indicated by setting
+ pd_special> to equal the pagesize).
- By using %TYPE you don't need to know
+ By using %TYPE you do not need to know
the data type of the structure you are referencing,
and most important, if the data type of the
referenced item changes in the future (e.g: you
up a cursor> that encapsulates the query, and then read
the query result a few rows at a time. One reason for doing this is
to avoid memory overrun when the result contains a large number of
- rows. (However,
PL/pgSQL> users don't normally need
+ rows. (However,
PL/pgSQL> users do not normally need
to worry about that, since FOR loops automatically use a cursor
internally to avoid memory problems.) A more interesting usage is to
return a reference to a cursor that it has created, allowing the
default and NOT NULL clauses for the new column are not supported.
The new column always comes into being with all values NULL.
You can use the SET DEFAULT form
- of ALTER TABLE to set the default afterwards.
+ of ALTER TABLE to set the default afterward.
(You may also want to update the already existing rows to the
new default value, using
.)
- If a table has any descendant tables, it is not permitted to ADD or
- RENAME a column in the parent table without doing the same to the
- descendants --- that is, ALTER TABLE ONLY will be rejected. This
- ensures that the descendants always have columns matching the parent.
+ If a table has any descendant tables, it is not permitted to ADD
+ or RENAME a column in the parent table without doing the same to
+ the descendants --- that is, ALTER TABLE ONLY
+ will be rejected. This ensures that the descendants always have
+ columns matching the parent.
- A recursive DROP COLUMN operation will remove a descendant table's column
- only if the descendant does not inherit that column from any other
- parents and never had an independent definition of the column.
- A nonrecursive DROP COLUMN (i.e., ALTER TABLE ONLY ... DROP COLUMN)
- never removes any descendant columns, but instead marks them as
- independently defined rather than inherited.
+ A recursive DROP COLUMN operation will remove a
+ descendant table's column only if the descendant does not inherit
+ that column from any other parents and never had an independent
+ definition of the column. A nonrecursive DROP
+ COLUMN (i.e., ALTER TABLE ONLY ... DROP
+ COLUMN) never removes any descendant columns, but
+ instead marks them as independently defined rather than inherited.
CLUSTER without any parameter re-clusters all the tables
in the
current database that the calling user owns, or all tables if called
- by a superuser. (Never-clustered tables are not touched.) This
+ by a superuser. (Never-clustered tables are not included.) This
form of CLUSTER cannot be called from inside a
transaction or function.
+ When a table is being clustered, an ACCESS
+ EXCLUSIVE lock is acquired on it. This prevents any other
+ database operations (both reads and writes) from preceding on the
+ table until the CLUSTER is finished. See the
+ &cite-user; for more information on database locking.
+
+
1998-09-08
- CLUSTER preserves GRANT, inheritance, index, foreign
- key, and other ancillary information about the table.
+ CLUSTER preserves GRANT,
+ inheritance, index, foreign key, and other ancillary information
+ about the table.
Usage
- Cluster the employees relation on the basis of its ID attribute:
+ Cluster the employees relation on the basis of
+ its ID attribute:
CLUSTER emp_ind ON emp;
- Cluster the employees relation using the same index that was used before:
+ Cluster the employees relation using the same
+ index that was used before:
CLUSTER emp;
+
+
+
See Also
+
+
+
+
+
in a
PostgreSQL database. It finds tables
that have previously been clustered, and clusters them again on the same
index that was last used. Tables that have never been clustered are not
- touched.
+ affected.
DATABASE
- Recreate all system indexes of a specified database.
- (User-table indexes are not included.)
+ Recreate all system indexes of a specified database. Indexes on
+ user tables are not included. This form of REINDEX> can
+ only be used in standalone mode (see below).
Description
- REINDEX is used to rebuild corrupted indexes.
- Although in theory this should never be necessary, in practice
- indexes may become corrupted due to software bugs or hardware
- failures. REINDEX provides a recovery method.
-
+ REINDEX rebuilds an index based on the data
+ stored in the table, replacing the old copy of the index. There are
+ two main reasons to use REINDEX:
- REINDEX also removes certain dead index pages that
- can't be reclaimed any other way. See the "Routine Reindexing"
- section in the manual for more information.
+
+
+ An index has become corrupted, and no longer contains valid
+ data. Although in theory this should never be necessary, in
+ practice indexes may become corrupted due to software bugs or
+ hardware failures. REINDEX provides a
+ recovery method.
+
+
+
+
+ The index in question contains a lot of dead index pages that
+ are not being reclaimed. This can occur with B+-tree indexes
+ under certain access patterns. REINDEX
+ provides a way to reduce the space consumption of the index by
+ writing a new version of the index without the dead pages. See
+ the Routine Reindexing
section in the
+ &cite-admin; for more information. The rest of this section
+ mostly discusses how to use REINDEX to
+ recover from index corruption.
+
+
+
-
+
2001-04-21
-SET [ SESSION | LOCAL ] SESSION AUTHORIZATION <parameter>username>
+SET [ SESSION | LOCAL ] SESSION AUTHORIZATION <replaceable class="PARAMETER">username>
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
RESET SESSION AUTHORIZATION
This command sets the session user identifier and the current user
- identifier of the current SQL-session context to be
-
username. The user name may be written as
- either an identifier or a string literal.
- The session user identifier is valid for the duration of a
- connection; for example, it is possible to temporarily become an
- unprivileged user and later switch back to become a superuser.
+ identifier of the current SQL-session context to be
+ class="PARAMETER">username. The user name may be
+ written as either an identifier or a string literal. The session
+ user identifier is valid for the duration of a connection; for
+ example, it is possible to temporarily become an unprivileged user
+ and later switch back to become a superuser.
Examples
-
-SELECT SESSION_USER, CURRENT_USER;
- current_user | session_user
+SELECT SESSION_USER, CURRENT_USER;
+
+ session_user | current_user
--------------+--------------
peter | peter
-SET SESSION AUTHORIZATION 'paul';
+SET SESSION AUTHORIZATION 'paul';
+
+SELECT SESSION_USER, CURRENT_USER;
-SELECT SESSION_USER, CURRENT_USER;
- current_user | session_user
+ session_user | current_user
--------------+--------------
paul | paul
-screen>
+programlisting>
+
+
+
See Also
+
+
+
+
+
However, while the directory contents are secure, the default
client authentication setup allows any local user to connect to the
- database and even become the database superuser. If you don't trust
+ database and even become the database superuser. If you do not trust
other local users, we recommend you use initdb's
or option to assign a
password to the database superuser. After initdb,
probably means your kernel's limit on the size of shared memory is
smaller than the buffer area
PostgreSQL
is trying to create (83918612 bytes in this example). Or it could
- mean that you don't have System-V-style shared memory support
+ mean that you do not have System-V-style shared memory support
configured into your kernel at all. As a temporary workaround, you
can try starting the postmaster with a smaller-than-normal number
of buffers ( switch). You will eventually want
On the other side of the coin, some systems allow individual
processes to open large numbers of files; if more than a few
processes do so then the system-wide limit can easily be exceeded.
- If you find this happening, and don't want to alter the system-wide
+ If you find this happening, and you do not want to alter the system-wide
limit, you can set
PostgreSQL's
max_files_per_process configuration parameter to
limit the consumption of open files.
SQL has become the most popular relational query
language.
- The name
"SQL" is an abbreviation for
+ The name
SQL
is an abbreviation for
Structured Query Language.
In 1974 Donald Chamberlin and others defined the
language SEQUEL (Structured English Query
This original standard version of
SQL is often
referred to,
- informally, as
"SQL/86". In 1989 the original
+ informally, as
SQL/86
. In 1989 the original
standard was extended
and this new standard is often, again informally, referred to as
-
"SQL/89". Also in 1989, a related standard called
+
SQL/89
. Also in 1989, a related standard called
Database Language Embedded SQL
definition of a greatly expanded version of the original standard,
referred to informally as
SQL2
or
SQL/92. This version became a
- ratified standard - "International Standard ISO/IEC 9075:1992,
- Database Language
SQL" - in late 1992.
+ ratified standard -
International Standard ISO/IEC 9075:1992,
+ Database Language
SQL - in late 1992.
- normally meant when people refer to
"the
SQL
- standard". A detailed
+ normally meant when people refer to
the
SQL
+ standard. A detailed
description of
SQL/92 is given in
. At the time of
writing this document a new standard informally referred to
can be formulated using relational algebra can also be formulated
using the relational calculus and vice versa.
This was first proved by E. F. Codd in
- 1972. This proof is based on an algorithm ("Codd's reduction
- algorithm") by which an arbitrary expression of the relational
+ 1972. This proof is based on an algorithm (
Codd's reduction
+ algorithm) by which an arbitrary expression of the relational
calculus can be reduced to a semantically equivalent expression of
relational algebra. For a more detailed discussion on that refer to
- It is sometimes said that languages based on the relational calculus
- are "higher level" or "more declarative" than languages based on
- relational algebra because the algebra (partially) specifies the order
- of operations while the calculus leaves it to a compiler or
- interpreter to determine the most efficient order of evaluation.
+ It is sometimes said that languages based on the relational
+ calculus are higher level
or more
+ declarative than languages based on relational algebra
+ because the algebra (partially) specifies the order of operations
+ while the calculus leaves it to a compiler or interpreter to
+ determine the most efficient order of evaluation.
- Now we will illustrate the complex syntax of the SELECT statement
- with various examples. The tables used for the examples are defined in
- .
+ Now we will illustrate the complex syntax of the
+ SELECT statement with various examples. The
+ tables used for the examples are defined in
+ linkend="supplier-fig" endterm="supplier-fig">.
- Using "*" in the SELECT statement will deliver all attributes from
+ Using *
in the SELECT statement will deliver all attributes from
the table. If we want to retrieve only the attributes PNAME and PRICE
from table PART we use the statement:
Cam | 25
- Note that the
SQL SELECT corresponds to the
- "projection" in relational algebra not to the "selection"
- (see for more details).
+ Note that the
SQL SELECT corresponds to the
+ projection
in relational algebra not to the
+
selection
(see
+ endterm="rel-alg"> for more details).
- If you don't want to use your database anymore you can remove it.
+ If you do not want to use your database anymore you can remove it.
For example, if you are the owner (creator) of the database
mydb, you can destroy it using the following
command:
If any input arguments are unknown
, check the type
categories accepted at those argument positions by the remaining
-candidates. At each position, select the "string" category if any
+candidates. At each position, select the string
category if any
candidate accepts that category (this bias towards string is appropriate
since an unknown-type literal does look like a string). Otherwise, if
all the remaining candidates accept the same type category, select that
The newer version 1
calling convention is indicated by writing
a PG_FUNCTION_INFO_V1() macro call for the function,
as illustrated below. Lack of such a macro indicates an old-style
- ("version 0") function. The language name specified in CREATE FUNCTION
+ (version 0
) function. The language name specified in CREATE FUNCTION
is C in either case. Old-style functions are now deprecated
because of portability problems and lack of functionality, but they
are still supported for compatibility reasons.
zero, and a negative length requests that the remainder of the
value be returned. These routines provide more efficient access to
parts of large values in the case where they have storage type
- "external". (The storage type of a column can be specified using
+ external
. (The storage type of a column can be specified using
ALTER TABLE tablename ALTER
COLUMN colname SET STORAGE
storagetype. Storage type is one of
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
can be used to build a HeapTuple> given user data
- in C string form. "values" is an array of C strings, one for
+ in C string form. values
is an array of C strings, one for
each attribute of the return tuple. Each C string should be in
the form expected by the input function of the attribute data
type. In order to return a null value for one of the attributes,
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.208 2002/12/15 16:17:38 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.209 2003/02/19 04:06:28 momjian Exp $
*
*
* INTERFACE ROUTINES
/*
* Ensure to hold an exclusive lock throughout the transaction. The
- * lock could be less intensive (in the non-overwrite path) but now
- * it's AccessExclusiveLock for simplicity.
+ * lock could be less intensive (in the non-overwrite path) but for
+ * now it's AccessExclusiveLock for simplicity.
*/
rel = heap_open(relid, AccessExclusiveLock);