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

Commit 5725b9d

Browse files
committed
Support type modifiers for user-defined types, and pull most knowledge
about typmod representation for standard types out into type-specific typmod I/O functions. Teodor Sigaev, with some editorialization by Tom Lane.
1 parent 24b1f14 commit 5725b9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1628
-685
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.139 2006/12/23 00:43:08 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.140 2006/12/30 21:21:52 tgl Exp $ -->
22
<!--
33
Documentation of the system catalogs, directed toward PostgreSQL developers
44
-->
@@ -4393,6 +4393,20 @@
43934393
<entry>Output conversion function (binary format), or 0 if none</entry>
43944394
</row>
43954395

4396+
<row>
4397+
<entry><structfield>typmodin</structfield></entry>
4398+
<entry><type>regproc</type></entry>
4399+
<entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
4400+
<entry>Type modifier input function, or 0 if type does not support modifiers</entry>
4401+
</row>
4402+
4403+
<row>
4404+
<entry><structfield>typmodout</structfield></entry>
4405+
<entry><type>regproc</type></entry>
4406+
<entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
4407+
<entry>Type modifier output function, or 0 to use the standard format</entry>
4408+
</row>
4409+
43964410
<row>
43974411
<entry><structfield>typanalyze</structfield></entry>
43984412
<entry><type>regproc</type></entry>

doc/src/sgml/keywords.sgml

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/keywords.sgml,v 2.18 2006/10/08 20:51:52 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/keywords.sgml,v 2.19 2006/12/30 21:21:52 tgl Exp $ -->
22

33
<appendix id="sql-keywords-appendix">
44
<title><acronym>SQL</acronym> Key Words</title>
@@ -45,16 +45,17 @@
4545
In <xref linkend="keywords-table"> in the column for
4646
<productname>PostgreSQL</productname> we classify as
4747
<quote>non-reserved</quote> those key words that are explicitly
48-
known to the parser but are allowed in most or all contexts where an
49-
identifier is expected. Some key words that are otherwise
48+
known to the parser but are allowed as column or table names.
49+
Some key words that are otherwise
5050
non-reserved cannot be used as function or data type names and are
5151
marked accordingly. (Most of these words represent built-in
5252
functions or data types with special syntax. The function or type
5353
is still available but it cannot be redefined by the user.) Labeled
54-
<quote>reserved</quote> are those tokens that are only allowed as
55-
<quote>AS</quote> column label names (and perhaps in very few other
56-
contexts). Some reserved key words are allowable as names for
57-
functions; this is also shown in the table.
54+
<quote>reserved</quote> are those tokens that are not allowed as
55+
column or table names. Some reserved key words are
56+
allowable as names for functions or data types; this is also shown in the
57+
table. If not so marked, a reserved key word is only allowed as an
58+
<quote>AS</quote> column label name.
5859
</para>
5960

