Add version macro to libpq-fe.h

From: Boris Kolpackov <boris(at)codesynthesis(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: alvaro(dot)herrera(at)2ndquadrant(dot)com
Subject: Add version macro to libpq-fe.h
Date: 2021-06-17 09:04:06
Message-ID: boris.20210617102439@codesynthesis.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I am making use of the new pipeline mode added to libpq in
PostgreSQL 14. At the same time I would still like to support
older libpq versions by not providing the extended functionality
that depends on this mode.

The natural way to achieve this in C/C++ is to conditionally
enable code that depends on the additional APIs based on the
preprocessor macro. And I could easily do this if libpq-fe.h
provided a macro containing its version.

Now, such a macro (PG_VERSION_NUM) is provided by pg_config.h
that normally accompanies libpq-fe.h. However, I don't believe
the presence of this file is guaranteed. All the documentation
says[1] about headers is this:

"Client programs that use libpq must include the header file
libpq-fe.h and must link with the libpq library."

And there are good reasons why packagers of libpq may decide to
omit this header (in a nutshell, it embeds target architecture-
specific information, see this discussion for background[2]). And
I may not want to include it in my code (it defines a lot of free-
named macros that may clash with my names).

So I am wondering if it would make sense to provide a better way
to obtain the libpq version as a macro?

To me, as a user, the simplest way would be to have such a macro
defined by libpq-fe.h. This would also provide a reasonable
fallback for previous versions: if this macro is not defined, I
know I am dealing with version prior to 14 and if I need to know
which exactly I can try to include pg_config.h (perhaps with the
help of __has_include if I am using C++).

If simply moving this macro to libpq-fe.h is not desirable (for
example, because it is auto-generated), then perhaps we could
move this (and a few other version-related macros[3]) to a
separate header (for example, libpq-version.h) and either include
it from libpq-fe.h or define a macro in libpq-fe.h that signals
its presence (e.g., PG_HAS_VERSION or some such).

What do you think?

[1] https://www.postgresql.org/docs/9.3/libpq.html

[2] https://bugzilla.redhat.com/show_bug.cgi?id=828467

[3] PG_MAJORVERSION
PG_MAJORVERSION_NUM
PG_MINORVERSION_NUM
PG_VERSION
PG_VERSION_NUM
PG_VERSION_STR (this one includes target so maybe leave it in pg_config.h)

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Masahiko Sawada 2021-06-17 09:14:20 Re: Teaching users how they can get the most out of HOT in Postgres 14
Previous Message Yugo NAGATA 2021-06-17 08:55:42 Re: pgbench logging broken by time logic changes