pg_typeof equivalent for numeric scale, numeric/timestamp precision?

From: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: pg_typeof equivalent for numeric scale, numeric/timestamp precision?
Date: 2012-10-02 02:19:18
Message-ID: 506A4F26.4050008@ringerc.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all

While examining a reported issue with the JDBC driver I'm finding myself
wanting SQL-level functions to get the scale and precision of a numeric
result from an operation like:

select NUMERIC(8,4) '1.9999'
union
select INTEGER 4;

I can write:

SELECT pg_typeof(a), a FROM (
select NUMERIC(8,4) '1.9999'
union
select 4::integer
) x(a);

but I didn' t see any SQL-level way to get the scale and precision. The
output of `pg_typeof` is a `regtype` so it doesn't have any given scale
and precision, it's just the raw type. I didn't find any functions with
"scale" or "precision" in their name, nor any functions matching
*numeric* that looked promising. *typmod* only found in- and out-
functions. Nothing matching *type* looked good.

There's `format_type`, but it requires you to supply the typomod, it
can't get it from a result for you. Worse, it doesn't seem to offer a
way to set scale, only precision, so it's of limited utility for numeric
anyway, since every numeric it produces is invalid ("numeric precision
must be between 1 and 1000").

Will I need to do this from C with a custom function, or via libpq's
metadata APIs? And re format_type, am I misunderstanding it or is it
just busted for numeric?

--
Craig Ringer

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Toby Corkindale 2012-10-02 02:33:39 Can I force a query plan to materialise part?
Previous Message Greg Sabino Mullane 2012-10-01 23:02:39 Re: Securing .pgpass File?