Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ltnwq-0002lH-15 for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Jun 2021 09:04:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ltnwp-0000kS-09 for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Jun 2021 09:04:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ltnwo-0000kK-Kc for pgsql-hackers@lists.postgresql.org; Thu, 17 Jun 2021 09:04:26 +0000 Received: from codesynthesis.com ([188.40.148.39]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ltnwm-0001lC-6l for pgsql-hackers@lists.postgresql.org; Thu, 17 Jun 2021 09:04:26 +0000 Received: from brak.codesynthesis.com (unknown [105.225.47.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by codesynthesis.com (Postfix) with ESMTPSA id 28624602CD; Thu, 17 Jun 2021 09:04:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codesynthesis.com; s=mail1; t=1623920662; bh=GIexCqpHHTNjz5+HGLLDDGwhJBGxxFseMWIxkc16i/g=; h=Date:From:To:Subject:Message-ID:MIME-Version:From; b=bhSZk2k7OEHv5ulrCPUWO31rWIg1hOmiDAW4Wx62+3L/qnwmdsdhiq+YWrVif+Hrb mFYqPqxhojnjoU0hsQDi3wff6wAO7Cl6S8kRwKBvX7nEH9gs+iISk90PNN/RflrsNz l5VKaZkzCEZq20P76iAk7VwdcfTY8rd4owr9ZkkEiaPa4RcrS66jZqZJP5kNhGkd+i tKuk/cvjV5N9YGa/Ob9LG/sXhWM/2EwkoutLF0luK31FBdZkYy34c93jcD1z51laO9 b07CAAEbr2ZnzryJhjoA7P5i3aql49kUbmNZVVu74Q562BXvthrHzK75DmRYCacE7c z4LgUFdkS+VxA== Received: by brak.codesynthesis.com (Postfix, from userid 1000) id 525481A802C0; Thu, 17 Jun 2021 11:04:06 +0200 (SAST) Date: Thu, 17 Jun 2021 11:04:06 +0200 From: Boris Kolpackov To: pgsql-hackers@lists.postgresql.org Cc: alvaro.herrera@2ndquadrant.com Subject: Add version macro to libpq-fe.h Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Organization: Code Synthesis User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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)