'use client'; import { Postgres, PostgresTransaction, PostgresVersion } from "../sql/database.cjs"; import { FullSchema } from "../schema.cjs"; import { IndexToCreate } from "./genalgo.cjs"; import { z } from "zod"; //#region src/optimizer/statistics.d.ts type Path = string; declare const StatisticsSource: z.ZodUnion; path: z.ZodString; }, z.core.$strip>, z.ZodObject<{ kind: z.ZodLiteral<"inline">; }, z.core.$strip>]>; declare const ExportedStatsStatistics: z.ZodObject<{ stawidth: z.ZodNumber; stainherit: z.ZodDefault; stadistinct: z.ZodNumber; stanullfrac: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; }, z.core.$strip>; declare const ExportedStatsColumns: z.ZodObject<{ columnName: z.ZodString; attlen: z.ZodNullable; dataType: z.ZodOptional; stats: z.ZodNullable; stadistinct: z.ZodNumber; stanullfrac: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; }, z.core.$strip>>; }, z.core.$strip>; declare const ExportedStatsIndex: z.ZodObject<{ indexName: z.ZodString; amname: z.ZodDefault; relpages: z.ZodNumber; reltuples: z.ZodNumber; relallvisible: z.ZodNumber; relallfrozen: z.ZodOptional; fillfactor: z.ZodDefault; columns: z.ZodDefault; }, z.core.$strip>>>; }, z.core.$strip>; declare const ExportedStatsV1: z.ZodObject<{ tableName: z.ZodString; schemaName: z.ZodString; relpages: z.ZodNumber; reltuples: z.ZodNumber; relallvisible: z.ZodNumber; relallfrozen: z.ZodOptional; columns: z.ZodDefault; dataType: z.ZodOptional; stats: z.ZodNullable; stadistinct: z.ZodNumber; stanullfrac: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; }, z.core.$strip>>; }, z.core.$strip>>>; indexes: z.ZodArray; relpages: z.ZodNumber; reltuples: z.ZodNumber; relallvisible: z.ZodNumber; relallfrozen: z.ZodOptional; fillfactor: z.ZodDefault; columns: z.ZodDefault; }, z.core.$strip>>>; }, z.core.$strip>>; }, z.core.$strip>; declare const ExportedStats: z.ZodUnion; columns: z.ZodDefault; dataType: z.ZodOptional; stats: z.ZodNullable; stadistinct: z.ZodNumber; stanullfrac: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; }, z.core.$strip>>; }, z.core.$strip>>>; indexes: z.ZodArray; relpages: z.ZodNumber; reltuples: z.ZodNumber; relallvisible: z.ZodNumber; relallfrozen: z.ZodOptional; fillfactor: z.ZodDefault; columns: z.ZodDefault; }, z.core.$strip>>>; }, z.core.$strip>>; }, z.core.$strip>]>; type ExportedStats = z.infer; declare const StatisticsMode: z.ZodDiscriminatedUnion<[z.ZodObject<{ kind: z.ZodLiteral<"fromAssumption">; reltuples: z.ZodNumber; scale: z.ZodOptional; }, z.core.$strip>, z.ZodObject<{ kind: z.ZodLiteral<"fromStatisticsExport">; stats: z.ZodArray; columns: z.ZodDefault; dataType: z.ZodOptional; stats: z.ZodNullable; stadistinct: z.ZodNumber; stanullfrac: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; }, z.core.$strip>>; }, z.core.$strip>>>; indexes: z.ZodArray; relpages: z.ZodNumber; reltuples: z.ZodNumber; relallvisible: z.ZodNumber; relallfrozen: z.ZodOptional; fillfactor: z.ZodDefault; columns: z.ZodDefault; }, z.core.$strip>>>; }, z.core.$strip>>; }, z.core.$strip>]>>; source: z.ZodUnion; path: z.ZodString; }, z.core.$strip>, z.ZodObject<{ kind: z.ZodLiteral<"inline">; }, z.core.$strip>]>; scale: z.ZodOptional; }, z.core.$strip>], "kind">; type StatisticsMode = z.infer; declare const ComputedColumnStats: z.ZodObject<{ schema_name: z.ZodString; table_name: z.ZodString; column_name: z.ZodString; data_type: z.ZodOptional; stainherit: z.ZodBoolean; stanullfrac: z.ZodNumber; stawidth: z.ZodNumber; stadistinct: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; _value_type1: z.ZodNullable; _value_type2: z.ZodNullable; _value_type3: z.ZodNullable; _value_type4: z.ZodNullable; _value_type5: z.ZodNullable; }, z.core.$strip>; type ComputedColumnStats = z.infer; declare const ComputedReltuples: z.ZodObject<{ relname: z.ZodString; schema_name: z.ZodString; reltuples: z.ZodNumber; relpages: z.ZodNumber; relallvisible: z.ZodNumber; relallfrozen: z.ZodOptional; }, z.core.$strip>; type ComputedReltuples = z.infer; declare const ComputedStats: z.ZodObject<{ columnStats: z.ZodArray; stainherit: z.ZodBoolean; stanullfrac: z.ZodNumber; stawidth: z.ZodNumber; stadistinct: z.ZodNumber; stakind1: z.ZodNumber; stakind2: z.ZodNumber; stakind3: z.ZodNumber; stakind4: z.ZodNumber; stakind5: z.ZodNumber; staop1: z.ZodString; staop2: z.ZodString; staop3: z.ZodString; staop4: z.ZodString; staop5: z.ZodString; stacoll1: z.ZodString; stacoll2: z.ZodString; stacoll3: z.ZodString; stacoll4: z.ZodString; stacoll5: z.ZodString; stanumbers1: z.ZodNullable>; stanumbers2: z.ZodNullable>; stanumbers3: z.ZodNullable>; stanumbers4: z.ZodNullable>; stanumbers5: z.ZodNullable>; stavalues1: z.ZodNullable>; stavalues2: z.ZodNullable>; stavalues3: z.ZodNullable>; stavalues4: z.ZodNullable>; stavalues5: z.ZodNullable>; _value_type1: z.ZodNullable; _value_type2: z.ZodNullable; _value_type3: z.ZodNullable; _value_type4: z.ZodNullable; _value_type5: z.ZodNullable; }, z.core.$strip>>; reltuples: z.ZodArray; }, z.core.$strip>>; }, z.core.$strip>; type ComputedStats = z.infer; declare const DUMP_STATS_SQL: string; declare class Statistics { readonly postgresVersion: PostgresVersion; readonly ownMetadata: ExportedStats[]; readonly mode: StatisticsMode; readonly computedStats: ComputedStats; private readonly exportedMetadata; private readonly synthesizedReltuples; private readonly inferredColumnStats; private additionalIndexes; static readonly defaultStatsMode: StatisticsMode; /** * Private: construct through {@link Statistics.forExport} or * {@link Statistics.forAssumption}. Going through a named entry point is what * makes the current schema mandatory in export mode — see `forExport`. */ private constructor(); /** * The tables (as "schema.table") whose stats were synthesized because the * export didn't cover them — added to the schema since the snapshot was * captured. Their costs are modeled, not verified against real data. Empty * unless a current schema was supplied in fromStatisticsExport mode. */ get syntheticTables(): string[]; setAdditionalIndexes(additionalIndexes: IndexToCreate[]): void; private buildComputedStats; static statsModeFromAssumption({ reltuples }: { reltuples: number; }): StatisticsMode; /** * Create a statistic mode from stats exported from another database **/ static statsModeFromExport(stats: ExportedStats[]): StatisticsMode; /** * Cost against an exported snapshot. * * `schema` is required, and that is the point: tables the snapshot doesn't * cover — added by a migration since it was captured — are sized by the * synthesizer from the tables it does cover. Without a schema they fall to a * flat default, a sequential scan over them looks cheap enough that no index * can win, and the query reports a clean pass it never earned. Every caller * costing against an export already holds a schema, so requiring it costs * nothing and removes the way to get this wrong. * * `plannerTables` describes the database the stats are restored *into*; * `snapshot` carries the real numbers. Passing the same array as both is * valid (the snapshot describes the target) but rarely what you want. */ static forExport(input: { postgresVersion: PostgresVersion; plannerTables: ExportedStats[]; snapshot: ExportedStats[]; schema: FullSchema; scale?: number; source?: z.infer; }): Statistics; /** Cost against a synthetic assumption. Takes no schema: nothing to synthesize. */ static forAssumption(input: { postgresVersion: PostgresVersion; plannerTables: ExportedStats[]; reltuples?: number; scale?: number; }): Statistics; /** `forAssumption`, dumping the planner's own table list from a live connection. */ static forAssumptionFromPostgres(db: Postgres, opts?: { reltuples?: number; scale?: number; }): Promise; restoreStats(tx: PostgresTransaction): Promise<{ tablesNotInExports: string[]; tablesNotInTest: string[]; tableNotAnalyzed: string[]; statsMissing: { statistic: string; table: string; schema: string; column: string; }[]; }>; approximateTotalRows(): number; /** * We have to cast stavaluesN to the correct type * This derives that type for us so it can be used in `array_in` */ private stavalueKind; /** * PostgreSQL's anyarray columns in pg_statistic can hold arrays of arrays * for columns with array types (e.g. text[], int4[]). These create * multidimensional arrays that can be "ragged" (sub-arrays with different * lengths). jsonb_to_recordset can't reconstruct ragged multidimensional * arrays from JSON, so we need to drop these values. */ private static safeStavalues; /** * When inserting fake stats for existing tables and indexes, we also need to * account for data on newly created indexes by the optimizer. * * However we assume that index reltuples = table reltuples. * Meaning this logic is going to be a little off for partial indexes * or posting lists for duplicate values in btrees. * Meaning the deduplication that happens in pg side * 4 reltuples [(2, 1),(2, 9),(2, 4),(2, 8)] -> 1 reltuple [2, (1, 9, 4, 8)] * does not get accounted for here. */ private getAdditionalIndexReltuples; private restoreStats17; private static readonly columnStatsSQL; static dumpStats(db: PostgresTransaction, postgresVersion: PostgresVersion): Promise; } type ColumnMetadata = { columnName: string; dataType: string; isNullable: boolean; stats: ColumnStats | null; }; type ColumnStats = { stainherit: boolean; stanullfrac: number; stawidth: number; stadistinct: number; stakind1: number; stakind2: number; stakind3: number; stakind4: number; stakind5: number; staop1: number; staop2: number; staop3: number; staop4: number; staop5: number; stacoll1: number; stacoll2: number; stacoll3: number; stacoll4: number; stacoll5: number; stanumbers1: number; stanumbers2: number; stanumbers3: number; stanumbers4: number; stanumbers5: number; }; type TableMetadata = { tableName: string; schemaName: string; reltuples: number; relpages: number; relallvisible: number; relallfrozen?: number; columns: ColumnMetadata[]; }; type TableName = string; type TableStats = { tupleEstimate: bigint; pageCount: number; }; type SerializeResult = { schema: TableMetadata[]; serialized: string; sampledRecords: Record; }; //#endregion export { ColumnMetadata, ComputedColumnStats, ComputedReltuples, ComputedStats, DUMP_STATS_SQL, ExportedStats, ExportedStatsColumns, ExportedStatsIndex, ExportedStatsStatistics, ExportedStatsV1, Path, SerializeResult, Statistics, StatisticsMode, StatisticsSource, TableMetadata, TableStats }; //# sourceMappingURL=statistics.d.cts.map