@@ -41,8 +41,6 @@ TupleDesc
41
41
CreateTemplateTupleDesc (int natts , bool hasoid )
42
42
{
43
43
TupleDesc desc ;
44
- char * stg ;
45
- int attroffset ;
46
44
47
45
/*
48
46
* sanity checks
@@ -51,38 +49,10 @@ CreateTemplateTupleDesc(int natts, bool hasoid)
51
49
52
50
/*
53
51
* Allocate enough memory for the tuple descriptor, including the
54
- * attribute rows, and set up the attribute row pointers.
55
- *
56
- * Note: we assume that sizeof(struct tupleDesc) is a multiple of the
57
- * struct pointer alignment requirement, and hence we don't need to insert
58
- * alignment padding between the struct and the array of attribute row
59
- * pointers.
60
- *
61
- * Note: Only the fixed part of pg_attribute rows is included in tuple
62
- * descriptors, so we only need ATTRIBUTE_FIXED_PART_SIZE space per attr.
63
- * That might need alignment padding, however.
52
+ * attribute rows.
64
53
*/
65
- attroffset = sizeof (struct tupleDesc ) + natts * sizeof (Form_pg_attribute );
66
- attroffset = MAXALIGN (attroffset );
67
- stg = palloc (attroffset + natts * MAXALIGN (ATTRIBUTE_FIXED_PART_SIZE ));
68
- desc = (TupleDesc ) stg ;
69
-
70
- if (natts > 0 )
71
- {
72
- Form_pg_attribute * attrs ;
73
- int i ;
74
-
75
- attrs = (Form_pg_attribute * ) (stg + sizeof (struct tupleDesc ));
76
- desc -> attrs = attrs ;
77
- stg += attroffset ;
78
- for (i = 0 ; i < natts ; i ++ )
79
- {
80
- attrs [i ] = (Form_pg_attribute ) stg ;
81
- stg += MAXALIGN (ATTRIBUTE_FIXED_PART_SIZE );
82
- }
83
- }
84
- else
85
- desc -> attrs = NULL ;
54
+ desc = (TupleDesc ) palloc (offsetof(struct tupleDesc , attrs ) +
55
+ natts * sizeof (FormData_pg_attribute ));
86
56
87
57
/*
88
58
* Initialize other fields of the tupdesc.
@@ -99,33 +69,22 @@ CreateTemplateTupleDesc(int natts, bool hasoid)
99
69
100
70
/*
101
71
* CreateTupleDesc
102
- * This function allocates a new TupleDesc pointing to a given
72
+ * This function allocates a new TupleDesc by copying a given
103
73
* Form_pg_attribute array.
104
74
*
105
- * Note: if the TupleDesc is ever freed, the Form_pg_attribute array
106
- * will not be freed thereby.
107
- *
108
75
* Tuple type ID information is initially set for an anonymous record type;
109
76
* caller can overwrite this if needed.
110
77
*/
111
78
TupleDesc
112
79
CreateTupleDesc (int natts , bool hasoid , Form_pg_attribute * attrs )
113
80
{
114
81
TupleDesc desc ;
82
+ int i ;
115
83
116
- /*
117
- * sanity checks
118
- */
119
- AssertArg (natts >= 0 );
84
+ desc = CreateTemplateTupleDesc (natts , hasoid );
120
85
121
- desc = (TupleDesc ) palloc (sizeof (struct tupleDesc ));
122
- desc -> attrs = attrs ;
123
- desc -> natts = natts ;
124
- desc -> constr = NULL ;
125
- desc -> tdtypeid = RECORDOID ;
126
- desc -> tdtypmod = -1 ;
127
- desc -> tdhasoid = hasoid ;
128
- desc -> tdrefcount = -1 ; /* assume not reference-counted */
86
+ for (i = 0 ; i < natts ; ++ i )
87
+ memcpy (TupleDescAttr (desc , i ), attrs [i ], ATTRIBUTE_FIXED_PART_SIZE );
129
88
130
89
return desc ;
131
90
}
@@ -147,10 +106,12 @@ CreateTupleDescCopy(TupleDesc tupdesc)
147
106
148
107
for (i = 0 ; i < desc -> natts ; i ++ )
149
108
{
150
- memcpy (desc -> attrs [i ], tupdesc -> attrs [i ], ATTRIBUTE_FIXED_PART_SIZE );
151
- desc -> attrs [i ]-> attnotnull = false;
152
- desc -> attrs [i ]-> atthasdef = false;
153
- desc -> attrs [i ]-> attidentity = '\0' ;
109
+ Form_pg_attribute att = TupleDescAttr (desc , i );
110
+
111
+ memcpy (att , & tupdesc -> attrs [i ], ATTRIBUTE_FIXED_PART_SIZE );
112
+ att -> attnotnull = false;
113
+ att -> atthasdef = false;
114
+ att -> attidentity = '\0' ;
154
115
}
155
116
156
117
desc -> tdtypeid = tupdesc -> tdtypeid ;
0 commit comments