Skip to content

Commit 5afe7cc

Browse files
author
hackorum
committed
Apply 0001-on_update_set_default.patch
1 parent 4255812 commit 5afe7cc

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
@@ -153,6 +153,15 @@ static relopt_bool boolRelOpts[] =
153153
},
154154
true
155155
},
156+
{
157+
{
158+
"on_update_set_default",
159+
"Automatically set the column to its default value if the column has a default value in an UPDATE statement",
160+
RELOPT_KIND_ATTRIBUTE,
161+
ShareUpdateExclusiveLock
162+
},
163+
false
164+
},
156165
/* list terminator */
157166
{{NULL}}
158167
};
@@ -2418,7 +2427,8 @@ attribute_reloptions(Datum reloptions, bool validate)
24182427
{
24192428
static const relopt_parse_elt tab[] = {
24202429
{"n_distinct", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct)},
2421-
{"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)}
2430+
{"n_distinct_inherited", RELOPT_TYPE_REAL, offsetof(AttributeOpts, n_distinct_inherited)},
2431+
{"on_update_set_default", RELOPT_TYPE_BOOL, offsetof(AttributeOpts, on_update_set_default)}
24222432
};
24232433

24242434
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
@@ -41,6 +41,7 @@
4141
#include "rewrite/rewriteSearchCycle.h"
4242
#include "rewrite/rowsecurity.h"
4343
#include "tcop/tcopprot.h"
44+
#include "utils/attoptcache.h"
4445
#include "utils/builtins.h"
4546
#include "utils/lsyscache.h"
4647
#include "utils/rel.h"
@@ -897,6 +898,7 @@ rewriteTargetListIU(List *targetList,
897898
{
898899
TargetEntry *new_tle = new_tles[attrno - 1];
899900
bool apply_default;
901+
bool on_update_set_default = false;
900902

901903
att_tup = TupleDescAttr(target_relation->rd_att, attrno - 1);
902904

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

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

10781109
if (new_tle)
10791110
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)