6061
<para>
@@ -326,7 +327,7 @@
326327
</row>
327328
<row>
328329
<entry><token>AUTHORIZATION</token></entry>
329-
<entry>reserved (can be function)</entry>
330+
<entry>reserved (can be function or type)</entry>
330331
<entry>reserved</entry>
331332
<entry>reserved</entry>
332333
<entry>reserved</entry>
@@ -368,7 +369,7 @@
368369
</row>
369370
<row>
370371
<entry><token>BETWEEN</token></entry>
371-
<entry>reserved (can be function)</entry>
372+
<entry>reserved (can be function or type)</entry>
372373
<entry>reserved</entry>
373374
<entry>non-reserved</entry>
374375
<entry>reserved</entry>
@@ -382,7 +383,7 @@
382383
</row>
383384
<row>
384385
<entry><token>BINARY</token></entry>
385-
<entry>reserved (can be function)</entry>
386+
<entry>reserved (can be function or type)</entry>
386387
<entry>reserved</entry>
387388
<entry>reserved</entry>
388389
<entry></entry>
@@ -956,7 +957,7 @@
956957
</row>
957958
<row>
958959
<entry><token>CROSS</token></entry>
959-
<entry>reserved (can be function)</entry>
960+
<entry>reserved (can be function or type)</entry>
960961
<entry>reserved</entry>
961962
<entry>reserved</entry>
962963
<entry>reserved</entry>
@@ -1642,7 +1643,7 @@
16421643
</row>
16431644
<row>
16441645
<entry><token>FREEZE</token></entry>
1645-
<entry>reserved (can be function)</entry>
1646+
<entry>reserved (can be function or type)</entry>
16461647
<entry></entry>
16471648
<entry></entry>
16481649
<entry></entry>
@@ -1656,7 +1657,7 @@
16561657
</row>
16571658
<row>
16581659
<entry><token>FULL</token></entry>
1659-
<entry>reserved (can be function)</entry>
1660+
<entry>reserved (can be function or type)</entry>
16601661
<entry>reserved</entry>
16611662
<entry>reserved</entry>
16621663
<entry>reserved</entry>
@@ -1831,7 +1832,7 @@
18311832
</row>
18321833
<row>
18331834
<entry><token>ILIKE</token></entry>
1834-
<entry>reserved (can be function)</entry>
1835+
<entry>reserved (can be function or type)</entry>
18351836
<entry></entry>
18361837
<entry></entry>
18371838
<entry></entry>
@@ -1943,7 +1944,7 @@
19431944
</row>
19441945
<row>
19451946
<entry><token>INNER</token></entry>
1946-
<entry>reserved (can be function)</entry>
1947+
<entry>reserved (can be function or type)</entry>
19471948
<entry>reserved</entry>
19481949
<entry>reserved</entry>
19491950
<entry>reserved</entry>
@@ -2048,14 +2049,14 @@
20482049
</row>
20492050
<row>
20502051
<entry><token>IS</token></entry>
2051-
<entry>reserved (can be function)</entry>
2052+
<entry>reserved (can be function or type)</entry>
20522053
<entry>reserved</entry>
20532054
<entry>reserved</entry>
20542055
<entry>reserved</entry>
20552056
</row>
20562057
<row>
20572058
<entry><token>ISNULL</token></entry>
2058-
<entry>reserved (can be function)</entry>
2059+
<entry>reserved (can be function or type)</entry>
20592060
<entry></entry>
20602061
<entry></entry>
20612062
<entry></entry>
@@ -2076,7 +2077,7 @@
20762077
</row>
20772078
<row>
20782079
<entry><token>JOIN</token></entry>
2079-
<entry>reserved (can be function)</entry>
2080+
<entry>reserved (can be function or type)</entry>
20802081
<entry>reserved</entry>
20812082
<entry>reserved</entry>
20822083
<entry>reserved</entry>
@@ -2160,7 +2161,7 @@
21602161
</row>
21612162
<row>
21622163
<entry><token>LEFT</token></entry>
2163-
<entry>reserved (can be function)</entry>
2164+
<entry>reserved (can be function or type)</entry>
21642165
<entry>reserved</entry>
21652166
<entry>reserved</entry>
21662167
<entry>reserved</entry>
@@ -2188,7 +2189,7 @@
21882189
</row>
21892190
<row>
21902191
<entry><token>LIKE</token></entry>
2191-
<entry>reserved (can be function)</entry>
2192+
<entry>reserved (can be function or type)</entry>
21922193
<entry>reserved</entry>
21932194
<entry>reserved</entry>
21942195
<entry>reserved</entry>
@@ -2475,7 +2476,7 @@
24752476
</row>
24762477
<row>
24772478
<entry><token>NATURAL</token></entry>
2478-
<entry>reserved (can be function)</entry>
2479+
<entry>reserved (can be function or type)</entry>
24792480
<entry>reserved</entry>
24802481
<entry>reserved</entry>
24812482
<entry>reserved</entry>
@@ -2608,7 +2609,7 @@
26082609
</row>
26092610
<row>
26102611
<entry><token>NOTNULL</token></entry>
2611-
<entry>reserved (can be function)</entry>
2612+
<entry>reserved (can be function or type)</entry>
26122613
<entry></entry>
26132614
<entry></entry>
26142615
<entry></entry>
@@ -2811,7 +2812,7 @@
28112812
</row>
28122813
<row>
28132814
<entry><token>OUTER</token></entry>
2814-
<entry>reserved (can be function)</entry>
2815+
<entry>reserved (can be function or type)</entry>
28152816
<entry>reserved</entry>
28162817
<entry>reserved</entry>
28172818
<entry>reserved</entry>
@@ -2832,7 +2833,7 @@
28322833
</row>
28332834
<row>
28342835
<entry><token>OVERLAPS</token></entry>
2835-
<entry>reserved (can be function)</entry>
2836+
<entry>reserved (can be function or type)</entry>
28362837
<entry>reserved</entry>
28372838
<entry>non-reserved</entry>
28382839
<entry>reserved</entry>
@@ -3385,7 +3386,7 @@
33853386
</row>
33863387
<row>
33873388
<entry><token>RIGHT</token></entry>
3388-
<entry>reserved (can be function)</entry>
3389+
<entry>reserved (can be function or type)</entry>
33893390
<entry>reserved</entry>
33903391
<entry>reserved</entry>
33913392
<entry>reserved</entry>
@@ -3658,7 +3659,7 @@
36583659
</row>
36593660
<row>
36603661
<entry><token>SIMILAR</token></entry>
3661-
<entry>reserved (can be function)</entry>
3662+
<entry>reserved (can be function or type)</entry>
36623663
<entry>reserved</entry>
36633664
<entry>non-reserved</entry>
36643665
<entry></entry>
@@ -4414,7 +4415,7 @@
44144415
</row>
44154416
<row>
44164417
<entry><token>VERBOSE</token></entry>
4417-
<entry>reserved (can be function)</entry>
4418+
<entry>reserved (can be function or type)</entry>
44184419
<entry></entry>
44194420
<entry></entry>
44204421
<entry></entry>

