]> begriffs open source - ai-pg/blob - full-docs/txt/typeconv-query.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / typeconv-query.txt
1
2 10.4. Value Storage #
3
4    Values to be inserted into a table are converted to the destination
5    column's data type according to the following steps.
6
7    Value Storage Type Conversion
8     1. Check for an exact match with the target.
9     2. Otherwise, try to convert the expression to the target type. This
10        is possible if an assignment cast between the two types is
11        registered in the pg_cast catalog (see CREATE CAST). Alternatively,
12        if the expression is an unknown-type literal, the contents of the
13        literal string will be fed to the input conversion routine for the
14        target type.
15     3. Check to see if there is a sizing cast for the target type. A
16        sizing cast is a cast from that type to itself. If one is found in
17        the pg_cast catalog, apply it to the expression before storing into
18        the destination column. The implementation function for such a cast
19        always takes an extra parameter of type integer, which receives the
20        destination column's atttypmod value (typically its declared
21        length, although the interpretation of atttypmod varies for
22        different data types), and it may take a third boolean parameter
23        that says whether the cast is explicit or implicit. The cast
24        function is responsible for applying any length-dependent semantics
25        such as size checking or truncation.
26
27    Example 10.9. character Storage Type Conversion
28
29    For a target column declared as character(20) the following statement
30    shows that the stored value is sized correctly:
31 CREATE TABLE vv (v character(20));
32 INSERT INTO vv SELECT 'abc' || 'def';
33 SELECT v, octet_length(v) FROM vv;
34
35           v           | octet_length
36 ----------------------+--------------
37  abcdef               |           20
38 (1 row)
39
40    What has really happened here is that the two unknown literals are
41    resolved to text by default, allowing the || operator to be resolved as
42    text concatenation. Then the text result of the operator is converted
43    to bpchar (“blank-padded char”, the internal name of the character data
44    type) to match the target column type. (Since the conversion from text
45    to bpchar is binary-coercible, this conversion does not insert any real
46    function call.) Finally, the sizing function bpchar(bpchar, integer,
47    boolean) is found in the system catalog and applied to the operator's
48    result and the stored column length. This type-specific function
49    performs the required length check and addition of padding spaces.