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)