14
14
*/
15
15
#include "postgres.h"
16
16
17
- #include "access/genam.h"
18
17
#include "access/relation.h"
19
18
#include "access/table.h"
20
- #include "catalog/catalog.h"
21
19
#include "catalog/dependency.h"
22
20
#include "catalog/indexing.h"
23
21
#include "catalog/objectaccess.h"
24
22
#include "catalog/pg_attrdef.h"
25
- #include "executor/executor.h"
26
- #include "optimizer/optimizer.h"
27
- #include "utils/array.h"
28
23
#include "utils/builtins.h"
29
24
#include "utils/fmgroids.h"
30
25
#include "utils/rel.h"
35
30
* Store a default expression for column attnum of relation rel.
36
31
*
37
32
* 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.
44
33
*/
45
34
Oid
46
35
StoreAttrDefault (Relation rel , AttrNumber attnum ,
47
- Node * expr , bool is_internal , bool add_column_mode )
36
+ Node * expr , bool is_internal )
48
37
{
49
38
char * adbin ;
50
39
Relation adrel ;
51
40
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};
54
43
Relation attrrel ;
55
44
HeapTuple atttup ;
56
45
Form_pg_attribute attStruct ;
@@ -72,8 +61,8 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
72
61
attrdefOid = GetNewOidWithIndex (adrel , AttrDefaultOidIndexId ,
73
62
Anum_pg_attrdef_oid );
74
63
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 ) ;
77
66
values [Anum_pg_attrdef_adbin - 1 ] = CStringGetTextDatum (adbin );
78
67
79
68
tuple = heap_form_tuple (adrel -> rd_att , values , nulls );
@@ -105,71 +94,17 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
105
94
attgenerated = attStruct -> attgenerated ;
106
95
if (!attStruct -> atthasdef )
107
96
{
108
- Form_pg_attribute defAttStruct ;
109
-
110
- ExprState * exprState ;
111
- Expr * expr2 = (Expr * ) expr ;
112
- EState * estate = NULL ;
113
- ExprContext * econtext ;
114
97
Datum valuesAtt [Natts_pg_attribute ] = {0 };
115
98
bool nullsAtt [Natts_pg_attribute ] = {0 };
116
99
bool replacesAtt [Natts_pg_attribute ] = {0 };
117
- Datum missingval = (Datum ) 0 ;
118
- bool missingIsNull = true;
119
100
120
- valuesAtt [Anum_pg_attribute_atthasdef - 1 ] = true;
101
+ valuesAtt [Anum_pg_attribute_atthasdef - 1 ] = BoolGetDatum ( true) ;
121
102
replacesAtt [Anum_pg_attribute_atthasdef - 1 ] = true;
122
103
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
- }
166
104
atttup = heap_modify_tuple (atttup , RelationGetDescr (attrrel ),
167
105
valuesAtt , nullsAtt , replacesAtt );
168
106
169
107
CatalogTupleUpdate (attrrel , & atttup -> t_self , atttup );
170
-
171
- if (!missingIsNull )
172
- pfree (DatumGetPointer (missingval ));
173
108
}
174
109
table_close (attrrel , RowExclusiveLock );
175
110
heap_freetuple (atttup );
0 commit comments