Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4528768

Browse files
committed
Remove now-dead code in StoreAttrDefault().
StoreAttrDefault() is no longer responsible for filling attmissingval, so remove the code for that. Get rid of RawColumnDefault.missingMode, too, as we no longer need that to pass information around. While here, clean up some sloppy coding in StoreAttrDefault(), such as failure to use XXXGetDatum macros. These aren't bugs but they're not good code either. Reported-by: jian he <jian.universality@gmail.com> Author: jian he <jian.universality@gmail.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CACJufxHFssPvkP1we7WMhPD_1kwgbG52o=kQgL+TnVoX5LOyCQ@mail.gmail.com
1 parent 95f6506 commit 4528768

File tree

5 files changed

+11
-83
lines changed

5 files changed

+11
-83
lines changed

src/backend/catalog/heap.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
23202320
{
23212321
case CONSTR_DEFAULT:
23222322
con->conoid = StoreAttrDefault(rel, con->attnum, con->expr,
2323-
is_internal, false);
2323+
is_internal);
23242324
break;
23252325
case CONSTR_CHECK:
23262326
con->conoid =
@@ -2447,8 +2447,7 @@ AddRelationNewConstraints(Relation rel,
24472447
castNode(Const, expr)->constisnull))
24482448
continue;
24492449

2450-
defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal,
2451-
false);
2450+
defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal);
24522451

24532452
cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
24542453
cooked->contype = CONSTR_DEFAULT;

src/backend/catalog/pg_attrdef.c

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@
1414
*/
1515
#include "postgres.h"
1616

