Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/src/routes/schema-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const expectedSchema = z.object({
checks: z.array(z.object({ name, validated: z.boolean() })).max(500),
foreignKeys: z.array(z.object({ name, columns: z.array(name).max(100), refTable: name, refColumns: z.array(name).max(100), validated: z.boolean() })).max(500),
indexes: z.array(z.object({ name, columns: z.array(name.nullable()).max(100), unique: z.boolean() })).max(1_000),
views: z.array(name).max(1_000),
})).max(100),
absent: z.array(name).max(100),
});
Expand Down
4 changes: 2 additions & 2 deletions server/src/schema-change/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ describe("tables, renames and drops", () => {
expect(errors(compile(plan({ type: "create_table", table: { name: "t", fields: [field, { name: "ID", dataType: "bigint" }] } }), facts()))).toEqual(["column-exists"]);
});

it("renames a table and says it breaks callers; later steps see the new name", () => {
const runbook = compile(plan({ type: "rename_table", tableName: "orders", newName: "sales_orders" }, { type: "add_column", tableName: "sales_orders", field: { name: "x", dataType: "text" } }), facts(orders()));
it("renames a table and says it breaks callers when compatibility views are off; later steps see the new name", () => {
const runbook = compile(plan({ type: "rename_table", tableName: "orders", newName: "sales_orders" }, { type: "add_column", tableName: "sales_orders", field: { name: "x", dataType: "text" } }), facts(orders()), { settings: { compatViews: false } });
expect(errors(runbook)).toEqual([]);
expect(runbook.steps[0]).toMatchObject({ breaksClients: true, lock: "ACCESS EXCLUSIVE" });
expect(runbook.steps[1].sql).toContain("ALTER TABLE public.sales_orders ADD COLUMN x text;");
Expand Down
103 changes: 87 additions & 16 deletions server/src/schema-change/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class Compiler {
return {
schema, name, qualified: qualifiedName(schema, name), partitioned: false, partitionCount: 0, partitions: [], nestedPartitions: false,
estimatedRows: 0, heapBytes: 0, indexBytes: 0, totalBytes: 0, columns: [], primaryKey: [], uniques: [], checks: [], foreignKeys: [], referencedBy: [],
indexes: [], dependentViews: [], dependents: [], userTriggers: 0, stats: null, isNew: true,
indexes: [], dependentViews: [], dependents: [], userTriggers: 0, rowSecurity: false, privileges: [], stats: null, isNew: true,
};
}

Expand Down Expand Up @@ -395,22 +395,87 @@ class Compiler {
const target = qualifiedName(table.schema, newName);
this.claimRelation(table.schema, newName, "Table");
const oldQualified = table.qualified;
const oldName = table.name;
const from = qualifiedIdent(table.schema, oldName);
const to = qualifiedIdent(table.schema, newName);
const keepOldName = this.settings.compatViews && !table.isNew;

this.add({
title: `Rename table ${oldQualified} to ${newName}`,
sql: `ALTER TABLE ${qualifiedIdent(table.schema, table.name)} RENAME TO ${ident(newName)};`,
mode: "short",
lock: "ACCESS EXCLUSIVE",
work: "metadata",
table,
breaksClients: true,
notes: [
"The old name stops working immediately. Queries, functions and application code that use it will fail until they are changed.",
"Views, foreign keys and indexes follow the table. Index and constraint names keep their old text.",
...(table.dependentViews.length ? [`Views that use it: ${table.dependentViews.join(", ")}.`] : []),
],
undo: `ALTER TABLE ${qualifiedIdent(table.schema, newName)} RENAME TO ${ident(table.name)};`,
});
if (!keepOldName) {
this.add({
title: `Rename table ${oldQualified} to ${newName}`,
sql: `ALTER TABLE ${from} RENAME TO ${ident(newName)};`,
mode: "short",
lock: "ACCESS EXCLUSIVE",
work: "metadata",
table,
breaksClients: !table.isNew,
notes: [
...(table.isNew ? [] : ["The old name stops working immediately. Queries, functions and application code that use it will fail until they are changed."]),
"Views, foreign keys and indexes follow the table. Index and constraint names keep their old text.",
...(table.dependentViews.length ? [`Views that use it: ${table.dependentViews.join(", ")}.`] : []),
],
undo: `ALTER TABLE ${to} RENAME TO ${ident(oldName)};`,
});
} else {
// The old name stays usable as a view over the renamed table, in the same transaction as the rename, so no query ever sees it missing.
const invoker = this.facts.serverVersionNum >= 150000;
if (table.rowSecurity && !invoker) {
this.fail("unsupported", `${oldQualified} has row-level security. Before PostgreSQL 15 a view over it would run as its owner, who is not held to the policies, so a compatibility view under the old name would let applications around them. Rename it directly by turning compatibility views off (compatViews: false), or move to PostgreSQL 15 or later.`, table.qualified);
}
const skipped = new Set<string>();
const grants = table.privileges
.filter((privilege) => !privilege.owner)
.filter((privilege) => {
const copied = ["SELECT", "INSERT", "UPDATE", "DELETE"].includes(privilege.privilege);
if (!copied) skipped.add(privilege.privilege);
return copied;
});
const byGrantee = new Map<string, { privileges: string[]; grantable: string[] }>();
for (const grant of grants) {
const entry = byGrantee.get(grant.grantee) ?? { privileges: [], grantable: [] };
entry.privileges.push(grant.privilege);
if (grant.grantable) entry.grantable.push(grant.privilege);
byGrantee.set(grant.grantee, entry);
}
const grantSql: string[] = [];
for (const [grantee, entry] of byGrantee) {
const who = grantee === "PUBLIC" ? "PUBLIC" : ident(grantee);
const plain = entry.privileges.filter((privilege) => !entry.grantable.includes(privilege));
if (plain.length) grantSql.push(`GRANT ${plain.join(", ")} ON ${from} TO ${who};`);
if (entry.grantable.length) grantSql.push(`GRANT ${entry.grantable.join(", ")} ON ${from} TO ${who} WITH GRANT OPTION;`);
}
this.add({
title: `Rename table ${oldQualified} to ${newName}, and keep ${oldName} working as a view`,
sql: [`ALTER TABLE ${from} RENAME TO ${ident(newName)};`, `CREATE VIEW ${from}${invoker ? " WITH (security_invoker = true)" : ""} AS SELECT * FROM ${to};`, ...grantSql].join("\n"),
mode: "short",
lock: "ACCESS EXCLUSIVE",
work: "metadata",
table,
notes: [
`One transaction: ${newName} is the table and ${oldName} is a view over it, so applications using the old name keep working until they have switched.`,
`Through the view, SELECT, INSERT, UPDATE and DELETE work (a view that is just SELECT * is updatable, and the table's defaults still apply to INSERT). ALTER TABLE, TRUNCATE, COPY, LOCK and the like do not work on a view, and the view lists the columns the table has now: columns added later are not in it.`,
...(grantSql.length ? [`The table's SELECT, INSERT, UPDATE and DELETE grants are copied onto the view (${[...byGrantee.keys()].join(", ")}).`] : ["The table has no grants other than its owner's, so none are copied."]),
...(skipped.size ? [`Grants of ${[...skipped].join(", ")} are not copied (a view cannot hold them), and column-level grants are not copied either.`] : ["Column-level grants, if there are any, are not copied."]),
...(invoker ? ["The view runs with the caller's own rights on the table (security_invoker), so row-level security and privileges apply as before."] : []),
"Index and constraint names keep their old text. Views, foreign keys and indexes follow the table.",
...(table.dependentViews.length ? [`Views that use it: ${table.dependentViews.join(", ")}.`] : []),
`Until the view is dropped, a column of ${newName} that the view lists cannot be dropped or have its type changed: drop the view first.`,
],
undo: `DROP VIEW ${from};\nALTER TABLE ${to} RENAME TO ${ident(oldName)};`,
});
this.add({
title: `Contract: drop the old name ${oldQualified}, the compatibility view`,
sql: `DROP VIEW ${from};`,
mode: "short",
lock: "ACCESS EXCLUSIVE",
work: "metadata",
table,
optional: true,
breaksClients: true,
notes: ["Run this later, once nothing uses the old name any more. Anything that still does will fail from then on."],
undo: `CREATE VIEW ${from}${invoker ? " WITH (security_invoker = true)" : ""} AS SELECT * FROM ${to};`,
});
}

table.name = newName;
table.qualified = target;
Expand All @@ -419,6 +484,12 @@ class Compiler {
for (const fk of other.foreignKeys) if (fk.refSchema === table.schema && fk.refTable === splitQualified(oldQualified).name) fk.refTable = newName;
for (const inbound of other.referencedBy) if (inbound.schema === table.schema && inbound.table === splitQualified(oldQualified).name) inbound.table = newName;
}
if (keepOldName) {
// the view now depends on every column it lists, and later steps in the plan have to know
table.dependentViews.push(oldQualified);
this.claimedRelations.add(relationKey(table.schema, oldName));
for (const column of table.columns) column.dependents.push({ kind: "view", description: `view ${oldQualified}`, blocks: true, external: true });
}
}

private dropTable(operation: Extract<Operation, { type: "drop_table" }>): void {
Expand Down
12 changes: 11 additions & 1 deletion server/src/schema-change/expected-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface ExpectedTable {
checks: { name: string; validated: boolean }[];
foreignKeys: { name: string; columns: string[]; refTable: string; refColumns: string[]; validated: boolean }[];
indexes: { name: string; columns: (string | null)[]; unique: boolean }[];
/** Views that depend on the table, schema qualified: the others it already had, and the old name a rename leaves behind. */
views: string[];
}

export interface ExpectedState {
Expand All @@ -30,7 +32,7 @@ export interface ExpectedState {
export interface Mismatch {
table: string;
/** What differs: a column, index, constraint, foreign key, the primary key, or the table itself. */
kind: "table" | "column" | "primary-key" | "unique" | "check" | "foreign-key" | "index";
kind: "table" | "column" | "primary-key" | "unique" | "check" | "foreign-key" | "index" | "view";
message: string;
}

Expand All @@ -43,6 +45,7 @@ export function expectedState(original: Map<string, TableFacts>, final: Map<stri
checks: table.checks.map((check) => ({ name: check.name, validated: check.validated })),
foreignKeys: table.foreignKeys.map((fk) => ({ name: fk.name, columns: [...fk.columns], refTable: `${fk.refSchema}.${fk.refTable}`, refColumns: [...fk.refColumns], validated: fk.validated })),
indexes: table.indexes.filter((index) => !index.primary).map((index) => ({ name: index.name, columns: [...index.columns], unique: index.unique })),
views: [...table.dependentViews].sort(),
}));
const kept = new Set(tables.map((table) => table.qualified));
const absent = [...original.keys()].filter((key) => !kept.has(key));
Expand Down Expand Up @@ -112,6 +115,13 @@ export function diffExpected(expected: ExpectedState, actual: Facts): Mismatch[]
if (!table.foreignKeys.some((fk) => fk.name === have.name)) note("foreign-key", `Foreign key ${have.name} exists, but the plan did not expect it.`);
}

for (const view of table.views) {
if (!real.dependentViews.includes(view)) note("view", `View ${view} should depend on this table, but it does not exist.`);
}
for (const view of real.dependentViews) {
if (!table.views.includes(view)) note("view", `View ${view} depends on this table, but the plan did not expect it.`);
}

for (const index of table.indexes) {
const have = real.indexes.find((candidate) => candidate.name === index.name);
if (!have) note("index", `Index ${index.name} should exist, but it does not.`);
Expand Down
16 changes: 16 additions & 0 deletions server/src/schema-change/facts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ export interface TableStats {
modificationsSinceAnalyze: number;
}

/** One privilege on a table as the catalog lists it (aclexplode of relacl); `grantee` is a role name or PUBLIC. */
export interface TablePrivilege {
grantee: string;
privilege: string;
grantable: boolean;
/** The grantee owns the table: its privileges are implied and a copy of the table's grants does not repeat them. */
owner: boolean;
}

export interface TableFacts {
schema: string;
name: string;
Expand Down Expand Up @@ -121,6 +130,9 @@ export interface TableFacts {
/** Objects on other tables, and functions, that depend on this table or any of its columns: what would block `DROP TABLE`. */
dependents: Dependent[];
userTriggers: number;
/** Row-level security is switched on: a view over the table would not apply the policies to its owner. */
rowSecurity: boolean;
privileges: TablePrivilege[];
stats: TableStats | null;
/** True for a table the plan itself creates: empty, with no history. */
isNew?: boolean;
Expand Down Expand Up @@ -192,6 +204,8 @@ export async function loadFacts(client: Queryable, qualified: string[]): Promise
t.relkind = 'p' AND EXISTS (SELECT 1 FROM pg_partition_tree(t.oid) p WHERE p.level > 1) AS nested_partitions,
CASE WHEN t.relkind = 'p' THEN (SELECT COALESCE(json_agg(p.name ORDER BY p.name), '[]'::json) FROM (SELECT pn.nspname || '.' || pc.relname AS name FROM pg_partition_tree(t.oid) pt JOIN pg_class pc ON pc.oid = pt.relid JOIN pg_namespace pn ON pn.oid = pc.relnamespace WHERE pt.level = 1 ORDER BY 1 LIMIT ${MAX_LISTED_PARTITIONS}) p) ELSE '[]'::json END AS partitions,
(SELECT count(*) FROM pg_trigger g WHERE g.tgrelid = t.oid AND NOT g.tgisinternal)::int AS user_triggers,
(SELECT c3.relrowsecurity FROM pg_class c3 WHERE c3.oid = t.oid) AS row_security,
COALESCE((SELECT json_agg(json_build_object('grantee', CASE WHEN a.grantee = 0 THEN 'PUBLIC' ELSE pg_get_userbyid(a.grantee) END, 'privilege', a.privilege_type, 'grantable', a.is_grantable, 'owner', a.grantee = c2.relowner) ORDER BY a.grantee, a.privilege_type) FROM pg_class c2, aclexplode(c2.relacl) a WHERE c2.oid = t.oid), '[]'::json) AS privileges,
s.n_live_tup::text AS live, s.n_dead_tup::text AS dead, s.last_vacuum, s.last_autovacuum, s.last_analyze, s.last_autoanalyze,
s.seq_scan::text AS seq_scan, s.idx_scan::text AS idx_scan, s.n_mod_since_analyze::text AS mods
FROM t LEFT JOIN pg_stat_user_tables s ON s.relid = t.oid`,
Expand Down Expand Up @@ -303,6 +317,8 @@ export async function loadFacts(client: Queryable, qualified: string[]): Promise
dependentViews: [],
dependents: [],
userTriggers: Number(row.user_triggers),
rowSecurity: row.row_security === true,
privileges: (row.privileges as TablePrivilege[] | null) ?? [],
stats: hasStats
? {
liveTuples: Number(row.live),
Expand Down
Loading
Loading