Restore proper linkage of pg_char_to_encoding() and friends.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 7 Oct 2023 16:08:10 +0000 (12:08 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 7 Oct 2023 16:08:10 +0000 (12:08 -0400)
commitb6c7cfac88c47a9194d76f3d074129da3c46545a
treec5237e88f7349b2bb5b2be16466f148f6ae384d4
parente8c334c47abb6c8f648cb50241c2cd65c9e4e6dc
Restore proper linkage of pg_char_to_encoding() and friends.

Back in the 8.3 era we discovered that it was problematic if
libpq.so had encoding ID assignments different from the backend,
which is possible because on some platforms libpq.so might be
of a different major version from the calling programs.
psql should use libpq's assignments, but initdb has to use the
backend's, else it will put wrong values into pg_database.
The solution devised in commit 8468146b0 relied on giving initdb
its own copy of encnames.c rather than relying on the functions
exported by libpq.  Later, that metamorphosed into ensuring that
libpgcommon got linked before libpq -- which made things OK for
initdb but broke psql.  We didn't notice for lack of any changes
in enum pg_enc since then.  Commit 06843df4a reversed that, fixing
the latent bug in psql but adding one in initdb.  The meson build
infrastructure is also not being sufficiently careful about link
order, and trying to make it so would be equally fragile.

Hence, let's use a new scheme based on giving the libpq-exported
symbols different real names than the same functions exported from
libpgcommon.a or libpgcommon_srv.a.  (We could distinguish those
two cases as well, but there seems no need to.)  libpq gets the
official names to avoid an ABI break for libpq clients, while the
other cases use #define's to make the real names "xxx_private"
rather than "xxx".  By controlling where the #define's are
applied, we can force any particular client program to use one
set or the other of the encnames.c functions.

We cannot back-patch this, since it'd be an ABI break for backend
loadable modules, but there seems little need to.  We're just
trying to ensure that the world is safe for hypothetical future
additions to enum pg_enc.

In passing this should fix "duplicate symbol" linker warnings
that we've been seeing on AIX buildfarm members since commit
06843df4a.  It's not very clear why that linker is complaining
now, when there were strictly *more* duplicates visible before,
but in any case this should remove the reason for complaint.

Patch by me; thanks to Andres Freund for review.

Discussion: https://postgr.es/m/2385119.1696354473@sss.pgh.pa.us
src/bin/initdb/Makefile
src/bin/initdb/meson.build
src/common/Makefile
src/common/meson.build
src/include/mb/pg_wchar.h