]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/typeconv-query.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / typeconv-query.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>10.4. Value Storage</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="typeconv-func.html" title="10.3. Functions" /><link rel="next" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">10.4. Value Storage</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="typeconv-func.html" title="10.3. Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><th width="60%" align="center">Chapter 10. Type Conversion</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Next</a></td></tr></table><hr /></div><div class="sect1" id="TYPECONV-QUERY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">10.4. Value Storage <a href="#TYPECONV-QUERY" class="id_link">#</a></h2></div></div></div><p>
3    Values to be inserted into a table are converted to the destination
4    column's data type according to the
5    following steps.
6   </p><div class="procedure" id="id-1.5.9.9.3"><p class="title"><strong>Value Storage Type Conversion</strong></p><ol class="procedure" type="1"><li class="step"><p>
7 Check for an exact match with the target.
8 </p></li><li class="step"><p>
9 Otherwise, try to convert the expression to the target type.  This is possible
10 if an <em class="firstterm">assignment cast</em> between the two types is registered in the
11 <code class="structname">pg_cast</code> catalog (see <a class="xref" href="sql-createcast.html" title="CREATE CAST"><span class="refentrytitle">CREATE CAST</span></a>).
12 Alternatively, if the expression is an unknown-type literal, the contents of
13 the literal string will be fed to the input conversion routine for the target
14 type.
15 </p></li><li class="step"><p>
16 Check to see if there is a sizing cast for the target type.  A sizing
17 cast is a cast from that type to itself.  If one is found in the
18 <code class="structname">pg_cast</code> catalog, apply it to the expression before storing
19 into the destination column.  The implementation function for such a cast
20 always takes an extra parameter of type <code class="type">integer</code>, which receives
21 the destination column's <code class="structfield">atttypmod</code> value (typically its
22 declared length, although the interpretation of <code class="structfield">atttypmod</code>
23 varies for different data types), and it may take a third <code class="type">boolean</code>
24 parameter that says whether the cast is explicit or implicit.  The cast
25 function
26 is responsible for applying any length-dependent semantics such as size
27 checking or truncation.
28 </p></li></ol></div><div class="example" id="id-1.5.9.9.4"><p class="title"><strong>Example 10.9. <code class="type">character</code> Storage Type Conversion</strong></p><div class="example-contents"><p>
29 For a target column declared as <code class="type">character(20)</code> the following
30 statement shows that the stored value is sized correctly:
31
32 </p><pre class="screen">
33 CREATE TABLE vv (v character(20));
34 INSERT INTO vv SELECT 'abc' || 'def';
35 SELECT v, octet_length(v) FROM vv;
36
37           v           | octet_length
38 ----------------------+--------------
39  abcdef               |           20
40 (1 row)
41 </pre><p>
42 </p><p>
43 What has really happened here is that the two unknown literals are resolved
44 to <code class="type">text</code> by default, allowing the <code class="literal">||</code> operator
45 to be resolved as <code class="type">text</code> concatenation.  Then the <code class="type">text</code>
46 result of the operator is converted to <code class="type">bpchar</code> (<span class="quote">“<span class="quote">blank-padded
47 char</span>”</span>, the internal name of the <code class="type">character</code> data type) to match the target
48 column type.  (Since the conversion from <code class="type">text</code> to
49 <code class="type">bpchar</code> is binary-coercible, this conversion does
50 not insert any real function call.)  Finally, the sizing function
51 <code class="literal">bpchar(bpchar, integer, boolean)</code> is found in the system catalog
52 and applied to the operator's result and the stored column length.  This
53 type-specific function performs the required length check and addition of
54 padding spaces.
55 </p></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="typeconv-func.html" title="10.3. Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Next</a></td></tr><tr><td width="40%" align="left" valign="top">10.3. Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 10.5. <code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs</td></tr></table></div></body></html>