+ class="PARAMETER">name IN SHARE MODE
+ statement will wait until any concurrent holders of ROW
+ EXCLUSIVE mode locks commit or roll back. Thus, once you
+ obtain the lock, there are no uncommitted writes outstanding;
+ furthermore none can begin until you release the lock.
-
+
- If a transaction of this sort is going to
- change the data in the table, then it should use SHARE ROW EXCLUSIVE lock
- mode instead of SHARE mode. This ensures that only one transaction of
- this type runs at a time. Without this, a deadlock is possible: two
- transactions might both acquire SHARE mode, and then be unable to also
- acquire ROW EXCLUSIVE mode to actually perform their updates. (Note that
- a transaction's own locks never conflict, so a transaction can acquire
- ROW EXCLUSIVE mode when it holds SHARE mode --- but not if anyone else
- holds SHARE mode.)
+ To achieve a similar effect when running a transaction at the
+ isolation level serializable, you have to execute the LOCK
+ TABLE> statement before executing any data modification
+ statement. A serializable transaction's view of data will be
+ frozen when its first data modification statement begins. A later
+ LOCK TABLE> will still prevent concurrent writes --- but it
+ won't ensure that what the transaction reads corresponds to the
+ latest committed values.
- Two general rules may be followed to prevent deadlock conditions:
+ If a transaction of this sort is going to change the data in the
+ table, then it should use SHARE ROW EXCLUSIVE> lock mode
+ instead of SHARE> mode. This ensures that only one
+ transaction of this type runs at a time. Without this, a deadlock
+ is possible: two transactions might both acquire SHARE>
+ mode, and then be unable to also acquire ROW EXCLUSIVE>
+ mode to actually perform their updates. (Note that a transaction's
+ own locks never conflict, so a transaction can acquire ROW
+ EXCLUSIVE> mode when it holds SHARE> mode --- but not
+ if anyone else holds SHARE> mode.) To avoid deadlocks,
+ make sure all transactions acquire locks on the same objects in the
+ same order, and if multiple lock modes are involved for a single
+ object, then transactions should always acquire the most
+ restrictive mode first.
-
-
- Transactions have to acquire locks on the same objects in the same order.
-
-
- For example, if one application updates row R1 and than updates
- row R2 (in the same transaction) then the second application shouldn't
- update row R2 if it's going to update row R1 later (in a single transaction).
- Instead, it should update rows R1 and R2 in the same order as the first
- application.
-
-
-
-
- If multiple lock modes are involved for a single object,
- then transactions should always acquire the most restrictive mode first.
-
-
- An example for this rule was given previously when discussing the
- use of SHARE ROW EXCLUSIVE mode rather than SHARE mode.
-
-
-
-
-
-
PostgreSQL does detect deadlocks and will
- rollback at least one waiting transaction to resolve the deadlock.
- If it is not practical to code an application to follow the above rules
- strictly, an alternative solution is to be prepared to retry transactions
- when they are aborted by deadlocks.
+ More information about the lock modes and locking strategies can be
+ found in .
+
- When locking multiple tables, the command LOCK a, b;> is
- equivalent to LOCK a; LOCK b;>. The tables are locked one-by-one
- in the order specified in the
- LOCK command.
-
+
+
Parameters
+
+
+
+ name
+
+ The name (optionally schema-qualified) of an existing table to
+ lock.
+
+
+ The command LOCK a, b;> is equivalent to
+ LOCK a; LOCK b;>. The tables are locked one-by-one in
+ the order specified in the LOCK command.
+
+
+
+
+
+ lockmode
+
+ The lock mode specifies which locks this lock conflicts with.
+ Lock modes are described in .
+
+
+ If no lock mode is specified, then ACCESS
+ EXCLUSIVE, the most restrictive mode, is used.
+
+
+
+
+
-
-
- 1999-06-08
-
-
- Notes
-
+
+
Diagnostics
+
+
+
+ LOCK TABLE
+
+ Message returned if the lock was successfully acquired.
+
+
+
+
+
+
+
+
Notes
LOCK ... IN ACCESS SHARE MODE> requires SELECT>
LOCK is useful only inside a transaction block
- (BEGIN>...COMMIT>), since the lock is dropped
+ (BEGIN>/COMMIT> pair), since the lock is dropped
as soon as the transaction ends. A LOCK> command appearing
outside any transaction block forms a self-contained transaction, so the
lock will be dropped as soon as it is obtained.
-
RDBMS locking uses the following standard terminology:
-
-
-
- EXCLUSIVE
-
- An exclusive lock prevents other locks of the same type from being
- granted.
-
-
-
-
-
- SHARE
-
- A shared lock allows others to also hold the same type of lock,
- but prevents the corresponding EXCLUSIVE lock from being granted.
-
-
-
-
-
- ACCESS
-
- Locks table schema.
-
-
-
-
-
- ROW
-
- Locks individual rows.
-
-
-
-
+ LOCK TABLE> only deals with table-level locks, and so
+ the mode names involving ROW> are all misnomers. These
+ mode names should generally be read as indicating the intention of
+ the user to acquire row-level locks within the locked table. Also,
+ ROW EXCLUSIVE> mode is a sharable table lock. Keep in
+ mind that all the lock modes have identical semantics so far as
+ LOCK TABLE> is concerned, differing only in the rules
+ about which modes conflict with which.
-
-
PostgreSQL does not follow this terminology
- exactly. LOCK TABLE> only deals with table-level locks, and
- so the mode names involving ROW are all misnomers. These mode names
- should generally be read as indicating the intention of the user to
- acquire row-level locks within the locked table. Also,
- ROW EXCLUSIVE mode does not follow this naming convention accurately,
- since it is a sharable table lock. Keep in mind that all the lock modes
- have identical semantics so far as LOCK TABLE> is concerned,
- differing only in the rules about which modes conflict with which.
-
-
-
-
-
- Usage
-
+
+
Examples
- Obtain a SHARE lock on a primary key table when going to perform
+ Obtain a SHARE> lock on a primary key table when going to perform
inserts into a foreign key table:
BEGIN WORK;
LOCK TABLE films IN SHARE MODE;
SELECT id FROM films
INSERT INTO films_user_comments VALUES
(_id_, 'GREAT! I was waiting for it for so long!');
COMMIT WORK;
-
+
- Take a SHARE ROW EXCLUSIVE lock on a primary key table when going to perform
+ Take a SHARE ROW EXCLUSIVE> lock on a primary key table when going to perform
a delete operation:
BEGIN WORK;
LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
DELETE FROM films_user_comments WHERE id IN
(SELECT id FROM films WHERE rating < 5);
DELETE FROM films WHERE rating < 5;
COMMIT WORK;
-
+
-
-
- Compatibility
-
+
+
Compatibility
-
-
- 1998-09-24
-
-
- SQL92
-
- There is no
LOCK TABLE in
SQL92,
- which instead uses SET TRANSACTION to specify
- concurrency levels on transactions. We support that too; see
- for details.
-
+ There is no LOCK TABLE in the SQL standard,
+ which instead uses SET TRANSACTION to specify
+ concurrency levels on transactions. PostgreSQL supports that too;
+ see
+ endterm="SQL-SET-TRANSACTION-TITLE"> for details.
+
- Except for ACCESS SHARE, ACCESS EXCLUSIVE, and SHARE UPDATE EXCLUSIVE lock
-
modes, the PostgreSQL lock modes and the
- LOCK TABLE syntax are compatible with those
-
- refsect2>
+ Except for ACCESS SHARE>, ACCESS EXCLUSIVE>,
+ and SHARE UPDATE EXCLUSIVE> lock modes, the
+
PostgreSQL lock modes and the
+ LOCK TABLE syntax are compatible with those
+ para>
NOTIFY
SQL - Language Statements
+
-
- NOTIFY
-
-
- generate a notification
-
+ NOTIFY
+ generate a notification
+
-
- 1999-07-20
-
-
+
NOTIFY name
-
-
-
-
- 1998-10-07
-
-
- Inputs
-
-
-
-
- notifyname
-
- Notify condition to be signaled.
-
-
-
-
-
-
-
-
-
- 1998-10-07
-
-
- Outputs
-
-
-
-
-
-NOTIFY
-
-
- Acknowledgement that notify command has executed.
-
-
-
-
- Notify events
-
- Events are delivered to listening frontends; whether and how each frontend
- application reacts depends on its programming.
-
-
-
-
-
-
+
-
-
- 1998-10-07
-
-
- Description
-
+
+
Description
+
- The NOTIFY command sends a notify event to each
- frontend application that has previously executed
- LISTEN notifyname
- for the specified notify condition in the current database.
+ The NOTIFY command sends a notification event to each
+ client application that has previously executed
+ LISTEN name
+ for the specified notification name in the current database.
+
- The information passed to the frontend for a notify event includes the notify
-
condition name and the notifying backend process's
PID>. It is up to the
- database designer to define the condition names that will be used in a given
+ The information passed to the client for a notifiation event includes the notification
+
name and the notifying session's server process
PID>. It is up to the
+ database designer to define the notification names that will be used in a given
database and what each one means.
+
- Commonly, the notify condition name is the same as the name of some table in
- the database, and the notify event essentially means I changed this table,
+ Commonly, the notification name is the same as the name of some table in
+ the database, and the notify event essentially means, I changed this table,
take a look at it to see what's new. But no such association is enforced by
the NOTIFY and LISTEN commands. For
- example, a database designer could use several different condition names
+ example, a database designer could use several different notification names
to signal different sorts of changes to a single table.
+
NOTIFY provides a simple form of signal or
-
IPC> (interprocess communication) mechanism for a collection of processes
+ interprocess communication mechanism for a collection of processes
accessing the same
PostgreSQL database.
Higher-level mechanisms can be built by using tables in the database to
- pass additional data (beyond a mere condition name) from notifier to
+ pass additional data (beyond a mere notification name) from notifier to
listener(s).
+
When NOTIFY is used to signal the occurrence of changes
to a particular table, a useful programming technique is to put the
In this way, notification happens automatically when the table is changed,
and the application programmer can't accidentally forget to do it.
+
NOTIFY interacts with SQL transactions in some important
ways. Firstly, if a NOTIFY is executed inside a
transaction, the notify events are not delivered until and unless the
transaction is committed. This is appropriate, since if the transaction
- is aborted we would like all the commands within it to have had no
+ is aborted, all the commands within it have had no
effect, including NOTIFY. But it can be disconcerting if one
- is expecting the notify events to be delivered immediately. Secondly, if
- a listening backend receives a notify signal while it is within a transaction,
- the notify event will not be delivered to its connected frontend until just
+ is expecting the notification events to be delivered immediately. Secondly, if
+ a listening session receives a notification signal while it is within a transaction,
+ the notification event will not be delivered to its connected client until just
after the transaction is completed (either committed or aborted). Again, the
- reasoning is that if a notify were delivered within a transaction that was
+ reasoning is that if a notification were delivered within a transaction that was
later aborted, one would want the notification to be undone somehow---but
- the backend cannot take back
a notify once it has sent it to the frontend.
- So notify events are only delivered between transactions. The upshot of this
+ the server cannot take back
a notification once it has sent it to the client.
+ So notification events are only delivered between transactions. The upshot of this
is that applications using NOTIFY for real-time signaling
should try to keep their transactions short.
+
NOTIFY behaves like Unix signals in one important
- respect: if the same condition name is signaled multiple times in quick
- succession, recipients may get only one notify event for several executions
+ respect: if the same notification name is signaled multiple times in quick
+ succession, recipients may get only one notification event for several executions
of NOTIFY. So it is a bad idea to depend on the number
- of notifies received. Instead, use NOTIFY to wake up
+ of notifications received. Instead, use NOTIFY to wake up
applications that need to pay attention to something, and use a database
object (such as a sequence) to keep track of what happened or how many times
it happened.
+
- It is common for a frontend that sends NOTIFY to be
- listening on the same notify name itself. In that case it will get back a
- notify event, just like all the other listening frontends. Depending on the
- application logic, this could result in useless work---for example,
- re-reading a database table to find the same updates that that frontend just
- wrote out. In
PostgreSQL 6.4 and later, it is
- possible to avoid such extra work by noticing whether the notifying backend
- process's
PID> (supplied in the notify event message) is the same as one's own
- backend's
PID> (available from libpq>). When they are the same, the notify
- event is one's own work bouncing back, and can be ignored. (Despite what was
- said in the preceding paragraph, this is a safe technique.
-
PostgreSQL keeps self-notifies separate from notifies
- arriving from other backends, so you cannot miss an outside notify by ignoring
- your own notifies.)
+ It is common for a client that executes NOTIFY
+ to be listening on the same notification name itself. In that case
+ it will get back a notification event, just like all the other
+ listening sessions. Depending on the application logic, this could
+ result in useless work, for example, reading a database table to
+ find the same updates that that session just wrote out. It is
+ possible to avoid such extra work by noticing whether the notifying
+ session's server process
PID> (supplied in the
+ notification event message) is the same as one's own session's
+
PID> (available from libpq>). When they
+ are the same, the notification event is one's own work bouncing
+ back, and can be ignored. (Despite what was said in the preceding
+ paragraph, this is a safe technique.
+
PostgreSQL keeps self-notifiications
+ separate from notifications arriving from other sessions, so you
+ cannot miss an outside notification by ignoring your own
+ notifications.)
+
+
+
+
Parameters
+
+
+
+ name
+
+ Name of the notification to be signaled (any identifier).
+
+
+
+
+
+
+
+
Diagnostics
-
-
- 1998-10-07
-
-
- Notes
-
- name
- can be any string valid as a name;
- it need not correspond to the name of any actual table. If
- name
- is enclosed in double-quotes, it need not even be a syntactically
- valid name, but can be any string up to 63 characters long.
-
- In some previous releases of
- name
- had to be enclosed in double-quotes when it did not correspond to any existing
- table name, even if syntactically valid as a name. That is no longer required.
-
- In
PostgreSQL releases prior to 6.4, the backend
-
PID> delivered in a notify message was always the PID> of the frontend's own
- backend. So it was not possible to distinguish one's own notifies from other
- clients' notifies in those earlier releases.
-
-
+
+
+ NOTIFY
+
+ Message returned when the commmand has executed.
+
+
+
+
-
-
- Usage
-
+
+
Examples
+
Configure and execute a listen/notify sequence from
-
-
- Compatibility
-
-
-
-
- 1998-09-24
-
-
- SQL92
-
- There is no NOTIFY statement in
-
-
+
+
Compatibility
+
+ There is no NOTIFY statement in the SQL
+ standard.
+
PREPARE
SQL - Language Statements
+
-
- PREPARE
-
-
- create a prepared query
-
+ PREPARE
+ prepare a statement for execution
+
-
- 2002-08-12
-
-
- PREPARE plan_name [ (datatype [, ...] ) ] AS query
-
-
-
-
- 2002-08-12
-
-
- Inputs
-
-
-
-
- plan_name
-
- An arbitrary name given to this particular prepared query. It
- must be unique within a single session, and is used to execute
- or remove a previously prepared query.
-
-
-
-
- datatype
-
- The data-type of a parameter to the prepared query.
- To refer to the parameters in the prepared query itself,
- use $1, $2, etc.
-
-
-
-
- query
-
- Any SELECT>, INSERT>, UPDATE>,
- or DELETE> query.
-
-
-
-
-
-
-
-
-
- 2002-08-12
-
-
- Outputs
-
-
-
-
-
- PREPARE
-
-
- The query has been prepared successfully.
-
-
-
-
-
-
-
+
+PREPARE plan_name [ (datatype [, ...] ) ] AS statement
+
-
-
- 2002-08-12
-
-
- Description
-
+
+
Description
+
- PREPARE creates a prepared query. A prepared
- query is a server-side object that can be used to optimize
+ PREPARE creates a prepared statement. A prepared
+ statement is a server-side object that can be used to optimize
performance. When the PREPARE statement is
- executed, the specified query is parsed, rewritten, and
- planned. When a subsequent EXECUTE statement is
- issued, the prepared query need only be executed. Thus, the
+ executed, the specified statement is parsed, rewritten, and
+ planned. When an EXECUTE command is subsequently
+ issued, the prepared statement need only be executed. Thus, the
parsing, rewriting, and planning stages are only performed once,
- instead of every time the query is executed.
+ instead of every time the statement is executed.
- Prepared queries can take parameters: values that are
- substituted into the query when it is executed. To specify the
- parameters to a prepared query, include a list of data-types with
- the PREPARE statement. In the query itself, you
- can refer to the parameters by position using
+ Prepared statements can take parameters: values that are
+ substituted into the statement when it is executed. To include
+ parameters in a prepared statement, supply a list of data types in
+ the PREPARE statement, and, in the statement to
+ be prepared itself, refer to the parameters by position using
$1, $2, etc. When executing
- the query, specify the actual values for these parameters in the
-
EXECUTE statement -- refer to
- linkend="sql-execute" endterm="sql-execute-title">
- for more information.
+ the statement, specify the actual values for these parameters in
+
the EXECUTE statement. Refer to
+ linkend="sql-execute" endterm="sql-execute-title"> for more
+ information about that.
- Prepared queries are stored locally (in the current backend), and
- only exist for the duration of the current database session. When
- the client exits, the prepared query is forgotten, and so it must be
- re-created before being used again. This also means that a single
- prepared query cannot be used by multiple simultaneous database
- clients; however, each client can create their own prepared query
+ Prepared statements are only stored in and for the duration of
+ the current database session. When
+ the session ends, the prepared statement is forgotten, and so it must be
+ recreated before being used again. This also means that a single
+ prepared statement cannot be used by multiple simultaneous database
+ clients; however, each client can create their own prepared statement
to use.
- Prepared queries have the largest performance advantage when a
- single backend is being used to execute a large number of similar
- queries. The performance difference will be particularly
- significant if the queries are complex to plan or rewrite. For
+ Prepared statements have the largest performance advantage when a
+ single session is being used to execute a large number of similar
+ statements. The performance difference will be particularly
+ significant if the statements are complex to plan or rewrite, for
example, if the query involves a join of many tables or requires
- the application of several rules. If the query is relatively simple
+ the application of several rules. If the statement is relatively simple
to plan and rewrite but relatively expensive to execute, the
- performance advantage of prepared queries will be less noticeable.
+ performance advantage of prepared statements will be less noticeable.
+
-
-
- 2002-08-12
-
-
- Notes
-
-
- In some situations, the query plan produced by
-
PostgreSQL for a prepared query may be
- inferior to the plan produced if the query were submitted and
- executed normally. This is because when the query is planned (and
- the optimizer attempts to determine the optimal query plan), the
- actual values of any parameters specified in the query are
- unavailable.
PostgreSQL collects
- statistics on the distribution of data in the table, and can use
- constant values in a query to make guesses about the likely
- result of executing the query. Since this data is unavailable when
- planning prepared queries with parameters, the chosen plan may be
- sub-optimal. To examine the query plan
-
PostgreSQL has chosen for a prepared
- query, use EXPLAIN EXECUTE.
-
-
- For more information on query planning and the statistics
- collected by
PostgreSQL for query
- optimization purposes, see the
- endterm="sql-analyze-title"> documentation.
-
-
+
+
Parameters
+
+
+
+ plan_name
+
+ An arbitrary name given to this particular prepared
+ statement. It must be unique within a single session and is
+ subsequently used to execute or deallocate a previously prepared
+ statement.
+
+
+
+
+
+ datatype
+
+ The data type of a parameter to the prepared statement. To
+ refer to the parameters in the prepared statement itself, use
+ $1, $2, etc.
+
+
+
+
+
+ statement
+
+ Any SELECT>, INSERT>, UPDATE>,
+ or DELETE> statement.
+
+
+
+
-
-
- Compatibility
-
-
-
-
- 2002-08-12
-
-
- SQL92
-
- SQL92 includes a PREPARE statement, but it is
- only for use in embedded SQL clients. The
- PREPARE statement implemented by
-
PostgreSQL also uses a somewhat
- different syntax.
-
-
+
+
Diagnostics
+
+
+
+ PREPARE
+
+ Message returned if the statement has been prepared successfully.
+
+
+
+
+
+
+
+
Notes
+
+ In some situations, the query plan produced by for a prepared
+ statement may be inferior to the plan produced if the statement
+ were submitted and executed normally. This is because when the
+ statement is planned and the planer attempts to determine the
+ optimal query plan, the actual values of any parameters specified
+ in the statement are
+ unavailable.
PostgreSQL collects
+ statistics on the distribution of data in the table, and can use
+ constant values in a statement to make guesses about the likely
+ result of executing the statement. Since this data is unavailable
+ when planning prepared statements with parameters, the chosen plan
+ may be suboptimal. To examine the query plan
+
PostgreSQL has chosen for a prepared
+ statement, use EXPLAIN EXECUTE.
+
+
+ For more information on query planning and the statistics collected
+ by
PostgreSQL for that purpose, see
+ the
+ documentation.
+
+
+
+
+
Compatibility
+
+ The SQL standard includes a PREPARE statement,
+ but it is only for use in embedded SQL. This version of the
+ PREPARE statement also uses a somewhat different
+ syntax.
+
REINDEX
SQL - Language Statements
+
-
- REINDEX
-
-
- rebuild corrupted indexes
-
+ REINDEX
+ rebuild indexes
+
-
- 2000-03-30
-
-
+
REINDEX { DATABASE | TABLE | INDEX } name [ FORCE ]
-
-
-
-
- 2000-03-30
-
-
- Inputs
-
-
-
-
- DATABASE
-
- 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).
-
-
-
-
- TABLE
-
- Recreate all indexes of a specified table.
-
-
-
-
- INDEX
-
- Recreate a specified index.
-
-
-
-
- name
-
- The name of the specific database/table/index to be reindexed.
- Table and index names may be schema-qualified.
-
-
-
-
- FORCE
-
- Force rebuild of system indexes. Without this keyword
- REINDEX> skips system indexes that are not marked invalid.
- FORCE is irrelevant for REINDEX INDEX>, or when reindexing
- user indexes.
-
-
-
-
-
-
-
-
-
- 2000-03-30
-
-
- Outputs
-
-
-
-
-
-REINDEX
-
-
- Message returned if the table is successfully reindexed.
-
-
-
-
-
-
+
-
-
- 2000-03-30
-
-
- Description
-
+
+
Description
+
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:
- <orderedlist>
+ <itemizedlist>
An index has become corrupted, and no longer contains valid
- data. Although in theory this should never be necessary, in
+ data. Although in theory this should never happen, 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
+ are not being reclaimed. This can occur with B-tree indexes in PostgreSQL
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
- for more information. The rest of this reference page
- mostly discusses how to use REINDEX to
- recover from index corruption.
+ for more information.
- orderedlist>
+ itemizedlist>
- If you suspect corruption of an index on a user table, you can
- simply rebuild that index, or all indexes on the table, using
- REINDEX INDEX or REINDEX TABLE.
+ If you suspect corruption of an index on a user table, you can
+ simply rebuild that index, or all indexes on the table, using
+ REINDEX INDEX or REINDEX
+ TABLE. Another approach to dealing with a corrupted
+ user-table index is just to drop and recreate it. This may in fact
+ be preferable if you would like to maintain some semblance of
+ normal operation on the table meanwhile. REINDEX>
+ acquires exclusive lock on the table, while CREATE
+ INDEX> only locks out writes not reads of the table.
-
- Another approach to dealing with a corrupted user-table index is
- just to drop and recreate it. This may in fact be preferable if
- you would like to maintain some semblance of normal operation on
- the table meanwhile. REINDEX> acquires exclusive lock
- on the table, while CREATE INDEX> only locks out writes
- not reads of the table.
-
-
-
- Things are more difficult if you need to recover from corruption of an
- index on a system table. In this case it's important for the backend
- doing the recovery to not have used any of the suspect indexes itself.
- (Indeed, in this sort of scenario you may find that backends are
- crashing immediately at start-up, due to reliance on the corrupted
- indexes.) To recover safely, the postmaster must be shut down and a
- stand-alone
PostgreSQL backend must be
- started instead, giving it
- the command-line options -O and -P (these options allow system table
- modifications and prevent use of system indexes, respectively). Then
- issue REINDEX DATABASE>, REINDEX TABLE>,
- REINDEX INDEX>, or depending on how much you want to reconstruct.
- If in doubt, use REINDEX DATABASE FORCE> to force reconstruction
- of all system indexes in the database. Then quit the standalone backend
- and restart the postmaster.
+ Things are more difficult if you need to recover from corruption of
+ an index on a system table. In this case it's important for the
+ system to not have used any of the suspect indexes itself.
+ (Indeed, in this sort of scenario you may find that server
+ processes are crashing immediately at start-up, due to reliance on
+ the corrupted indexes.) To recover safely, the server must be shut
+ down and a stand-alone
PostgreSQL server
+ must be started instead with the command-line options
+ and . (These options allow
+ system table modifications and prevent use of system indexes,
+ respectively.) Then, REINDEX DATABASE>,
+ REINDEX TABLE>, or REINDEX INDEX> can be
+ issued, depending on how much you want to reconstruct. If in
+ doubt, use REINDEX DATABASE FORCE> to force
+ reconstruction of all system indexes in the database. Then quit
+ the standalone server session and restart the real server.
- Since this is likely the only situation when most people will ever use
- a standalone backend, some usage notes might be in order:
+ See the reference page for more
+ information about how to interact with the stand-alone server
+ interface.
+
+
+
+
+
Parameters
-
+
+
+ DATABASE
- Start the backend with a command like
-
-postgres -D $PGDATA -O -P my_database
-
- Provide the correct path to the database area with
- make sure that the environment variable PGDATA> is set.
- Also specify the name of the particular database you want to work in.
+ Recreate all system indexes of a specified database. Indexes on
+ user tables are not included. This form of REINDEX>
+ can only be used in stand-alone mode (see above).
+
+
+ TABLE
- You can issue any SQL command, not only REINDEX>.
+ Recreate all indexes of a specified table.
+
+
+ INDEX
- Be aware that the standalone backend treats newline as the command
- entry terminator; there is no intelligence about semicolons,
- as there is in
psql>. To continue a command
- across multiple lines, you must type backslash just before each
- newline except the last one.
- Also, you won't have any of the conveniences of command-line editing
- (no command history, for example).
+ Recreate a specified index.
+
+
+ name
- T
o quit the backend, type EOF> (
- action="simul">Control>D>>, usually).
+ The name of the specific database, table, or index to be
+ reindexed. Table and index names may be schema-qualified.
+
-
+
+ FORCE
+
+ Force rebuild of system indexes. Without this key word,
+ REINDEX> skips system indexes that are not marked
+ invalid. FORCE> is irrelevant for REINDEX
+ INDEX> or when reindexing user indexes.
+
+
+
+
+
+
+
+
Diagnostics
- See the reference page for more information.
-
+
+
+ REINDEX
+
+ Message returned if the indexes were successfully recreated.
+
+
+
+
-
-
- Usage
-
+
+
Examples
+
- Recreate the indexes on the table mytable:
+ Recreate the indexes on the table my_table:
- REINDEX TABLE mytable;
-
+REINDEX TABLE my_table;
+
Rebuild a single index:
- REINDEX INDEX my_index;
-
+REINDEX INDEX my_index;
+
- Rebuild all system indexes (this will only work in a standalone backend):
+ Rebuild all system indexes (this will only work in a stand-alone
+ server session):
- REINDEX DATABASE my_database FORCE;
-
+REINDEX DATABASE my_database FORCE;
+
-
-
- Compatibility
-
-
-
-
- 2000-03-30
-
-
- SQL92
-
- There is no
REINDEX in
SQL92.
-
-
+
+
Compatibility
+
+ There is no REINDEX command in the SQL standard.
+
Description
- REVOKE allows the creator of an object to revoke
- previously granted permissions from one or more users or groups of users.
- The key word PUBLIC refers to the implicitly defined
- group of all users.
+ The REVOKE command revokes previously granted
+ privileges from one or more users or groups of users. The key word
+ PUBLIC refers to the implicitly defined group of
+ all users.
Note that any particular user will have the sum
of privileges granted directly to him, privileges granted to any group he
is presently a member of, and privileges granted to
- PUBLIC. Thus, for example, revoking SELECT privilege
+ PUBLIC. Thus, for example, revoking SELECT> privilege
from PUBLIC does not necessarily mean that all users
- have lost SELECT privilege on the object: those who have it granted
+ have lost SELECT> privilege on the object: those who have it granted
directly or via a group will still have it.
Compatibility
-
-
SQL92
-
The compatibility notes of the command
apply analogously to REVOKE. The syntax summary is:
-REVOKE [ GRANT OPTION FOR ] { SELECT | INSERT | UPDATE | DELETE | REFERENCES }
+REVOKE [ GRANT OPTION FOR ] privileges
ON object [ ( column [, ...] ) ]
FROM { PUBLIC | username [, ...] }
{ RESTRICT | CASCADE }
One of RESTRICT or CASCADE
is required.
-
ROLLBACK
SQL - Language Statements
+
-
- ROLLBACK
-
-
- abort the current transaction
-
+ ROLLBACK
+ abort the current transaction
+
-
- 1999-07-20
-
-
+
ROLLBACK [ WORK | TRANSACTION ]
-
-
-
-
- 1998-09-24
-
-
- Inputs
-
- None.
-
-
-
-
-
- 1998-09-24
-
-
- Outputs
-
-
-
-
-
-ROLLBACK
-
-
- Message returned if successful.
-
-
-
-
-
-
-WARNING: ROLLBACK: no transaction in progress
-
-
- If there is not any transaction currently in progress.
-
-
-
-
-
-
+
-
-
- 1998-09-24
-
-
- Description
-
+
+
Description
ROLLBACK rolls back the current transaction and causes
all the updates made by the transaction to be discarded.
+
+
+
+
Parameters
-
-
- 1998-09-24
-
-
- Notes
-
- Use
- to successfully terminate a transaction.
- is a
- synonym for ROLLBACK.
-
-
+
+
+ WORK
+ TRANSACTION
+
+ Optional key words. They have no effect.
+
+
+
+
-
-
- Usage
-
+
+
Diagnostics
+
+
+
+ ROLLBACK
+
+ Message returned if successful.
+
+
+
+
+
+ WARNING: ROLLBACK: no transaction in progress
+
+ If there is not any transaction currently in progress.
+
+
+
+
+
+
+
+
Notes
+
- To abort all changes:
+ Use to
+ successfully terminate a transaction.
+
+
-ROLLBACK WORK;
-
+
+
Examples
+
+ To abort all changes:
+ROLLBACK;
+
-
-
- Compatibility
-
+
+
Compatibility
-
-
- 1998-09-24
-
-
- SQL92
-
-
SQL92 only specifies the two forms
ROLLBACK
- and ROLLBACK WORK. Otherwise full compatibility.
-
-
+ The SQL standard only specifies the two forms
+ ROLLBACK and ROLLBACK
+ WORK. Otherwise, this command is fully conforming.
+
-
- 2002-07-26
-
-
START TRANSACTION
SQL - Language Statements
-
+
START TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ONLY ]
-
-
-
-
- 1998-09-27
-
-
- Inputs
-
-
- None.
-
-
-
-
-
- 1998-09-27
-
-
- Outputs
-
-
-
-
-
-
-START TRANSACTION
-
-
- Message returned if successful.
-
-
-
-
-
-WARNING: BEGIN: already a transaction in progress
-
-
- If there is already a transaction in progress when the
- command is issued.
-
-
-
-
-
-
+
respects, the behavior of this command is identical to the
linkend="sql-begin" endterm="sql-begin-title"> command.
+
+
+
+
Parameters
+ See under
+ endterm="sql-set-transaction-title"> about the meaning of the
+ parameters.
+
+
+
+
+
Diagnostics
+
+
+
+ START TRANSACTION
+
+ Message returned if successful.
+
+
+
+
+
+ WARNING: BEGIN: already a transaction in progress
+
+ Message returned if there was already a transaction in progress
+ when the command was issued.
+
+
+
+
- id="R1-SQL-START-TRANSACTION-3">
+
Compatibility
- SQL99; but see also the compatibility section of
- linkend="sql-set-transaction" endterm="sql-set-transaction-title">.
+ This command conforms to the SQL standard; but see also the
+ compatibility section of
+ endterm="sql-set-transaction-title">.
TRUNCATE
SQL - Language Statements
+
-
- TRUNCATE
-
-
- empty a table
-
+ TRUNCATE
+ empty a table
+
-
- 1999-07-20
-
-
+
TRUNCATE [ TABLE ] name
-
-
-
-
- 1998-09-08
-
-
- Inputs
-
-
-
-
- name
-
- The name (optionally schema-qualified) of the table to be truncated.
-
-
-
-
-
-
-
-
-
- 1998-09-08
-
-
- Outputs
-
-
-
-
-
-TRUNCATE TABLE
-
-
- Message returned if the table is successfully truncated.
-
-
-
-
-
-
+
-
-
- 1998-09-08
-
-
- Description
-
+
+
Description
+
TRUNCATE quickly removes all rows from a
table. It has the same effect as an unqualified
table it is faster. This is most useful on large tables.
+
+
+
Parameter
+
+
+
+ name
+
+ The name (optionally schema-qualified) of the table to be truncated.
+
+
+
+
+
+
+
+
Diagnostics
+
+
+
+ TRUNCATE TABLE
+
+ Message returned if the table was successfully truncated.
+
+
+
+
+
+
+
+
Examples
-
-
- Usage
-
Truncate the table bigtable:
TRUNCATE TABLE bigtable;
-
+
-
-
- Compatibility
-
-
-
-
- 1998-09-08
-
-
- SQL92
-
- There is no
TRUNCATE in
SQL92.
-
-
+
+
Compatibility
+
+ There is no TRUNCATE command in the SQL standard.
+
UNLISTEN
SQL - Language Statements
+
-
- UNLISTEN
-
-
- stop listening for a notification
-
+ UNLISTEN
+ stop listening for a notification
+
-
- 1998-10-19
-
-
-UNLISTEN { notifyname | * }
-
-
-
-
- 1998-10-19
-
-
- Inputs
-
-
-
-
- notifyname
-
- Name of previously registered notify condition.
-
-
-
-
- *
-
- All current listen registrations for this backend are cleared.
-
-
-
-
-
-
-
-
-
- 1998-10-19
-
-
- Outputs
-
-
-
-
-
-UNLISTEN
-
-
- Acknowledgment that statement has executed.
-
-
-
-
-
-
+
+UNLISTEN { name | * }
+
-
-
- 1998-10-19
-
-
- Description
-
+
+
Description
- UNLISTEN
- is used to remove an existing NOTIFY registration.
- UNLISTEN cancels any existing registration of the current
-
PostgreSQL session as a listener on the notify
- condition notifyname.
- The special condition wildcard * cancels all listener registrations
- for the current session.
+ UNLISTEN is used to remove an existing
+ registration for NOTIFY events.
+ UNLISTEN cancels any existing registration of
+ the current
PostgreSQL session as a
+ listener on the notification
+ class="PARAMETER">name. The special wildcard
+ * cancels all listener registrations for the
+ current session.
discussion of the use of LISTEN and
NOTIFY.
+
+
+
+
Parameters
+
+
+
+ name
+
+ Name of a notification (any identifier).
+
+
+
+
+
+ *
+
+ All current listen registrations for this session are cleared.
+
+
+
+
+
+
+
+
Diagnostics
+
+
+
+ UNLISTEN
+
+ Message returned when the command has executed.
+
+
+
+
+
+
+
+
Notes
+
+ You unlisten something you were not listening for; no warning or error will appear.
+
-
-
- 1998-10-19
-
-
- Notes
-
- notifyname
- need not be a valid class name but can be any string valid
- as a name up to 64 characters long.
-
- The backend does not complain if you unlisten something you were not
- listening for.
- Each backend will automatically execute UNLISTEN * when
- exiting.
-
-
+ At the end of each session, UNLISTEN * ist
+ automatically executed.
+
-
-
- Usage
-
+
+
Examples
- To subscribe to an existing registration:
+ To make a registration:
LISTEN virtual;
-LISTEN
NOTIFY virtual;
-NOTIFY
Asynchronous NOTIFY 'virtual' from backend with pid '8448' received
UNLISTEN virtual;
-UNLISTEN
NOTIFY virtual;
-NOTIFY
--- notice no NOTIFY event is received
+-- no NOTIFY event is received
-
-
- Compatibility
-
-
-
-
- 1998-10-19
-
-
- SQL92
-
- There is no
UNLISTEN in
SQL92.
-
-
+
+
Compatibility
+
+ There is no UNLISTEN command in the SQL standard.
+
UPDATE
SQL - Language Statements
+
-
- UPDATE
-
-
- update rows of a table
-
+ UPDATE
+ update rows of a table
+
-
- 1999-07-20
-
-
-UPDATE [ ONLY ] table SET col = expression [, ...]
+
+UPDATE [ ONLY ] table SET column = expression [, ...]
[ FROM fromlist ]
[ WHERE condition ]
-
-
-
-
- 1998-09-24
-
-
- Inputs
-
-
-
-
- table
-
- The name (optionally schema-qualified) of an existing table. If
- ONLY> is specified, only that table is updated. If
- ONLY> is not specified, the table and all its
- descendant tables (if any) are updated. *> can be
- appended to the table name to indicate that descendant tables are
- to be scanned, but in the current version, this is the default
- behavior. (In releases before 7.1, ONLY> was the
- default behavior.) The default can be altered by changing the
- configuration option.
-
-
-
-
- column
-
- The name of a column in table.
-
-
-
-
- expression
-
- A valid expression or value to assign to column.
-
-
-
-
- fromlist
-
- non-standard extension to allow columns
- from other tables to appear in the WHERE condition.
-
-
-
-
- condition
-
- Refer to the SELECT statement for a further description
- of the WHERE clause.
-
-
-
-
-
-
-
-
-
- 1998-09-24
-
-
- Outputs
-
-
-
-
-
-UPDATE #
-
-
- Message returned if successful.
- The #
- means the number of rows updated.
- If #
- is 0 no rows are updated.
-
-
-
-
-
-
+
-
-
- 1998-09-24
-
-
- Description
-
- UPDATE changes the values of the columns specified for
- all rows which satisfy condition. Only the columns
- to be modified need appear as columns in the statement.
-
+
+
Description
- Array references use the same syntax found in
- .
- That is, either single array elements, a range of array
- elements or the entire array may be replaced with a single
- query.
+ UPDATE changes the values of the specified
+ columns in all rows that satisfy the condition. Only the columns to
+ be modified need appear as columns in the statement.
- You must have write access to the table in order to modify
- it, as well as read access to any table whose values are
- mentioned in the WHERE condition.
+ By default, UPDATE will update rows in the
+ specified table and all its subtables. If you wish to only update
+ the specific table mentioned, you must use the ONLY>
+ clause.
- By default UPDATE will update tuples in the table specified
- and all its sub-tables. If you wish to only update the
- specific table mentioned, you should use the ONLY clause.
+ You must have the UPDATE privilege on the table
+ to update it, as well as the SELECT
+ privilege to any table whose values are read in the
+ class="parameter">condition.
-
-
- Usage
-
+
+
Parameters
+
+
+
+ table
+
+ The name (optionally schema-qualified) of the table to update.
+
+
+
+
+
+ column
+
+ The name of a column in table.
+
+
+
+
+
+ expression
+
+ An expression or value to assign to the column.
+
+
+
+
+
+ fromlist
+
+ A list of table expressions, allowing columns from other tables
+ to appear in the WHERE> condition.
+
+
+
+
+
+ condition
+
+ A value expression that returns a value of type
+ boolean that determines the rows which are to be
+ updated.
+
+
+
+
+
+
+
+
Diagnostics
+
+
+
+ UPDATE count
+
+ Message returned if successful. The value
+ class="parameter">count is the number of rows
+ updated. If count
+ is 0, no rows were updated.
+
+
+
+
+
+
+
+
Examples
- Change word Drama> with Dramatic> on column kind>:
+ Change the word Drama> to Dramatic> in the
+ column kind> of the table films:
-UPDATE films
-SET kind = 'Dramatic'
-WHERE kind = 'Drama';
-SELECT *
-FROM films
-WHERE kind = 'Dramatic' OR kind = 'Drama';
-
- code | title | did | date_prod | kind | len
--------+---------------+-----+------------+----------+-------
- BL101 | The Third Man | 101 | 1949-12-23 | Dramatic | 01:44
- P_302 | Becket | 103 | 1964-02-03 | Dramatic | 02:28
- M_401 | War and Peace | 104 | 1967-02-12 | Dramatic | 05:57
- T_601 | Yojimbo | 106 | 1961-06-16 | Dramatic | 01:50
- DA101 | Das Boot | 110 | 1981-11-11 | Dramatic | 02:29
+UPDATE filme SET kind = 'Dramatic' WHERE kind = 'Drama';
-
-
- Compatibility
-
-
-
-
- 1998-09-24
-
-
- SQL92
-
-
SQL92 defines a different syntax for
- the positioned UPDATE statement:
-
-
-UPDATE table SET column = expression [, ...]
- WHERE CURRENT OF cursor
-
-
- where cursor
- identifies an open cursor.
-
-
+
+
Compatibility
+
+ This command conforms to the SQL standard. The
+ FROM clause is a PostgreSQL extension.
+
VACUUM
SQL - Language Statements
+
-
- VACUUM
-
-
- garbage-collect and optionally analyze a database
-
+ VACUUM
+ garbage-collect and optionally analyze a database
+
-
- 2001-08-26
-
-
+
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]
-
-
-
-
- 2001-07-10
-
-
- Inputs
-
-
-
-
- FULL
-
- Selects full
vacuum, which may reclaim more space,
- but takes much longer and exclusively locks the table.
-
-
-
-
- FREEZE
-
- Selects aggressive freezing
of tuples.
-
-
-
-
- VERBOSE
-
- Prints a detailed vacuum activity report for each table.
-
-
-
-
- ANALYZE
-
- Updates statistics used by the optimizer to
- determine the most efficient way to execute a query.
-
-
-
-
- table
-
- The name (optionally schema-qualified) of a specific table to
- vacuum. Defaults to all tables in the current database.
-
-
-
-
- column
-
- The name of a specific column to analyze. Defaults to all columns.
-
-
-
-
-
-
-
-
-
- 1998-10-04
-
-
- Outputs
-
-
-
-
-
-VACUUM
-
-
- The command is complete.
-
-
-
-
-
-
-INFO: --Relation table--
-
-
- The report header for table.
-
-
-
-
-
-
-INFO: Pages 98: Changed 25, Reapped 74, Empty 0, New 0;
- Tup 1000: Vac 3000, Crash 0, UnUsed 0, MinLen 188, MaxLen 188;
- Re-using: Free/Avail. Space 586952/586952; EndEmpty/Avail. Pages 0/74.
- Elapsed 0/0 sec.
-
-
- The analysis for table itself.
-
-
-
-
-
-
-INFO: Index index: Pages 28;
- Tuples 1000: Deleted 3000. Elapsed 0/0 sec.
-
-
- The analysis for an index on the target table.
-
-
-
-
-
-
+
-
-
- 2001-07-10
-
-
- Description
-
+
+
Description
+
VACUUM reclaims storage occupied by deleted tuples.
In normal
PostgreSQL operation, tuples that
- are deleted or obsoleted by UPDATE are not physically removed from
+ are deleted or obsoleted by an update are not physically removed from
their table; they remain present until a VACUUM is
done. Therefore it's necessary to do VACUUM
periodically, especially on frequently-updated tables.
are no other open transactions in the same database, then it is guaranteed
that all tuples in the database are frozen> and will not be
subject to transaction ID wraparound problems, no matter how long the
- database is left un-vacuumed.
+ database is left unvacuumed.
FREEZE is not recommended for routine use. Its only
intended usage is in connection with preparation of user-defined template
databases, or other databases that are completely read-only and will not
receive routine maintenance VACUUM> operations.
See for details.
+
+
+
+
Parameters
+
+
+
+ FULL
+
+ Selects full
vacuum, which may reclaim more
+ space, but takes much longer and exclusively locks the table.
+
+
+
+
+
+ FREEZE
+
+ Selects aggressive freezing
of tuples.
+
+
+
+
+
+ VERBOSE
+
+ Prints a detailed vacuum activity report for each table.
+
+
+
+
+
+ ANALYZE
+
+ Updates statistics used by the planner to determine the most
+ efficient way to execute a query.
+
+
+
+
+
+ table
+
+ The name (optionally schema-qualified) of a specific table to
+ vacuum. Defaults to all tables in the current database.
+
+
+
-
-
- 2001-07-10
-
-
- Notes
-
+
+ column
+
+ The name of a specific column to analyze. Defaults to all columns.
+
+
+
+
+
+
+
+
Diagnostics
+
+
+
+ VACUUM
+
+ The command is complete.
+
+
+
+
+
+ INFO: --Relation table--
+
+ The report header for table.
+
+
+
+
+
+ INFO: Pages 98: Changed 25, Reapped 74, Empty 0, New 0;
+ Tup 1000: Vac 3000, Crash 0, UnUsed 0, MinLen 188, MaxLen 188;
+ Re-using: Free/Avail. Space 586952/586952; EndEmpty/Avail. Pages 0/74.
+ Elapsed 0/0 sec.
+
+ The analysis for table itself.
+
+
+
+
+
+ INFO: Index index: Pages 28;
+ Tuples 1000: Deleted 3000. Elapsed 0/0 sec.
+
+ The analysis for an index on the target table.
+
+
+
+
+
+
+
+
Notes
We recommend that active production databases be
- VACUUM-ed frequently (at least nightly), in order to
+ vacuumed frequently (at least nightly), in order to
remove expired rows. After adding or deleting a large number
- of records, it may be a good idea to issue a VACUUM
+ of rows, it may be a good idea to issue a VACUUM
ANALYZE command for the affected table. This will update the
system catalogs with
the results of all recent changes, and allow the
-
PostgreSQL query
optimizer to make better
- choices in planning user queries.
+
PostgreSQL query
planner to make better
+ choices in planning queries.
to occupy less disk space. VACUUM FULL will usually
shrink the table more than a plain VACUUM would.
-
-
-
-
- Usage
-
+
+
Examples
+
The following is an example from running VACUUM on a table
in the regression database:
-regression=> VACUUM VERBOSE ANALYZE onek;
+=> VACUUM VERBOSE ANALYZE onek;
INFO: --Relation onek--
INFO: Index onek_unique1: Pages 14; Tuples 1000: Deleted 3000.
CPU 0.00s/0.11u sec elapsed 0.12 sec.
Total CPU 0.05s/0.45u sec elapsed 0.59 sec.
INFO: Analyzing onek
VACUUM
-
+
-
-
- Compatibility
-
-
-
-
- 1998-10-04
-
-
- SQL92
-
- There is no
VACUUM statement in
SQL92.
-
-
+
+
Compatibility
+
+ There is no VACUUM statement in the SQL standard.
+