@@ -6984,22 +6984,15 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
6984
6984
Relation pgclass,
6985
6985
attrdesc;
6986
6986
HeapTuple reltup;
6987
- FormData_pg_attribute attribute;
6987
+ Form_pg_attribute attribute;
6988
6988
int newattnum;
6989
6989
char relkind;
6990
- HeapTuple typeTuple;
6991
- Oid typeOid;
6992
- int32 typmod;
6993
- Oid collOid;
6994
- Form_pg_type tform;
6995
6990
Expr *defval;
6996
6991
List *children;
6997
6992
ListCell *child;
6998
6993
AlterTableCmd *childcmd;
6999
- AclResult aclresult;
7000
6994
ObjectAddress address;
7001
6995
TupleDesc tupdesc;
7002
- FormData_pg_attribute *aattr[] = {&attribute};
7003
6996
7004
6997
/* At top level, permission check was done in ATPrepCmd, else do it */
7005
6998
if (recursing)
@@ -7121,59 +7114,21 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
7121
7114
errmsg("tables can have at most %d columns",
7122
7115
MaxHeapAttributeNumber)));
7123
7116
7124
- typeTuple = typenameType(NULL, colDef->typeName, &typmod);
7125
- tform = (Form_pg_type) GETSTRUCT(typeTuple);
7126
- typeOid = tform->oid;
7117
+ /*
7118
+ * Construct new attribute's pg_attribute entry.
7119
+ */
7120
+ tupdesc = BuildDescForRelation(list_make1(colDef));
7127
7121
7128
- aclresult = object_aclcheck(TypeRelationId, typeOid, GetUserId(), ACL_USAGE);
7129
- if (aclresult != ACLCHECK_OK)
7130
- aclcheck_error_type(aclresult, typeOid);
7122
+ attribute = TupleDescAttr(tupdesc, 0);
7131
7123
7132
- collOid = GetColumnDefCollation(NULL, colDef, typeOid);
7124
+ /* Fix up attribute number */
7125
+ attribute->attnum = newattnum;
7133
7126
7134
7127
/* make sure datatype is legal for a column */
7135
- CheckAttributeType(colDef->colname, typeOid, collOid ,
7128
+ CheckAttributeType(NameStr(attribute->attname), attribute->atttypid, attribute->attcollation ,
7136
7129
list_make1_oid(rel->rd_rel->reltype),
7137
7130
0);
7138
7131
7139
- /*
7140
- * Construct new attribute's pg_attribute entry. (Variable-length fields
7141
- * are handled by InsertPgAttributeTuples().)
7142
- */
7143
- attribute.attrelid = myrelid;
7144
- namestrcpy(&(attribute.attname), colDef->colname);
7145
- attribute.atttypid = typeOid;
7146
- attribute.attstattarget = -1;
7147
- attribute.attlen = tform->typlen;
7148
- attribute.attnum = newattnum;
7149
- if (list_length(colDef->typeName->arrayBounds) > PG_INT16_MAX)
7150
- ereport(ERROR,
7151
- errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
7152
- errmsg("too many array dimensions"));
7153
- attribute.attndims = list_length(colDef->typeName->arrayBounds);
7154
- attribute.atttypmod = typmod;
7155
- attribute.attbyval = tform->typbyval;
7156
- attribute.attalign = tform->typalign;
7157
- if (colDef->storage_name)
7158
- attribute.attstorage = GetAttributeStorage(typeOid, colDef->storage_name);
7159
- else
7160
- attribute.attstorage = tform->typstorage;
7161
- attribute.attcompression = GetAttributeCompression(typeOid,
7162
- colDef->compression);
7163
- attribute.attnotnull = colDef->is_not_null;
7164
- attribute.atthasdef = false;
7165
- attribute.atthasmissing = false;
7166
- attribute.attidentity = colDef->identity;
7167
- attribute.attgenerated = colDef->generated;
7168
- attribute.attisdropped = false;
7169
- attribute.attislocal = colDef->is_local;
7170
- attribute.attinhcount = colDef->inhcount;
7171
- attribute.attcollation = collOid;
7172
-
7173
- ReleaseSysCache(typeTuple);
7174
-
7175
- tupdesc = CreateTupleDesc(lengthof(aattr), (FormData_pg_attribute **) &aattr);
7176
-
7177
7132
InsertPgAttributeTuples(attrdesc, tupdesc, myrelid, NULL, NULL);
7178
7133
7179
7134
table_close(attrdesc, RowExclusiveLock);
@@ -7203,7 +7158,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
7203
7158
RawColumnDefault *rawEnt;
7204
7159
7205
7160
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
7206
- rawEnt->attnum = attribute. attnum;
7161
+ rawEnt->attnum = attribute-> attnum;
7207
7162
rawEnt->raw_default = copyObject(colDef->raw_default);
7208
7163
7209
7164
/*
@@ -7277,31 +7232,31 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
7277
7232
NextValueExpr *nve = makeNode(NextValueExpr);
7278
7233
7279
7234
nve->seqid = RangeVarGetRelid(colDef->identitySequence, NoLock, false);
7280
- nve->typeId = typeOid ;
7235
+ nve->typeId = attribute->atttypid ;
7281
7236
7282
7237
defval = (Expr *) nve;
7283
7238
7284
7239
/* must do a rewrite for identity columns */
7285
7240
tab->rewrite |= AT_REWRITE_DEFAULT_VAL;
7286
7241
}
7287
7242
else
7288
- defval = (Expr *) build_column_default(rel, attribute. attnum);
7243
+ defval = (Expr *) build_column_default(rel, attribute-> attnum);
7289
7244
7290
- if (!defval && DomainHasConstraints(typeOid ))
7245
+ if (!defval && DomainHasConstraints(attribute->atttypid ))
7291
7246
{
7292
7247
Oid baseTypeId;
7293
7248
int32 baseTypeMod;
7294
7249
Oid baseTypeColl;
7295
7250
7296
- baseTypeMod = typmod ;
7297
- baseTypeId = getBaseTypeAndTypmod(typeOid , &baseTypeMod);
7251
+ baseTypeMod = attribute->atttypmod ;
7252
+ baseTypeId = getBaseTypeAndTypmod(attribute->atttypid , &baseTypeMod);
7298
7253
baseTypeColl = get_typcollation(baseTypeId);
7299
7254
defval = (Expr *) makeNullConst(baseTypeId, baseTypeMod, baseTypeColl);
7300
7255
defval = (Expr *) coerce_to_target_type(NULL,
7301
7256
(Node *) defval,
7302
7257
baseTypeId,
7303
- typeOid ,
7304
- typmod ,
7258
+ attribute->atttypid ,
7259
+ attribute->atttypmod ,
7305
7260
COERCION_ASSIGNMENT,
7306
7261
COERCE_IMPLICIT_CAST,
7307
7262
-1);
@@ -7314,17 +7269,17 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
7314
7269
NewColumnValue *newval;
7315
7270
7316
7271
newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue));
7317
- newval->attnum = attribute. attnum;
7272
+ newval->attnum = attribute-> attnum;
7318
7273
newval->expr = expression_planner(defval);
7319
7274
newval->is_generated = (colDef->generated != '\0');
7320
7275
7321
7276
tab->newvals = lappend(tab->newvals, newval);
7322
7277
}
7323
7278
7324
- if (DomainHasConstraints(typeOid ))
7279
+ if (DomainHasConstraints(attribute->atttypid ))
7325
7280
tab->rewrite |= AT_REWRITE_DEFAULT_VAL;
7326
7281
7327
- if (!TupleDescAttr(rel->rd_att, attribute. attnum - 1)->atthasmissing)
7282
+ if (!TupleDescAttr(rel->rd_att, attribute-> attnum - 1)->atthasmissing)
7328
7283
{
7329
7284
/*
7330
7285
* If the new column is NOT NULL, and there is no missing value,
@@ -7337,8 +7292,8 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
7337
7292
/*
7338
7293
* Add needed dependency entries for the new column.
7339
7294
*/
7340
- add_column_datatype_dependency(myrelid, newattnum, attribute. atttypid);
7341
- add_column_collation_dependency(myrelid, newattnum, attribute. attcollation);
7295
+ add_column_datatype_dependency(myrelid, newattnum, attribute-> atttypid);
7296
+ add_column_collation_dependency(myrelid, newattnum, attribute-> attcollation);
7342
7297
7343
7298
/*
7344
7299
* Propagate to children as appropriate. Unlike most other ALTER
0 commit comments