Skip to content

Commit 2d158ba

Browse files
author
hackorum
committed
Apply 0001-on_update_set_default.patch
1 parent b597835 commit 2d158ba

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/backend/access/common/reloptions.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ static relopt_bool boolRelOpts[] =
162162
},
163163
true
164164
},
165+
{
166+
{
167+
"on_update_set_default",
168+
"Automatically set the column to its default value if the column has a default value in an UPDATE statement",
169+
RELOPT_KIND_ATTRIBUTE,
170+
ShareUpdateExclusiveLock
171+
},
172+
false
173+
},
165174
/* list terminator */
166175
{{NULL}}
167176
};
@@ -2215,7 +2224,8 @@ attribute_reloptions(Datum reloptions, bool validate)
22152224
{
22162225
static const relopt_parse_elt tab[] = {
22172226
{"n_distinct", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct)},
2218-
{"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)}
2227+
{"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)},
2228+
{"on_update_set_default", RELOPT_TYPE_BOOL, offsetof(AttributeOpts, on_update_set_default)}
22192229
};
22202230

22212231
return (bytea *) build_reloptions(reloptions, validate,

src/backend/rewrite/rewriteHandler.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "rewrite/rewriteSearchCycle.h"
4343
#include "rewrite/rowsecurity.h"
4444
#include "tcop/tcopprot.h"
45+
#include "utils/attoptcache.h"
4546
#include "utils/builtins.h"
4647
#include "utils/lsyscache.h"
4748
#include "utils/rel.h"
@@ -899,6 +900,7 @@ rewriteTargetListIU(List *targetList,
899900
{
900901
TargetEntry *new_tle = new_tles[attrno - 1];
901902
bool apply_default;
903+
bool on_update_set_default = false;
902904

903905
att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
904906

@@ -1035,6 +1037,17 @@ rewriteTargetListIU(List *targetList,
10351037
NameStr(att_tup->attname)),
10361038
errdetail("Column \"%s\" is a generated column.",
10371039
NameStr(att_tup->attname))));
1040+
1041+
if (!new_tle)
1042+
{
1043+
AttributeOpts *aopt;
1044+
1045+
Assert(!apply_default);
1046+
1047+
aopt = get_attribute_options(target_relation->rd_id, attrno);
1048+
if (aopt != NULL && aopt->on_update_set_default)
1049+
on_update_set_default = true;
1050+
}
10381051
}
10391052

10401053
if (att_tup->attgenerated)
@@ -1076,6 +1089,24 @@ rewriteTargetListIU(List *targetList,
10761089
pstrdup(NameStr(att_tup->attname)),
10771090
false);
10781091
}
1092+
else if(on_update_set_default)
1093+
{
1094+
Node *new_expr;
1095+
1096+
Assert(!new_tle);
1097+
1098+
new_expr = build_column_default(target_relation, attrno);
1099+
/*
1100+
* If there is a default value, it should be automatically updated
1101+
* to the default value; otherwise, we should not modify the value
1102+
* of that column.
1103+
*/
1104+
if (new_expr)
1105+
new_tle = makeTargetEntry((Expr *) new_expr,
1106+
attrno,
1107+
pstrdup(NameStr(att_tup->attname)),
1108+
false);
1109+
}
10791110

10801111
if (new_tle)
10811112
new_tlist = lappend(new_tlist, new_tle);

src/include/utils/attoptcache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typedef struct AttributeOpts
2121
int32 vl_len_; /* varlena header (do not touch directly!) */
2222
float8 n_distinct;
2323
float8 n_distinct_inherited;
24+
bool on_update_set_default;
2425
} AttributeOpts;
2526

2627
extern AttributeOpts *get_attribute_options(Oid attrelid, int attnum);

0 commit comments

Comments
 (0)