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.

3.8 KB
Raw Blame
"""Direct-query Postgres helpers — manages the live-query `DataWarehouseTable` that lets HogQL hit
Postgres without going through Delta. Code shared with warehouse mode lives in `postgres_helpers.py`.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from products.warehouse_sources.backend.facade.models import ExternalDataSource
from products.warehouse_sources.backend.facade.types import DataWarehouseTableCreatedVia, DataWarehouseTableFormat

if TYPE_CHECKING:
    from products.warehouse_sources.backend.models.table import DataWarehouseTable

type DirectPostgresColumns = dict[str, dict[str, Any]]

DIRECT_POSTGRES_URL_PATTERN = "direct://postgres"
DIRECT_POSTGRES_CATALOG_OPTION = "direct_postgres_catalog"
DIRECT_POSTGRES_SCHEMA_OPTION = "direct_postgres_schema"
DIRECT_POSTGRES_TABLE_OPTION = "direct_postgres_table"


def get_direct_postgres_table_options(
    *, source_catalog: str | None = None, source_schema: str, source_table_name: str
) -> dict[str, str]:
    options = {
        DIRECT_POSTGRES_SCHEMA_OPTION: source_schema,
        DIRECT_POSTGRES_TABLE_OPTION: source_table_name,
    }
    if source_catalog:
        options[DIRECT_POSTGRES_CATALOG_OPTION] = source_catalog
    return options


def upsert_direct_postgres_table(
    existing_table: DataWarehouseTable | None,
    *,
    schema_name: str,
    source: ExternalDataSource,
    columns: DirectPostgresColumns,
    source_catalog: str | None = None,
    source_schema: str,
    source_table_name: str,
) -> DataWarehouseTable:
    from products.warehouse_sources.backend.facade.models import DataWarehouseTable

    options = {
        **(existing_table.options if existing_table is not None and isinstance(existing_table.options, dict) else {}),
        **get_direct_postgres_table_options(
            source_catalog=source_catalog,
            source_schema=source_schema,
            source_table_name=source_table_name,
        ),
    }

    if existing_table is None:
        return DataWarehouseTable.objects.create(
            name=schema_name,
            format=DataWarehouseTableFormat.Parquet,
            team_id=source.team_id,
            url_pattern=DIRECT_POSTGRES_URL_PATTERN,
            external_data_source=source,
            columns=columns,
            options=options,
            created_via=DataWarehouseTableCreatedVia.SOURCE,
        )

    existing_table.name = schema_name
    existing_table.url_pattern = DIRECT_POSTGRES_URL_PATTERN
    existing_table.external_data_source = source
    existing_table.columns = columns
    existing_table.options = options
    existing_table.deleted = False
    existing_table.deleted_at = None
    # DIRECT_POSTGRES_URL_PATTERN is a fixed sentinel, not request input, so this upsert is a
    # trusted writer of a credential-less table's URL.
    existing_table.save(
        update_fields=[
            "name",
            "url_pattern",
            "external_data_source",
            "columns",
            "options",
            "deleted",
            "deleted_at",
            "updated_at",
        ],
        internally_computed_url_pattern=True,
    )
    return existing_table


def hide_direct_postgres_table(table: DataWarehouseTable | None) -> None:
    if table is not None and not table.deleted:
        table.soft_delete()


def rename_direct_postgres_join_references(*, team_id: int, old_name: str, new_name: str) -> None:
    if old_name == new_name:
        return
    from products.data_tools.backend.models.join import DataWarehouseJoin

    # Saved one by one because a queryset update writes no activity log row.
    for field in ("source_table_name", "joining_table_name"):
        for join in DataWarehouseJoin.objects.filter(team_id=team_id, **{field: old_name}):
            setattr(join, field, new_name)
            join.save(update_fields=[field])