pgsql: tests: Add a test C++ extension module
tests: Add a test C++ extension module
While we already test that our headers are valid C++ using
headerscheck, it turns out that the macros we define might still
expand to invalid C++ code. This adds a minimal test extension that
is compiled using C++ to test that it's actually possible to build and
run extensions written in C++. Future commits will improve C++
compatibility of some of our macros and add usage of them to this
extension make sure that they don't regress in the future.
The test module is for the moment disabled when using MSVC. In
particular, the use of designated initializers in PG_MODULE_MAGIC
would require C++20, for which we are currently not set up. (GCC and
Clang support it as extensions.) It is planned to fix this.
Author: Jelte Fennema-Nio <postgres@jeltef.nl>
Discussion: /messages/by-id/CAGECzQR21OnnKiZO_1rLWO0-16kg1JBxnVq-wymYW0-_1cUNtg@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/476b35d4e311a3d77a550280d93393e518908b27
Modified Files
--------------
configure | 8 +++++
configure.ac | 7 ++++
src/Makefile.global.in | 2 ++
src/makefiles/meson.build | 2 ++
src/test/modules/Makefile | 6 ++++
src/test/modules/meson.build | 1 +
src/test/modules/test_cplusplusext/.gitignore | 3 ++
src/test/modules/test_cplusplusext/Makefile | 26 ++++++++++++++
src/test/modules/test_cplusplusext/README | 10 ++++++
.../expected/test_cplusplusext.out | 7 ++++
src/test/modules/test_cplusplusext/meson.build | 42 ++++++++++++++++++++++
.../test_cplusplusext/sql/test_cplusplusext.sql | 3 ++
.../test_cplusplusext/test_cplusplusext--1.0.sql | 8 +++++
.../test_cplusplusext/test_cplusplusext.control | 4 +++
.../test_cplusplusext/test_cplusplusext.cpp | 37 +++++++++++++++++++
15 files changed, 166 insertions(+)
On Wed, Jan 21, 2026 at 2:43 AM Peter Eisentraut <peter@eisentraut.org> wrote:
tests: Add a test C++ extension module
While we already test that our headers are valid C++ using
headerscheck, it turns out that the macros we define might still
expand to invalid C++ code. This adds a minimal test extension that
is compiled using C++ to test that it's actually possible to build and
run extensions written in C++. Future commits will improve C++
compatibility of some of our macros and add usage of them to this
extension make sure that they don't regress in the future.The test module is for the moment disabled when using MSVC. In
particular, the use of designated initializers in PG_MODULE_MAGIC
would require C++20, for which we are currently not set up. (GCC and
Clang support it as extensions.) It is planned to fix this.Author: Jelte Fennema-Nio <postgres@jeltef.nl>
Discussion: /messages/by-id/CAGECzQR21OnnKiZO_1rLWO0-16kg1JBxnVq-wymYW0-_1cUNtg@mail.gmail.comBranch
------
masterDetails
-------
https://git.postgresql.org/pg/commitdiff/476b35d4e311a3d77a550280d93393e518908b27Modified Files
--------------
configure | 8 +++++
configure.ac | 7 ++++
src/Makefile.global.in | 2 ++
src/makefiles/meson.build | 2 ++
src/test/modules/Makefile | 6 ++++
src/test/modules/meson.build | 1 +
src/test/modules/test_cplusplusext/.gitignore | 3 ++
src/test/modules/test_cplusplusext/Makefile | 26 ++++++++++++++
src/test/modules/test_cplusplusext/README | 10 ++++++
.../expected/test_cplusplusext.out | 7 ++++
src/test/modules/test_cplusplusext/meson.build | 42 ++++++++++++++++++++++
.../test_cplusplusext/sql/test_cplusplusext.sql | 3 ++
.../test_cplusplusext/test_cplusplusext--1.0.sql | 8 +++++
.../test_cplusplusext/test_cplusplusext.control | 4 +++
.../test_cplusplusext/test_cplusplusext.cpp | 37 +++++++++++++++++++
15 files changed, 166 insertions(+)
Hi.
'make check-world' is suddenly failing for me, apparently caused by
this new test.
FYI - I am building in a 'Linux CentOS7-x64 ' environment, which I
know has reached end-of-life, but even so, everything was fine before
this test appeared.
======
Kind Regards,
Peter Smith.
Fujitsu Australia
~~~
install.log output is below:
make[4]: Entering directory
`/home/postgres/oss_postgres_misc/src/test/modules/test_cplusplusext'
for extra in src/test/modules/test_cplusplusext; do make -C
'../../../..'/$extra
DESTDIR='/home/postgres/oss_postgres_misc'/tmp_install install ||
exit; done
make[5]: Entering directory
`/home/postgres/oss_postgres_misc/src/test/modules/test_cplusplusext'
g++ -Wall -Wpointer-arith -Wendif-labels -Wmissing-format-attribute
-Wformat-security -fno-strict-aliasing -fwrapv -g -O2 -fPIC
-fvisibility=hidden -fvisibility-inlines-hidden -I. -I.
-I../../../../src/include -D_GNU_SOURCE -c -o test_cplusplusext.o
test_cplusplusext.cpp
In file included from test_cplusplusext.cpp:19:0:
../../../../src/include/fmgr.h:510:1: warning: identifier
‘static_assert’ is a keyword in C++11 [-Wc++0x-compat]
StaticAssertDecl(sizeof(FMGR_ABI_EXTRA) <= sizeof(((Pg_abi_values *)
0)->abi_extra),
^
In file included from ../../../../src/include/c.h:57:0,
from ../../../../src/include/postgres.h:48,
from test_cplusplusext.cpp:18:
../../../../src/include/pg_config.h:552:25: error: expected identifier
before numeric constant
#define MAXIMUM_ALIGNOF 8
^
../../../../src/include/c.h:1113:10: note: in expansion of macro
‘MAXIMUM_ALIGNOF’
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
../../../../src/include/pg_config.h:552:25: error: expected ‘,’ or
‘...’ before numeric constant
#define MAXIMUM_ALIGNOF 8
^
../../../../src/include/c.h:1113:10: note: in expansion of macro
‘MAXIMUM_ALIGNOF’
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
In file included from ../../../../src/include/postgres.h:48:0,
from test_cplusplusext.cpp:18:
../../../../src/include/c.h:1113:25: error: ISO C++ forbids
declaration of ‘alignas’ with no type [-fpermissive]
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
../../../../src/include/c.h:1113:25: error: expected ‘;’ at end of
member declaration
In file included from ../../../../src/include/c.h:58:0,
from ../../../../src/include/postgres.h:48,
from test_cplusplusext.cpp:18:
../../../../src/include/pg_config_manual.h:223:27: error: expected
identifier before numeric constant
#define PG_IO_ALIGN_SIZE 4096
^
../../../../src/include/c.h:1113:25: error: expected ‘;’ at end of
member declaration
In file included from ../../../../src/include/c.h:58:0,
from ../../../../src/include/postgres.h:48,
from test_cplusplusext.cpp:18:
../../../../src/include/pg_config_manual.h:223:27: error: expected
identifier before numeric constant
#define PG_IO_ALIGN_SIZE 4096
^
../../../../src/include/c.h:1126:10: note: in expansion of macro
‘PG_IO_ALIGN_SIZE’
alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
^
In file included from ../../../../src/include/postgres.h:48:0,
from test_cplusplusext.cpp:18:
../../../../src/include/c.h:1126:26: error: ISO C++ forbids
declaration of ‘alignas’ with no type [-fpermissive]
alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
^
../../../../src/include/c.h:1126:26: error: expected ‘;’ at end of
member declaration
In file included from ../../../../src/include/c.h:58:0,
from ../../../../src/include/postgres.h:48,
from test_cplusplusext.cpp:18:
../../../../src/include/pg_config_manual.h:223:27: error: expected
identifier before numeric constant
#define PG_IO_ALIGN_SIZE 4096
^
../../../../src/include/c.h:1132:10: note: in expansion of macro
‘PG_IO_ALIGN_SIZE’
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
../../../../src/include/pg_config_manual.h:223:27: error: expected ‘,’
or ‘...’ before numeric constant
#define PG_IO_ALIGN_SIZE 4096
^
../../../../src/include/c.h:1132:10: note: in expansion of macro
‘PG_IO_ALIGN_SIZE’
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
In file included from ../../../../src/include/postgres.h:48:0,
from test_cplusplusext.cpp:18:
../../../../src/include/c.h:1132:26: error: ISO C++ forbids
declaration of ‘alignas’ with no type [-fpermissive]
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
../../../../src/include/c.h:1132:26: error: expected ‘;’ at end of
member declaration
../../../../src/include/c.h:943:15: error: expected constructor,
destructor, or type conversion before ‘(’ token
static_assert(condition, errmessage)
^
../../../../src/include/fmgr.h:510:1: note: in expansion of macro
‘StaticAssertDecl’
StaticAssertDecl(sizeof(FMGR_ABI_EXTRA) <= sizeof(((Pg_abi_values *)
0)->abi_extra),
^
make[5]: *** [test_cplusplusext.o] Error 1
make[5]: Leaving directory
`/home/postgres/oss_postgres_misc/src/test/modules/test_cplusplusext'
make[4]: *** [checkprep] Error 2
make[4]: Leaving directory
`/home/postgres/oss_postgres_misc/src/test/modules/test_cplusplusext'
make[3]: *** [checkprep-test_cplusplusext-recurse] Error 2
On Wed Jan 21, 2026 at 4:27 AM CET, Peter Smith wrote:
'make check-world' is suddenly failing for me, apparently caused by
this new test.FYI - I am building in a 'Linux CentOS7-x64 ' environment, which I
know has reached end-of-life, but even so, everything was fine before
this test appeared.
I'm still not clear on what our policy is on supporting such EOL
distros. But this patch seems sensible regardless and it fixes
compilation for me when running on a centos 7 docker image.
Attachments:
v1-0001-Detect-if-flags-are-needed-for-C-11-support.patchtext/x-patch; charset=utf-8; name=v1-0001-Detect-if-flags-are-needed-for-C-11-support.patchDownload+110-3
On 21.01.26 09:50, Jelte Fennema-Nio wrote:
On Wed Jan 21, 2026 at 4:27 AM CET, Peter Smith wrote:
'make check-world' is suddenly failing for me, apparently caused by
this new test.FYI - I am building in a 'Linux CentOS7-x64 ' environment, which I
know has reached end-of-life, but even so, everything was fine before
this test appeared.I'm still not clear on what our policy is on supporting such EOL
distros. But this patch seems sensible regardless and it fixes
compilation for me when running on a centos 7 docker image.
Yes, I was just in the process of writing that same patch. I'll see if
there are any differences and then move ahead with this.
On Tue, Jan 20, 2026 at 10:43 AM Peter Eisentraut <peter@eisentraut.org> wrote:
tests: Add a test C++ extension module
This patch caused my usual build process to fail. I have been setting
CC='ccache clang' in my environment, and I was able to fix make it
work again by also setting CXX='ccache clang'. Before that, I was
getting my macPorts version of clang (21.1.2) for C source files but
the OS version of clang (15.0.0) for c++ files. But I still don't
really understand how that caused this failure, which doesn't look
like it has anything to do with mixing compilers:
[1/3] Compiling C++ object src/test/mo...plusext.dylib.p/test_cplusplusext.cpp.
FAILED: [code=1]
src/test/modules/test_cplusplusext/test_cplusplusext.dylib.p/test_cplusplusext.cpp.o
ccache c++ -Isrc/test/modules/test_cplusplusext/test_cplusplusext.dylib.p
-Isrc/include -I../pgsql/src/include -I/opt/local/include
-I/opt/local/include/libxml2 -I/opt/local/libexec/openssl3/include
-fdiagnostics-color=always -D_LIBCPP_ENABLE_ASSERTIONS=1 -Wall
-Winvalid-pch -O2 -g -fno-strict-aliasing -fwrapv -Wmissing-prototypes
-Wpointer-arith -Werror=vla -Werror=unguarded-availability-new
-Wendif-labels -Wmissing-format-attribute -Wcast-function-type
-Wformat-security -Wno-unused-command-line-argument
-Wno-compound-token-split-by-macro -fvisibility=hidden
-fvisibility=hidden -fvisibility-inlines-hidden -MD -MQ
src/test/modules/test_cplusplusext/test_cplusplusext.dylib.p/test_cplusplusext.cpp.o
-MF src/test/modules/test_cplusplusext/test_cplusplusext.dylib.p/test_cplusplusext.cpp.o.d
-o src/test/modules/test_cplusplusext/test_cplusplusext.dylib.p/test_cplusplusext.cpp.o
-c ../pgsql/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
In file included from
../pgsql/src/test/modules/test_cplusplusext/test_cplusplusext.cpp:18:
In file included from ../pgsql/src/include/postgres.h:48:
../pgsql/src/include/c.h:1113:10: error: expected parameter declarator
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
src/include/pg_config.h:342:25: note: expanded from macro 'MAXIMUM_ALIGNOF'
#define MAXIMUM_ALIGNOF 8
^
In file included from
../pgsql/src/test/modules/test_cplusplusext/test_cplusplusext.cpp:18:
In file included from ../pgsql/src/include/postgres.h:48:
../pgsql/src/include/c.h:1113:10: error: expected ')'
src/include/pg_config.h:342:25: note: expanded from macro 'MAXIMUM_ALIGNOF'
#define MAXIMUM_ALIGNOF 8
^
../pgsql/src/include/c.h:1113:9: note: to match this '('
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
../pgsql/src/include/c.h:1113:2: error: a type specifier is required
for all declarations
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
../pgsql/src/include/c.h:1113:26: error: expected ';' at end of declaration list
alignas(MAXIMUM_ALIGNOF) char data[BLCKSZ];
^
;
../pgsql/src/include/c.h:1126:10: error: expected parameter declarator
alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
^
../pgsql/src/include/pg_config_manual.h:223:27: note: expanded from
macro 'PG_IO_ALIGN_SIZE'
#define PG_IO_ALIGN_SIZE 4096
^
In file included from
../pgsql/src/test/modules/test_cplusplusext/test_cplusplusext.cpp:18:
In file included from ../pgsql/src/include/postgres.h:48:
../pgsql/src/include/c.h:1126:10: error: expected ')'
../pgsql/src/include/pg_config_manual.h:223:27: note: expanded from
macro 'PG_IO_ALIGN_SIZE'
#define PG_IO_ALIGN_SIZE 4096
^
../pgsql/src/include/c.h:1126:9: note: to match this '('
alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
^
../pgsql/src/include/c.h:1126:2: error: a type specifier is required
for all declarations
alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
^
../pgsql/src/include/c.h:1126:27: error: expected ';' at end of declaration list
alignas(PG_IO_ALIGN_SIZE) char data[BLCKSZ];
^
;
../pgsql/src/include/c.h:1132:10: error: expected parameter declarator
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
../pgsql/src/include/pg_config_manual.h:223:27: note: expanded from
macro 'PG_IO_ALIGN_SIZE'
#define PG_IO_ALIGN_SIZE 4096
^
In file included from
../pgsql/src/test/modules/test_cplusplusext/test_cplusplusext.cpp:18:
In file included from ../pgsql/src/include/postgres.h:48:
../pgsql/src/include/c.h:1132:10: error: expected ')'
../pgsql/src/include/pg_config_manual.h:223:27: note: expanded from
macro 'PG_IO_ALIGN_SIZE'
#define PG_IO_ALIGN_SIZE 4096
^
../pgsql/src/include/c.h:1132:9: note: to match this '('
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
../pgsql/src/include/c.h:1132:2: error: a type specifier is required
for all declarations
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
../pgsql/src/include/c.h:1132:27: error: expected ';' at end of declaration list
alignas(PG_IO_ALIGN_SIZE) char data[XLOG_BLCKSZ];
^
;
12 errors generated.
ninja: build stopped: subcommand failed.
--
Robert Haas
EDB: http://www.enterprisedb.com
Import Notes
Reply to msg id not found: E1viDt1-001d7E-2I@gemulon.postgresql.orgReference msg id not found: E1viDt1-001d7E-2I@gemulon.postgresql.org
On Wed, 21 Jan 2026 at 21:25, Robert Haas <robertmhaas@gmail.com> wrote:
On Tue, Jan 20, 2026 at 10:43 AM Peter Eisentraut <peter@eisentraut.org> wrote:
tests: Add a test C++ extension module
This patch caused my usual build process to fail. I have been setting
CC='ccache clang' in my environment, and I was able to fix make it
work again by also setting CXX='ccache clang'. Before that, I was
getting my macPorts version of clang (21.1.2) for C source files but
the OS version of clang (15.0.0) for c++ files. But I still don't
really understand how that caused this failure, which doesn't look
like it has anything to do with mixing compilers:
Seems like the same error as Peter Smith. So I guess clang 15 doesn't
use C++11 by default. Did you try if my proposed patch solves it?
Another way of fixing your problem would have been:
CXX='/path/to/os-version/clang -std=gnu++11'
Jelte Fennema-Nio <postgres@jeltef.nl> writes:
On Wed, 21 Jan 2026 at 21:25, Robert Haas <robertmhaas@gmail.com> wrote:
This patch caused my usual build process to fail. I have been setting
CC='ccache clang' in my environment, and I was able to fix make it
work again by also setting CXX='ccache clang'. Before that, I was
getting my macPorts version of clang (21.1.2) for C source files but
the OS version of clang (15.0.0) for c++ files. But I still don't
really understand how that caused this failure, which doesn't look
like it has anything to do with mixing compilers:
Seems like the same error as Peter Smith.
BF member sevengill is showing the identical error, but my
various macOS-based animals are OK. Maybe comparing their
configuration choices would yield insight.
regards, tom lane
On Wed, Jan 21, 2026 at 4:00 PM Jelte Fennema-Nio <postgres@jeltef.nl> wrote:
Seems like the same error as Peter Smith. So I guess clang 15 doesn't
use C++11 by default. Did you try if my proposed patch solves it?
Another way of fixing your problem would have been:
CXX='/path/to/os-version/clang -std=gnu++11'
Oh, I didn't realize that it was the same problem. I've now tested
your patch and I find that it makes the build work for me without
needing to set CXX. IOW, yes, it does fix the problem.
The worst part about this, IMHO, is that the error message was totally
impenetrable. But I guess we can't do much about that.
--
Robert Haas
EDB: http://www.enterprisedb.com
On Wed, Jan 21, 2026 at 7:50 PM Jelte Fennema-Nio <postgres@jeltef.nl> wrote:
On Wed Jan 21, 2026 at 4:27 AM CET, Peter Smith wrote:
'make check-world' is suddenly failing for me, apparently caused by
this new test.FYI - I am building in a 'Linux CentOS7-x64 ' environment, which I
know has reached end-of-life, but even so, everything was fine before
this test appeared.I'm still not clear on what our policy is on supporting such EOL
distros. But this patch seems sensible regardless and it fixes
compilation for me when running on a centos 7 docker image.
The patch fixed my problem. Thanks.
======
Kind Regards,
Peter Smith.
Fujitsu Australia