Re: typmod is always -1

From: Chapman Flack <chap(at)anastigmatix(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Subject: Re: typmod is always -1
Date: 2016-03-18 00:27:23
Message-ID: 56EB4B6B.5020405@anastigmatix.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 03/17/16 09:35, Tom Lane wrote:
> Chapman Flack <chap(at)anastigmatix(dot)net> writes:
>> I'm in the same boat ... I have an input function I want to test, and so
>> far I have failed to think of *any* sql construct that causes it to be
>> invoked with other than -1 for the typmod.
>
> COPY was the first case a quick grep came across.

Thanks, that does make a working test. Given a table with a typmod'd
column, COPY FROM exercises the 'input' function with a typmod != -1,
and COPY FROM (FORMAT BINARY) likewise exercises the 'receive' function.

While I'm here, I guess I should check the sense I am getting of what
can and can't be workable semantics for type modifiers.

It seems that a typmod can only be used restrict the set of possible
values of the unmodified type (as clearly seen in the language "length
conversion cast", since certainly a typmod allowing { string | length < N }
is doing nothing but enforcing a subset of { string }. Each element of
the subset is still a valid element of the whole set (naturally, boring)
*and has to be represented the same way* (interesting): the representation
mustn't do clever things that you would need to know the typmod in order to
interpret, because most uses of a value are without access to the typmod.

So, the generalization of "length conversion cast" could be something like
"typmod application cast" and the only things a typmod application cast can
do to a value V are:

1. pass V unchanged if it is in the subset implied by the typmod
2. silently pass some V' that is in that subset and "close to" V
in some sense (longest initial substring shorter than N, nearest
numeric value with no more than N precision digits, etc.)
3. fail

with sometimes the choice of (2) or (3) depending on whether the cast
is explicit or not.

All in all, very like a domain, except a domain can only do (1) or (3),
not (2).

Differences in representation, like short strings getting 1-byte headers,
are only possible as a consequence of a lower layer doing that consistently
to all values that happen to be short, and not as an effect of a typmod.

Am I getting it about right?

-Chap

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2016-03-18 00:35:16 Re: [HACKERS] Request - repeat value of \pset title during \watch interations
Previous Message Peter Geoghegan 2016-03-18 00:05:01 Re: Make primnodes.h gender neutral