Version Number Function?

Lists: pgsql-hackers
From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Version Number Function?
Date: 2008-10-11 23:52:13
Message-ID: 294EAAB0-9E42-448C-8962-641521AC8C4C@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Howdy,

Any interest in adding a function like this to core?

Datum
pg_version(PG_FUNCTION_ARGS)
{
PG_RETURN_INT32(PG_VERSION_NUM);
}

That returns an integer, such as

try=# select pg_version();
pg_version
------------
80304
(1 row)

I've whipped this up for pgtap, as it'll be useful for determing when
to skip tests based on a version of PostgreSQL, but I thought it might
be generally useful enough to add to core.

Thoughts?

Best,

David


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 02:57:44
Message-ID: 2756.1223780264@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"David E. Wheeler" <david(at)kineticode(dot)com> writes:
> Any interest in adding a function like this to core?

No, because it's already there: see "show server_version_num".

(It's probably worth noting that none of our existing clients that would
have any use for this information look at server_version_num, because
it's only available in 8.2 and up. A function introduced as of 8.4
would be an additional two major releases behind the curve.)

regards, tom lane


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 18:15:18
Message-ID: 369D974D-88AC-453F-91E8-080D40629D6F@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 11, 2008, at 19:57, Tom Lane wrote:

> "David E. Wheeler" <david(at)kineticode(dot)com> writes:
>> Any interest in adding a function like this to core?
>
> No, because it's already there: see "show server_version_num".
>
> (It's probably worth noting that none of our existing clients that
> would
> have any use for this information look at server_version_num, because
> it's only available in 8.2 and up. A function introduced as of 8.4
> would be an additional two major releases behind the curve.)

Yeah, but I want to use it in WHERE clauses or CASE statements. I'm
fine to just include it in pgtap, though.

Best,

David


From: Magnus Hagander <magnus(at)hagander(dot)net>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 18:21:49
Message-ID: 48F2403D.9000801@hagander.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David E. Wheeler wrote:
> On Oct 11, 2008, at 19:57, Tom Lane wrote:
>
>> "David E. Wheeler" <david(at)kineticode(dot)com> writes:
>>> Any interest in adding a function like this to core?
>>
>> No, because it's already there: see "show server_version_num".
>>
>> (It's probably worth noting that none of our existing clients that would
>> have any use for this information look at server_version_num, because
>> it's only available in 8.2 and up. A function introduced as of 8.4
>> would be an additional two major releases behind the curve.)
>
> Yeah, but I want to use it in WHERE clauses or CASE statements. I'm fine
> to just include it in pgtap, though.

You could do:
select setting from pg_settings where name='server_version_num';

(wrapped in the appropriate subselect to use it in a WHERE clause)

//Magnus


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 19:11:32
Message-ID: 4132D09B-7D3C-47DE-B55F-3DA4597162F8@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 12, 2008, at 11:21, Magnus Hagander wrote:

>> Yeah, but I want to use it in WHERE clauses or CASE statements. I'm
>> fine
>> to just include it in pgtap, though.
>
> You could do:
> select setting from pg_settings where name='server_version_num';
>
> (wrapped in the appropriate subselect to use it in a WHERE clause)

Right, but I want to make it as simple as possible for test writers to
use.

Best,

David


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 19:42:10
Message-ID: 12238.1223840530@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"David E. Wheeler" <david(at)kineticode(dot)com> writes:
> On Oct 11, 2008, at 19:57, Tom Lane wrote:
>> "David E. Wheeler" <david(at)kineticode(dot)com> writes:
>>> Any interest in adding a function like this to core?
>>
>> No, because it's already there: see "show server_version_num".

> Yeah, but I want to use it in WHERE clauses or CASE statements.

current_setting('server_version_num')

regards, tom lane


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 20:08:37
Message-ID: FD00E789-4535-47BA-B34B-7ABE8ECB7A16@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 12, 2008, at 12:42, Tom Lane wrote:

>> Yeah, but I want to use it in WHERE clauses or CASE statements.
>
> current_setting('server_version_num')

Hrm. That's nice. I don't suppose there's any way to get something
like that in 8.1 and earlier? I was going to fake it in the .c file.

Thanks,

David


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 21:11:20
Message-ID: 12997.1223845880@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"David E. Wheeler" <david(at)kineticode(dot)com> writes:
> On Oct 12, 2008, at 12:42, Tom Lane wrote:
>> current_setting('server_version_num')

> Hrm. That's nice. I don't suppose there's any way to get something
> like that in 8.1 and earlier? I was going to fake it in the .c file.

