Correct/improve the datetime_precision field in the information schema.
authorPeter Eisentraut <peter_e@gmx.net>
Wed, 10 Jun 2009 07:03:34 +0000 (07:03 +0000)
committerPeter Eisentraut <peter_e@gmx.net>
Wed, 10 Jun 2009 07:03:34 +0000 (07:03 +0000)
In particular, always show 0 for the date type instead of null, and show
6 (the default) for time, timestamp, and interval without a declared
precision.  This is now in fuller conformance with the SQL standard.

Also clarify the documentation about this.

discovered and analyzed by Konstantin Izmailov and Tom Lane

doc/src/sgml/information_schema.sgml
src/backend/catalog/information_schema.sql

index 19cbb5d65f71e98a1e5f16d14d2a89c5b7f359fa..8e145d7ef1163d8cfd1fd3689999378e71565523 100644 (file)
       <entry><literal>datetime_precision</literal></entry>
       <entry><type>cardinal_number</type></entry>
       <entry>
-       If <literal>data_type</literal> identifies a date, time, or
-       interval type, the declared precision; null for all other data
-       types or if no precision was declared.
+       If <literal>data_type</literal> identifies a date, time,
+       timestamp, or interval type, this column contains the (declared
+       or implicit) fractional seconds precision of the type for this
+       attribute, that is, the number of decimal digits maintained
+       following the decimal point in the seconds value.  For all
+       other data types, this column is null.
       </entry>
      </row>
 
       <entry><literal>datetime_precision</literal></entry>
       <entry><type>cardinal_number</type></entry>
       <entry>
-       If <literal>data_type</literal> identifies a date, time, or
-       interval type, the declared precision; null for all other data
-       types or if no precision was declared.
+       If <literal>data_type</literal> identifies a date, time,
+       timestamp, or interval type, this column contains the (declared
+       or implicit) fractional seconds precision of the type for this
+       column, that is, the number of decimal digits maintained
+       following the decimal point in the seconds value.  For all
+       other data types, this column is null.
       </entry>
      </row>
 
       <entry><type>cardinal_number</type></entry>
       <entry>
        If the domain has a numeric type, this column contains the
-       (declared or implicit) precision of the type for this column.
+       (declared or implicit) precision of the type for this domain.
        The precision indicates the number of significant digits.  It
        can be expressed in decimal (base 10) or binary (base 2) terms,
        as specified in the column
       <entry><type>cardinal_number</type></entry>
       <entry>
        If the domain has an exact numeric type, this column contains
-       the (declared or implicit) scale of the type for this column.
+       the (declared or implicit) scale of the type for this domain.
        The scale indicates the number of significant digits to the
        right of the decimal point.  It can be expressed in decimal
        (base 10) or binary (base 2) terms, as specified in the column
       <entry><literal>datetime_precision</literal></entry>
       <entry><type>cardinal_number</type></entry>
       <entry>
-       If the domain has a date, time, or interval type, the declared
-       precision; null for all other data types or if no precision was
-       declared.
+       If <literal>data_type</literal> identifies a date, time,
+       timestamp, or interval type, this column contains the (declared
+       or implicit) fractional seconds precision of the type for this
+       domain, that is, the number of decimal digits maintained
+       following the decimal point in the seconds value.  For all
+       other data types, this column is null.
       </entry>
      </row>
 
index 9c5672f3e13692cf3770440b112fede562420952..fe753221c6e6f097120a8b74804179592b4ff75a 100644 (file)
@@ -160,12 +160,12 @@ CREATE FUNCTION _pg_datetime_precision(typid oid, typmod int4) RETURNS integer
     RETURNS NULL ON NULL INPUT
     AS
 $$SELECT
-  CASE WHEN $2 = -1 /* default typmod */
-       THEN null
+  CASE WHEN $1 IN (1082) /* date */
+           THEN 0
        WHEN $1 IN (1083, 1114, 1184, 1266) /* time, timestamp, same + tz */
-       THEN $2
+           THEN CASE WHEN $2 < 0 THEN 6 ELSE $2 END
        WHEN $1 IN (1186) /* interval */
-       THEN $2 & 65535
+           THEN CASE WHEN $2 < 0 THEN 6 ELSE $2 & 65535 END
        ELSE null
   END$$;