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.

2.7 KB
Raw Blame
use async_trait::async_trait;

use crate::storage::error::StorageResult;
use crate::storage::postgres::ConsistencyLevel;
use crate::storage::types::HashKeyOverrideContext;

/// Feature flag hash key override operations
#[async_trait]
pub trait FeatureFlagStorage: Send + Sync {
    /// Gets the context needed for hash key override decisions.
    ///
    /// This resolves distinct IDs to person IDs and returns existing hash key
    /// override information for each person. A stored override whose hash key is
    /// the cookieless sentinel counts as no override.
    ///
    /// When `check_person_exists` is true, only returns results for persons that
    /// exist in the posthog_person table. This uses an EXISTS subquery against
    /// the person table.
    ///
    /// ## Consistency
    ///
    /// The `consistency` parameter controls which database pool is used:
    /// - `Strong`: Uses the primary database, guaranteeing read-after-write consistency.
    ///   This is important when the caller has just written hash key overrides and needs
    ///   to read them back immediately.
    /// - `Eventual`: Uses the replica database, which may have replication lag.
    ///
    /// Note: When the personhog-leader service is implemented, the person table will be
    /// cached on the leader pods. At that point, strong consistency for person data will
    /// require routing to the leader service, not the primary database. The current
    /// implementation queries the primary database directly as a temporary solution.
    async fn get_hash_key_override_context(
        &self,
        team_id: i64,
        distinct_ids: &[String],
        check_person_exists: bool,
        consistency: ConsistencyLevel,
    ) -> StorageResult<Vec<HashKeyOverrideContext>>;

    /// Upsert hash key overrides by resolving distinct_ids to person_ids server-side.
    ///
    /// Resolves all `distinct_ids` to person_ids via `posthog_persondistinctid`,
    /// cross-joins with `feature_flag_keys`, and inserts overrides with the given
    /// `hash_key`, replacing only a stored cookieless sentinel. Returns the number
    /// of rows inserted or replaced.
    async fn upsert_hash_key_overrides(
        &self,
        team_id: i64,
        distinct_ids: &[String],
        feature_flag_keys: &[String],
        hash_key: &str,
    ) -> StorageResult<i64>;

    /// Delete up to `batch_size` hash key overrides across the specified teams.
    /// Selects rows with FOR UPDATE SKIP LOCKED so concurrent batches don't contend.
    /// Returns the number of deleted rows; 0 means no more rows to delete.
    async fn delete_hash_key_overrides_by_teams(
        &self,
        team_ids: &[i64],
        batch_size: i64,
    ) -> StorageResult<i64>;
}