Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Compatibility with Redshift #18677

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion posthog/temporal/workflows/redshift_batch_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime as dt
import itertools
import json
import os
import typing
from dataclasses import dataclass

Expand Down Expand Up @@ -32,6 +33,31 @@
)


@contextlib.asynccontextmanager
async def redshift_connection(inputs) -> typing.AsyncIterator[psycopg.AsyncConnection]:
"""Manage a Redshift connection.

This just yields a Postgres connection but we adjust a couple of things required for
psycopg to work with Redshift:
1. Set PGCLIENTENCODING to utf-8 as Redshift reports back UNICODE.
2. Set prepare_threshold to None on the connection as psycopg attempts to run DEALLOCATE ALL otherwise
which is not supported on Redshift.
"""
old_value = os.environ.get("PGCLIENTENCODING", None)
os.environ["PGCLIENTENCODING"] = "utf-8"

try:
async with postgres_connection(inputs) as connection:
connection.prepare_threshold = None
yield connection

finally:
if old_value is None:
del os.environ["PGCLIENTENCODING"]
else:
os.environ["PGCLIENTENCODING"] = old_value


async def insert_records_to_redshift(
records: collections.abc.Iterator[dict[str, typing.Any]],
redshift_connection: psycopg.AsyncConnection,
Expand Down Expand Up @@ -186,7 +212,7 @@ async def insert_into_redshift_activity(inputs: RedshiftInsertInputs):
)
properties_type = "VARCHAR(65535)" if inputs.properties_data_type == "varchar" else "SUPER"

async with postgres_connection(inputs) as connection:
async with redshift_connection(inputs) as connection:
await create_table_in_postgres(
connection,
schema=inputs.schema,
Expand Down
Loading