From: Peter Eisentraut Date: Fri, 24 Aug 2001 20:03:45 +0000 (+0000) Subject: Start adding some more documentation about the number types. Make X-Git-Tag: REL9_0_0~19909 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=9cf701f324ef26428668ab5e2de91bb1858f408e;p=pg-rex%2Fsyncrep.git Start adding some more documentation about the number types. Make bigserial and alias for serial8 for consistency with bigint/int8. --- diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index 7f16688f32..2c253d7640 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -1,5 +1,5 @@ @@ -54,6 +54,12 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tg + bigserial + serial8 + autoincrementing eight-byte integer + + + bit fixed-length bit string @@ -204,12 +210,6 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tg - serial8 - - autoincrementing eight-byte integer - - - text variable-length character string @@ -346,8 +346,8 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tg - Type Name - Storage + Type name + Storage size Description Range @@ -370,46 +370,46 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tg bigint 8 bytes Very large range fixed-precision - about 18 decimal places + about 18 decimal digits decimal variable - User-specified precision + user-specified precision, exact no limit numeric variable - User-specified precision + user-specified precision, exact no limit real 4 bytes - Variable-precision - 6 decimal places + variable-precision, inexact + 6 decimal digits precision double precision 8 bytes - Variable-precision - 15 decimal places + variable-precision, inexact + 15 decimal digits precision - serial4 + serial 4 bytes - Identifier or cross-reference + autoincrementing integer 1 to 2147483647 - serial8 + bigserial 8 bytes - Identifier or cross-reference + autoincrementing integer 1 to 9223372036854775807 @@ -422,15 +422,187 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tg . The numeric types have a full set of corresponding arithmetic operators and functions. Refer to for more - information. + information. The following sections describe the types in detail. - - The bigint type may not function correctly on all platforms, - since it relies on compiler support for eight-byte integers. On a machine - without such support, bigint acts the same - as integer (but still takes up eight bytes of storage). - + + The Integer Types + + + The types smallint, integer, + bigint store whole numbers, that is, numbers without + fractional components, of various ranges. Attempts to store + values outside of the allowed range will result in an error. + + + + The type integer is the usual choice, as it offers + the best balance between range, storage size, and performance. + The smallint type is generally only used if disk + space is at a premium. The bigint type should only + be used if the integer range is not sufficient, + because the latter is definitely faster. + + + + The bigint type may not function correctly on all + platforms, since it relies on compiler support for eight-byte + integers. On a machine without such support, bigint + acts the same as integer (but still takes up eight + bytes of storage). However, we are not aware of any reasonable + platform where this is actually the case. + + + + + If you have a column of type smallint or + bigint with an index, you may encounter problems + getting the system to use that index. For instance, a clause of + the form + +... WHERE smallint_column = 42 + + will not use an index, because the system assigns type + integer to the 42, and PostgreSQL currently cannot + use an index when two different data types are involved. A + workaround is to single-quote the constant, thus: + +... WHERE smallint_column = '42' + + This will cause the system to delay the type resolution and will + assign the right type to the constant. + + + + + SQL only specifies the integer types integer (or + int) and smallint. The type + bigint, and the type names int2, + int4, and int8 are extensions, which + are shared with various other RDBMS products. + + + + + + Arbitrary Precision Numbers + + + The type numeric can store numbers of practically + unlimited size and precision, while being able to store all + numbers and carry out all calculations exactly. It is especially + recommended for storing monetary amounts and other quantities + where exactness is required. However, the numeric + type is very slow compared to the floating point types described + in the next section. + + + + In what follows we use these terms: The + scale of a numeric is the + count of decimal digits in the fractional part, to the right of + the decimal point. The precision of a + numeric is the total count of significant digits in + the whole number, that is, the number of digits to both sides of + the decimal point. So the number 23.5141 has a precision of 6 + and a scale of 4. Integers can be considered to have a scale of + zero. + + + + Both the precision and the scale of the numeric type can be + configured. To declare a column of type numeric use + the syntax + +NUMERIC(precision, scale) + + The precision must be positive, the scale zero or positive. + Alternatively, + +NUMERIC(precision) + + selects a scale of 0. Merely specifying + +NUMERIC + + uses a default precision and scale, which is currently (30,6). + (The SQL standard requires a default scale of 0. We find this a + bit useless. If you're concerned about portability, always + specify the precision and scale explicitly.) + + + + If the precision or scale of a value is greater than the declared + precision or scale of a column, the system will attempt to round + the value. If the value cannot be rounded so as to satisfy the + declared limits, an error is raised. + + + + The types decimal and numeric are + equivalent. Both types are part of the SQL standard. + + + + + + Floating Point Types + + + The data types real and double + precision are inexact, variable precision numeric types. + In practice, these types are usually implementations of IEEE 754 + binary floating point (single and double precision, + respectively), to the extent that the underlying processor, + operating system, and compiler support it. + + + + Inexact means that some values cannot be converted exactly to the + internal format and are stored as approximations, so that storing + and printing back out a value may show slight discrepancies. + Managing these errors and how they propagate through calculations + is the subject of an entire branch of mathematics and computer + science and will not be discussed further here, except for the + following points: + + + + If you require exact storage and calculations (such as for + monetary amounts), use the numeric type instead. + + + + + + If you want to do complicated calculations with these types + for anything important, especially if you rely on certain + behavior in boundary cases (infinity, underflow), you should + evaluate the implementation carefully. + + + + + + Comparing two floating point values for equality may or may + not work as expected. + + + + + + + Normally, the real type has a range of at least + -1E+37 to +1E+37 with a precision of at least 6. The + double precision type normally has a range of around + -1E+308 to +1E+308 with a precision of at least 15. Values that + are too large or too small will cause an error. Rounding may + take place if the precision of an input number is too high. + Numbers too close to zero that are not representable as distinct + from zero will cause an underflow error. + + + The Serial Types @@ -463,17 +635,17 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tg in tables. In the current implementation, specifying - + CREATE TABLE tablename (colname SERIAL); - + is equivalent to specifying: - + CREATE SEQUENCE tablename_colname_seq; CREATE TABLE tablename - (colname integer DEFAULT nextval('tablename_colname_seq') UNIQUE NOT NULL; - + (colname integer DEFAULT nextval('tablename_colname_seq') UNIQUE NOT NULL); + Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. UNIQUE and NOT NULL @@ -492,10 +664,10 @@ CREATE TABLE tablename The type names serial and serial4 are equivalent: both create integer columns. The type - name serial8 works just the same way, except that it - creates a bigint column. serial8 should - be used if you anticipate use of more than 2^31 identifiers over - the lifetime of the table. + names bigserial and serial8 works just + the same way, except that it creates a bigint + column. serial8 should be used if you anticipate + use of more than 2^31 identifiers over the lifetime of the table. @@ -503,11 +675,11 @@ CREATE TABLE tablename not automatically dropped when a table containing a serial type is dropped. So, the following commands executed in order will likely fail: - + CREATE TABLE tablename (colname SERIAL); DROP TABLE tablename; CREATE TABLE tablename (colname SERIAL); - + The sequence will remain in the database until explicitly dropped using DROP SEQUENCE. diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 6c5e5f7e0d..528cc91c96 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.196 2001/08/21 16:36:03 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.197 2001/08/24 20:03:45 petere Exp $ * *------------------------------------------------------------------------- */ @@ -732,7 +732,8 @@ transformCreateStmt(ParseState *pstate, CreateStmt *stmt) is_serial = true; column->typename->name = pstrdup("int4"); } - else if (strcmp(column->typename->name, "serial8") == 0) + else if (strcmp(column->typename->name, "bigserial") == 0 || + strcmp(column->typename->name, "serial8") == 0) { is_serial = true; column->typename->name = pstrdup("int8");