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

Commit 5f6f840

Browse files
committed
Reduce the alignment requirement of type "name" from int to char, and arrange
to suppress zero-padding of "name" entries in indexes. The alignment change is unlikely to save any space, but it is really needed anyway to make the world safe for our widespread practice of passing plain old C strings to functions that are declared as taking Name. In the previous coding, the C compiler was entitled to assume that a Name pointer was word-aligned; but we were failing to guarantee that. I think the reason we'd not seen failures is that usually the only thing that gets done with such a pointer is strcmp(), which is hard to optimize in a way that exploits word-alignment. Still, some enterprising compiler guy will probably think of a way eventually, or we might change our code in a way that exposes more-obvious optimization opportunities. The padding change is accomplished in one-liner fashion by declaring the "name" index opclasses to use storage type "cstring" in pg_opclass.h. Normally btree and hash don't allow a nondefault storage type, because they don't have any provisions for converting the input datum to another type. However, because name and cstring are effectively the same thing except for padding, no conversion is needed --- we only need index_form_tuple() to treat the datum as being cstring not name, and this is sufficient. This seems to make for about a one-third reduction in the typical sizes of system catalog indexes that involve "name" columns, of which we have many. These two changes are only weakly related, but the alignment change makes me feel safer that the padding change won't introduce problems, so I'm committing them together.
1 parent 3dc59be commit 5f6f840

File tree

8 files changed

+34
-31
lines changed

8 files changed

+34
-31
lines changed

src/backend/bootstrap/bootstrap.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.243 2008/05/12 00:00:46 alvherre Exp $
11+
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.244 2008/06/24 17:58:27 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -125,7 +125,7 @@ static const struct typinfo TypInfo[] = {
125125
F_INT4IN, F_INT4OUT},
126126
{"float4", FLOAT4OID, 0, 4, FLOAT4PASSBYVAL, 'i', 'p',
127127
F_FLOAT4IN, F_FLOAT4OUT},
128-
{"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'i', 'p',
128+
{"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'c', 'p',
129129
F_NAMEIN, F_NAMEOUT},
130130
{"regclass", REGCLASSOID, 0, 4, true, 'i', 'p',
131131
F_REGCLASSIN, F_REGCLASSOUT},

src/backend/utils/adt/name.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/adt/name.c,v 1.62 2008/05/27 00:13:09 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/name.c,v 1.63 2008/06/24 17:58:27 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -315,7 +315,7 @@ current_schemas(PG_FUNCTION_ARGS)
315315
NAMEOID,
316316
NAMEDATALEN, /* sizeof(Name) */
317317
false, /* Name is not by-val */
318-
'i'); /* alignment of Name */
318+
'c'); /* alignment of Name */
319319

320320
PG_RETURN_POINTER(array);
321321
}

src/include/c.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $PostgreSQL: pgsql/src/include/c.h,v 1.227 2008/06/17 16:09:06 momjian Exp $
15+
* $PostgreSQL: pgsql/src/include/c.h,v 1.228 2008/06/24 17:58:27 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -442,16 +442,12 @@ typedef struct
442442
} oidvector; /* VARIABLE LENGTH STRUCT */
443443

444444
/*
445-
* We want NameData to have length NAMEDATALEN and int alignment,
446-
* because that's how the data type 'name' is defined in pg_type.
447-
* Use a union to make sure the compiler agrees. Note that NAMEDATALEN
448-
* must be a multiple of sizeof(int), else sizeof(NameData) will probably
449-
* not come out equal to NAMEDATALEN.
445+
* Representation of a Name: effectively just a C string, but null-padded to
446+
* exactly NAMEDATALEN bytes. The use of a struct is historical.
450447
*/
451-
typedef union nameData
448+
typedef struct nameData
452449
{
453450
char data[NAMEDATALEN];
454-
int alignmentDummy;
455451
} NameData;
456452
typedef NameData *Name;
457453