You'd have to parse the result of version().

regards, tom lane


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-12 21:39:14
Message-ID: 0B482C37-19B8-4F05-94F2-86DA6625CE2C@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 12, 2008, at 14:11, Tom Lane wrote:

> You'd have to parse the result of version().

As I figured. This is what I'm trying:

pg_version_num(PG_FUNCTION_ARGS)
{
#ifdef PG_VERSION_NUM
PG_RETURN_INT32(PG_VERSION_NUM);
#else
/* Code borrowed from dumputils.c. */
int cnt;
int vmaj,
vmin,
vrev;

cnt = sscanf(PG_VERSION, "%d.%d.%d", &vmaj, &vmin, &vrev);

if (cnt < 2)
return -1;

if (cnt == 2)
vrev = 0;

PG_RETURN_INT32( (100 * vmaj + vmin) * 100 + vrev );
#endif

Best,

David


From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 14:44:04
Message-ID: 1223995444.8222.3.camel@huvostro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, 2008-10-12 at 14:39 -0700, David E. Wheeler wrote:
> On Oct 12, 2008, at 14:11, Tom Lane wrote:
>
> > You'd have to parse the result of version().
>
> As I figured. This is what I'm trying:

if performance is not critical, then you could use this:

hannu=# create or replace function pg_version_num() returns int language
SQL as $$
select
10000 *
cast(substring(version()
from
'^PostgreSQL +([0-9]+)[.][0-9]+[.][0-9]+ +') as int)
+
100 *
cast(substring(version()
from
'^PostgreSQL +[0-9]+[.]([0-9]+)[.][0-9]+ +') as int)
+
cast(substring(version()
from
'^PostgreSQL +[0-9]+[.][0-9]+[.]([0-9]+) +') as int);
$$;
CREATE FUNCTION

hannu=# select pg_version_num();
pg_version_num
----------------
80303
(1 row)

> pg_version_num(PG_FUNCTION_ARGS)
> {
> #ifdef PG_VERSION_NUM
> PG_RETURN_INT32(PG_VERSION_NUM);
> #else
> /* Code borrowed from dumputils.c. */
> int cnt;
> int vmaj,
> vmin,
> vrev;
>
> cnt = sscanf(PG_VERSION, "%d.%d.%d", &vmaj, &vmin, &vrev);
>
> if (cnt < 2)
> return -1;
>
> if (cnt == 2)
> vrev = 0;
>
> PG_RETURN_INT32( (100 * vmaj + vmin) * 100 + vrev );
> #endif
>
> Best,
>
> David
>
>


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 15:33:39
Message-ID: B2B21B77-B95C-44D4-A5FB-55B8F3EA67EF@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Well, the C version I borrowed from dumpitils seems to work great. Any
reason I shouldn't stay with that?

Best,

David

Sent from my iPhone

On Oct 14, 2008, at 7:44, Hannu Krosing <hannu(at)2ndQuadrant(dot)com> wrote:

> On Sun, 2008-10-12 at 14:39 -0700, David E. Wheeler wrote:
>> On Oct 12, 2008, at 14:11, Tom Lane wrote:
>>
>>> You'd have to parse the result of version().
>>
>> As I figured. This is what I'm trying:
>
> if performance is not critical, then you could use this:
>
> hannu=# create or replace function pg_version_num() returns int
> language
> SQL as $$
> select
> 10000 *
> cast(substring(version()
> from
> '^PostgreSQL +([0-9]+)[.][0-9]+[.][0-9]+ +') as int)
> +
> 100 *
> cast(substring(version()
> from
> '^PostgreSQL +[0-9]+[.]([0-9]+)[.][0-9]+ +') as int)
> +
> cast(substring(version()
> from
> '^PostgreSQL +[0-9]+[.][0-9]+[.]([0-9]+) +') as int);
> $$;
> CREATE FUNCTION
>
> hannu=# select pg_version_num();
> pg_version_num
> ----------------
> 80303
> (1 row)
>
>> pg_version_num(PG_FUNCTION_ARGS)
>> {
>> #ifdef PG_VERSION_NUM
>> PG_RETURN_INT32(PG_VERSION_NUM);
>> #else
>> /* Code borrowed from dumputils.c. */
>> int cnt;
>> int vmaj,
>> vmin,
>> vrev;
>>
>> cnt = sscanf(PG_VERSION, "%d.%d.%d", &vmaj, &vmin, &vrev);
>>
>> if (cnt < 2)
>> return -1;
>>
>> if (cnt == 2)
>> vrev = 0;
>>
>> PG_RETURN_INT32( (100 * vmaj + vmin) * 100 + vrev );
>> #endif
>>
>> Best,
>>
>> David
>>
>>
>


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: David E(dot) Wheeler <david(at)kineticode(dot)com>
Cc: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 16:53:43
Message-ID: 0D51B226-9689-48F5-A08E-31208ADA7B58@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 14, 2008, at 08:33, David E. Wheeler wrote:

> Well, the C version I borrowed from dumpitils seems to work great.
> Any reason I shouldn't stay with that?

Also, here's a simpler SQL version, for those following along at home:

create or replace function pg_version_num() returns int language
SQL as $$
SELECT SUM(
(string_to_array(current_setting('server_version'), '.'))
[i]::int
* CASE i WHEN 1 THEN 10000 WHEN 2 THEN 100 ELSE 1 end
)::int FROM generate_series(1, 3) AS gen(i);
$$;
CREATE FUNCTION

There must be a way to get string_to_array() to evaluate only once, yes?

Best,

David


From: "Robert Haas" <robertmhaas(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: "Hannu Krosing" <hannu(at)2ndquadrant(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "PostgreSQL-development Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 19:41:39
Message-ID: 603c8f070810141241l7a26fef7p2b2864f20c229aea@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> There must be a way to get string_to_array() to evaluate only once, yes?

WITH, but that's not going to help you for backward compatibility.

If you used plpgsql you could assign the string_to_array result to a
variable and then work off the variable, but I'm not sure that's
really better.

...Robert


From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 21:32:57
Message-ID: 1224019977.9912.7.camel@huvostro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, 2008-10-14 at 08:33 -0700, David E. Wheeler wrote:
> Well, the C version I borrowed from dumpitils seems to work great. Any
> reason I shouldn't stay with that?

SQL is the only "PL" available by default, no need to compile or install
anything.

It can be written more effectively in almost any other pl, and probably
in SQL as well ;)

---------------
Hannu


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 21:39:33
Message-ID: A13DE4F0-2F13-4ACE-9EDC-56B39AC596BB@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 14, 2008, at 14:32, Hannu Krosing wrote:

> On Tue, 2008-10-14 at 08:33 -0700, David E. Wheeler wrote:
>> Well, the C version I borrowed from dumpitils seems to work great.
>> Any
>> reason I shouldn't stay with that?
>
> SQL is the only "PL" available by default, no need to compile or
> install
> anything.
>
> It can be written more effectively in almost any other pl, and
> probably
> in SQL as well ;)

Yes, but I'm putting this into pgTAP, where I already have some C
functions I'm defining, so another won't hurt any. :-)

Thanks,

David


From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 21:50:57
Message-ID: 1224021057.9912.12.camel@huvostro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, 2008-10-14 at 09:53 -0700, David E. Wheeler wrote:
> On Oct 14, 2008, at 08:33, David E. Wheeler wrote:
>
> > Well, the C version I borrowed from dumpitils seems to work great.
> > Any reason I shouldn't stay with that?
>
> Also, here's a simpler SQL version, for those following along at home:
>
> create or replace function pg_version_num() returns int language
> SQL as $$
> SELECT SUM(
> (string_to_array(current_setting('server_version'), '.'))
> [i]::int
> * CASE i WHEN 1 THEN 10000 WHEN 2 THEN 100 ELSE 1 end
> )::int FROM generate_series(1, 3) AS gen(i);
> $$;
> CREATE FUNCTION

Was current_setting('server_version') available in 8.1 ?

> There must be a way to get string_to_array() to evaluate only once, yes?

SELECT s.a[1]::int * 10000 + s.a[2]::int * 100 + s.a[3]::int
FROM
(SELECT string_to_array(current_setting('server_version'), '.') AS a)
AS s;

----------------
Hannu


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Version Number Function?
Date: 2008-10-14 22:16:18
Message-ID: 3207C56B-90F0-4387-9102-87B24A49803D@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Oct 14, 2008, at 14:50, Hannu Krosing wrote:

> Was current_setting('server_version') available in 8.1 ?

Yes. In 8.0, too.

>> There must be a way to get string_to_array() to evaluate only once,
>> yes?
>
> SELECT s.a[1]::int * 10000 + s.a[2]::int * 100 + s.a[3]::int
> FROM
> (SELECT string_to_array(current_setting('server_version'), '.') AS a)
> AS s;

Oh, duh. Even better!

Best,

David