Lesson 9 of 13 Amazon Redshift

Avoiding Lock Conflicts in Redshift

This lesson explains how Redshift's table-level locking and isolation rules decide when concurrent sessions wait for each other, and how long transactions can block loads and queries for everyone.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how Redshift isolates transactions with table-level locks.
  • Find blocking sessions with system tables and resolve them.
  • Avoid conflicts with short transactions and scheduling.

How Redshift isolates transactions

Redshift controls concurrent access with table-level locks: a transaction that writes to a table locks the whole table, not individual rows. Provisioned clusters use serializable isolation by default, which guarantees that concurrent transactions produce the same result as if they had run one after another; Redshift Serverless uses snapshot isolation by default, and either can be set for each database.

Readers do not block writers, and writers do not block readers, because readers see a consistent snapshot of the data. Concurrent writes to the same table, however, conflict. When two transactions cannot be serialised, for example because both read and then update the same table, Redshift cancels one with a serializable isolation error, and the application must retry it.

Finding and resolving blocking

When queries or loads hang, they are often waiting for a lock that another session holds. STV_LOCKS shows the locks currently held on tables. SVV_TRANSACTIONS shows open transactions with the locks each holds or is waiting for, which identifies the session at the start of the chain, often a transaction left open by a client. PG_TERMINATE_BACKEND(pid) ends that session, releasing its locks. The lasting fix is to keep transactions short and commit promptly. The following table summarises the tools.

ToolUse
STV_LOCKSShows current table locks
SVV_TRANSACTIONSShows open transactions and the locks they hold or wait for
PG_TERMINATE_BACKEND(pid)Ends a blocking session

The following diagram shows the sequence that blocked the load and how it was cleared.

Why the nightly load hung. An analyst's SQL client began a transaction on orders in the afternoon and never committed, so its session held a lock. The load's exclusive lock request waited behind it until PG_TERMINATE_BACKEND ended the idle session, releasing the lock.
The times are illustrative.
Match the pairs

Finding a blocker

Exam skills 2.1.6

Match each tool with what it shows or does.

Log in to try this activity Interactive activities are free with an account.

Avoiding conflicts

Most locking problems can be avoided by design. Transactions should be kept short and committed promptly; a client that opens a transaction and then leaves the session idle holds its locks indefinitely and blocks everyone who needs those tables.

Loads should write to staging tables first, which nobody else uses, and then apply the changes to the target table in one short transaction, so the target is locked only briefly. Jobs that write to the same table should be scheduled so that they do not overlap. Finally, applications and pipelines should be built to retry transactions that fail with serializable isolation errors, because occasional conflicts are expected in a busy warehouse.

Select all that apply

Avoiding lock conflicts

Exam skills 2.1.6

Identify the practices that avoid locking problems in Redshift.

Log in to try this activity Interactive activities are free with an account.
Worked example

A load that never finishes

The nightly load into orders hangs. STV_LOCKS shows an exclusive lock request waiting behind a lock held by a session that an analyst opened in a SQL client the afternoon before, began a transaction in, and never committed.

SELECT table_id, last_update, lock_owner_pid, lock_status FROM stv_locks;
SELECT PG_TERMINATE_BACKEND(31274);  -- the idle session's process ID

Ending the idle session releases its lock and the load proceeds. To prevent a recurrence, the team sets an idle session timeout for interactive users and changes the load to write into a staging table, then apply its changes in one short transaction.

Check yourself

What should an application do when a transaction fails with a serializable isolation error?

Show answer

Retry the transaction.

Exam Tips

Remember for the exam

  • When a session is blocked by locks, find it in STV_LOCKS and end it with PG_TERMINATE_BACKEND.
  • Redshift uses table-level locks, with serializable or snapshot isolation.
  • Retry transactions that fail with serializable isolation errors.
  • Load into staging tables and apply changes in short transactions.
  • Reads do not block writes in Redshift.