View a markdown version of this page

Examples - Amazon Redshift

Amazon Redshift will no longer support the use of Python UDFs after June 30, 2026. We will start enforcing it in phases. For more information on the details of Python end of life and migration options, see the blog post that was published on June 30, 2025.

Examples

The examples use an aws secret named aws_main and attach each target by its endpoint. Create the secret first with the following SQL statement:

LOAD redshift FROM aws.amazon.com; -- Create a DuckDB secret that supplies AWS credentials to each attach. CREATE OR REPLACE SECRET aws_main (TYPE aws, PROVIDER credential_chain, REGION 'us-east-1');

Join Redshift aggregates with local data

Reduce the data in Redshift first, then join the small result with a local DuckDB table:

-- Open a handle named rs to the Redshift target. You query it through redshift_query, not as a schema. ATTACH 'redshift://my-workgroup.111122223333.us-east-1.redshift-serverless.amazonaws.com/dev' AS rs (TYPE redshift, SECRET aws_main); -- Build a small local table from literal rows. VALUES lists the rows, and t(n_regionkey, region, target) names the inline table t and its three columns. CREATE TEMP TABLE targets AS SELECT * FROM (VALUES (0, 'AFRICA', 4), (1, 'AMERICA', 5), (2, 'ASIA', 5)) t(n_regionkey, region, target); -- redshift_query runs the SQL on Redshift and returns its rows as a table (aliased r) that you join to the local targets table. SELECT t.region, r.nations, t.target FROM redshift_query('rs', 'SELECT n_regionkey, count(*) AS nations FROM tpch.nation GROUP BY 1') r JOIN targets t USING (n_regionkey) ORDER BY t.region;

Bind parameters

-- $1 and $2 in the Redshift SQL are filled from the params list in order. params := [...] is DuckDB's named-argument syntax, and the list holds the values. SELECT * FROM redshift_query('rs', 'SELECT o_orderkey, o_orderstatus, o_totalprice FROM tpch.orders WHERE o_orderstatus = $1 AND o_orderdate = $2::date ORDER BY o_orderkey LIMIT 5', params := ['F', '1994-01-01']);

Keep a local copy

-- Store the Redshift result in a permanent local DuckDB table. The doubled single quotes escape the quotes around the date inside the SQL string. CREATE TABLE recent_orders AS SELECT * FROM redshift_query('rs', 'SELECT * FROM tpch.orders WHERE o_orderdate >= DATE ''1998-07-01''');

Export a result to Parquet

-- DuckDB's COPY ... TO writes a local file, here Parquet. This is not Redshift's COPY, which loads to or from Amazon S3. COPY (SELECT * FROM redshift_query('rs', 'SELECT * FROM tpch.nation')) TO 'nation.parquet';

Combine two Redshift targets

Two targets need two handles, and each handle can appear once per DuckDB statement:

-- Attach each Redshift target under its own handle. IF NOT EXISTS skips the attach when the handle already exists. ATTACH IF NOT EXISTS 'redshift://my-workgroup.111122223333.us-east-1.redshift-serverless.amazonaws.com/dev' AS sales_rs (TYPE redshift, SECRET aws_main); ATTACH IF NOT EXISTS 'redshift://my-cluster.abc123def456.us-east-1.redshift.amazonaws.com:5439/dev' AS fin (TYPE redshift, SECRET aws_main); -- Each handle appears once per statement. Here two Redshift results are joined together in DuckDB. SELECT o.o_orderpriority, count(DISTINCT o.o_orderkey) AS orders, sum(l.qty) AS quantity FROM redshift_query('sales_rs', 'SELECT o_orderkey, o_orderpriority FROM tpch.orders WHERE o_orderdate = DATE ''1998-06-01''') o JOIN redshift_query('fin', 'SELECT l_orderkey, sum(l_quantity) AS qty FROM tpch.lineitem WHERE l_shipdate = DATE ''1998-06-01'' GROUP BY 1') l ON o.o_orderkey = l.l_orderkey GROUP BY 1 ORDER BY 1;

To read the same target twice in one statement, attach it under a second handle, or stage one result in a temporary table in a separate statement:

-- Stage one result in a temp table first, so the single rs handle is used only once per statement. CREATE TEMP TABLE open_orders AS SELECT * FROM redshift_query('rs', 'SELECT o_orderkey FROM tpch.orders WHERE o_orderstatus = ''O'''); SELECT count(*) FROM redshift_query('rs', 'SELECT l_orderkey FROM tpch.lineitem') l JOIN open_orders o ON o.o_orderkey = l.l_orderkey;

Write to Redshift

DML and DDL run through redshift_query and return a rowcount. To load rows from DuckDB, keep each statement under the Redshift statement size limit, such as a multi-row INSERT under 16 MB, or stage the data in Amazon S3 and run the Redshift COPY command. The extension refuses a write through the handle's catalog.

-- Run DDL and DML on Redshift by passing the statement to redshift_query. It returns a rowcount, not rows. SELECT * FROM redshift_query('rs', 'CREATE TABLE sales.audit (id BIGINT, note VARCHAR(256))'); SELECT * FROM redshift_query('rs', 'INSERT INTO sales.audit SELECT id, note FROM sales.staging');
Warning

A failed write might still have taken effect. Check the target before you run it again.

Temporary tables and session state

Session state lasts for the handle's session on this DuckDB connection. A second handle on the same target, or another DuckDB connection, doesn't see it:

-- These run in one Redshift session on the rs handle, so the SET and the temp table are visible to the later query. SELECT * FROM redshift_query('rs', 'SET query_group TO ''nightly_report'''); SELECT * FROM redshift_query('rs', 'CREATE TEMP TABLE recent AS SELECT * FROM tpch.orders WHERE o_orderdate >= DATE ''1998-07-01'''); SELECT * FROM redshift_query('rs', 'SELECT count(*) AS n FROM recent');

Type Redshift SQL with CONNECT

Connect to a handle to type Redshift SQL straight to it. The statements run in the same Redshift session as redshift_query on this DuckDB connection, so a temporary table created while connected is still there afterward:

CONNECT rs; CREATE TEMP TABLE top_orders AS SELECT o_orderkey, o_totalprice FROM tpch.orders ORDER BY o_totalprice DESC LIMIT 10; SELECT * FROM top_orders; DISCONNECT; -- DISCONNECT returns to DuckDB, so this query runs through redshift_query again. SELECT * FROM redshift_query('rs', 'SELECT o_orderkey, o_totalprice FROM top_orders');

Python client

Each cursor is its own DuckDB connection, so it gets its own Redshift session. DuckDB connections in Python run in autocommit mode unless you call begin(), so don't call redshift_query inside begin() and commit():

import duckdb con = duckdb.connect() con.execute("LOAD redshift FROM aws.amazon.com") con.execute("CREATE SECRET aws_main (TYPE aws, PROVIDER credential_chain, REGION 'us-east-1')") con.execute(""" ATTACH 'redshift://my-workgroup.111122223333.us-east-1.redshift-serverless.amazonaws.com/dev' AS rs (TYPE redshift, SECRET 'aws_main') """) # $1 is filled from the params list. The query runs on Redshift and the rows come back to Python. rows = con.execute(""" SELECT * FROM redshift_query('rs', 'SELECT o_orderkey, o_totalprice FROM tpch.orders WHERE o_orderstatus = $1 LIMIT 10', params := ['F']) """).fetchall() # Each cursor is its own DuckDB connection, so it gets its own Redshift session. cur = con.cursor() print(cur.execute("SELECT * FROM redshift_query('rs', 'SELECT pg_backend_pid()')").fetchone())