17-
#include "access/genam.h"
1817
#include "access/relation.h"
1918
#include "access/table.h"
20-
#include "catalog/catalog.h"
2119
#include "catalog/dependency.h"
2220
#include "catalog/indexing.h"
2321
#include "catalog/objectaccess.h"
2422
#include "catalog/pg_attrdef.h"
25-
#include "executor/executor.h"
26-
#include "optimizer/optimizer.h"
27-
#include "utils/array.h"
2823
#include "utils/builtins.h"
2924
#include "utils/fmgroids.h"
3025
#include "utils/rel.h"
@@ -35,22 +30,16 @@
3530
* Store a default expression for column attnum of relation rel.
3631
*
3732
* Returns the OID of the new pg_attrdef tuple.
38-
*
39-
* add_column_mode must be true if we are storing the default for a new
40-
* attribute, and false if it's for an already existing attribute. The reason
41-
* for this is that the missing value must never be updated after it is set,
42-
* which can only be when a column is added to the table. Otherwise we would
43-
* in effect be changing existing tuples.
4433
*/
4534
Oid
4635
StoreAttrDefault(Relation rel, AttrNumber attnum,
47-
Node *expr, bool is_internal, bool add_column_mode)
36+
Node *expr, bool is_internal)
4837
{
4938
char *adbin;
5039
Relation adrel;
5140
HeapTuple tuple;
52-
Datum values[4];
53-
static bool nulls[4] = {false, false, false, false};
41+
Datum values[Natts_pg_attrdef];
42+
static bool nulls[Natts_pg_attrdef] = {false, false, false, false};
5443
Relation attrrel;
5544
HeapTuple atttup;
5645
Form_pg_attribute attStruct;
@@ -72,8 +61,8 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
7261
attrdefOid = GetNewOidWithIndex(adrel, AttrDefaultOidIndexId,
7362
Anum_pg_attrdef_oid);
7463
values[Anum_pg_attrdef_oid - 1] = ObjectIdGetDatum(attrdefOid);
75-
values[Anum_pg_attrdef_adrelid - 1] = RelationGetRelid(rel);
76-
values[Anum_pg_attrdef_adnum - 1] = attnum;
64+
values[Anum_pg_attrdef_adrelid - 1] = ObjectIdGetDatum(RelationGetRelid(rel));
65+
values[Anum_pg_attrdef_adnum - 1] = Int16GetDatum(attnum);
7766
values[Anum_pg_attrdef_adbin - 1] = CStringGetTextDatum(adbin);
7867

7968
tuple = heap_form_tuple(adrel->rd_att, values, nulls);
@@ -105,71 +94,17 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
10594
attgenerated = attStruct->attgenerated;
10695
if (!attStruct->atthasdef)
10796
{
108-
Form_pg_attribute defAttStruct;
109-
110-
ExprState *exprState;
111-
Expr *expr2 = (Expr *) expr;
112-
EState *estate = NULL;
113-
ExprContext *econtext;
11497
Datum valuesAtt[Natts_pg_attribute] = {0};
11598
bool nullsAtt[Natts_pg_attribute] = {0};
11699
bool replacesAtt[Natts_pg_attribute] = {0};
117-
Datum missingval = (Datum) 0;
118-
bool missingIsNull = true;
119100

120-
valuesAtt[Anum_pg_attribute_atthasdef - 1] = true;
101+
valuesAtt[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(true);
121102
replacesAtt[Anum_pg_attribute_atthasdef - 1] = true;
122103

123-
/*
124-
* Note: this code is dead so far as core Postgres is concerned,
125-
* because no caller passes add_column_mode = true anymore. We keep
126-
* it in back branches on the slight chance that some extension is
127-
* depending on it.
128-
*/
129-
if (rel->rd_rel->relkind == RELKIND_RELATION && add_column_mode &&
130-
!attgenerated)
131-
{
132-
expr2 = expression_planner(expr2);
133-
estate = CreateExecutorState();
134-
exprState = ExecPrepareExpr(expr2, estate);
135-
econtext = GetPerTupleExprContext(estate);
136-
137-
missingval = ExecEvalExpr(exprState, econtext,
138-
&missingIsNull);
139-
140-
FreeExecutorState(estate);
141-
142-
defAttStruct = TupleDescAttr(rel->rd_att, attnum - 1);
143-
144-
if (missingIsNull)
145-
{
146-
/* if the default evaluates to NULL, just store a NULL array */
147-
missingval = (Datum) 0;
148-
}
149-
else
150-
{
151-
/* otherwise make a one-element array of the value */
152-
missingval = PointerGetDatum(construct_array(&missingval,
153-
1,
154-
defAttStruct->atttypid,
155-
defAttStruct->attlen,
156-
defAttStruct->attbyval,
157-
defAttStruct->attalign));
158-
}
159-
160-
valuesAtt[Anum_pg_attribute_atthasmissing - 1] = !missingIsNull;
161-
replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
162-
valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval;
163-
replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
164-
nullsAtt[Anum_pg_attribute_attmissingval - 1] = missingIsNull;
165-
}
166104
atttup = heap_modify_tuple(atttup, RelationGetDescr(attrrel),
167105
valuesAtt, nullsAtt, replacesAtt);
168106

169107
CatalogTupleUpdate(attrrel, &atttup->t_self, atttup);
170-
171-
if (!missingIsNull)
172-
pfree(DatumGetPointer(missingval));
173108
}
174109
table_close(attrrel, RowExclusiveLock);
175110
heap_freetuple(atttup);

src/backend/commands/tablecmds.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
967967
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
968968
rawEnt->attnum = attnum;
969969
rawEnt->raw_default = colDef->raw_default;
970-
rawEnt->missingMode = false;
971970
rawEnt->generated = colDef->generated;
972971
rawDefaults = lappend(rawDefaults, rawEnt);
973972
attr->atthasdef = true;
@@ -7321,7 +7320,6 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
73217320
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
73227321
rawEnt->attnum = attribute->attnum;
73237322
rawEnt->raw_default = copyObject(colDef->raw_default);
7324-
rawEnt->missingMode = false; /* XXX vestigial */
73257323
rawEnt->generated = colDef->generated;
73267324

73277325
/*
@@ -8077,7 +8075,6 @@ ATExecColumnDefault(Relation rel, const char *colName,
80778075
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
80788076
rawEnt->attnum = attnum;
80798077
rawEnt->raw_default = newDefault;
8080-
rawEnt->missingMode = false;
80818078
rawEnt->generated = '\0';
80828079

80838080
/*
@@ -8115,7 +8112,7 @@ ATExecCookedColumnDefault(Relation rel, AttrNumber attnum,
81158112
RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, false,
81168113
true);
81178114

8118-
(void) StoreAttrDefault(rel, attnum, newDefault, true, false);
8115+
(void) StoreAttrDefault(rel, attnum, newDefault, true);
81198116

81208117
ObjectAddressSubSet(address, RelationRelationId,
81218118
RelationGetRelid(rel), attnum);
@@ -8578,7 +8575,6 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName,
85788575
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
85798576
rawEnt->attnum = attnum;
85808577
rawEnt->raw_default = newExpr;
8581-
rawEnt->missingMode = false;
85828578
rawEnt->generated = attgenerated;
85838579

85848580
/* Store the generated expression */
@@ -14130,7 +14126,7 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
1413014126
RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, true,
1413114127
true);
1413214128

14133-
StoreAttrDefault(rel, attnum, defaultexpr, true, false);
14129+
(void) StoreAttrDefault(rel, attnum, defaultexpr, true);
1413414130
}
1413514131

1413614132
ObjectAddressSubSet(address, RelationRelationId,

src/include/catalog/heap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ typedef struct RawColumnDefault
2929
{
3030
AttrNumber attnum; /* attribute to attach default to */
3131
Node *raw_default; /* default value (untransformed parse tree) */
32-
bool missingMode; /* obsolete, no longer used */
3332
char generated; /* attgenerated setting */
3433
} RawColumnDefault;
3534

src/include/catalog/pg_attrdef.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ DECLARE_FOREIGN_KEY((adrelid, adnum), pg_attribute, (attrelid, attnum));
5757

5858

5959
extern Oid StoreAttrDefault(Relation rel, AttrNumber attnum,
60-
Node *expr, bool is_internal,
61-
bool add_column_mode);
60+
Node *expr, bool is_internal);
6261
extern void RemoveAttrDefault(Oid relid, AttrNumber attnum,
6362
DropBehavior behavior,
6463
bool complain, bool internal);

0 commit comments

Comments
 (0)