How to check the postgresql version

Started by Ruey-Lung Hsiaoabout 21 years ago5 messages
#1Ruey-Lung Hsiao
rlhsiao@gmail.com

Hi,

Sorry if this problem seems stupid but I would appreciate it if
someone helps.

I've been implementing several user-defined types and their
corresponding GiST index in postgresql 7.4.3. They worked well. However,
I have noticed there are several changes in SPI functions and their
parameters in 8.0beta. I want to be able to compile my codes in
Postgresql 7.4.X and 8.0.X, so the only way to do that is to use the C
preprocessor directives to check PG_VERSION (defined in pg_config.h).

My problem is: I can't find a way to compare strings in C
preprocessor directive since PG_VERSION is defined as something like
"7.4.3" or "7.4.6". I ultimately want to do the following things:

#if PG_VERSION starts with "8.X"
... call 8.0 compatible functions
#else
... call 7.4.x compatible functions
#endif

Could anyone tell me how to do this kind of version check task?
Thank you very much.

Ruey-Lung Hsiao

#2Neil Conway
neilc@samurai.com
In reply to: Ruey-Lung Hsiao (#1)
Re: How to check the postgresql version

Ruey-Lung Hsiao wrote:

My problem is: I can't find a way to compare strings in C preprocessor
directive since PG_VERSION is defined as something like "7.4.3" or
"7.4.6".

You could try using CATALOG_VERSION_NO in src/include/catversion.h as a
substitute for the version number.

I think it would probably be a good idea to add a PG_VERSION-workalike
that is more amenable to use with cpp, though.

-Neil

#3Joe Conway
mail@joeconway.com
In reply to: Neil Conway (#2)
Re: How to check the postgresql version

Neil Conway wrote:

Ruey-Lung Hsiao wrote:

My problem is: I can't find a way to compare strings in C
preprocessor directive since PG_VERSION is defined as something like
"7.4.3" or "7.4.6".

You could try using CATALOG_VERSION_NO in src/include/catversion.h as a
substitute for the version number.

Along those lines, this is what I've been using in PL/R:

#if (CATALOG_VERSION_NO <= 200211021)
#define PG_VERSION_73_COMPAT
#elif (CATALOG_VERSION_NO <= 200310211)
#define PG_VERSION_74_COMPAT
#else
#define PG_VERSION_80_COMPAT
#endif

I wasn't following this thread earlier, but if you need to distinguish
between, for example, 7.4.3 and 7.4.6, the above won't help. If you just
need major Postgres version, it works well.

I think it would probably be a good idea to add a PG_VERSION-workalike
that is more amenable to use with cpp, though.

I agree. As an example, here's how it's done for R:

/*
* R version is calculated thus:
* Maj * 65536 + Minor * 256 + Build * 1
* So:
* version 1.8.0 results in:
* (1 * 65536) + (8 * 256) + (0 * 1) == 67584
* version 1.9.0 results in:
* (1 * 65536) + (9 * 256) + (0 * 1) == 67840
*/

Joe

#4Thomas Hallgren
thhal@mailblocks.com
In reply to: Joe Conway (#3)
Re: How to check the postgresql version

Joe Conway wrote:

Along those lines, this is what I've been using in PL/R:

#if (CATALOG_VERSION_NO <= 200211021)
#define PG_VERSION_73_COMPAT
#elif (CATALOG_VERSION_NO <= 200310211)
#define PG_VERSION_74_COMPAT
#else
#define PG_VERSION_80_COMPAT
#endif

I wasn't following this thread earlier, but if you need to distinguish
between, for example, 7.4.3 and 7.4.6, the above won't help. If you just
need major Postgres version, it works well.

PL/Java used to have this in the Makefile.

SS_VERSION := $(subst ., ,$(subst devel,.devel,$(VERSION)))
PGSQL_MAJOR_VER = $(word 1,$(SS_VERSION))
PGSQL_MINOR_VER = $(word 2,$(SS_VERSION))
PGSQL_PATCH_VER = $(word 3,$(SS_VERSION))

override CPPFLAGS := \
-DPGSQL_MAJOR_VER=$(PGSQL_MAJOR_VER) \
-DPGSQL_MINOR_VER=$(PGSQL_MINOR_VER) \
-DPGSQL_PATCH_VER=$(PGSQL_MINOR_VER)

but I later removed the PGSQL_PATCH_VER since I don't plan to support
different binaries for different patch levels. I'll try to do that using
a more dynamic approach (i.e. through "SELECT version").

In the code, I do things like:

#if PGSQL_MAJOR_VER >= 8

or

#if PGSQL_MAJOR_VER == 7 && PGSQL_MINOR_VER < 3

Regards,
Thomas Hallgren

#5Greg Sabino Mullane
greg@turnstep.com
In reply to: Ruey-Lung Hsiao (#1)
Re: How to check the postgresql version

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It's not quite the same, but check out the libpq function
PQserverVersion, which returns the version in a standard
6-digit format, which can then be used to easily do
numeric comparisons. Look for "sversion" in interfaces/libpq/fe-exec.c
and PQserverVersion in interfaces/libpq/fe-connect.c for
more details.

- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200411222127
-----BEGIN PGP SIGNATURE-----

iD4DBQFBoqBJvJuQZxSWSsgRAleLAJQOmBAN69x7A/79IGiAV6T0GZt5AKDDDTFw
WWXPNFSsUp/iRNgM/Mgy1g==
=QV4u
-----END PGP SIGNATURE-----