Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(postgres): decode inet/cidr columns to strings (#86670)

Tom Owers9694624115c1
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
index b0217e457868..a73d8f703fb1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
@@ -1701,6 +1701,21 @@ class RangeAsStringLoader(Loader):
return bytes(data).decode("utf-8")
+class NetworkAsStringLoader(Loader):
+ """Load PostgreSQL inet/cidr values as their string representation.
+
+ psycopg's default loaders convert these to `ipaddress.IPv4Address`/`IPv4Network`
+ (and IPv6 counterparts) objects. `PostgreSQLColumn.to_arrow_field` maps `inet`/`cidr`
+ to `pa.string()` via the default case, so pyarrow rejects those objects with
+ "Expected bytes, got a '...' object" when building the column array.
+ """
+
+ def load(self, data):
+ if data is None:
+ return None
+ return bytes(data).decode("utf-8")
+
+
class SafeDateLoader(Loader):
"""Load PostgreSQL dates, handling edge cases beyond Python's date range.
@@ -3361,6 +3376,8 @@ def postgres_source(
connection.adapters.register_loader("timestamptz", SafeTimestamptzLoader)
connection.adapters.register_loader("time", SafeTimeLoader)
connection.adapters.register_loader("timetz", SafeTimetzLoader)
+ connection.adapters.register_loader("inet", NetworkAsStringLoader)
+ connection.adapters.register_loader("cidr", NetworkAsStringLoader)
# Bump statement_timeout for the streaming connection. A server
# cursor FETCH inherits the session statement_timeout, and on
# wide/partitioned scans the source's default (often 30-60s)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
index 6e7f2e93575c..90c3a3563db3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
@@ -66,6 +66,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.p
SSL_REQUIRED_AFTER_DATE,
XMIN_PROJECTED_COLUMN,
JsonAsStringLoader,
+ NetworkAsStringLoader,
PostgresDiscoveredSchema,
PostgresImplementation,
PostgreSQLColumn,
@@ -5927,6 +5928,24 @@ class TestRangeAsStringLoader:
assert loader.load(b"[,10)") == "[,10)"
+class TestNetworkAsStringLoader:
+ @pytest.fixture
+ def loader(self):
+ return NetworkAsStringLoader(oid=869)
+
+ def test_loads_inet_address(self, loader):
+ assert loader.load(b"192.168.1.1") == "192.168.1.1"
+
+ def test_loads_cidr_network(self, loader):
+ assert loader.load(b"192.168.1.0/24") == "192.168.1.0/24"
+
+ def test_loads_ipv6(self, loader):
+ assert loader.load(b"::1") == "::1"
+
+ def test_none_returns_none(self, loader):
+ assert loader.load(None) is None
+
+
class TestSSLRequiredAfterDate:
def test_date_value(self):
assert SSL_REQUIRED_AFTER_DATE == datetime(2026, 2, 18, tzinfo=UTC)