Make more use of makeColumnDef()
authorPeter Eisentraut <peter@eisentraut.org>
Tue, 29 Aug 2023 06:41:04 +0000 (08:41 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Tue, 29 Aug 2023 06:45:05 +0000 (08:45 +0200)
Since we already have it, we might as well make full use of it,
instead of assembling ColumnDef by hand in several places.

Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da@eisentraut.org

src/backend/commands/sequence.c
src/backend/commands/tablecmds.c
src/backend/parser/parse_utilcmd.c

index ef1b8a92f25ede875e9edb50f0a122e2f342c0f3..0b0003fcc83c0f9d6ef0c47aa43f11ca27f67ec3 100644 (file)
@@ -172,40 +172,27 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
    stmt->tableElts = NIL;
    for (i = SEQ_COL_FIRSTCOL; i <= SEQ_COL_LASTCOL; i++)
    {
-       ColumnDef  *coldef = makeNode(ColumnDef);
-
-       coldef->inhcount = 0;
-       coldef->is_local = true;
-       coldef->is_not_null = true;
-       coldef->is_from_type = false;
-       coldef->storage = 0;
-       coldef->raw_default = NULL;
-       coldef->cooked_default = NULL;
-       coldef->collClause = NULL;
-       coldef->collOid = InvalidOid;
-       coldef->constraints = NIL;
-       coldef->location = -1;
-
-       null[i - 1] = false;
+       ColumnDef  *coldef;
 
        switch (i)
        {
            case SEQ_COL_LASTVAL:
-               coldef->typeName = makeTypeNameFromOid(INT8OID, -1);
-               coldef->colname = "last_value";
+               coldef = makeColumnDef("last_value", INT8OID, -1, InvalidOid);
                value[i - 1] = Int64GetDatumFast(seqdataform.last_value);
                break;
            case SEQ_COL_LOG:
-               coldef->typeName = makeTypeNameFromOid(INT8OID, -1);
-               coldef->colname = "log_cnt";
+               coldef = makeColumnDef("log_cnt", INT8OID, -1, InvalidOid);
                value[i - 1] = Int64GetDatum((int64) 0);
                break;
            case SEQ_COL_CALLED:
-               coldef->typeName = makeTypeNameFromOid(BOOLOID, -1);
-               coldef->colname = "is_called";
+               coldef = makeColumnDef("is_called", BOOLOID, -1, InvalidOid);
                value[i - 1] = BoolGetDatum(false);
                break;
        }
+
+       coldef->is_not_null = true;
+       null[i - 1] = false;
+
        stmt->tableElts = lappend(stmt->tableElts, coldef);
    }
 
index 2ac6f8b334f75ccd78e9fa8c732eae488811e8e0..a8c40ace10cf8f45d06950f9a7fb650aa93b4c34 100644 (file)
@@ -2755,10 +2755,8 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
                /*
                 * No, create a new inherited column
                 */
-               def = makeNode(ColumnDef);
-               def->colname = pstrdup(attributeName);
-               def->typeName = makeTypeNameFromOid(attribute->atttypid,
-                                                   attribute->atttypmod);
+               def = makeColumnDef(attributeName, attribute->atttypid,
+                                   attribute->atttypmod, attribute->attcollation);
                def->inhcount = 1;
                def->is_local = false;
                /* mark attnotnull if parent has it and it's not NO INHERIT */
@@ -2766,20 +2764,11 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
                    bms_is_member(parent_attno - FirstLowInvalidHeapAttributeNumber,
                                  pkattrs))
                    def->is_not_null = true;
-               def->is_from_type = false;
                def->storage = attribute->attstorage;
-               def->raw_default = NULL;
-               def->cooked_default = NULL;
                def->generated = attribute->attgenerated;
-               def->collClause = NULL;
-               def->collOid = attribute->attcollation;
-               def->constraints = NIL;
-               def->location = -1;
                if (CompressionMethodIsValid(attribute->attcompression))
                    def->compression =
                        pstrdup(GetCompressionMethodName(attribute->attcompression));
-               else
-                   def->compression = NULL;
                inhSchema = lappend(inhSchema, def);
                newattmap->attnums[parent_attno - 1] = ++child_attno;
 
index bab7b87fe863eda8d8d35af0531f7e84a6c22569..55c315f0e28c7fcf0ea98b98f715986606b6b997 100644 (file)
@@ -1066,7 +1066,6 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
    {
        Form_pg_attribute attribute = TupleDescAttr(tupleDesc,
                                                    parent_attno - 1);
-       char       *attributeName = NameStr(attribute->attname);
        ColumnDef  *def;
 
        /*
@@ -1076,29 +1075,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
            continue;
 
        /*
-        * Create a new column, which is marked as NOT inherited.
-        *
+        * Create a new column definition
+        */
+       def = makeColumnDef(NameStr(attribute->attname), attribute->atttypid,
+                           attribute->atttypmod, attribute->attcollation);
+
+       /*
         * For constraints, ONLY the not-null constraint is inherited by the
         * new column definition per SQL99; however we cannot do that
         * correctly here, so we leave it for expandTableLikeClause to handle.
         */
-       def = makeNode(ColumnDef);
-       def->colname = pstrdup(attributeName);
-       def->typeName = makeTypeNameFromOid(attribute->atttypid,
-                                           attribute->atttypmod);
-       def->inhcount = 0;
-       def->is_local = true;
-       def->is_not_null = false;
        if (attribute->attnotnull)
            process_notnull_constraints = true;
-       def->is_from_type = false;
-       def->storage = 0;
-       def->raw_default = NULL;
-       def->cooked_default = NULL;
-       def->collClause = NULL;
-       def->collOid = attribute->attcollation;
-       def->constraints = NIL;
-       def->location = -1;
 
        /*
         * Add to column list
@@ -1635,20 +1623,10 @@ transformOfType(CreateStmtContext *cxt, TypeName *ofTypename)
        if (attr->attisdropped)
            continue;
 
-       n = makeNode(ColumnDef);
-       n->colname = pstrdup(NameStr(attr->attname));
-       n->typeName = makeTypeNameFromOid(attr->atttypid, attr->atttypmod);
-       n->inhcount = 0;
-       n->is_local = true;
-       n->is_not_null = false;
+       n = makeColumnDef(NameStr(attr->attname), attr->atttypid,
+                         attr->atttypmod, attr->attcollation);
        n->is_from_type = true;
-       n->storage = 0;
-       n->raw_default = NULL;
-       n->cooked_default = NULL;
-       n->collClause = NULL;
-       n->collOid = attr->attcollation;
-       n->constraints = NIL;
-       n->location = -1;
+
        cxt->columns = lappend(cxt->columns, n);
    }
    ReleaseTupleDesc(tupdesc);