feat(postgres): support concurrent index creation
This commit is contained in:
1 parent
ebf412b144
commit
aa6d60533c
38 files changed
+1917
-34
No files matched your search
+18
-1
@@ -1,7 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { frontendQueryTimeoutDelayMs, frontendQueryTimeoutSecsForSql, queryTimeoutSecsForConnection } from "@/lib/sql/queryTimeout";
|
||||
import { CONCURRENT_INDEX_QUERY_TIMEOUT_SECS, frontendQueryTimeoutDelayMs, frontendQueryTimeoutSecsForSql, queryTimeoutSecsForConcurrentIndex, queryTimeoutSecsForConnection } from "@/lib/sql/queryTimeout";
|
||||
|
||||
describe("queryTimeout", () => {
|
||||
it("gives CREATE INDEX CONCURRENTLY a dedicated long budget instead of the 30s default", () => {
|
||||
expect(CONCURRENT_INDEX_QUERY_TIMEOUT_SECS).toBe(1800);
|
||||
expect(CONCURRENT_INDEX_QUERY_TIMEOUT_SECS).toBeGreaterThan(30);
|
||||
expect(frontendQueryTimeoutDelayMs(CONCURRENT_INDEX_QUERY_TIMEOUT_SECS)).toBe(1_800_000);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["non-concurrent", 30, false, 30],
|
||||
["non-concurrent", 3600, false, 3600],
|
||||
["concurrent + 0 (unlimited)", 0, true, 0],
|
||||
["concurrent + 30 (below floor)", 30, true, 1800],
|
||||
["concurrent + 600 (below floor)", 600, true, 1800],
|
||||
["concurrent + 1800 (floor)", 1800, true, 1800],
|
||||
["concurrent + 3600 (above floor)", 3600, true, 3600],
|
||||
] as const)("queryTimeoutSecsForConcurrentIndex preserves %s", (_name, configured, concurrent, expected) => {
|
||||
expect(queryTimeoutSecsForConcurrentIndex(configured, concurrent)).toBe(expected);
|
||||
});
|
||||
it("lets PostgreSQL row queries use the backend inactivity timeout", () => {
|
||||
expect(frontendQueryTimeoutSecsForSql("SELECT * FROM sample_records LIMIT 2000", "postgres", 30)).toBe(0);
|
||||
expect(frontendQueryTimeoutSecsForSql("/* page */\nWITH rows AS (SELECT 1) SELECT * FROM rows", "postgres", 30)).toBe(0);
|
||||
|
||||
@@ -38,6 +38,22 @@ describe("tableStructureCapabilities", () => {
|
||||
expect(getTableStructureCapabilities("sqlserver", "sqlserver", "10.0").indexInclude).toBe(true);
|
||||
});
|
||||
|
||||
it("enables concurrent index creation only for PostgreSQL", () => {
|
||||
// PostgreSQL (including pre-11 versions that disable INCLUDE) advertises it.
|
||||
expect(getTableStructureCapabilities("postgres", "postgres").indexConcurrent).toBe(true);
|
||||
expect(getTableStructureCapabilities("postgres", "postgres", "10.15").indexConcurrent).toBe(true);
|
||||
|
||||
// PostgreSQL-family engines sharing the Postgres dialect stay off until verified.
|
||||
for (const databaseType of ["kingbase", "gaussdb", "opengauss", "highgo", "uxdb", "vastbase", "kwdb", "firebird"] as const) {
|
||||
expect(getTableStructureCapabilities(databaseType, databaseType).indexConcurrent).toBe(false);
|
||||
}
|
||||
|
||||
// Unrelated engines never advertise it.
|
||||
for (const databaseType of ["mysql", "sqlite", "sqlserver", "oracle", "dameng", "duckdb", "questdb"] as const) {
|
||||
expect(getTableStructureCapabilities(databaseType, databaseType).indexConcurrent).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
it("removes unsupported included columns before SQL generation", () => {
|
||||
const index = {
|
||||
id: "new:index",
|
||||
|
||||
@@ -171,6 +171,8 @@ export const listForeignKeys = forward("listForeignKeys");
|
||||
export const listTriggers = forward("listTriggers");
|
||||
export const listConstraints = forward("listConstraints");
|
||||
export const listPartitions = forward("listPartitions");
|
||||
export const getTablePartitionStatus = forward("getTablePartitionStatus");
|
||||
export const listInvalidIndexes = forward("listInvalidIndexes");
|
||||
export const listSubpartitions = forward("listSubpartitions");
|
||||
export const getTableDdl = forward("getTableDdl");
|
||||
export const getTableDisplayDdl = forward("getTableDisplayDdl");
|
||||
|
||||
@@ -869,6 +869,19 @@ export async function listPartitions(connectionId: string, database: string, sch
|
||||
return get(`/api/schema/partitions?${qs({ connection_id: connectionId, database, schema, table, catalog })}`);
|
||||
}
|
||||
|
||||
export interface TablePartitionStatus {
|
||||
isPartitionedParent: boolean;
|
||||
isPartition: boolean;
|
||||
}
|
||||
|
||||
export async function getTablePartitionStatus(connectionId: string, database: string, schema: string, table: string): Promise<TablePartitionStatus> {
|
||||
return get(`/api/schema/table-partition-status?${qs({ connection_id: connectionId, database, schema, table })}`);
|
||||
}
|
||||
|
||||
export async function listInvalidIndexes(connectionId: string, database: string, schema: string, table: string): Promise<string[]> {
|
||||
return get(`/api/schema/invalid-indexes?${qs({ connection_id: connectionId, database, schema, table })}`);
|
||||
}
|
||||
|
||||
export async function listSubpartitions(connectionId: string, database: string, schema: string, table: string, catalog?: string): Promise<SubpartitionInfo[]> {
|
||||
return get(`/api/schema/subpartitions?${qs({ connection_id: connectionId, database, schema, table, catalog })}`);
|
||||
}
|
||||
@@ -1105,12 +1118,13 @@ export async function closeClientConnectionSession(connectionId: string, databas
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeBatch(connectionId: string, database: string, statements: string[], schema?: string): Promise<QueryResult> {
|
||||
export async function executeBatch(connectionId: string, database: string, statements: string[], schema?: string, timeoutSecs?: number): Promise<QueryResult> {
|
||||
return post("/api/query/execute-batch", {
|
||||
connectionId,
|
||||
database,
|
||||
statements,
|
||||
schema,
|
||||
timeoutSecs,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1644,6 +1644,29 @@ export async function listPartitions(connectionId: string, database: string, sch
|
||||
});
|
||||
}
|
||||
|
||||
export interface TablePartitionStatus {
|
||||
isPartitionedParent: boolean;
|
||||
isPartition: boolean;
|
||||
}
|
||||
|
||||
export async function getTablePartitionStatus(connectionId: string, database: string, schema: string, table: string): Promise<TablePartitionStatus> {
|
||||
return invoke("get_table_partition_status", {
|
||||
connectionId,
|
||||
database,
|
||||
schema,
|
||||
table,
|
||||
});
|
||||
}
|
||||
|
||||
export async function listInvalidIndexes(connectionId: string, database: string, schema: string, table: string): Promise<string[]> {
|
||||
return invoke("list_invalid_indexes", {
|
||||
connectionId,
|
||||
database,
|
||||
schema,
|
||||
table,
|
||||
});
|
||||
}
|
||||
|
||||
export async function listSubpartitions(connectionId: string, database: string, schema: string, table: string, catalog?: string): Promise<SubpartitionInfo[]> {
|
||||
return invoke("list_subpartitions", {
|
||||
connectionId,
|
||||
|
||||
@@ -39,6 +39,7 @@ export function createDraftIndex(tableName: string, columns: string[], existingI
|
||||
indexType: "",
|
||||
includedColumns: [],
|
||||
comment: "",
|
||||
concurrently: false,
|
||||
markedForDrop: false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,6 +3,21 @@ import { tokenizeSqlSemantic } from "@/lib/sql/semantic/tokens";
|
||||
import type { ConnectionConfig, DatabaseType } from "@/types/database";
|
||||
|
||||
export const DEFAULT_QUERY_TIMEOUT_SECS = 30;
|
||||
|
||||
/**
|
||||
* Query timeout (seconds) the table structure editor applies when the change
|
||||
* script contains a PostgreSQL `CREATE INDEX CONCURRENTLY`.
|
||||
*
|
||||
* Concurrent builds scan the whole table while only taking `ShareUpdateExclusiveLock`,
|
||||
* so they routinely run far beyond the default 30s query timeout — the default
|
||||
* budget would cancel a legitimate build and leave an INVALID index behind.
|
||||
* PostgreSQL itself does not impose a build deadline
|
||||
* (https://www.postgresql.org/docs/current/sql-createindex.html). This is the
|
||||
* dedicated 30-minute floor used by `queryTimeoutSecsForConcurrentIndex` for
|
||||
* such statements; it never overrides an unlimited setting (0) and never
|
||||
* shortens a larger configured timeout.
|
||||
*/
|
||||
export const CONCURRENT_INDEX_QUERY_TIMEOUT_SECS = 1800;
|
||||
const MAX_BROWSER_TIMER_DELAY_MS = 2_147_483_647;
|
||||
|
||||
const POSTGRES_ROW_STATEMENT_KEYWORDS = new Set(["select", "show", "explain", "table", "with"]);
|
||||
@@ -17,6 +32,19 @@ export function queryTimeoutSecsForConnection(connection?: Pick<ConnectionConfig
|
||||
return Number.isFinite(value) && value >= 0 ? value : DEFAULT_QUERY_TIMEOUT_SECS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Effective query timeout (seconds) for a table-structure change script.
|
||||
*
|
||||
* Concurrent builds use at least the dedicated 30-minute floor while
|
||||
* preserving an unlimited setting (0) and any larger configured timeout.
|
||||
* Non-concurrent scripts keep the configured timeout unchanged.
|
||||
*/
|
||||
export function queryTimeoutSecsForConcurrentIndex(configuredTimeoutSecs: number, hasConcurrentIndexBuild: boolean): number {
|
||||
if (!hasConcurrentIndexBuild) return configuredTimeoutSecs;
|
||||
if (configuredTimeoutSecs === 0) return 0;
|
||||
return Math.max(configuredTimeoutSecs, CONCURRENT_INDEX_QUERY_TIMEOUT_SECS);
|
||||
}
|
||||
|
||||
export function frontendQueryTimeoutSecsForSql(sql: string, databaseType: DatabaseType | undefined, queryTimeoutSecs: number): number {
|
||||
if (queryTimeoutSecs === 0) return 0;
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Pure decision helper for the PostgreSQL `CREATE INDEX CONCURRENTLY` checkbox
|
||||
* in the table structure editor (PR #6361).
|
||||
*
|
||||
* Concurrent builds are scoped to *new* indexes on *non-partitioned* tables:
|
||||
* editing an existing index would require a `DROP INDEX CONCURRENTLY` +
|
||||
* `CREATE INDEX CONCURRENTLY` replace flow (not implemented), and PostgreSQL
|
||||
* rejects `CREATE INDEX CONCURRENTLY` on partitioned parent tables. The core
|
||||
* SQL builder enforces the same scope as a hard error, so this only decides
|
||||
* whether the option is actionable in the UI.
|
||||
*/
|
||||
export type ConcurrentIndexUnavailableReason = "existing" | "partitioned" | "unknown" | "primary" | "markedForDrop" | "unsupported";
|
||||
|
||||
export interface ConcurrentIndexAvailability {
|
||||
enabled: boolean;
|
||||
reason?: ConcurrentIndexUnavailableReason;
|
||||
}
|
||||
|
||||
export interface ConcurrentIndexAvailabilityInput {
|
||||
/** The draft edits an index that already exists on the table. */
|
||||
hasOriginal: boolean;
|
||||
isPrimary: boolean;
|
||||
markedForDrop: boolean;
|
||||
/** `relkind = 'p'` partitioned parent table. */
|
||||
isPartitionedParent: boolean;
|
||||
/**
|
||||
* The partition status could not be verified (probe failed). Fail closed:
|
||||
* do not offer Concurrent when we cannot rule out a partitioned parent.
|
||||
*/
|
||||
partitionStatusKnown: boolean;
|
||||
supportsIndexConcurrent: boolean;
|
||||
supportsCreateIndex: boolean;
|
||||
}
|
||||
|
||||
export function getConcurrentIndexAvailability(input: ConcurrentIndexAvailabilityInput): ConcurrentIndexAvailability {
|
||||
if (input.markedForDrop) return { enabled: false, reason: "markedForDrop" };
|
||||
if (input.isPrimary) return { enabled: false, reason: "primary" };
|
||||
if (input.hasOriginal) return { enabled: false, reason: "existing" };
|
||||
if (input.isPartitionedParent) return { enabled: false, reason: "partitioned" };
|
||||
if (!input.partitionStatusKnown) return { enabled: false, reason: "unknown" };
|
||||
if (!input.supportsIndexConcurrent || !input.supportsCreateIndex) return { enabled: false, reason: "unsupported" };
|
||||
return { enabled: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Unquoted index names referenced by `CREATE [UNIQUE] INDEX CONCURRENTLY`
|
||||
* statements, used to detect same-name INVALID leftovers before applying a
|
||||
* concurrent build. The PG builder emits the index name as a single
|
||||
* double-quoted identifier (optionally schema-qualified).
|
||||
*/
|
||||
export function concurrentIndexNamesInStatements(statements: string[]): string[] {
|
||||
const names: string[] = [];
|
||||
for (const sql of statements) {
|
||||
const match = sql.match(/CREATE\s+(?:UNIQUE\s+)?INDEX\s+CONCURRENTLY\s+(?:(?:"[^"]+")\s*\.\s*)?"([^"]+)"/i);
|
||||
if (match?.[1]) names.push(match[1]);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Report index drafts whose Concurrent availability is no longer `enabled`.
|
||||
* Definitive states (partitioned parent, existing index, capability loss, ...)
|
||||
* clear the stale flag. A transiently unknown partition status preserves the
|
||||
* user's intent so a later successful probe can restore the concurrent SQL.
|
||||
* Callers must gate save/SQL generation on every reported invalidation.
|
||||
*/
|
||||
export function normalizeUnsupportedConcurrentIndexes<IIndex extends { id: string; concurrently?: boolean }>(indexes: readonly IIndex[], availabilityResolver: (index: IIndex) => ConcurrentIndexAvailability): { indexes: IIndex[]; invalidatedIds: string[] } {
|
||||
const invalidatedIds: string[] = [];
|
||||
const normalized = indexes.map((index) => {
|
||||
if (!index.concurrently) return index;
|
||||
const availability = availabilityResolver(index);
|
||||
if (availability.enabled) return index;
|
||||
invalidatedIds.push(index.id);
|
||||
if (availability.reason === "unknown") return index;
|
||||
return { ...index, concurrently: false };
|
||||
});
|
||||
return { indexes: normalized, invalidatedIds };
|
||||
}
|
||||
@@ -25,6 +25,7 @@ export interface TableStructureCapabilities {
|
||||
indexInclude: boolean;
|
||||
indexFilter: boolean;
|
||||
indexComment: boolean;
|
||||
indexConcurrent: boolean;
|
||||
foreignKey: boolean;
|
||||
}
|
||||
|
||||
@@ -49,6 +50,7 @@ const unsupportedCapabilities: TableStructureCapabilities = {
|
||||
indexInclude: false,
|
||||
indexFilter: false,
|
||||
indexComment: false,
|
||||
indexConcurrent: false,
|
||||
foreignKey: false,
|
||||
};
|
||||
|
||||
@@ -115,6 +117,7 @@ const postgresCapabilities = capabilities({
|
||||
const postgresBefore11Capabilities = capabilities({
|
||||
...postgresCapabilities,
|
||||
indexInclude: false,
|
||||
indexConcurrent: true,
|
||||
});
|
||||
|
||||
const redshiftCapabilities = capabilities({
|
||||
@@ -318,7 +321,7 @@ const capabilityByType: Partial<Record<DatabaseType, TableStructureCapabilities>
|
||||
oscar: damengCapabilities,
|
||||
databend: mysqlCapabilities,
|
||||
gbase: gbaseCapabilities,
|
||||
postgres: postgresCapabilities,
|
||||
postgres: capabilities({ ...postgresCapabilities, indexConcurrent: true }),
|
||||
gaussdb: postgresCapabilities,
|
||||
kwdb: postgresCapabilities,
|
||||
opengauss: postgresCapabilities,
|
||||
|
||||
@@ -44,6 +44,8 @@ export interface EditableStructureIndex {
|
||||
indexType: string;
|
||||
includedColumns: string[];
|
||||
comment: string;
|
||||
/** Build the index with PostgreSQL `CREATE INDEX CONCURRENTLY` (PostgreSQL only, default off). */
|
||||
concurrently?: boolean;
|
||||
original?: IndexInfo;
|
||||
markedForDrop: boolean;
|
||||
}
|
||||
@@ -81,6 +83,10 @@ export interface BuildTableStructureChangeSqlOptions {
|
||||
triggers?: EditableStructureTrigger[];
|
||||
tableComment?: string;
|
||||
originalTableComment?: string;
|
||||
/** The target table is a PostgreSQL partitioned parent (`relkind = 'p'`);
|
||||
* the backend rejects `CREATE INDEX CONCURRENTLY` on such tables (fail
|
||||
* closed) instead of downgrading to a blocking `CREATE INDEX`. */
|
||||
partitioned?: boolean;
|
||||
}
|
||||
|
||||
export interface TableStructureChangeSql {
|
||||
|
||||
Reference in new issue
Block a user