src/include/catalog/catversion.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.463 2008/06/17 19:10:56 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.464 2008/06/24 17:58:27 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200806171
56+
#define CATALOG_VERSION_NO 200806241
5757

5858
#endif

src/include/catalog/pg_attribute.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_attribute.h,v 1.137 2008/04/21 00:26:46 tgl Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_attribute.h,v 1.138 2008/06/24 17:58:27 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -217,7 +217,7 @@ typedef FormData_pg_attribute *Form_pg_attribute;
217217
* ----------------
218218
*/
219219
#define Schema_pg_type \
220-
{ 1247, {"typname"}, 19, -1, NAMEDATALEN, 1, 0, -1, -1, false, 'p', 'i', true, false, false, true, 0 }, \
220+
{ 1247, {"typname"}, 19, -1, NAMEDATALEN, 1, 0, -1, -1, false, 'p', 'c', true, false, false, true, 0 }, \
221221
{ 1247, {"typnamespace"}, 26, -1, 4, 2, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
222222
{ 1247, {"typowner"}, 26, -1, 4, 3, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
223223
{ 1247, {"typlen"}, 21, -1, 2, 4, 0, -1, -1, true, 'p', 's', true, false, false, true, 0 }, \
@@ -244,7 +244,7 @@ typedef FormData_pg_attribute *Form_pg_attribute;
244244
{ 1247, {"typdefaultbin"}, 25, -1, -1, 25, 0, -1, -1, false, 'x', 'i', false, false, false, true, 0 }, \
245245
{ 1247, {"typdefault"}, 25, -1, -1, 26, 0, -1, -1, false, 'x', 'i', false, false, false, true, 0 }
246246

247-
DATA(insert ( 1247 typname 19 -1 NAMEDATALEN 1 0 -1 -1 f p i t f f t 0));
247+
DATA(insert ( 1247 typname 19 -1 NAMEDATALEN 1 0 -1 -1 f p c t f f t 0));
248248
DATA(insert ( 1247 typnamespace 26 -1 4 2 0 -1 -1 t p i t f f t 0));
249249
DATA(insert ( 1247 typowner 26 -1 4 3 0 -1 -1 t p i t f f t 0));
250250
DATA(insert ( 1247 typlen 21 -1 2 4 0 -1 -1 t p s t f f t 0));
@@ -283,7 +283,7 @@ DATA(insert ( 1247 tableoid 26 0 4 -7 0 -1 -1 t p i t f f t 0));
283283
* ----------------
284284
*/
285285
#define Schema_pg_proc \
286-
{ 1255, {"proname"}, 19, -1, NAMEDATALEN, 1, 0, -1, -1, false, 'p', 'i', true, false, false, true, 0 }, \
286+
{ 1255, {"proname"}, 19, -1, NAMEDATALEN, 1, 0, -1, -1, false, 'p', 'c', true, false, false, true, 0 }, \
287287
{ 1255, {"pronamespace"}, 26, -1, 4, 2, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
288288
{ 1255, {"proowner"}, 26, -1, 4, 3, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
289289
{ 1255, {"prolang"}, 26, -1, 4, 4, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
@@ -305,7 +305,7 @@ DATA(insert ( 1247 tableoid 26 0 4 -7 0 -1 -1 t p i t f f t 0));
305305
{ 1255, {"proconfig"}, 1009, -1, -1, 20, 1, -1, -1, false, 'x', 'i', false, false, false, true, 0 }, \
306306
{ 1255, {"proacl"}, 1034, -1, -1, 21, 1, -1, -1, false, 'x', 'i', false, false, false, true, 0 }
307307

308-
DATA(insert ( 1255 proname 19 -1 NAMEDATALEN 1 0 -1 -1 f p i t f f t 0));
308+
DATA(insert ( 1255 proname 19 -1 NAMEDATALEN 1 0 -1 -1 f p c t f f t 0));
309309
DATA(insert ( 1255 pronamespace 26 -1 4 2 0 -1 -1 t p i t f f t 0));
310310
DATA(insert ( 1255 proowner 26 -1 4 3 0 -1 -1 t p i t f f t 0));
311311
DATA(insert ( 1255 prolang 26 -1 4 4 0 -1 -1 t p i t f f t 0));
@@ -340,7 +340,7 @@ DATA(insert ( 1255 tableoid 26 0 4 -7 0 -1 -1 t p i t f f t 0));
340340
*/
341341
#define Schema_pg_attribute \
342342
{ 1249, {"attrelid"}, 26, -1, 4, 1, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
343-
{ 1249, {"attname"}, 19, -1, NAMEDATALEN, 2, 0, -1, -1, false, 'p', 'i', true, false, false, true, 0 }, \
343+
{ 1249, {"attname"}, 19, -1, NAMEDATALEN, 2, 0, -1, -1, false, 'p', 'c', true, false, false, true, 0 }, \
344344
{ 1249, {"atttypid"}, 26, -1, 4, 3, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
345345
{ 1249, {"attstattarget"}, 23, -1, 4, 4, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
346346
{ 1249, {"attlen"}, 21, -1, 2, 5, 0, -1, -1, true, 'p', 's', true, false, false, true, 0 }, \
@@ -358,7 +358,7 @@ DATA(insert ( 1255 tableoid 26 0 4 -7 0 -1 -1 t p i t f f t 0));
358358
{ 1249, {"attinhcount"}, 23, -1, 4, 17, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }
359359

360360
DATA(insert ( 1249 attrelid 26 -1 4 1 0 -1 -1 t p i t f f t 0));
361-
DATA(insert ( 1249 attname 19 -1 NAMEDATALEN 2 0 -1 -1 f p i t f f t 0));
361+
DATA(insert ( 1249 attname 19 -1 NAMEDATALEN 2 0 -1 -1 f p c t f f t 0));
362362
DATA(insert ( 1249 atttypid 26 -1 4 3 0 -1 -1 t p i t f f t 0));
363363
DATA(insert ( 1249 attstattarget 23 -1 4 4 0 -1 -1 t p i t f f t 0));
364364
DATA(insert ( 1249 attlen 21 -1 2 5 0 -1 -1 t p s t f f t 0));
@@ -387,7 +387,7 @@ DATA(insert ( 1249 tableoid 26 0 4 -7 0 -1 -1 t p i t f f t 0));
387387
* ----------------
388388
*/
389389
#define Schema_pg_class \
390-
{ 1259, {"relname"}, 19, -1, NAMEDATALEN, 1, 0, -1, -1, false, 'p', 'i', true, false, false, true, 0 }, \
390+
{ 1259, {"relname"}, 19, -1, NAMEDATALEN, 1, 0, -1, -1, false, 'p', 'c', true, false, false, true, 0 }, \
391391
{ 1259, {"relnamespace"}, 26, -1, 4, 2, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
392392
{ 1259, {"reltype"}, 26, -1, 4, 3, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
393393
{ 1259, {"relowner"}, 26, -1, 4, 4, 0, -1, -1, true, 'p', 'i', true, false, false, true, 0 }, \
@@ -415,7 +415,7 @@ DATA(insert ( 1249 tableoid 26 0 4 -7 0 -1 -1 t p i t f f t 0));
415415
{ 1259, {"relacl"}, 1034, -1, -1, 26, 1, -1, -1, false, 'x', 'i', false, false, false, true, 0 }, \
416416
{ 1259, {"reloptions"}, 1009, -1, -1, 27, 1, -1, -1, false, 'x', 'i', false, false, false, true, 0 }
417417

418-
DATA(insert ( 1259 relname 19 -1 NAMEDATALEN 1 0 -1 -1 f p i t f f t 0));
418+
DATA(insert ( 1259 relname 19 -1 NAMEDATALEN 1 0 -1 -1 f p c t f f t 0));
419419
DATA(insert ( 1259 relnamespace 26 -1 4 2 0 -1 -1 t p i t f f t 0));
420420
DATA(insert ( 1259 reltype 26 -1 4 3 0 -1 -1 t p i t f f t 0));
421421
DATA(insert ( 1259 relowner 26 -1 4 4 0 -1 -1 t p i t f f t 0));

src/include/catalog/pg_opclass.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
2929
* Portions Copyright (c) 1994, Regents of the University of California
3030
*
31-
* $PostgreSQL: pgsql/src/include/catalog/pg_opclass.h,v 1.81 2008/05/27 00:13:09 tgl Exp $
31+
* $PostgreSQL: pgsql/src/include/catalog/pg_opclass.h,v 1.82 2008/06/24 17:58:27 tgl Exp $
3232
*
3333
* NOTES
3434
* the genbki.sh script reads this file and generates .bki
@@ -121,8 +121,15 @@ DATA(insert ( 403 interval_ops PGNSP PGUID 1982 1186 t 0 ));
121121
DATA(insert ( 405 interval_ops PGNSP PGUID 1983 1186 t 0 ));
122122
DATA(insert ( 403 macaddr_ops PGNSP PGUID 1984 829 t 0 ));
123123
DATA(insert ( 405 macaddr_ops PGNSP PGUID 1985 829 t 0 ));
124-
DATA(insert ( 403 name_ops PGNSP PGUID 1986 19 t 0 ));
125-
DATA(insert ( 405 name_ops PGNSP PGUID 1987 19 t 0 ));
124+
/*
125+
* Here's an ugly little hack to save space in the system catalog indexes.
126+
* btree and hash don't ordinarily allow a storage type different from input
127+
* type; but cstring and name are the same thing except for trailing padding,
128+
* and we can safely omit that within an index entry. So we declare the
129+
* opclasses for name as using cstring storage type.
130+
*/
131+
DATA(insert ( 403 name_ops PGNSP PGUID 1986 19 t 2275 ));
132+
DATA(insert ( 405 name_ops PGNSP PGUID 1987 19 t 2275 ));
126133
DATA(insert ( 403 numeric_ops PGNSP PGUID 1988 1700 t 0 ));
127134
DATA(insert ( 405 numeric_ops PGNSP PGUID 1998 1700 t 0 ));
128135
DATA(insert OID = 1981 ( 403 oid_ops PGNSP PGUID 1989 26 t 0 ));

src/include/catalog/pg_type.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_type.h,v 1.195 2008/04/21 00:26:47 tgl Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_type.h,v 1.196 2008/06/24 17:58:27 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -267,7 +267,7 @@ DATA(insert OID = 18 ( char PGNSP PGUID 1 t b t \054 0 0 1002 charin charout
267267
DESCR("single character");
268268
#define CHAROID 18
269269

270-
DATA(insert OID = 19 ( name PGNSP PGUID NAMEDATALEN f b t \054 0 18 1003 namein nameout namerecv namesend - - - i p f 0 -1 0 _null_ _null_ ));
270+
DATA(insert OID = 19 ( name PGNSP PGUID NAMEDATALEN f b t \054 0 18 1003 namein nameout namerecv namesend - - - c p f 0 -1 0 _null_ _null_ ));
271271
DESCR("63-character type for storing system identifiers");
272272
#define NAMEOID 19
273273

src/include/pg_config_manual.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* for developers. If you edit any of these, be sure to do a *full*
77
* rebuild (and an initdb if noted).
88
*
9-
* $PostgreSQL: pgsql/src/include/pg_config_manual.h,v 1.33 2008/05/02 19:52:37 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/pg_config_manual.h,v 1.34 2008/06/24 17:58:27 tgl Exp $
1010
*------------------------------------------------------------------------
1111
*/
1212

1313
/*
1414
* Maximum length for identifiers (e.g. table names, column names,
15-
* function names). It must be a multiple of sizeof(int) (typically
16-
* 4).
15+
* function names). Names actually are limited to one less byte than this,
16+
* because the length must include a trailing zero byte.
1717
*
1818
* Changing this requires an initdb.
1919
*/

0 commit comments

Comments
 (0)