Locking in the Teradata Interface

Overview

The SAS/ACCESS Teradata interface has LIBNAME and data set options that let you modify how Teradata handles locking. Use SAS/ACCESS locking options only when Teradata standard locking is undesirable. For tips on using these options, see Understanding SAS/ACCESS Locking Options and When to Use SAS/ACCESS Locking Options. Teradata examples are available.

READ_LOCK_TYPE= TABLE | VIEW | ROW

UPDATE_LOCK_TYPE= TABLE | VIEW

READ_MODE_WAIT= YES | NO

UPDATE_MODE_WAIT= YES | NO

READ_ISOLATION_LEVEL= ACCESS | READ | WRITE

Here are the valid values for this option.

Read Isolation Levels for Teradata

Isolation Level

Definition

ACCESS

Obtains an ACCESS lock by ignoring other users' ACCESS, READ, and WRITE locks. Permits other users to obtain a lock on the table or view. You see a snapshot of the data when the lock is specified.

Can return inconsistent or unusual results if another user is modifying the data at the same time that you are accessing the data.

READ

Obtains a READ lock if no other user holds a WRITE or EXCLUSIVE lock. Does not prevent other users from reading the object.

Specify this isolation level whenever possible. READ is usually adequate for most SAS/ACCESS processing.

WRITE

Obtains a WRITE lock on the table or view if no other user has a READ, WRITE, or EXCLUSIVE lock on the resource. You cannot explicitly release a WRITE lock. It is released only when the table is closed. Prevents other users from acquiring any lock but ACCESS.

This is unnecessarily restrictive, because it locks an entire table until a Read operation is finished.

UPDATE_ISOLATION_LEVEL= ACCESS | READ | WRITE

The valid values for this option, ACCESS, READ, and WRITE, are defined in the following table.

Update Isolation Levels for Teradata

Isolation Level

Definition

ACCESS

Obtains an ACCESS lock by ignoring other users' ACCESS, READ, and WRITE locks. Avoids a potential deadlock but can cause data corruption if another user is updating the same data.

READ

Obtains a READ lock if no other user holds a WRITE or EXCLUSIVE lock. Prevents other users from being granted a WRITE or EXCLUSIVE lock.

Locks an entire table or view, allowing other users to acquire READ locks. Can lead to deadlock situations.

WRITE

Obtains a WRITE lock on the table or view if no other user has a READ, WRITE, or EXCLUSIVE lock on the resource. You cannot explicitly release a WRITE lock. It is released only when the table is closed. Prevents other users from acquiring any lock but ACCESS.

Prevents all users, except those with ACCESS locks, from accessing a table. Prevents the possibility of a deadlock, but limits concurrent use of the table.

These locking options cause the LIBNAME engine to transmit a locking request to the DBMS; Teradata performs all data-locking. If you correctly specify a set of SAS/ACCESS read or update locking options, SAS/ACCESS generates locking modifiers that override the Teradata standard locking.

If you specify an incomplete set of locking options, SAS/ACCESS returns an error message. If you do not use SAS/ACCESS locking options, Teradata lock defaults are in effect. For a complete description of Teradata locking, see the LOCKING statement in your Teradata SQL reference documentation.

Understanding SAS/ACCESS Locking Options

SAS/ACCESS locking options modify Teradata standard locking. Teradata usually locks at the row level. SAS/ACCESS lock options can lock at the row, table, or view level. A change in the scope of a lock from row to table affects concurrent access to DBMS objects. Specifically, READ and WRITE table locks increase the amount of time that other users must wait to access the table and can decrease overall system performance. These measures help minimize these negative effects.

If you specify these read locking options, SAS/ACCESS generates and submits to Teradata locking modifiers that contain the values that you specify for the three read lock options:

If you specify these update lock options, SAS/ACCESS generates and submits to Teradata locking modifiers that contain the values that you specify for the three update lock options:

When to Use SAS/ACCESS Locking Options

This section describes situations that might require SAS/ACCESS lock options instead of the standard locking that Teradata provides.

One of these situations can result from the options and values in this situation.

When Lock Options Are Not Recognized

There are situations when the values for locking options are not recognized by Teradata. These are some examples of those situations:

To verify the lock type that is used, you can specify MSGLEVEL=I in the OPTIONS statement, or you can turn on lock logging in Teradata and review the locks in Teradata Viewpoint.

Examples

Specifying the Isolation Level to ACCESS for Teradata Tables

  /* This generates a quick survey of unusual customer purchases. */
libname cust teradata user=myusr1 password=mypwd1
               READ_ISOLATION_LEVEL=ACCESS
               READ_LOCK_TYPE=TABLE
               READ_MODE_WAIT=YES
               CONNECTION=UNIQUE;

proc print data=cust.purchases(where= (bill<2));
run;

data local;
 set cust.purchases (where= (quantity>1000));
run;

Here is what SAS/ACCESS does in the above example.

  • Connects to the Teradata DBMS and specifies the three SAS/ACCESS LIBNAME Read lock options.
  • Opens the Purchases table and obtains an ACCESS lock if another user does not hold an EXCLUSIVE lock on the table.
  • Reads and displays table rows with a value less than 2 in the Bill column.
  • Closes the Purchases table and releases the ACCESS lock.
  • Opens the Purchases table again and obtains an ACCESS lock if a different user does not hold an EXCLUSIVE lock on the table.
  • Reads table rows with a value greater than 1000 in the Quantity column.
  • Closes the Purchases table and releases the ACCESS lock.

Setting Isolation Level to WRITE to Update a Teradata Table

/* This updates the critical Rebate row. */
libname cust teradata user=myusr1 password=mypwd1;

proc sql;
  update cust.purchases(UPDATE_ISOLATION_LEVEL=WRITE
        UPDATE_MODE_WAIT=YES
        UPDATE_LOCK_TYPE=TABLE)
  set rebate=10 where bill>100;
quit;

In this example, here is what SAS/ACCESS does:

  • Connects to the Teradata DBMS and specifies the three SAS/ACCESS data set Update lock options.
  • Opens the Purchases table and obtains a WRITE lock if a different user does not hold a READ, WRITE, or EXCLUSIVE lock on the table.
  • Updates table rows with Bill greater than 100 and sets the Rebate column to 10.
  • Closes the Purchases table and releases the WRITE lock.
Note: UPDATE options are applied only to a preparatory SELECT statement when the SQL statements are passed to Teradata. As a result, deadlock errors are possible if the same table is being updated by other processes at the same time. In this case, the solution is to specify UPDATE_ISOLATION_LEVEL=ACCESS.

Preventing a Hung SAS Session When Reading and Inserting to the Same Table

/* SAS/ACCESS lock options prevent the session hang */
/* that occurs when reading & inserting into the same table. */
libname tra teradata user=myusr1 password=mypwd1 connection=unique;

proc sql;
insert into tra.sametable
   select * from tra.sametable(read_isolation_level=access
          read_mode_wait=yes
          read_lock_type=table);

Here is what SAS/ACCESS does in the above example:

  • Creates a read connection to fetch the rows selected (SELECT *) from Tra.Sametable and specifies an ACCESS lock (READ_ISOLATION_LEVEL=ACCESS). Teradata grants the ACCESS lock.
  • Creates an insert connection to Teradata to process the Insert operation to Tra.Sametable. Because the ACCESS lock that is already on the table permits access to the table, Teradata grants a WRITE lock.
  • Performs the Insert operation without hanging (suspending) your SAS session.
Last updated: February 3, 2026