Redshift loading, remote access, and locks
DEA-C01 · Lesson 9 of 23 · 1:11:02
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the DEA-C01 exam (AWS Certified Data Engineer – Associate). In this lesson: the COPY command from S3, DynamoDB, EMR, or a remote host, with an IAM role, split files, and a manifest, UNLOAD to S3 as text, CSV, JSON, or Parquet, Redshift Spectrum and external tables, federated queries to RDS and Aurora, materialized views and their refresh, and finding and clearing blocking sessions with STV_LOCKS, SVV_TRANSACTIONS, and pg_terminate_backend. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Four hours to load
Every night at AnyCompany Games, a script adds the match results into Redshift one row at a time, and the load now takes four hours. Analysts also want to join the warehouse with player data that stays in Aurora and with years of old events in S3, and one morning, a forgotten session blocked every load. This lesson covers loading and unloading with COPY and UNLOAD, remote access with Redshift Spectrum and federated queries, materialized views, and locks.
Loading with COPY
The COPY command loads data in parallel into a Redshift table, from files in S3, from a DynamoDB table, from an EMR cluster, or from a remote host over SSH. It runs with an IAM role that the cluster assumes to read the files, so no keys are written in the SQL. Each slice of the cluster loads files at the same time, so split the data into several files of similar size, ideally a multiple of the number of slices, between one megabyte and one gigabyte after compression. A manifest, which is a file in JSON that lists every file to load, makes sure that COPY loads exactly the right files, even from different buckets or prefixes. And a single COPY is much faster than many single-row inserts, which is why the nightly script takes hours.
Unloading with UNLOAD
UNLOAD does the opposite: it writes the result of a query to files in S3. By default, it writes pipe-delimited text, one or more files per slice, in parallel, and you can choose CSV, JSON, or Parquet instead. Parquet is a columnar format, faster to unload and smaller to store, and ready for Athena, Glue, or EMR. PARTITION BY writes the files into folders by column value, like one folder per day, and MANIFEST writes the list of the files created. And UNLOAD connects to S3 over HTTPS, and the files are encrypted on the server side, with an AWS KMS key if you choose one.
Remote access
Sometimes the data should stay where it is. Redshift Spectrum queries files in S3 directly, through external tables defined in an external data catalog like the AWS Glue Data Catalog, without loading them into the cluster. On provisioned clusters, it pushes filtering and aggregation down to a separate fleet of servers, so it uses little of the cluster's capacity, and partitions on the external tables skip the files that a query does not need. Federated queries read live data in Amazon RDS and Aurora databases, PostgreSQL or MySQL, and join it with the warehouse and the lake in one query. Neither one copies the data into the cluster, so the results are always as current as the source.
Materialized views
A materialized view stores the precomputed result of a query, like the join and aggregate behind a dashboard, so that queries read the result instead of the base tables. REFRESH MATERIALIZED VIEW brings it up to date, and Redshift chooses an incremental refresh, which processes only the changes, when it can, or a full refresh otherwise. With AUTO REFRESH YES, Redshift refreshes the view on its own, every five minutes by default or at an interval that you set, and only when the view is stale. Redshift can also rewrite a query automatically to use an up-to-date materialized view, even when the query does not name it. And materialized views can also be built on Spectrum external tables, which keeps a fast local result of data in the lake.
Locks
Redshift locks tables so that two sessions never write to the same table at the same time, DDL like DROP TABLE and TRUNCATE takes exclusive locks, and the LOCK command takes one explicitly inside a transaction. When a load waits, STV_LOCKS shows the current table updates, and SVV_TRANSACTIONS shows the open transactions that hold locks, with their process IDs. If a forgotten session holds a lock, end its transaction with COMMIT or ROLLBACK, or terminate the session with pg_terminate_backend and its process ID. In RDS for PostgreSQL, the same blocking shows up as the Lock Relation wait event, and a lock_timeout makes a waiting query give up instead of waiting forever.
Recap
Let's recap. COPY loads files in parallel from S3, DynamoDB, EMR, or a remote host, with an IAM role, split files, and a manifest. UNLOAD writes query results to S3 in parallel, as text, CSV, JSON, or Parquet, with partitions and a manifest. Redshift Spectrum queries files in S3 through external tables, and federated queries read live data in RDS and Aurora. Materialized views store precomputed results, refreshed incrementally or automatically. And STV_LOCKS, SVV_TRANSACTIONS, and pg_terminate_backend find and clear the sessions that block a table.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Each night, AnyCompany Games saves its match results as one large uncompressed file of 60 GB, and the COPY into Redshift is slow. What should the data engineer change first?
- Load the rows with thousands of single INSERT statements
- Split the export into compressed files of similar size
- Run UNLOAD on the file before the load
- Query the file through a federated query
Show the answer
Split the export into compressed files of similar size
COPY loads files in parallel across the slices, so several compressed files of similar size load much faster than one large file. “Load the rows with thousands of single INSERT statements” would be even slower than a single COPY. “Run UNLOAD on the file before the load” would write query results to S3, not load them. And “Query the file through a federated query” is for querying RDS and Aurora databases, not files in S3.
A dashboard runs the same heavy join and aggregation on Redshift every few seconds. The base tables change a few times an hour, and the dashboard must stay current within minutes. What should the data engineer use?
- An UNLOAD of the result every minute
- A regular view on the base tables
- A federated query to an Aurora replica
- A materialized view with auto refresh
Show the answer
A materialized view with auto refresh
The materialized view stores the result of the join, and auto refresh keeps it current, only when the base tables change. “An UNLOAD of the result every minute” would write files to S3, which the dashboard would not read. “A regular view on the base tables” would run the heavy join again on every query. And “A federated query to an Aurora replica” would query another database, without the warehouse tables.
Every morning, the load into Redshift waits forever. SVV_TRANSACTIONS shows that an analyst's session opened a transaction yesterday and still holds a lock on the table. What should the data engineer do?
- Terminate the session with pg_terminate_backend
- Run VACUUM on the locked table to reclaim space
- Add an explicit LOCK to the load job
- Resize the cluster with more nodes and more slices
Show the answer
Terminate the session with pg_terminate_backend
Pg_terminate_backend ends the forgotten session, its open transaction rolls back, and the lock is released. “Run VACUUM on the locked table to reclaim space” would only reclaim space and sort the rows, without releasing the lock. “Add an explicit LOCK to the load job” would make the load wait for yet another lock. And “Resize the cluster with more nodes and more slices” would add capacity, but the lock would still be held.