Counting across parallel branches with the dbms_atomic package

Named Atomic Values

The body of a parallel loop must not modify any variable that is declared outside the loop. Previously, you could only work with a table per row plus a count(*) afterwards. That remains possible, but for example, it is not possible to determine within a parallel branch whether that branch is the last running one.

The dbms_atomic package offers possibilities to share values within a session and refer to them by name. These values are reliable even in parallel processing with PARALLEL (see Meer datadoorvoer dankzij "parallel" PSQL constructies).

The values of these shared values can be one of three types:

  • number,
  • text,
  • list of text.

The name of each procedure and function in dbms_atomic ends with the data type of the value it operates on (with the exception of remove). For example, compare_and_set_number works on a number.

A named value is created at first use and thereafter has one type. Reading an unknown name returns null.

Counting in a Parallel Loop

The most commonly used operation is increment_number. This increases the value of a number and returns the new value.

Data Dictionary

The session values are found in the Data Dictionary view SystemPSqlAtomics:

select name
,      kind
,      number_value
,      element_count
,      total_length
,      operation_count
,      contended_count
,      wait_duration_ms
,      last_modified_by_instance
from   SystemPSqlAtomics@DataDictionary
order
by     wait_duration_ms desc

Lifespan of a Value

A value exists as long as the session exists, not as long as the job or loop exists.

Limits

A session contains a maximum of 10,000 names, a list a maximum of 100,000 elements, and text a maximum of 10,000,000 characters. An error message will follow in case of (impending) exceedance.

Availability

dbms_atomic is available from release 27.0 onwards on all products.

See Also