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

Commit 61eaefe

Browse files
committed
This patch for Versions 1 and 2 corrects the following bug:
In a catalog class that has a "name" type attribute, UPDATEing of an instance of that class may destroy all of the attributes of that instance that are stored as or after the "name" attribute. This is caused by the alignment value of the "name" type being set to "double" in Class pg_type, but "integer" in Class pg_attribute. Postgres constructs a tuple using double alignment, but interprets it using integer alignment. The fix is to change the alignment to integer in pg_type. Note that this corrects the problem for new Postgres systems. Existing databases already contain the error and it can't easily be repaired because this very bug prevents updating the class that contains it. -- Bryan Henderson Phone 408-227-6803 San Jose, California
1 parent 208a30f commit 61eaefe

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

src/backend/catalog/pg_attribute.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_attribute.h,v 1.2 1996/08/21 04:25:47 scrappy Exp $
10+
* $Id: pg_attribute.h,v 1.3 1996/08/24 20:56:13 scrappy Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -44,13 +44,19 @@ CATALOG(pg_attribute) BOOTSTRAP {
4444
Oid attrelid;
4545
NameData attname;
4646
Oid atttypid;
47+
/* atttypid is the OID of the instance in Catalog Class pg_type that
48+
defines the data type of this attribute (e.g. int4). Information in
49+
that instance is redundant with the attlen, attbyval, and attalign
50+
attributes of this instance, so they had better match or Postgres
51+
will fail.
52+
*/
4753
Oid attdefrel;
4854
int4 attnvals;
4955
Oid atttyparg; /* type arg for arrays/spquel/procs */
5056
int2 attlen;
51-
/* attlen is the number of bytes we use to represent the value
52-
of this attribute, e.g. 4 for an int4. But for a variable length
53-
attribute, attlen is -1.
57+
/* attlen is a copy of the typlen field from pg_type for this
58+
attribute. See atttypid above. See struct TypeTupleFormData for
59+
definition.
5460
*/
5561
int2 attnum;
5662
/* attnum is the "attribute number" for the attribute: A
@@ -68,6 +74,10 @@ CATALOG(pg_attribute) BOOTSTRAP {
6874
*/
6975
int2 attbound;
7076
bool attbyval;
77+
/* attbyval is a copy of the typbyval field from pg_type for this
78+
attribute. See atttypid above. See struct TypeTupleFormData for
79+
definition.
80+
*/
7181
bool attcanindex;
7282
Oid attproc; /* spquel? */
7383
int4 attnelems;
@@ -80,7 +90,11 @@ CATALOG(pg_attribute) BOOTSTRAP {
8090
walking process.
8191
*/
8292
bool attisset;
83-
char attalign; /* alignment (c=char, s=short, i=int, d=double) */
93+
char attalign;
94+
/* attalign is a copy of the typalign field from pg_type for this
95+
attribute. See atttypid above. See struct TypeTupleFormData for
96+
definition.
97+
*/
8498
} FormData_pg_attribute;
8599

86100
/*

src/backend/catalog/pg_type.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_type.h,v 1.3 1996/07/19 05:21:28 scrappy Exp $
10+
* $Id: pg_type.h,v 1.4 1996/08/24 20:56:16 scrappy Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -30,14 +30,34 @@
3030
/* ----------------
3131
* pg_type definition. cpp turns this into
3232
* typedef struct FormData_pg_type
33+
*
34+
* Some of the values in a pg_type instance are copied into
35+
* pg_attribute intances. Some parts of Postgres use the pg_type copy,
36+
* while others use the pg_attribute copy, so they must match.
37+
* See struct FormData_pg_attribute for details.
3338
* ----------------
3439
*/
3540
CATALOG(pg_type) BOOTSTRAP {
3641
NameData typname;
3742
Oid typowner;
3843
int2 typlen;
44+
/* typlen is the number of bytes we use to represent a value of
45+
this type, e.g. 4 for an int4. But for a variable length
46+
attribute, typlen is -1.
47+
*/
3948
int2 typprtlen;
4049
bool typbyval;
50+
/* typbyval determines whether internal Postgres routines pass a value
51+
of this type by value or by reference. Postgres uses a 4 byte
52+
area for passing class data, so if the value is not 1, 2,
53+
or 4 bytes long, Postgres does not have the option of passing by
54+
value and ignores typbyval.
55+
56+
(I don't understand why this attribute exists. The above description
57+
may be an oversimplification. Also, there appear to be bugs in which
58+
Postgres doesn't ignore typbyval when it should, but I'm
59+
afraid to change them until I see proof of damage. -BRYANH 96.08).
60+
*/
4161
char typtype;
4262
bool typisdefined;
4363
char typdelim;
@@ -47,7 +67,23 @@ CATALOG(pg_type) BOOTSTRAP {
4767
regproc typoutput;
4868
regproc typreceive;
4969
regproc typsend;
50-
char typalign; /* alignment (c=char, s=short, i=int, d=double) */
70+
char typalign;
71+
/* typalign is the alignment required when storing a value of this
72+
type. It applies to storage on disk as well as most representations
73+
of the value inside Postgres. When multiple values are stored
74+
consecutively, such as in the representation of a complete tuple
75+
on disk, padding is inserted before a datum of this type so that it
76+
begins on the specified boundary. The alignment reference is the
77+
beginning of the first datum in the sequence.
78+
79+
'c' = 1 byte alignment.
80+
's' = 2 byte alignment.
81+
'i' = 4 byte alignment.
82+
'd' = 8 byte alignment.
83+
84+
(This might actually be flexible depending on machine architecture,
85+
but I doubt it - BRYANH 96.08).
86+
*/
5187
text typdefault; /* VARIABLE LENGTH FIELD */
5288
} TypeTupleFormData;
5389

@@ -87,6 +123,11 @@ typedef TypeTupleFormData *TypeTupleForm;
87123

88124
/* keep the following ordered by OID so that later changes can be made easier*/
89125

126+
/* Make sure the typlen, typbyval, and typalign values here match the initial
127+
values for attlen, attbyval, and attalign in both places in pg_attribute.h
128+
for every instance.
129+
*/
130+
90131
/* OIDS 1 - 99 */
91132
DATA(insert OID = 16 ( bool PGUID 1 1 t b t \054 0 0 boolin boolout boolin boolout c _null_ ));
92133

@@ -95,7 +136,7 @@ DATA(insert OID = 16 ( bool PGUID 1 1 t b t \054 0 0 boolin boolout
95136
DATA(insert OID = 17 ( bytea PGUID -1 -1 f b t \054 0 18 byteain byteaout byteain byteaout i _null_ ));
96137
DATA(insert OID = 18 ( char PGUID 1 1 t b t \054 0 0 charin charout charin charout c _null_ ));
97138

98-
DATA(insert OID = 19 ( name PGUID NAMEDATALEN NAMEDATALEN f b t \054 0 18 namein nameout namein nameout d _null_ ));
139+
DATA(insert OID = 19 ( name PGUID NAMEDATALEN NAMEDATALEN f b t \054 0 18 namein nameout namein nameout i _null_ ));
99140
DATA(insert OID = 20 ( char16 PGUID 16 16 f b t \054 0 18 char16in char16out char16in char16out i _null_ ));
100141
/*DATA(insert OID = 20 ( dt PGUID 4 10 t b t \054 0 0 dtin dtout dtin dtout i _null_ )); */
101142
DATA(insert OID = 21 ( int2 PGUID 2 5 t b t \054 0 0 int2in int2out int2in int2out s _null_ ));

0 commit comments

Comments
 (0)