cockroachdb / cockroachdb/cockroach
plpgsql: type coercion truncates when value is too wide
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
This issue tracks a difference between postgres and CRDB when coercing a PLpgSQL expression into a type that is too narrow. For context:
Explicit casts succeed, and truncate the value:
```
postgres=# SELECT ('foo'::TEXT)::CHAR;
bpchar
--------
f
(1 row)
```
Assignment casts fail with an error:
```
postgres=# CREATE TABLE t (c CHAR);
CREATE TABLE
postgres=# INSERT INTO t VALUES ('foo'::TEXT);
ERROR: value too long for type character(1)
```
Finally, PLpgSQL type coercion succeeds, but does not truncate:
```
postgres=# CREATE OR REPLACE FUNCTION f() RETURNS CHAR AS $$
BEGIN
RETURN 'abcd'::TEXT;
END
$$ LANGUAGE PLpgSQL;
CREATE FUNCTION
postgres=# SELECT f();
f
------
abcd
(1 row)
```
CRDB matches behavior for the first two cases, but truncates the value for the last case:
```
CREATE OR REPLACE FUNCTION f() RETURNS CHAR AS $$
BEGIN
RETURN 'abcd'::TEXT;
END
$$ LANGUAGE PLpgSQL;
query T
SELECT f();
----
a
```
Jira issue: CRDB-34004
Contributor guide
Assessment
This issue has not been assessed yet.