SELECT AND CONSUME - Teradata Database

SQL Data Manipulation Language

Product
Teradata Database
Release Number
15.00
Language
English (United States)
Last Update
2018-09-28
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata® Database

SELECT AND CONSUME

Purpose  

Returns data from the row with the oldest insertion timestamp in the specified queue table and deletes the row from the queue table.

Syntax  

where:

 

Syntax Element …

Specifies …

select_list

an asterisk ( * ) or a comma-separated list of valid SQL expressions.

If select_list specifies *, all columns from the queue table specified in the FROM clause are returned.

The select list must not contain aggregate or ordered analytical functions.

queue_table_name

the name of a queue table that was created with the QUEUE option in the CREATE TABLE statement.

ANSI Compliance

SELECT AND CONSUME is a Teradata extension to the ANSI SQL:2011 standard.

Required Privileges

To perform a SELECT AND CONSUME from a queue table, you must have the SELECT and DELETE privileges on that table.

Locking and Concurrency

A SELECT AND CONSUME operation sets a row hash‑level WRITE lock. To upgrade to an EXCLUSIVE lock, use the LOCKING request modifier (see “LOCKING Request Modifier” on page 414).

If the request enters a delay state, the lock is not granted until after a row is inserted into the queue table.

Attributes of a Queue Table

A queue table is similar to an ordinary base table, with the additional unique property of behaving like an asynchronous first-in-first-out (FIFO) queue.

The first column of a queue table contains Queue Insertion TimeStamp (QITS) values. The CREATE TABLE request must define the first column with these data type and attributes:

     TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)

The QITS value of a row indicates the time the row was inserted into the queue table, unless a different, user-supplied value is inserted.

Transaction Processing Semantics

Multiple SELECT AND CONSUME statements can appear in a transaction when:

  • The session mode is ANSI.
  • The session mode is Teradata and the SELECT AND CONSUME statements appear between BEGIN TRANSACTION (BT) and END TRANSACTION (ET) statements.
  • A SELECT AND CONSUME statement can consume any rows inserted into a queue table within the same transaction.

    When performing SELECT AND CONSUME statements:

  • Perform SELECT AND CONSUME statements early in a transaction to avoid conflicts with other database resources.
  • Perform as few SELECT AND CONSUME statements, ideally only one, in a transaction as possible.
  • FIFO Ordering of Queue Table Rows Is Approximate

    A queue table provides only provides an approximate FIFO ordering of its rows for these reasons:

  • System clocks on the nodes of an MPP system are not synchronized.
  • A user-supplied value that is different from the current timestamp can be inserted into the QITS column.
  • The rollback of a transaction or a request may restore rows with an earlier QITS value than rows already consumed.
  • Multiple rows will have the same value for the QITS column when the INSERT statements in a multistatement request use the default value.
  • Teradata workload analysis management software rules may defer the SELECT AND CONSUME statement. For example, if a statement references a user, account, or other object that has a rule defined on it, the statement is subject to that rule. Deferment might be because of a Teradata dynamic workload management software throttle against an attribute of the containing request that causes it to be delayed until some currently running requests finish.
  • The best practice is not to apply Teradata workload analyzer restrictions to sessions that execute SELECT AND CONSUME statements.

    For information about creating workload management rules, see Teradata Workload Analyzer User Guide.

    SELECT AND CONSUME Statements Operate Like a FIFO Pop Operation

    SELECT AND CONSUME statements operate like a FIFO pop in these ways.

  • Data is returned from the row that has the oldest value in the QITS column of the specified queue table.
  • The row is deleted from the queue table, guaranteeing that the row is processed only once.
  • Rule for Embedded SQL

    The selection form of a DECLARE CURSOR statement cannot specify a SELECT AND CONSUME statement if the preprocessor TRANSACT or -tr option is set to ANSI.

    Rules and Restrictions for SELECT AND CONSUME Statements

    The rules and restrictions are:

  • Only one SELECT AND CONSUME statement can appear in a multistatement request.
  • A multistatement request that includes a SELECT AND CONSUME statement cannot also include a DELETE, UPDATE, UPSERT, or MERGE statement that references the same queue table.
  • SELECT AND CONSUME statements cannot be specified in any of these SQL constructs:
  • Subqueries with the exception INSERT … SELECT statements
  • Search conditions
  • Logical predicates
  • Operations using the SQL UNION, MINUS, or EXCEPT/MINUS SET operators
  • A limit of 20 percent of the total possible PE sessions (24 per PE by default) can enter a delay state as a result of submitting a SELECT AND CONSUME statement.
  • Performance Characteristics of SELECT AND CONSUME Statements

    The performance of SELECT AND CONSUME statements is:

  • Similar to that of selecting a single row from a base table using a USI.
  • More expensive than selecting a single row from a base table using a primary index.
  • If a queue table contains no rows when a SELECT AND CONSUME statement is made on it, its containing transaction enters a delay state until one of the following occurs.

  • A row is inserted into the queue table.
  • The transaction aborts as a result of one of these reasons.
  • Direct user intervention, such as an ABORT statement
  • Indirect user intervention, such as a DROP TABLE statement on the queue table
  • Example : Returning the Row Having the Oldest QITS Value in a Queue Table

    This CREATE TABLE statement creates a queue table named shopping_cart:

         CREATE SET TABLE shopping_cart, QUEUE (
           cart_qits  TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
           product_id INTEGER,
           quantity   INTEGER )
       PRIMARY INDEX (product_id);

    This SELECT AND CONSUME statement returns all the columns of the row with the oldest value in the cart_qits column of the shopping_cart table:

         SELECT AND CONSUME TOP 1 * FROM shopping_cart;

    For More Information

    For more information about:

  • Using SELECT requests to browse through queue table data without deleting rows, see “SELECT and Queue Tables” on page 38
  • Inserting data into queue tables, see “Inserting Rows Into Queue Tables” on page 389.
  • Using cursors with queue tables, see SQL Stored Procedures and Embedded SQL.
  • Using SELECT AND CONSUME … INTO statements with embedded SQL, see SQL Stored Procedures and Embedded SQL.
  • Creating queue tables, see “CREATE TABLE (Queue Table Form)” in SQL Data Definition Language.