Heuristic:173TECH Sayn Redshift S3 Bulk Load
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Databases, Optimization |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Redshift bulk loading uses S3 as an intermediary — data is gzipped as JSON, uploaded to S3, then loaded via COPY — and falls back to INSERT when no S3 bucket is configured.
Description
SAYN's Redshift adapter overrides `_load_data_batch()` with an S3-based bulk loading method. When a `bucket` parameter is configured in the Redshift credentials, each batch of records is serialized as newline-delimited JSON using `orjson`, gzip-compressed, uploaded to the configured S3 bucket, and then loaded into Redshift via a `COPY` command from the S3 path. This is significantly faster than the default INSERT...VALUES method.
If no `bucket` is configured, the adapter falls back to the base `Database._load_data_batch()` which uses INSERT. The S3 bucket configuration supports a `name`, `region`, and optional `role` (for IAM role-based access). The file is uploaded as `batch.json.gz` and loaded with the Redshift COPY command rendered from `redshift_load_batch.sql`.
The adapter also supports IAM authentication for the database connection itself — when `cluster_id` is provided without a `password`, SAYN uses AWS IAM authentication via `redshift_connector`'s `iam=True` option.
Usage
Use this heuristic when configuring Redshift connections for copy tasks or Python tasks that call `load_data()`. Without an S3 bucket, loading large datasets via INSERT will be slow and may hit query size limits. Configure `bucket` in `settings.yaml` to enable fast S3-based loading. Also use IAM auth (`cluster_id` instead of `password`) for more secure credential management.
The Insight (Rule of Thumb)
- Action: Add a `bucket` key to the Redshift credential in `settings.yaml` to enable S3 bulk loading.
- Value: Either a string (bucket name) or a dict with `name`, `region`, and optional `role` keys.
- Trade-off: S3 loading requires boto3 (not included in SAYN extras — install separately) and appropriate AWS credentials. It adds S3 upload latency per batch but dramatically improves load throughput for large datasets.
- Action: For IAM database auth, provide `cluster_id` instead of `password`.
- Value: `cluster_id` string; SAYN sets `iam=True` on the redshift_connector connection.
- Compatibility: Redshift only. `boto3` must be installed separately (`pip install boto3`).
Reasoning
The S3 bulk load implementation in `Redshift._load_data_batch()` (`database/redshift.py:211-246`):
if self.bucket is None:
return super(Database, self)._load_data_batch(table, data, schema, db)
full_table_name = f"..."
template = self._jinja_env.get_template("redshift_load_batch.sql")
s3_client = self._boto_session.client("s3", region_name=self.bucket["region"])
buf = BytesIO()
with gzip.GzipFile(fileobj=buf, mode="w") as gf:
gf.write(b"\n".join([orjson.dumps(r, default=str) for r in data]))
buf.seek(0)
fname = "batch.json.gz"
s3_client.upload_fileobj(buf, self.bucket["name"], fname)
self.execute(template.render(
full_table_name=full_table_name,
temp_file_name=fname,
bucket=self.bucket["name"],
region=self.bucket["region"],
))
IAM authentication in `Redshift.create_engine()` (`database/redshift.py:120-126`):
if "cluster_id" in settings and "password" not in settings:
settings["connect_args"]["iam"] = True
settings["connect_args"]["db_user"] = settings.pop("user")
settings["connect_args"]["cluster_identifier"] = settings.pop("cluster_id")
The boto3 session setup in `Redshift.create_engine()` (`database/redshift.py:157-166`):
import boto3
self._boto_session = boto3.Session(
profile_name=self.profile,
region_name=region,
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
aws_session_token=self.session_token,
)
Redshift also uses a custom merge template (`database/redshift.py:248-269`) because Redshift does not support the standard merge SQL — it requires a DELETE + INSERT pattern instead.