8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.108 2004/12/31 21:59:07 pgsql Exp $
11
+ * $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.109 2005/03/07 04:42:16 tgl Exp $
12
12
*
13
13
* NOTES
14
14
* some of the executor utility code such as "ExecTypeFromTL" should be
31
31
#include "utils/typcache.h"
32
32
33
33
34
- /* ----------------------------------------------------------------
35
- * CreateTemplateTupleDesc
36
- *
37
- * This function allocates and zeros a tuple descriptor structure.
34
+ /*
35
+ * CreateTemplateTupleDesc
36
+ * This function allocates an empty tuple descriptor structure.
38
37
*
39
38
* Tuple type ID information is initially set for an anonymous record type;
40
39
* caller can overwrite this if needed.
41
- * ----------------------------------------------------------------
42
40
*/
43
41
TupleDesc
44
42
CreateTemplateTupleDesc (int natts , bool hasoid )
45
43
{
46
44
TupleDesc desc ;
45
+ char * stg ;
46
+ int attroffset ;
47
47
48
48
/*
49
49
* sanity checks
50
50
*/
51
51
AssertArg (natts >= 0 );
52
52
53
53
/*
54
- * Allocate enough memory for the tuple descriptor, and zero the
55
- * attrs[] array since TupleDescInitEntry assumes that the array is
56
- * filled with NULL pointers.
54
+ * Allocate enough memory for the tuple descriptor, including the
55
+ * attribute rows, and set up the attribute row pointers.
56
+ *
57
+ * Note: we assume that sizeof(struct tupleDesc) is a multiple of
58
+ * the struct pointer alignment requirement, and hence we don't need
59
+ * to insert alignment padding between the struct and the array of
60
+ * attribute row pointers.
57
61
*/
58
- desc = (TupleDesc ) palloc (sizeof (struct tupleDesc ));
62
+ attroffset = sizeof (struct tupleDesc ) + natts * sizeof (Form_pg_attribute );
63
+ attroffset = MAXALIGN (attroffset );
64
+ stg = palloc (attroffset + natts * MAXALIGN (ATTRIBUTE_TUPLE_SIZE ));
65
+ desc = (TupleDesc ) stg ;
59
66
60
67
if (natts > 0 )
61
- desc -> attrs = (Form_pg_attribute * )
62
- palloc0 (natts * sizeof (Form_pg_attribute ));
68
+ {
69
+ Form_pg_attribute * attrs ;
70
+ int i ;
71
+
72
+ attrs = (Form_pg_attribute * ) (stg + sizeof (struct tupleDesc ));
73
+ desc -> attrs = attrs ;
74
+ stg += attroffset ;
75
+ for (i = 0 ; i < natts ; i ++ )
76
+ {
77
+ attrs [i ] = (Form_pg_attribute ) stg ;
78
+ stg += MAXALIGN (ATTRIBUTE_TUPLE_SIZE );
79
+ }
80
+ }
63
81
else
64
82
desc -> attrs = NULL ;
65
83
@@ -75,15 +93,16 @@ CreateTemplateTupleDesc(int natts, bool hasoid)
75
93
return desc ;
76
94
}
77
95
78
- /* ----------------------------------------------------------------
79
- * CreateTupleDesc
80
- *
96
+ /*
97
+ * CreateTupleDesc
81
98
* This function allocates a new TupleDesc pointing to a given
82
- * Form_pg_attribute array
99
+ * Form_pg_attribute array.
100
+ *
101
+ * Note: if the TupleDesc is ever freed, the Form_pg_attribute array
102
+ * will not be freed thereby.
83
103
*
84
104
* Tuple type ID information is initially set for an anonymous record type;
85
105
* caller can overwrite this if needed.
86
- * ----------------------------------------------------------------
87
106
*/
88
107
TupleDesc
89
108
CreateTupleDesc (int natts , bool hasoid , Form_pg_attribute * attrs )
@@ -106,53 +125,38 @@ CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
106
125
return desc ;
107
126
}
108
127
109
- /* ----------------------------------------------------------------
110
- * CreateTupleDescCopy
111
- *
128
+ /*
129
+ * CreateTupleDescCopy
112
130
* This function creates a new TupleDesc by copying from an existing
113
- * TupleDesc
131
+ * TupleDesc.
114
132
*
115
- * !!! Constraints and defaults are not copied !!!
116
- * ----------------------------------------------------------------
133
+ * !!! Constraints and defaults are not copied !!!
117
134
*/
118
135
TupleDesc
119
136
CreateTupleDescCopy (TupleDesc tupdesc )
120
137
{
121
138
TupleDesc desc ;
122
139
int i ;
123
140
124
- desc = ( TupleDesc ) palloc ( sizeof ( struct tupleDesc ) );
125
- desc -> natts = tupdesc -> natts ;
126
- if ( desc -> natts > 0 )
141
+ desc = CreateTemplateTupleDesc ( tupdesc -> natts , tupdesc -> tdhasoid );
142
+
143
+ for ( i = 0 ; i < desc -> natts ; i ++ )
127
144
{
128
- desc -> attrs = (Form_pg_attribute * )
129
- palloc (desc -> natts * sizeof (Form_pg_attribute ));
130
- for (i = 0 ; i < desc -> natts ; i ++ )
131
- {
132
- desc -> attrs [i ] = (Form_pg_attribute ) palloc (ATTRIBUTE_TUPLE_SIZE );
133
- memcpy (desc -> attrs [i ], tupdesc -> attrs [i ], ATTRIBUTE_TUPLE_SIZE );
134
- desc -> attrs [i ]-> attnotnull = false;
135
- desc -> attrs [i ]-> atthasdef = false;
136
- }
145
+ memcpy (desc -> attrs [i ], tupdesc -> attrs [i ], ATTRIBUTE_TUPLE_SIZE );
146
+ desc -> attrs [i ]-> attnotnull = false;
147
+ desc -> attrs [i ]-> atthasdef = false;
137
148
}
138
- else
139
- desc -> attrs = NULL ;
140
-
141
- desc -> constr = NULL ;
142
149
143
150
desc -> tdtypeid = tupdesc -> tdtypeid ;
144
151
desc -> tdtypmod = tupdesc -> tdtypmod ;
145
- desc -> tdhasoid = tupdesc -> tdhasoid ;
146
152
147
153
return desc ;
148
154
}
149
155
150
- /* ----------------------------------------------------------------
151
- * CreateTupleDescCopyConstr
152
- *
156
+ /*
157
+ * CreateTupleDescCopyConstr
153
158
* This function creates a new TupleDesc by copying from an existing
154
- * TupleDesc (including its constraints and defaults)
155
- * ----------------------------------------------------------------
159
+ * TupleDesc (including its constraints and defaults).
156
160
*/
157
161
TupleDesc
158
162
CreateTupleDescCopyConstr (TupleDesc tupdesc )
@@ -161,20 +165,12 @@ CreateTupleDescCopyConstr(TupleDesc tupdesc)
161
165
TupleConstr * constr = tupdesc -> constr ;
162
166
int i ;
163
167
164
- desc = ( TupleDesc ) palloc ( sizeof ( struct tupleDesc ) );
165
- desc -> natts = tupdesc -> natts ;
166
- if ( desc -> natts > 0 )
168
+ desc = CreateTemplateTupleDesc ( tupdesc -> natts , tupdesc -> tdhasoid );
169
+
170
+ for ( i = 0 ; i < desc -> natts ; i ++ )
167
171
{
168
- desc -> attrs = (Form_pg_attribute * )
169
- palloc (desc -> natts * sizeof (Form_pg_attribute ));
170
- for (i = 0 ; i < desc -> natts ; i ++ )
171
- {
172
- desc -> attrs [i ] = (Form_pg_attribute ) palloc (ATTRIBUTE_TUPLE_SIZE );
173
- memcpy (desc -> attrs [i ], tupdesc -> attrs [i ], ATTRIBUTE_TUPLE_SIZE );
174
- }
172
+ memcpy (desc -> attrs [i ], tupdesc -> attrs [i ], ATTRIBUTE_TUPLE_SIZE );
175
173
}
176
- else
177
- desc -> attrs = NULL ;
178
174
179
175
if (constr )
180
176
{
@@ -208,12 +204,9 @@ CreateTupleDescCopyConstr(TupleDesc tupdesc)
208
204
209
205
desc -> constr = cpy ;
210
206
}
211
- else
212
- desc -> constr = NULL ;
213
207
214
208
desc -> tdtypeid = tupdesc -> tdtypeid ;
215
209
desc -> tdtypmod = tupdesc -> tdtypmod ;
216
- desc -> tdhasoid = tupdesc -> tdhasoid ;
217
210
218
211
return desc ;
219
212
}
@@ -226,10 +219,6 @@ FreeTupleDesc(TupleDesc tupdesc)
226
219
{
227
220
int i ;
228
221
229
- for (i = 0 ; i < tupdesc -> natts ; i ++ )
230
- pfree (tupdesc -> attrs [i ]);
231
- if (tupdesc -> attrs )
232
- pfree (tupdesc -> attrs );
233
222
if (tupdesc -> constr )
234
223
{
235
224
if (tupdesc -> constr -> num_defval > 0 )
@@ -379,12 +368,10 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
379
368
return true;
380
369
}
381
370
382
- /* ----------------------------------------------------------------
383
- * TupleDescInitEntry
384
- *
371
+ /*
372
+ * TupleDescInitEntry
385
373
* This function initializes a single attribute structure in
386
- * a preallocated tuple descriptor.
387
- * ----------------------------------------------------------------
374
+ * a previously allocated tuple descriptor.
388
375
*/
389
376
void
390
377
TupleDescInitEntry (TupleDesc desc ,
@@ -404,18 +391,12 @@ TupleDescInitEntry(TupleDesc desc,
404
391
AssertArg (PointerIsValid (desc ));
405
392
AssertArg (attributeNumber >= 1 );
406
393
AssertArg (attributeNumber <= desc -> natts );
407
- AssertArg (!PointerIsValid (desc -> attrs [attributeNumber - 1 ]));
408
-
409
- /*
410
- * allocate storage for this attribute
411
- */
412
-
413
- att = (Form_pg_attribute ) palloc (ATTRIBUTE_TUPLE_SIZE );
414
- desc -> attrs [attributeNumber - 1 ] = att ;
415
394
416
395
/*
417
396
* initialize the attribute fields
418
397
*/
398
+ att = desc -> attrs [attributeNumber - 1 ];
399
+
419
400
att -> attrelid = 0 ; /* dummy value */
420
401
421
402
/*
0 commit comments