doc/src/sgml/ref/create_type.sgml

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_type.sgml,v 1.65 2006/12/23 01:28:09 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_type.sgml,v 1.66 2006/12/30 21:21:52 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -28,6 +28,8 @@ CREATE TYPE <replaceable class="parameter">name</replaceable> (
2828
OUTPUT = <replaceable class="parameter">output_function</replaceable>
2929
[ , RECEIVE = <replaceable class="parameter">receive_function</replaceable> ]
3030
[ , SEND = <replaceable class="parameter">send_function</replaceable> ]
31+
[ , TYPMOD_IN = <replaceable class="parameter">type_modifier_input_function</replaceable> ]
32+
[ , TYPMOD_OUT = <replaceable class="parameter">type_modifier_output_function</replaceable> ]
3133
[ , ANALYZE = <replaceable class="parameter">analyze_function</replaceable> ]
3234
[ , INTERNALLENGTH = { <replaceable class="parameter">internallength</replaceable> | VARIABLE } ]
3335
[ , PASSEDBYVALUE ]
@@ -83,12 +85,14 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
8385
(scalar type). The parameters may appear in any order, not only that
8486
illustrated above, and most are optional. You must register
8587
two or more functions (using <command>CREATE FUNCTION</command>) before
86-
defining the type. The support functions
88+
defining the type. The support functions
8789
<replaceable class="parameter">input_function</replaceable> and
8890
<replaceable class="parameter">output_function</replaceable>
8991
are required, while the functions
9092
<replaceable class="parameter">receive_function</replaceable>,
91-
<replaceable class="parameter">send_function</replaceable> and
93+
<replaceable class="parameter">send_function</replaceable>,
94+
<replaceable class="parameter">type_modifier_input_function</replaceable>,
95+
<replaceable class="parameter">type_modifier_output_function</replaceable> and
9296
<replaceable class="parameter">analyze_function</replaceable>
9397
are optional. Generally these functions have to be coded in C
9498
or another low-level language.
@@ -169,6 +173,34 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
169173
used normally.
170174
</para>
171175

176+
<para>
177+
The optional
178+
<replaceable class="parameter">type_modifier_input_function</replaceable>
179+
and <replaceable class="parameter">type_modifier_output_function</replaceable>
180+
are needed if the type supports modifiers, that is optional constraints
181+
attached to a type declaration, such as <literal>char(5)</> or
182+
<literal>numeric(30,2)</>. <productname>PostgreSQL</productname> allows
183+
user-defined types to take one or more integer constants as modifiers;
184+
however, this information must be capable of being packed into a single
185+
non-negative integer value for storage in the system catalogs. The
186+
<replaceable class="parameter">type_modifier_input_function</replaceable>
187+
is passed the declared modifier(s) in the form of an <type>integer</>
188+
array. It must check the values for validity (throwing an error if they
189+
are wrong), and if they are correct, return a single non-negative
190+
<type>integer</> value that will be stored as the column <quote>typmod</>.
191+
Type modifiers will be rejected if the type does not have a
192+
<replaceable class="parameter">type_modifier_input_function</replaceable>.
193+
The <replaceable class="parameter">type_modifier_output_function</replaceable>
194+
converts the internal integer typmod value back to the correct form for
195+
user display. It must return a <type>cstring</> value that is the exact
196+
string to append to the type name; for example <type>numeric</>'s
197+
function might return <literal>(30,2)</>.
198+
It is allowed to omit the
199+
<replaceable class="parameter">type_modifier_output_function</replaceable>,
200+
in which case the default display format is just the stored typmod value
201+
enclosed in parentheses.
202+
</para>
203+
172204
<para>
173205
The optional <replaceable class="parameter">analyze_function</replaceable>
174206
performs type-specific statistics collection for columns of the data type.
@@ -265,7 +297,7 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
265297
<title>Array Types</title>
266298

267299
<para>
268-
Whenever a user-defined base data type is created,
300+
Whenever a user-defined base data type is created,
269301
<productname>PostgreSQL</productname> automatically creates an
270302
associated array type, whose name consists of the base type's
271303
name prepended with an underscore. The parser understands this
@@ -298,7 +330,7 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
298330
</para>
299331
</refsect2>
300332
</refsect1>
301-
333+
302334
<refsect1>
303335
<title>Parameters</title>
304336

@@ -371,6 +403,26 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
371403
</listitem>
372404
</varlistentry>
373405

406+
<varlistentry>
407+
<term><replaceable class="parameter">type_modifier_input_function</replaceable></term>
408+
<listitem>
409+
<para>
410+
The name of a function that converts numeric modifier(s) for the type
411+
into internal form.
412+
</para>
413+
</listitem>
414+
</varlistentry>
415+
416+
<varlistentry>
417+
<term><replaceable class="parameter">type_modifier_output_function</replaceable></term>
418+
<listitem>
419+
<para>
420+
The name of a function that converts the internal form of the type's
421+
modifier(s) to external textual form.
422+
</para>
423+
</listitem>
424+
</varlistentry>
425+
374426
<varlistentry>
375427
<term><replaceable class="parameter">analyze_function</replaceable></term>
376428
<listitem>
@@ -499,7 +551,7 @@ CREATE TYPE <replaceable class="parameter">name</replaceable>
499551
</para>
500552

501553
</refsect1>
502-
554+
503555
<refsect1>
504556
<title>Examples</title>
505557

src/backend/access/common/tupdesc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.118 2006/07/14 14:52:16 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.119 2006/12/30 21:21:52 tgl Exp $
1212
*
1313
* NOTES
1414
* some of the executor utility code such as "ExecTypeFromTL" should be
@@ -508,6 +508,7 @@ BuildDescForRelation(List *schema)
508508
AttrDefault *attrdef = NULL;
509509
TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
510510
char *attname;
511+
Oid atttypid;
511512
int32 atttypmod;
512513
int attdim;
513514
int ndef = 0;
@@ -533,7 +534,8 @@ BuildDescForRelation(List *schema)
533534
attnum++;
534535

535536
attname = entry->colname;
536-
atttypmod = entry->typename->typmod;
537+
atttypid = typenameTypeId(NULL, entry->typename);
538+
atttypmod = typenameTypeMod(NULL, entry->typename, atttypid);
537539
attdim = list_length(entry->typename->arrayBounds);
538540

539541
if (entry->typename->setof)
@@ -543,8 +545,7 @@ BuildDescForRelation(List *schema)
543545
attname)));
544546

545547
TupleDescInitEntry(desc, attnum, attname,
546-
typenameTypeId(NULL, entry->typename),
547-
atttypmod, attdim);
548+
atttypid, atttypmod, attdim);
548549

549550
/* Fill in additional stuff not handled by TupleDescInitEntry */
550551
if (entry->is_not_null)

0 commit comments

Comments
 (0)