Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9713c06

Browse files
committed
Change the declaration of struct varlena so that the length word is
represented as "char ...[4]" not "int32". Since the length word is neversupposed to be accessed via this struct member anyway, this won't breakany existing code that is following the rules. The advantage is that Ccompilers will no longer assume that a pointer to struct varlena isword-aligned, which prevents incorrect optimizations in TOAST-pointeraccess and perhaps other places. gcc doesn't seem to do this (at leastnot at -O2), but the problem is demonstrable on some other compilers.I changed struct inet as well, but didn't bother to touch a lot of otherstruct definitions in which it wouldn't make any difference because therewere other fields forcing int alignment anyway. Hopefully none of thosestruct definitions are used for accessing unaligned Datums.
1 parent870993e commit9713c06

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

‎doc/src/sgml/xtypes.sgml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/xtypes.sgml,v 1.29 2007/05/15 17:39:54 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/xtypes.sgml,v 1.30 2008/02/23 19:11:45 tgl Exp $ -->
22

33
<sect1 id="xtypes">
44
<title>User-Defined Types</title>
@@ -219,7 +219,7 @@ CREATE TYPE complex (
219219
<productname>PostgreSQL</productname> automatically provides support
220220
for arrays of that
221221
type.<indexterm><primary>array</primary><secondary>of user-defined
222-
type</secondary></indexterm>For historical reasons, thearray type
222+
type</secondary></indexterm> Thearray type typically
223223
has the same name as the base type with the underscore character
224224
(<literal>_</>) prepended.
225225
</para>
@@ -240,15 +240,16 @@ CREATE TYPE complex (
240240
If the values of your data type vary in size (in internal form), you should
241241
make the data type <acronym>TOAST</>-able (see <xref
242242
linkend="storage-toast">). You should do this even if the data are always
243-
too small to be compressed or stored externally because
244-
<productname>Postgres</> can save space on small datausing
245-
<acronym>TOAST</> as well.
243+
too small to be compressed or stored externally, because
244+
<acronym>TOAST</> can save space on small datatoo, by reducing header
245+
overhead.
246246
</para>
247247

248248
<para>
249249
To do this, the internal representation must follow the standard layout for
250-
variable-length data: the first four bytes must be an <type>int32</type>
251-
which is never accessed directly (customarily named <literal>vl_len_</>). You
250+
variable-length data: the first four bytes must be a <type>char[4]</type>
251+
field which is never accessed directly (customarily named
252+
<structfield>vl_len_</>). You
252253
must use <function>SET_VARSIZE()</function> to store the size of the datum
253254
in this field and <function>VARSIZE()</function> to retrieve it. The C
254255
functions operating on the data type must always be careful to unpack any
@@ -265,12 +266,25 @@ CREATE TYPE complex (
265266
to avoid some of the overhead of <function>PG_DETOAST_DATUM</>. You can use
266267
<function>PG_DETOAST_DATUM_PACKED</> instead (customarily hidden by
267268
defining a <function>GETARG_DATATYPE_PP</> macro) and using the macros
268-
<function>VARSIZE_ANY_EXHDR</> and <function>VARDATA_ANY</> macros.
269+
<function>VARSIZE_ANY_EXHDR</> and <function>VARDATA_ANY</> to access
270+
a potentially-packed datum.
269271
Again, the data returned by these macros is not aligned even if the data
270272
type definition specifies an alignment. If the alignment is important you
271273
must go through the regular <function>PG_DETOAST_DATUM</> interface.
272274
</para>
273275

276+
<note>
277+
<para>
278+
Older code frequently declares <structfield>vl_len_</> as an
279+
<type>int32</> field instead of <type>char[4]</>. This is OK as long as
280+
the struct definition has other fields that have at least <type>int32</>
281+
alignment. But it is dangerous to use such a struct definition when
282+
working with a potentially unaligned datum; the compiler may take it as
283+
license to assume the datum actually is aligned, leading to core dumps on
284+
architectures that are strict about alignment.
285+
</para>
286+
</note>
287+
274288
<para>
275289
For further details see the description of the
276290
<xref linkend="sql-createtype" endterm="sql-createtype-title"> command.

‎src/backend/access/heap/tuptoaster.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.81 2008/01/01 19:45:46 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.82 2008/02/23 19:11:45 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -65,7 +65,8 @@
6565
#defineVARATT_EXTERNAL_GET_POINTER(toast_pointer,attr) \
6666
do { \
6767
varattrib_1b_e *attre = (varattrib_1b_e *) (attr); \
68-
Assert(VARSIZE_ANY_EXHDR(attre) == sizeof(toast_pointer)); \
68+
Assert(VARATT_IS_EXTERNAL(attre)); \
69+
Assert(VARSIZE_EXTERNAL(attre) == sizeof(toast_pointer) + VARHDRSZ_EXTERNAL); \
6970
memcpy(&(toast_pointer), VARDATA_EXTERNAL(attre), sizeof(toast_pointer)); \
7071
} while (0)
7172

‎src/include/c.h

Lines changed: 2 additions & 2 deletions
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.222 2008/01/01 19:45:56 momjian Exp $
15+
* $PostgreSQL: pgsql/src/include/c.h,v 1.223 2008/02/23 19:11:45 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -410,7 +410,7 @@ typedef struct
410410
*/
411411
structvarlena
412412
{
413-
int32vl_len_;/* Do not touch this field directly! */
413+
charvl_len_[4];/* Do not touch this field directly! */
414414
charvl_dat[1];
415415
};
416416

‎src/include/utils/inet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/inet.h,v 1.28 2008/01/01 19:45:59 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/inet.h,v 1.29 2008/02/23 19:11:45 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -49,7 +49,7 @@ typedef struct
4949
*/
5050
typedefstruct
5151
{
52-
int32vl_len_;/* Do not touch this field directly! */
52+
charvl_len_[4];/* Do not touch this field directly! */
5353
inet_structinet_data;
5454
}inet;
5555

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp