From f685d2b6dd5dbc7a34234b6b3cdbd9f2aef1c29d Mon Sep 17 00:00:00 2001 From: jian he Date: Mon, 16 Dec 2024 20:22:16 +0800 Subject: [PATCH v10 1/1] make ALTER TABLE SET EXPRESSION an no-op if not changed if new and old generated expression is the same, make ALTER TABLE ALTER COLUMN SET EXPRESSION a no-op. --- src/backend/commands/tablecmds.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 28623672fa..effa3767e4 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -8407,6 +8407,11 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, Expr *defval; NewColumnValue *newval; RawColumnDefault *rawEnt; + ParseState *pstate; + Node *expr; + Node *old_default; + TupleDesc tupdesc = RelationGetDescr(rel); + ParseNamespaceItem *nsitem; tuple = SearchSysCacheAttName(RelationGetRelid(rel), colName); if (!HeapTupleIsValid(tuple)) @@ -8462,6 +8467,24 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, ReleaseSysCache(tuple); + old_default = TupleDescGetDefault(tupdesc, attnum); + pstate = make_parsestate(NULL); + pstate->p_sourcetext = NULL; + nsitem = addRangeTableEntryForRelation(pstate, + rel, + AccessShareLock, + NULL, + false, + true); + addNSItemToQuery(pstate, nsitem, true, true, true); + expr = cookDefault(pstate, newExpr, + attTup->atttypid, attTup->atttypmod, + NameStr(attTup->attname), + attTup->attgenerated); + + if(equal(expr, old_default)) + return InvalidObjectAddress; + if (rewrite) { /* -- 2.34.1