Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c629324

Browse files
committed
Partially flatten struct tupleDesc so that it can be used in DSM.
TupleDesc's attributes were already stored in contiguous memory after the struct. Go one step further and get rid of the array of pointers to attributes so that they can be stored in shared memory mapped at different addresses in each backend. This won't work for TupleDescs with contraints and defaults, since those point to other objects, but for many purposes only attributes are needed. Author: Thomas Munro Reviewed-By: Andres Freund Discussion: https://postgr.es/m/CAEepm=0ZtQ-SpsgCyzzYpsXS6e=kZWqk3g5Ygn3MDV7A8dabUA@mail.gmail.com
1 parent 2cd7084 commit c629324

File tree

2 files changed

+18
-57
lines changed

2 files changed

+18
-57
lines changed

src/backend/access/common/tupdesc.c

+14-53
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ TupleDesc
4141
CreateTemplateTupleDesc(int natts, bool hasoid)
4242
{
4343
TupleDesc desc;
44-
char *stg;
45-
int attroffset;
4644

4745
/*
4846
* sanity checks
@@ -51,38 +49,10 @@ CreateTemplateTupleDesc(int natts, bool hasoid)
5149

5250
/*
5351
* 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.
6453
*/
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));
8656

8757
/*
8858
* Initialize other fields of the tupdesc.
@@ -99,33 +69,22 @@ CreateTemplateTupleDesc(int natts, bool hasoid)
9969

10070
/*
10171
* CreateTupleDesc
102-
* This function allocates a new TupleDesc pointing to a given
72+
* This function allocates a new TupleDesc by copying a given
10373
* Form_pg_attribute array.
10474
*
105-
* Note: if the TupleDesc is ever freed, the Form_pg_attribute array
106-
* will not be freed thereby.
107-
*
10875
* Tuple type ID information is initially set for an anonymous record type;
10976
* caller can overwrite this if needed.
11077
*/
11178
TupleDesc
11279
CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
11380
{
11481
TupleDesc desc;
82+
int i;
11583

116-
/*
117-
* sanity checks
118-
*/
119-
AssertArg(natts >= 0);
84+
desc = CreateTemplateTupleDesc(natts, hasoid);
12085

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);
12988

13089
return desc;
13190
}
@@ -147,10 +106,12 @@ CreateTupleDescCopy(TupleDesc tupdesc)
147106

148107
for (i = 0; i < desc->natts; i++)
149108
{
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';
154115
}
155116

156117
desc->tdtypeid = tupdesc->tdtypeid;

src/include/access/tupdesc.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ typedef struct tupleConstr
7171
typedef struct tupleDesc
7272
{
7373
int natts; /* number of attributes in the tuple */
74-
Form_pg_attribute *attrs;
75-
/* attrs[N] is a pointer to the description of Attribute Number N+1 */
76-
TupleConstr *constr; /* constraints, or NULL if none */
7774
Oid tdtypeid; /* composite type ID for tuple type */
7875
int32 tdtypmod; /* typmod for tuple type */
7976
bool tdhasoid; /* tuple has oid attribute in its header */
8077
int tdrefcount; /* reference count, or -1 if not counting */
78+
TupleConstr *constr; /* constraints, or NULL if none */
79+
/* attrs[N] is the description of Attribute Number N+1 */
80+
FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
8181
} *TupleDesc;
8282

8383
/* Accessor for the i'th attribute of tupdesc. */
84-
#define TupleDescAttr(tupdesc, i) ((tupdesc)->attrs[(i)])
84+
#define TupleDescAttr(tupdesc, i) (&(tupdesc)->attrs[(i)])
8585

8686
extern TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid);
8787

0 commit comments

Comments
 (0)