From df630b0dd5ea2de52972d456f5978a012436115e Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 7 Oct 2014 17:23:34 -0300 Subject: Implement SKIP LOCKED for row-level locks This clause changes the behavior of SELECT locking clauses in the presence of locked rows: instead of causing a process to block waiting for the locks held by other processes (or raise an error, with NOWAIT), SKIP LOCKED makes the new reader skip over such rows. While this is not appropriate behavior for general purposes, there are some cases in which it is useful, such as queue-like tables. Catalog version bumped because this patch changes the representation of stored rules. Reviewed by Craig Ringer (based on a previous attempt at an implementation by Simon Riggs, who also provided input on the syntax used in the current patch), David Rowley, and Álvaro Herrera. Author: Thomas Munro --- doc/src/sgml/ref/select.sgml | 26 +++++++++++++++++--------- doc/src/sgml/sql.sgml | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 940d1aa5c0d..473939ab4eb 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -45,7 +45,7 @@ SELECT [ ALL | DISTINCT [ ON ( expressioncount | ALL } ] [ OFFSET start [ ROW | ROWS ] ] [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ] - [ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ] + [ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF table_name [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ] where from_item can be one of: @@ -1284,7 +1284,7 @@ FETCH { FIRST | NEXT } [ count ] { The locking clause has the general form -FOR lock_strength [ OF table_name [, ...] ] [ NOWAIT ] +FOR lock_strength [ OF table_name [, ...] ] [ NOWAIT | SKIP LOCKED ] where lock_strength can be one of @@ -1360,9 +1360,15 @@ KEY SHARE To prevent the operation from waiting for other transactions to commit, - use the NOWAIT option. With NOWAIT, the statement - reports an error, rather than waiting, if a selected row - cannot be locked immediately. Note that NOWAIT applies only + use either the NOWAIT or SKIP LOCKED + option. With NOWAIT, the statement reports an error, rather + than waiting, if a selected row cannot be locked immediately. + With SKIP LOCKED, any selected rows that cannot be + immediately locked are skipped. Skipping locked rows provides an + inconsistent view of the data, so this is not suitable for general purpose + work, but can be used to avoid lock contention with multiple consumers + accessing a queue-like table. + Note that NOWAIT and SKIP LOCKED apply only to the row-level lock(s) — the required ROW SHARE table-level lock is still taken in the ordinary way (see ). You can use @@ -1394,7 +1400,9 @@ KEY SHARE then it is processed as if it was only specified by the strongest one. Similarly, a table is processed as NOWAIT if that is specified in any of the clauses - affecting it. + affecting it. Otherwise, it is processed + as SKIP LOCKED if that is specified in any of the + clauses affecting it. @@ -1931,9 +1939,9 @@ SELECT distributors.* WHERE distributors.name = 'Westward'; PostgreSQL allows it in any SELECT query as well as in sub-SELECTs, but this is an extension. The FOR NO KEY UPDATE, FOR SHARE and - FOR KEY SHARE variants, - as well as the NOWAIT option, - do not appear in the standard. + FOR KEY SHARE variants, as well as the NOWAIT + and SKIP LOCKED options, do not appear in the + standard. diff --git a/doc/src/sgml/sql.sgml b/doc/src/sgml/sql.sgml index ba92607966c..57396d7c245 100644 --- a/doc/src/sgml/sql.sgml +++ b/doc/src/sgml/sql.sgml @@ -863,7 +863,7 @@ SELECT [ ALL | DISTINCT [ ON ( expressionexpression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] [ LIMIT { count | ALL } ] [ OFFSET start ] - [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ] + [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ] -- cgit v1.2.3