10.4. Value Storage

|   10.4. Value Storage    |                   |                           |                  |                                 |
|:------------------------:|-------------------|---------------------------|------------------|---------------------------------|
|[Prev](typeconv-func.html)|[Up](typeconv.html)|Chapter 10. Type Conversion|[Home](index.html)| [Next](typeconv-union-case.html)|

---

## 10.4. Value Storage

 Values to be inserted into a table are converted to the destination column's data type according to the following steps.

**Value Storage Type Conversion**

1. Check for an exact match with the target.

2. Otherwise, try to convert the expression to the target type. This is possible
   if an *assignment cast* between the two types is registered in the`pg_cast` catalog (see [CREATE CAST](sql-createcast.html)).
   Alternatively, if the expression is an unknown-type literal, the contents of
   the literal string will be fed to the input conversion routine for the target
   type.

3. Check to see if there is a sizing cast for the target type. A sizing
   cast is a cast from that type to itself. If one is found in the`pg_cast` catalog, apply it to the expression before storing
   into the destination column. The implementation function for such a cast
   always takes an extra parameter of type `integer`, which receives
   the destination column's `atttypmod` value (typically its
   declared length, although the interpretation of `atttypmod`varies for different data types), and it may take a third `boolean`parameter that says whether the cast is explicit or implicit. The cast
   function
   is responsible for applying any length-dependent semantics such as size
   checking or truncation.

**Example 10.9. `character` Storage Type Conversion**

For a target column declared as `character(20)` the following
statement shows that the stored value is sized correctly:

```
CREATE TABLE vv (v character(20));
INSERT INTO vv SELECT 'abc' || 'def';
SELECT v, octet_length(v) FROM vv;

          v           | octet_length
----------------------+--------------
 abcdef               |           20
(1 row)

```

What has really happened here is that the two unknown literals are resolved
to `text` by default, allowing the `||` operator
to be resolved as `text` concatenation. Then the `text`result of the operator is converted to `bpchar` (“blank-padded
char”, the internal name of the `character` data type) to match the target
column type. (Since the conversion from `text` to`bpchar` is binary-coercible, this conversion does
not insert any real function call.) Finally, the sizing function`bpchar(bpchar, integer, boolean)` is found in the system catalog
and applied to the operator's result and the stored column length. This
type-specific function performs the required length check and addition of
padding spaces.

---

|[Prev](typeconv-func.html)|[Up](typeconv.html)|       [Next](typeconv-union-case.html)       |
|:-------------------------|:-----------------:|---------------------------------------------:|
|     10.3. Functions      |[Home](index.html) | 10.5. `UNION`, `CASE`, and Related Constructs|

 // \<![CDATA[ \<-- For SVG support if ('WebSocket' in window) { (function() { function refreshCSS() { var sheets = [].slice.call(document.getElementsByTagName("link")); var head = document.getElementsByTagName("head")[0]; for (var i = 0; i \< sheets.length; ++i) { var elem = sheets[i]; head.removeChild(elem); var rel = elem.rel; if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") { var url = elem.href.replace(/(&|\\?)\_cacheOverride=\\d+/, ''); elem.href = url + (url.indexOf('?') \>= 0 ? '&' : '?') + '\_cacheOverride=' + (new Date().valueOf()); } head.appendChild(elem); } } var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'; var address = protocol + window.location.host + window.location.pathname + '/ws'; var socket = new WebSocket(address); socket.onmessage = function(msg) { if (msg.data == 'reload') window.location.reload(); else if (msg.data == 'refreshcss') refreshCSS(); }; console.log('Live reload enabled.'); })(); } // ]]\>