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.

chore(data-warehouse): report the row-size probe when the fetch cap binds (#93704)

Daniel RCe56692697d2c
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 ca5ee0d5a359..44934f9f5a2f 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
@@ -2704,7 +2704,7 @@ def _size_sample_percent(row_estimate: int | None) -> float | None:
def _get_table_chunk_size(
- cursor: psycopg.Cursor, inner_query: sql.Composed, logger: FilteringBoundLogger
+ cursor: psycopg.Cursor, inner_query: sql.Composed, logger: FilteringBoundLogger, *, byte_bounded: bool = False
) -> _TableChunking:
# Under autocommit each statement is its own transaction — a failure can't poison
# subsequent commands, so no SAVEPOINT is needed. When called inside a shared
@@ -2764,11 +2764,20 @@ def _get_table_chunk_size(
# actually do; the sibling `SQLSourceImplementation.get_chunk_size` already floors it.
batch_rows = max(1, int(DEFAULT_TABLE_SIZE_BYTES / row_size_bytes))
chunking = _TableChunking(batch_rows=batch_rows, fetch_rows=_fetch_rows_for(batch_rows, wide_row_bytes))
- logger.debug(
+ measurements = (
f"_get_table_chunk_size: row_size_bytes={row_size_bytes}. wide_row_bytes={wide_row_bytes}. "
f"largest_row_bytes={largest_row_bytes}. DEFAULT_TABLE_SIZE_BYTES={DEFAULT_TABLE_SIZE_BYTES}. "
f"Using CHUNK_SIZE={chunking.batch_rows}, FETCH_ROWS={chunking.fetch_rows}"
)
+ # The page cap sits fractionally below the chunk on any table whose p99 exceeds its p95,
+ # which is most of them, so a bare comparison would report nearly every sync. An order of
+ # magnitude is the point where the cap starts to matter: the read issues about ten times
+ # the `FETCH` calls per batch. Off the byte bound the caller ignores the cap and fetches the
+ # whole chunk, so reporting there would claim a cap that the read never applied.
+ if byte_bounded and chunking.fetch_rows * 10 <= chunking.batch_rows:
+ logger.info(measurements)
+ else:
+ logger.debug(measurements)
return chunking
except Exception as e:
# Best-effort: any failure (including a statement_timeout / QueryCanceled) falls back to
@@ -3555,7 +3564,9 @@ def postgres_source(
)
logger.debug(f"Using chunk_size_override: {chunk_size_override}")
else:
- chunking = _get_table_chunk_size(cursor, inner_query_with_limit, logger)
+ chunking = _get_table_chunk_size(
+ cursor, inner_query_with_limit, logger, byte_bounded=byte_bounded_extraction
+ )
chunk_size = chunking.batch_rows
# The page cap only exists to bound what one `FETCH` materialises, so
# it belongs behind the same gate as the byte bound it serves. Applied
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 2041dd3d5164..04e57942a50f 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
@@ -6511,6 +6511,31 @@ class TestGetTableChunkSize:
assert chunking.batch_rows == 1
assert chunking.fetch_rows == 1
+ @parameterized.expand(
+ [
+ ("cap_binds_hard", True, 400.0, 3.0 * 1024 * 1024, "info"),
+ ("cap_at_exactly_ten_times", True, 1024.0, 10240, "info"),
+ ("cap_just_short_of_ten_times", True, 1024.0, 10239, "debug"),
+ ("cap_barely_moves", True, 400.0, 420.0, "debug"),
+ ("no_cap_at_all", True, 400.0, 400.0, "debug"),
+ ("cap_binds_but_byte_bound_off", False, 400.0, 3.0 * 1024 * 1024, "debug"),
+ ]
+ )
+ def test_the_probe_reports_at_info_only_when_an_applied_page_cap_binds(
+ self, _name, byte_bounded, p95, p99, expected_level
+ ):
+ cursor = self._ProbeCursor((p95, p99, int(p99)))
+ logger = mock.Mock()
+
+ _get_table_chunk_size(cast(Any, cursor), sql.SQL("SELECT 1").format(), logger, byte_bounded=byte_bounded)
+
+ levels = [
+ level
+ for level in ("info", "debug")
+ if any("CHUNK_SIZE" in str(call) for call in getattr(logger, level).call_args_list)
+ ]
+ assert levels == [expected_level]
+
def test_a_sample_that_measured_nothing_falls_back(self):
# NULL percentiles mean no row was measured, not that rows are one byte wide. Reading
# that as a size derives a 150-million-row chunk, and with no page cap in play the chunk