From b7d50a58dba4c780e144610a95e80b808cc963b6 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Wed, 5 Jun 2024 13:45:08 -0400
Subject: [PATCH v1] POC: Extension API for multithreading.

Calling this a POC is probably ambitious, but it illustrates one
possible approach to the problem: extensions can define
PG_THREADSAFE_EXTENSION before including any PostgreSQL headers other
than "postgres.h"; if they don't, they won't compile if
PG_MULTITHREADING was defined when building the server

Also, an extension built against a PG_MULTITHREADING PostgreSQL
cannot be loaded into one built without that #define, and the other
way around. Doing so will produce a complaint, similar to a library
version mismatch.
---
 contrib/amcheck/verify_nbtree.c               |  2 ++
 contrib/auth_delay/auth_delay.c               |  2 ++
 contrib/auto_explain/auto_explain.c           |  2 ++
 .../basebackup_to_shell/basebackup_to_shell.c |  2 ++
 contrib/basic_archive/basic_archive.c         |  2 ++
 contrib/bloom/blinsert.c                      |  2 ++
 contrib/bool_plperl/bool_plperl.c             |  2 ++
 contrib/btree_gin/btree_gin.c                 |  2 ++
 contrib/btree_gist/btree_gist.c               |  2 ++
 contrib/citext/citext.c                       |  2 ++
 contrib/cube/cube.c                           |  2 ++
 contrib/dblink/dblink.c                       |  2 ++
 contrib/dict_int/dict_int.c                   |  2 ++
 contrib/dict_xsyn/dict_xsyn.c                 |  2 ++
 contrib/earthdistance/earthdistance.c         |  2 ++
 contrib/file_fdw/file_fdw.c                   |  2 ++
 contrib/fuzzystrmatch/fuzzystrmatch.c         |  2 ++
 contrib/hstore/hstore_io.c                    |  2 ++
 contrib/hstore_plperl/hstore_plperl.c         |  2 ++
 contrib/hstore_plpython/hstore_plpython.c     |  2 ++
 contrib/intarray/_int_op.c                    |  2 ++
 contrib/isn/isn.c                             |  2 ++
 contrib/jsonb_plperl/jsonb_plperl.c           |  2 ++
 contrib/jsonb_plpython/jsonb_plpython.c       |  2 ++
 contrib/lo/lo.c                               |  2 ++
 contrib/ltree/ltree_op.c                      |  2 ++
 contrib/ltree_plpython/ltree_plpython.c       |  2 ++
 contrib/pageinspect/rawpage.c                 |  2 ++
 contrib/passwordcheck/passwordcheck.c         |  2 ++
 contrib/pg_buffercache/pg_buffercache_pages.c |  2 ++
 contrib/pg_freespacemap/pg_freespacemap.c     |  2 ++
 contrib/pg_prewarm/pg_prewarm.c               |  2 ++
 .../pg_stat_statements/pg_stat_statements.c   |  2 ++
 contrib/pg_surgery/heap_surgery.c             |  2 ++
 contrib/pg_trgm/trgm_op.c                     |  2 ++
 contrib/pg_visibility/pg_visibility.c         |  2 ++
 contrib/pg_walinspect/pg_walinspect.c         |  2 ++
 contrib/pgcrypto/pgcrypto.c                   |  2 ++
 contrib/pgrowlocks/pgrowlocks.c               |  2 ++
 contrib/pgstattuple/pgstattuple.c             |  2 ++
 contrib/postgres_fdw/postgres_fdw.c           |  2 ++
 contrib/seg/seg.c                             |  2 ++
 contrib/sepgsql/hooks.c                       |  2 ++
 contrib/spi/autoinc.c                         |  2 ++
 contrib/spi/insert_username.c                 |  2 ++
 contrib/spi/moddatetime.c                     |  2 ++
 contrib/spi/refint.c                          |  2 ++
 contrib/sslinfo/sslinfo.c                     |  2 ++
 contrib/tablefunc/tablefunc.c                 |  2 ++
 contrib/tcn/tcn.c                             |  2 ++
 contrib/test_decoding/test_decoding.c         |  2 ++
 contrib/tsm_system_rows/tsm_system_rows.c     |  2 ++
 contrib/tsm_system_time/tsm_system_time.c     |  2 ++
 contrib/unaccent/unaccent.c                   |  2 ++
 contrib/uuid-ossp/uuid-ossp.c                 |  2 ++
 contrib/xml2/xpath.c                          |  2 ++
 .../libpqwalreceiver/libpqwalreceiver.c       |  2 ++
 src/backend/replication/pgoutput/pgoutput.c   |  2 ++
 src/backend/snowball/dict_snowball.c          |  2 ++
 .../cyrillic_and_mic/cyrillic_and_mic.c       |  3 +++
 .../euc2004_sjis2004/euc2004_sjis2004.c       |  3 +++
 .../euc_cn_and_mic/euc_cn_and_mic.c           |  3 +++
 .../euc_jp_and_sjis/euc_jp_and_sjis.c         |  3 +++
 .../euc_kr_and_mic/euc_kr_and_mic.c           |  3 +++
 .../euc_tw_and_big5/euc_tw_and_big5.c         |  3 +++
 .../latin2_and_win1250/latin2_and_win1250.c   |  3 +++
 .../latin_and_mic/latin_and_mic.c             |  3 +++
 .../utf8_and_big5/utf8_and_big5.c             |  3 +++
 .../utf8_and_cyrillic/utf8_and_cyrillic.c     |  3 +++
 .../utf8_and_euc2004/utf8_and_euc2004.c       |  3 +++
 .../utf8_and_euc_cn/utf8_and_euc_cn.c         |  3 +++
 .../utf8_and_euc_jp/utf8_and_euc_jp.c         |  3 +++
 .../utf8_and_euc_kr/utf8_and_euc_kr.c         |  3 +++
 .../utf8_and_euc_tw/utf8_and_euc_tw.c         |  3 +++
 .../utf8_and_gb18030/utf8_and_gb18030.c       |  3 +++
 .../utf8_and_gbk/utf8_and_gbk.c               |  3 +++
 .../utf8_and_iso8859/utf8_and_iso8859.c       |  3 +++
 .../utf8_and_iso8859_1/utf8_and_iso8859_1.c   |  3 +++
 .../utf8_and_johab/utf8_and_johab.c           |  3 +++
 .../utf8_and_sjis/utf8_and_sjis.c             |  3 +++
 .../utf8_and_sjis2004/utf8_and_sjis2004.c     |  3 +++
 .../utf8_and_uhc/utf8_and_uhc.c               |  3 +++
 .../utf8_and_win/utf8_and_win.c               |  3 +++
 src/include/fmgr.h                            | 19 +++++++++++++++++++
 src/include/pg_config_manual.h                |  5 +++++
 src/pl/plperl/plperl.c                        |  1 +
 src/pl/plpgsql/src/pl_handler.c               |  2 ++
 src/pl/plpython/plpy_main.c                   |  2 ++
 src/pl/tcl/pltcl.c                            |  2 ++
 .../modules/delay_execution/delay_execution.c |  2 ++
 .../modules/dummy_index_am/dummy_index_am.c   |  2 ++
 .../modules/dummy_seclabel/dummy_seclabel.c   |  2 ++
 .../injection_points/injection_points.c       |  2 ++
 .../ldap_password_func/ldap_password_func.c   |  2 ++
 src/test/modules/plsample/plsample.c          |  2 ++
 .../modules/spgist_name_ops/spgist_name_ops.c |  2 ++
 .../ssl_passphrase_func.c                     |  2 ++
 .../test_bloomfilter/test_bloomfilter.c       |  2 ++
 .../test_copy_callbacks/test_copy_callbacks.c |  2 ++
 .../test_custom_rmgrs/test_custom_rmgrs.c     |  2 ++
 .../test_ddl_deparse/test_ddl_deparse.c       |  2 ++
 src/test/modules/test_dsa/test_dsa.c          |  2 ++
 .../test_dsm_registry/test_dsm_registry.c     |  2 ++
 .../test_ginpostinglist/test_ginpostinglist.c |  2 ++
 .../modules/test_integerset/test_integerset.c |  2 ++
 src/test/modules/test_lfind/test_lfind.c      |  2 ++
 .../modules/test_oat_hooks/test_oat_hooks.c   |  2 ++
 src/test/modules/test_parser/test_parser.c    |  2 ++
 .../modules/test_predtest/test_predtest.c     |  2 ++
 .../modules/test_radixtree/test_radixtree.c   |  2 ++
 src/test/modules/test_rbtree/test_rbtree.c    |  2 ++
 src/test/modules/test_regex/test_regex.c      |  2 ++
 .../test_resowner/test_resowner_basic.c       |  2 ++
 .../modules/test_rls_hooks/test_rls_hooks.c   |  2 ++
 src/test/modules/test_shm_mq/test.c           |  2 ++
 src/test/modules/test_slru/test_slru.c        |  2 ++
 .../modules/test_tidstore/test_tidstore.c     |  2 ++
 src/test/modules/worker_spi/worker_spi.c      |  2 ++
 .../modules/xid_wraparound/xid_wraparound.c   |  2 ++
 src/test/regress/regress.c                    |  2 ++
 120 files changed, 283 insertions(+)

diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c
index 70f65b645a..40b69b6b0e 100644
--- a/contrib/amcheck/verify_nbtree.c
+++ b/contrib/amcheck/verify_nbtree.c
@@ -23,6 +23,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/heaptoast.h"
 #include "access/htup_details.h"
 #include "access/nbtree.h"
diff --git a/contrib/auth_delay/auth_delay.c b/contrib/auth_delay/auth_delay.c
index ff0e1fd461..890c7cdf45 100644
--- a/contrib/auth_delay/auth_delay.c
+++ b/contrib/auth_delay/auth_delay.c
@@ -13,6 +13,8 @@
 
 #include <limits.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "libpq/auth.h"
 #include "port.h"
 #include "utils/guc.h"
diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c
index 677c135f59..b875e9d374 100644
--- a/contrib/auto_explain/auto_explain.c
+++ b/contrib/auto_explain/auto_explain.c
@@ -14,6 +14,8 @@
 
 #include <limits.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/parallel.h"
 #include "commands/explain.h"
 #include "common/pg_prng.h"
diff --git a/contrib/basebackup_to_shell/basebackup_to_shell.c b/contrib/basebackup_to_shell/basebackup_to_shell.c
index fe419d9080..0a956c20a2 100644
--- a/contrib/basebackup_to_shell/basebackup_to_shell.c
+++ b/contrib/basebackup_to_shell/basebackup_to_shell.c
@@ -10,6 +10,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/xact.h"
 #include "backup/basebackup_target.h"
 #include "common/percentrepl.h"
diff --git a/contrib/basic_archive/basic_archive.c b/contrib/basic_archive/basic_archive.c
index 028cf51c25..bfba902f71 100644
--- a/contrib/basic_archive/basic_archive.c
+++ b/contrib/basic_archive/basic_archive.c
@@ -30,6 +30,8 @@
 #include <sys/time.h>
 #include <unistd.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "archive/archive_module.h"
 #include "common/int.h"
 #include "miscadmin.h"
diff --git a/contrib/bloom/blinsert.c b/contrib/bloom/blinsert.c
index f8a1061abb..ce19e25ae0 100644
--- a/contrib/bloom/blinsert.c
+++ b/contrib/bloom/blinsert.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/genam.h"
 #include "access/generic_xlog.h"
 #include "access/tableam.h"
diff --git a/contrib/bool_plperl/bool_plperl.c b/contrib/bool_plperl/bool_plperl.c
index 0fa1eee8e5..5c4557da38 100644
--- a/contrib/bool_plperl/bool_plperl.c
+++ b/contrib/bool_plperl/bool_plperl.c
@@ -1,5 +1,7 @@
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "plperl.h"
 
diff --git a/contrib/btree_gin/btree_gin.c b/contrib/btree_gin/btree_gin.c
index 5e27906f80..9641cb4424 100644
--- a/contrib/btree_gin/btree_gin.c
+++ b/contrib/btree_gin/btree_gin.c
@@ -5,6 +5,8 @@
 
 #include <limits.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/stratnum.h"
 #include "utils/builtins.h"
 #include "utils/bytea.h"
diff --git a/contrib/btree_gist/btree_gist.c b/contrib/btree_gist/btree_gist.c
index 92520aedae..fb48808ae9 100644
--- a/contrib/btree_gist/btree_gist.c
+++ b/contrib/btree_gist/btree_gist.c
@@ -3,6 +3,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "utils/builtins.h"
 
 PG_MODULE_MAGIC;
diff --git a/contrib/citext/citext.c b/contrib/citext/citext.c
index 26af935a70..ce6a0a4ce9 100644
--- a/contrib/citext/citext.c
+++ b/contrib/citext/citext.c
@@ -3,6 +3,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/pg_collation.h"
 #include "common/hashfn.h"
 #include "utils/builtins.h"
diff --git a/contrib/cube/cube.c b/contrib/cube/cube.c
index 1fc447511a..ed7fb3672d 100644
--- a/contrib/cube/cube.c
+++ b/contrib/cube/cube.c
@@ -10,6 +10,8 @@
 
 #include <math.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/gist.h"
 #include "access/stratnum.h"
 #include "cubedata.h"
diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 755293456f..393e6cb827 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -34,6 +34,8 @@
 
 #include <limits.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "access/relation.h"
 #include "access/reloptions.h"
diff --git a/contrib/dict_int/dict_int.c b/contrib/dict_int/dict_int.c
index 82f3c60251..cdc43257b8 100644
--- a/contrib/dict_int/dict_int.c
+++ b/contrib/dict_int/dict_int.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/defrem.h"
 #include "tsearch/ts_public.h"
 
diff --git a/contrib/dict_xsyn/dict_xsyn.c b/contrib/dict_xsyn/dict_xsyn.c
index 62d2d84141..554c5c66a8 100644
--- a/contrib/dict_xsyn/dict_xsyn.c
+++ b/contrib/dict_xsyn/dict_xsyn.c
@@ -14,6 +14,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/defrem.h"
 #include "tsearch/ts_locale.h"
 #include "tsearch/ts_utils.h"
diff --git a/contrib/earthdistance/earthdistance.c b/contrib/earthdistance/earthdistance.c
index ded048c8ac..2e697460b8 100644
--- a/contrib/earthdistance/earthdistance.c
+++ b/contrib/earthdistance/earthdistance.c
@@ -4,6 +4,8 @@
 
 #include <math.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "utils/geo_decls.h"	/* for Point */
 
 /* X/Open (XSI) requires <math.h> to provide M_PI, but core POSIX does not */
diff --git a/contrib/file_fdw/file_fdw.c b/contrib/file_fdw/file_fdw.c
index 249d82d3a0..4f7807596b 100644
--- a/contrib/file_fdw/file_fdw.c
+++ b/contrib/file_fdw/file_fdw.c
@@ -15,6 +15,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "access/reloptions.h"
 #include "access/sysattr.h"
diff --git a/contrib/fuzzystrmatch/fuzzystrmatch.c b/contrib/fuzzystrmatch/fuzzystrmatch.c
index 72ae2ab91b..ae91a0abce 100644
--- a/contrib/fuzzystrmatch/fuzzystrmatch.c
+++ b/contrib/fuzzystrmatch/fuzzystrmatch.c
@@ -40,6 +40,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "mb/pg_wchar.h"
 #include "utils/builtins.h"
 #include "utils/varlena.h"
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index 999ddad76d..693e57a8df 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -5,6 +5,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "common/jsonapi.h"
diff --git a/contrib/hstore_plperl/hstore_plperl.c b/contrib/hstore_plperl/hstore_plperl.c
index 4a1629cad5..55f9951879 100644
--- a/contrib/hstore_plperl/hstore_plperl.c
+++ b/contrib/hstore_plperl/hstore_plperl.c
@@ -1,5 +1,7 @@
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "hstore/hstore.h"
 #include "plperl.h"
diff --git a/contrib/hstore_plpython/hstore_plpython.c b/contrib/hstore_plpython/hstore_plpython.c
index 310f63c30d..0aae12b512 100644
--- a/contrib/hstore_plpython/hstore_plpython.c
+++ b/contrib/hstore_plpython/hstore_plpython.c
@@ -1,5 +1,7 @@
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "hstore/hstore.h"
 #include "plpy_typeio.h"
diff --git a/contrib/intarray/_int_op.c b/contrib/intarray/_int_op.c
index 5b164f6788..fcadf10b38 100644
--- a/contrib/intarray/_int_op.c
+++ b/contrib/intarray/_int_op.c
@@ -3,6 +3,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "_int.h"
 
 PG_MODULE_MAGIC;
diff --git a/contrib/isn/isn.c b/contrib/isn/isn.c
index 71a543b947..dc2a67d894 100644
--- a/contrib/isn/isn.c
+++ b/contrib/isn/isn.c
@@ -14,6 +14,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "EAN13.h"
 #include "ISBN.h"
 #include "ISMN.h"
diff --git a/contrib/jsonb_plperl/jsonb_plperl.c b/contrib/jsonb_plperl/jsonb_plperl.c
index 2af1e0c02a..f5a62a194b 100644
--- a/contrib/jsonb_plperl/jsonb_plperl.c
+++ b/contrib/jsonb_plperl/jsonb_plperl.c
@@ -2,6 +2,8 @@
 
 #include <math.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "plperl.h"
 #include "utils/fmgrprotos.h"
diff --git a/contrib/jsonb_plpython/jsonb_plpython.c b/contrib/jsonb_plpython/jsonb_plpython.c
index a625727c5e..8e884dd08b 100644
--- a/contrib/jsonb_plpython/jsonb_plpython.c
+++ b/contrib/jsonb_plpython/jsonb_plpython.c
@@ -1,5 +1,7 @@
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "plpy_elog.h"
 #include "plpy_typeio.h"
 #include "plpython.h"
diff --git a/contrib/lo/lo.c b/contrib/lo/lo.c
index 457be26c4e..585cdc716a 100644
--- a/contrib/lo/lo.c
+++ b/contrib/lo/lo.c
@@ -7,6 +7,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/trigger.h"
 #include "executor/spi.h"
 #include "utils/builtins.h"
diff --git a/contrib/ltree/ltree_op.c b/contrib/ltree/ltree_op.c
index 24a21d3ea0..563b0f0d5e 100644
--- a/contrib/ltree/ltree_op.c
+++ b/contrib/ltree/ltree_op.c
@@ -7,6 +7,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_statistic.h"
 #include "common/hashfn.h"
diff --git a/contrib/ltree_plpython/ltree_plpython.c b/contrib/ltree_plpython/ltree_plpython.c
index ac159ea314..839c256af6 100644
--- a/contrib/ltree_plpython/ltree_plpython.c
+++ b/contrib/ltree_plpython/ltree_plpython.c
@@ -1,5 +1,7 @@
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "ltree/ltree.h"
 #include "plpython.h"
diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index 2800ebd62f..e55d49ff43 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -15,6 +15,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "access/relation.h"
 #include "catalog/namespace.h"
diff --git a/contrib/passwordcheck/passwordcheck.c b/contrib/passwordcheck/passwordcheck.c
index 0785618f2a..3bd0b7cd66 100644
--- a/contrib/passwordcheck/passwordcheck.c
+++ b/contrib/passwordcheck/passwordcheck.c
@@ -20,6 +20,8 @@
 #include <crack.h>
 #endif
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/user.h"
 #include "fmgr.h"
 #include "libpq/crypt.h"
diff --git a/contrib/pg_buffercache/pg_buffercache_pages.c b/contrib/pg_buffercache/pg_buffercache_pages.c
index 3ae0a018e1..9c9f5a70f9 100644
--- a/contrib/pg_buffercache/pg_buffercache_pages.c
+++ b/contrib/pg_buffercache/pg_buffercache_pages.c
@@ -8,6 +8,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "funcapi.h"
diff --git a/contrib/pg_freespacemap/pg_freespacemap.c b/contrib/pg_freespacemap/pg_freespacemap.c
index b82cab2d97..409db4dfce 100644
--- a/contrib/pg_freespacemap/pg_freespacemap.c
+++ b/contrib/pg_freespacemap/pg_freespacemap.c
@@ -8,6 +8,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/relation.h"
 #include "funcapi.h"
 #include "storage/freespace.h"
diff --git a/contrib/pg_prewarm/pg_prewarm.c b/contrib/pg_prewarm/pg_prewarm.c
index 5c859e983c..34b61e7299 100644
--- a/contrib/pg_prewarm/pg_prewarm.c
+++ b/contrib/pg_prewarm/pg_prewarm.c
@@ -15,6 +15,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/relation.h"
 #include "fmgr.h"
 #include "miscadmin.h"
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index d4197ae0f7..0939ae683a 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -47,6 +47,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/parallel.h"
 #include "catalog/pg_authid.h"
 #include "common/hashfn.h"
diff --git a/contrib/pg_surgery/heap_surgery.c b/contrib/pg_surgery/heap_surgery.c
index 37dffe3f7d..d483741b0b 100644
--- a/contrib/pg_surgery/heap_surgery.c
+++ b/contrib/pg_surgery/heap_surgery.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/heapam.h"
 #include "access/visibilitymap.h"
 #include "access/xloginsert.h"
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index c509d15ee4..b26ed4472e 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -5,6 +5,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/pg_type.h"
 #include "common/int.h"
 #include "lib/qunique.h"
diff --git a/contrib/pg_visibility/pg_visibility.c b/contrib/pg_visibility/pg_visibility.c
index 1a1a4ff7be..0a72592055 100644
--- a/contrib/pg_visibility/pg_visibility.c
+++ b/contrib/pg_visibility/pg_visibility.c
@@ -10,6 +10,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/heapam.h"
 #include "access/htup_details.h"
 #include "access/visibilitymap.h"
diff --git a/contrib/pg_walinspect/pg_walinspect.c b/contrib/pg_walinspect/pg_walinspect.c
index ee2918726d..a1f6e2cf18 100644
--- a/contrib/pg_walinspect/pg_walinspect.c
+++ b/contrib/pg_walinspect/pg_walinspect.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/xlog.h"
 #include "access/xlog_internal.h"
 #include "access/xlogreader.h"
diff --git a/contrib/pgcrypto/pgcrypto.c b/contrib/pgcrypto/pgcrypto.c
index 96447c5757..bf6bc3efdc 100644
--- a/contrib/pgcrypto/pgcrypto.c
+++ b/contrib/pgcrypto/pgcrypto.c
@@ -33,6 +33,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "parser/scansup.h"
 #include "pgcrypto.h"
 #include "px-crypt.h"
diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c
index adbc8279c3..4df8bc2618 100644
--- a/contrib/pgrowlocks/pgrowlocks.c
+++ b/contrib/pgrowlocks/pgrowlocks.c
@@ -24,6 +24,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/heapam.h"
 #include "access/multixact.h"
 #include "access/relscan.h"
diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c
index 3bd8b96197..c1ea8d3e5c 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -24,6 +24,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/gist_private.h"
 #include "access/hash.h"
 #include "access/heapam.h"
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 4053cd641c..6f447c2a40 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -14,6 +14,8 @@
 
 #include <limits.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "access/sysattr.h"
 #include "access/table.h"
diff --git a/contrib/seg/seg.c b/contrib/seg/seg.c
index 7f9fc24eb4..2326d700ae 100644
--- a/contrib/seg/seg.c
+++ b/contrib/seg/seg.c
@@ -12,6 +12,8 @@
 #include <float.h>
 #include <math.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/gist.h"
 #include "access/stratnum.h"
 #include "fmgr.h"
diff --git a/contrib/sepgsql/hooks.c b/contrib/sepgsql/hooks.c
index 0f206b1093..645616a27a 100644
--- a/contrib/sepgsql/hooks.c
+++ b/contrib/sepgsql/hooks.c
@@ -10,6 +10,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/dependency.h"
 #include "catalog/objectaccess.h"
 #include "catalog/pg_class.h"
diff --git a/contrib/spi/autoinc.c b/contrib/spi/autoinc.c
index 8bf742230e..4eaf12cfc7 100644
--- a/contrib/spi/autoinc.c
+++ b/contrib/spi/autoinc.c
@@ -3,6 +3,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "commands/sequence.h"
diff --git a/contrib/spi/insert_username.c b/contrib/spi/insert_username.c
index a2e1747ff7..2a7be76df3 100644
--- a/contrib/spi/insert_username.c
+++ b/contrib/spi/insert_username.c
@@ -6,6 +6,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "commands/trigger.h"
diff --git a/contrib/spi/moddatetime.c b/contrib/spi/moddatetime.c
index 3eb7004de9..a1cd5f92ba 100644
--- a/contrib/spi/moddatetime.c
+++ b/contrib/spi/moddatetime.c
@@ -15,6 +15,8 @@ OH, me, I'm Terry Mackintosh <terry@terrym.com>
 */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "commands/trigger.h"
diff --git a/contrib/spi/refint.c b/contrib/spi/refint.c
index 18062eb1cf..0d28ec5b39 100644
--- a/contrib/spi/refint.c
+++ b/contrib/spi/refint.c
@@ -9,6 +9,8 @@
 
 #include <ctype.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/trigger.h"
 #include "executor/spi.h"
 #include "utils/builtins.h"
diff --git a/contrib/sslinfo/sslinfo.c b/contrib/sslinfo/sslinfo.c
index 5fd46b9874..dce034f009 100644
--- a/contrib/sslinfo/sslinfo.c
+++ b/contrib/sslinfo/sslinfo.c
@@ -13,6 +13,8 @@
 #include <openssl/x509v3.h>
 #include <openssl/asn1.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "funcapi.h"
 #include "libpq/libpq-be.h"
diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c
index 7d1b5f5143..7ba5a73be5 100644
--- a/contrib/tablefunc/tablefunc.c
+++ b/contrib/tablefunc/tablefunc.c
@@ -34,6 +34,8 @@
 
 #include <math.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "common/pg_prng.h"
diff --git a/contrib/tcn/tcn.c b/contrib/tcn/tcn.c
index 8d23c824c1..342e46d022 100644
--- a/contrib/tcn/tcn.c
+++ b/contrib/tcn/tcn.c
@@ -15,6 +15,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "commands/async.h"
 #include "commands/trigger.h"
diff --git a/contrib/test_decoding/test_decoding.c b/contrib/test_decoding/test_decoding.c
index 7c50d13969..836bb1bc13 100644
--- a/contrib/test_decoding/test_decoding.c
+++ b/contrib/test_decoding/test_decoding.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/pg_type.h"
 
 #include "replication/logical.h"
diff --git a/contrib/tsm_system_rows/tsm_system_rows.c b/contrib/tsm_system_rows/tsm_system_rows.c
index a9f5d657d1..80a83e3ea9 100644
--- a/contrib/tsm_system_rows/tsm_system_rows.c
+++ b/contrib/tsm_system_rows/tsm_system_rows.c
@@ -28,6 +28,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/relscan.h"
 #include "access/tsmapi.h"
 #include "catalog/pg_type.h"
diff --git a/contrib/tsm_system_time/tsm_system_time.c b/contrib/tsm_system_time/tsm_system_time.c
index f187fd7765..71c52785e8 100644
--- a/contrib/tsm_system_time/tsm_system_time.c
+++ b/contrib/tsm_system_time/tsm_system_time.c
@@ -26,6 +26,8 @@
 
 #include <math.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/relscan.h"
 #include "access/tsmapi.h"
 #include "catalog/pg_type.h"
diff --git a/contrib/unaccent/unaccent.c b/contrib/unaccent/unaccent.c
index 707962305f..586c3be52f 100644
--- a/contrib/unaccent/unaccent.c
+++ b/contrib/unaccent/unaccent.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/namespace.h"
 #include "catalog/pg_ts_dict.h"
 #include "commands/defrem.h"
diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c
index 3172714eb6..bd182b9308 100644
--- a/contrib/uuid-ossp/uuid-ossp.c
+++ b/contrib/uuid-ossp/uuid-ossp.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "common/cryptohash.h"
 #include "common/sha1.h"
 #include "fmgr.h"
diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c
index b999b1f706..5deeb6e158 100644
--- a/contrib/xml2/xpath.c
+++ b/contrib/xml2/xpath.c
@@ -6,6 +6,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "executor/spi.h"
 #include "fmgr.h"
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 3c2b1bb496..58f9078d28 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -22,6 +22,8 @@
 #include <unistd.h>
 #include <sys/time.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "common/connect.h"
 #include "funcapi.h"
 #include "libpq-fe.h"
diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c
index d2b35cfb96..b5b6e61e4d 100644
--- a/src/backend/replication/pgoutput/pgoutput.c
+++ b/src/backend/replication/pgoutput/pgoutput.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/tupconvert.h"
 #include "catalog/partition.h"
 #include "catalog/pg_publication.h"
diff --git a/src/backend/snowball/dict_snowball.c b/src/backend/snowball/dict_snowball.c
index caf8649068..896fa69914 100644
--- a/src/backend/snowball/dict_snowball.c
+++ b/src/backend/snowball/dict_snowball.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/defrem.h"
 #include "tsearch/ts_locale.h"
 #include "tsearch/ts_public.h"
diff --git a/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c b/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
index aa61902a82..bce5c2733d 100644
--- a/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c b/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
index 8f5fa90070..5fce0e5beb 100644
--- a/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
+++ b/src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
@@ -11,6 +11,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c b/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
index 9551761a75..7872fa78ae 100644
--- a/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c b/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
index daf6eac974..a1378d80df 100644
--- a/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
+++ b/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c b/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
index 631766898f..88aa2c32a0 100644
--- a/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
index ed71678c98..119f014a59 100644
--- a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
+++ b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c b/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
index a2f6c3f723..ae558ee337 100644
--- a/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
+++ b/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c b/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
index e00aa38228..099aa6dd0e 100644
--- a/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
+++ b/src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c b/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
index 62643aaa29..c43489a106 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/big5_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c b/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
index 9672954c8b..507be3803c 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/utf8_to_koi8r.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c
index 25bc45cf37..2fded86f96 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/euc_jis_2004_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
index 3f5d2f3cb7..28c22a5ab1 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/euc_cn_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
index 837e7e98e9..0ca4a70d00 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/euc_jp_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
index 7ad7f26397..9ad51945a1 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/euc_kr_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
index 9e56f1fe24..2b948b0cf1 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/euc_tw_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c b/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
index c64c6d2b31..148f531af4 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/gb18030_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c b/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
index 696d741e91..87a93eebc5 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/gbk_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
index 20558de029..be35218485 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/iso8859_10_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
index 45d1504098..79c7557c84 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c b/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
index a0c13ae228..0f10596b18 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/johab_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c b/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
index e619fd932d..b9cbad0b29 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/sjis_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c b/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
index 5078475762..62346e9ab7 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/shift_jis_2004_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c b/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
index d6475c2197..4bced4818f 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/uhc_to_utf8.map"
diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c b/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
index e813d894f9..b903f6f274 100644
--- a/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
+++ b/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
@@ -12,6 +12,9 @@
  */
 
 #include "postgres.h"
+
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "mb/pg_wchar.h"
 #include "../../Unicode/utf8_to_win1250.map"
diff --git a/src/include/fmgr.h b/src/include/fmgr.h
index ccb4070a25..a6c61fa8bd 100644
--- a/src/include/fmgr.h
+++ b/src/include/fmgr.h
@@ -18,6 +18,8 @@
 #ifndef FMGR_H
 #define FMGR_H
 
+#include "pg_config_manual.h"
+
 /* We don't want to include primnodes.h here, so make some stub references */
 typedef struct Node *fmNodePtr;
 typedef struct Aggref *fmAggrefPtr;
@@ -465,6 +467,7 @@ typedef struct
 {
 	int			len;			/* sizeof(this struct) */
 	int			version;		/* PostgreSQL major version */
+	int			threading;		/* 1 if PG_MULTITHREADING, else 0 */
 	int			funcmaxargs;	/* FUNC_MAX_ARGS */
 	int			indexmaxkeys;	/* INDEX_MAX_KEYS */
 	int			namedatalen;	/* NAMEDATALEN */
@@ -472,11 +475,26 @@ typedef struct
 	char		abi_extra[32];	/* see pg_config_manual.h */
 } Pg_magic_struct;
 
+#ifdef PG_MULTITHREADING
+#define PG_MULTITHREADING_VALUE	1
+#else
+#define PG_MULTITHREADING_VALUE 0
+#endif
+
+#ifdef PG_THREADSAFE_EXTENSION
+#define PG_CHECK_THREAD_SAFETY
+#else
+#define PG_CHECK_THREAD_SAFETY \
+	StaticAssertStmt(PG_MULTITHREADING_VALUE == 0, \
+					 "must define PG_THREADSAFE_EXTENSION or use unthreaded PostgreSQL")
+#endif
+
 /* The actual data block contents */
 #define PG_MODULE_MAGIC_DATA \
 { \
 	sizeof(Pg_magic_struct), \
 	PG_VERSION_NUM / 100, \
+	PG_MULTITHREADING_VALUE, \
 	FUNC_MAX_ARGS, \
 	INDEX_MAX_KEYS, \
 	NAMEDATALEN, \
@@ -502,6 +520,7 @@ const Pg_magic_struct * \
 PG_MAGIC_FUNCTION_NAME(void) \
 { \
 	static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
+	PG_CHECK_THREAD_SAFETY; \
 	return &Pg_magic_data; \
 } \
 extern int no_such_variable
diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index f941ee2faf..e7e4e92037 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -379,6 +379,11 @@
  */
 #define TRACE_SORT 1
 
+/*
+ * Enable multithreading support (works great, no bugs at all!).
+ */
+/* #define PG_MULTITHREADING 1 */
+
 /*
  * Enable tracing of syncscan operations (see also the trace_syncscan GUC var).
  */
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c
index d68ad7be34..91fe7bfa34 100644
--- a/src/pl/plperl/plperl.c
+++ b/src/pl/plperl/plperl.c
@@ -14,6 +14,7 @@
 #include <unistd.h>
 
 /* postgreSQL stuff */
+#define PG_THREADSAFE_EXTENSION		1
 #include "access/htup_details.h"
 #include "access/xact.h"
 #include "catalog/pg_language.h"
diff --git a/src/pl/plpgsql/src/pl_handler.c b/src/pl/plpgsql/src/pl_handler.c
index fce459ade0..fddbdfd060 100644
--- a/src/pl/plpgsql/src/pl_handler.c
+++ b/src/pl/plpgsql/src/pl_handler.c
@@ -15,6 +15,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c
index 010a97378c..a6618ce0e6 100644
--- a/src/pl/plpython/plpy_main.c
+++ b/src/pl/plpython/plpy_main.c
@@ -6,6 +6,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 18d14a2b98..5140c5c866 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -13,6 +13,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "access/xact.h"
 #include "catalog/objectaccess.h"
diff --git a/src/test/modules/delay_execution/delay_execution.c b/src/test/modules/delay_execution/delay_execution.c
index 155c8a8d55..745356bd5a 100644
--- a/src/test/modules/delay_execution/delay_execution.c
+++ b/src/test/modules/delay_execution/delay_execution.c
@@ -22,6 +22,8 @@
 
 #include <limits.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "optimizer/planner.h"
 #include "utils/builtins.h"
 #include "utils/guc.h"
diff --git a/src/test/modules/dummy_index_am/dummy_index_am.c b/src/test/modules/dummy_index_am/dummy_index_am.c
index 18185d0206..22177538cd 100644
--- a/src/test/modules/dummy_index_am/dummy_index_am.c
+++ b/src/test/modules/dummy_index_am/dummy_index_am.c
@@ -13,6 +13,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/amapi.h"
 #include "access/reloptions.h"
 #include "catalog/index.h"
diff --git a/src/test/modules/dummy_seclabel/dummy_seclabel.c b/src/test/modules/dummy_seclabel/dummy_seclabel.c
index aded20e1be..e5de0ea94d 100644
--- a/src/test/modules/dummy_seclabel/dummy_seclabel.c
+++ b/src/test/modules/dummy_seclabel/dummy_seclabel.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "commands/seclabel.h"
 #include "fmgr.h"
 #include "miscadmin.h"
diff --git a/src/test/modules/injection_points/injection_points.c b/src/test/modules/injection_points/injection_points.c
index 5c44625d1d..ca134fb364 100644
--- a/src/test/modules/injection_points/injection_points.c
+++ b/src/test/modules/injection_points/injection_points.c
@@ -17,6 +17,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "miscadmin.h"
 #include "nodes/pg_list.h"
diff --git a/src/test/modules/ldap_password_func/ldap_password_func.c b/src/test/modules/ldap_password_func/ldap_password_func.c
index 99c18a8f1c..964335eb43 100644
--- a/src/test/modules/ldap_password_func/ldap_password_func.c
+++ b/src/test/modules/ldap_password_func/ldap_password_func.c
@@ -15,6 +15,8 @@
 #include <float.h>
 #include <stdio.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "libpq/auth.h"
 #include "libpq/libpq.h"
 #include "libpq/libpq-be.h"
diff --git a/src/test/modules/plsample/plsample.c b/src/test/modules/plsample/plsample.c
index 40c462e84e..c95a59387c 100644
--- a/src/test/modules/plsample/plsample.c
+++ b/src/test/modules/plsample/plsample.c
@@ -15,6 +15,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
 #include "commands/event_trigger.h"
diff --git a/src/test/modules/spgist_name_ops/spgist_name_ops.c b/src/test/modules/spgist_name_ops/spgist_name_ops.c
index 322881ba78..99490be3eb 100644
--- a/src/test/modules/spgist_name_ops/spgist_name_ops.c
+++ b/src/test/modules/spgist_name_ops/spgist_name_ops.c
@@ -21,6 +21,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/spgist.h"
 #include "catalog/pg_type.h"
 #include "utils/datum.h"
diff --git a/src/test/modules/ssl_passphrase_callback/ssl_passphrase_func.c b/src/test/modules/ssl_passphrase_callback/ssl_passphrase_func.c
index d599214982..0e3f38e2a6 100644
--- a/src/test/modules/ssl_passphrase_callback/ssl_passphrase_func.c
+++ b/src/test/modules/ssl_passphrase_callback/ssl_passphrase_func.c
@@ -14,6 +14,8 @@
 #include <float.h>
 #include <stdio.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "libpq/libpq.h"
 #include "libpq/libpq-be.h"
 #include "utils/guc.h"
diff --git a/src/test/modules/test_bloomfilter/test_bloomfilter.c b/src/test/modules/test_bloomfilter/test_bloomfilter.c
index b0633a8fa7..992930697a 100644
--- a/src/test/modules/test_bloomfilter/test_bloomfilter.c
+++ b/src/test/modules/test_bloomfilter/test_bloomfilter.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "common/pg_prng.h"
 #include "fmgr.h"
 #include "lib/bloomfilter.h"
diff --git a/src/test/modules/test_copy_callbacks/test_copy_callbacks.c b/src/test/modules/test_copy_callbacks/test_copy_callbacks.c
index 0bbd2aa6bb..b465fd1dca 100644
--- a/src/test/modules/test_copy_callbacks/test_copy_callbacks.c
+++ b/src/test/modules/test_copy_callbacks/test_copy_callbacks.c
@@ -14,6 +14,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/table.h"
 #include "commands/copy.h"
 #include "fmgr.h"
diff --git a/src/test/modules/test_custom_rmgrs/test_custom_rmgrs.c b/src/test/modules/test_custom_rmgrs/test_custom_rmgrs.c
index 85038f549d..94308c9a30 100644
--- a/src/test/modules/test_custom_rmgrs/test_custom_rmgrs.c
+++ b/src/test/modules/test_custom_rmgrs/test_custom_rmgrs.c
@@ -17,6 +17,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/xlog.h"
 #include "access/xlog_internal.h"
 #include "access/xloginsert.h"
diff --git a/src/test/modules/test_ddl_deparse/test_ddl_deparse.c b/src/test/modules/test_ddl_deparse/test_ddl_deparse.c
index 67ff2b6367..544b498dc6 100644
--- a/src/test/modules/test_ddl_deparse/test_ddl_deparse.c
+++ b/src/test/modules/test_ddl_deparse/test_ddl_deparse.c
@@ -10,6 +10,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/pg_type.h"
 #include "funcapi.h"
 #include "nodes/execnodes.h"
diff --git a/src/test/modules/test_dsa/test_dsa.c b/src/test/modules/test_dsa/test_dsa.c
index c78af68f61..2142396c74 100644
--- a/src/test/modules/test_dsa/test_dsa.c
+++ b/src/test/modules/test_dsa/test_dsa.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "storage/lwlock.h"
 #include "utils/dsa.h"
diff --git a/src/test/modules/test_dsm_registry/test_dsm_registry.c b/src/test/modules/test_dsm_registry/test_dsm_registry.c
index 96eaa850bf..e05ffdbcd7 100644
--- a/src/test/modules/test_dsm_registry/test_dsm_registry.c
+++ b/src/test/modules/test_dsm_registry/test_dsm_registry.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "storage/dsm_registry.h"
 #include "storage/lwlock.h"
diff --git a/src/test/modules/test_ginpostinglist/test_ginpostinglist.c b/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
index 04215cadd9..24f5a1009d 100644
--- a/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
+++ b/src/test/modules/test_ginpostinglist/test_ginpostinglist.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/gin_private.h"
 #include "access/ginblock.h"
 #include "access/htup_details.h"
diff --git a/src/test/modules/test_integerset/test_integerset.c b/src/test/modules/test_integerset/test_integerset.c
index 61f8d0b663..0331c40ba2 100644
--- a/src/test/modules/test_integerset/test_integerset.c
+++ b/src/test/modules/test_integerset/test_integerset.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "common/pg_prng.h"
 #include "fmgr.h"
 #include "lib/integerset.h"
diff --git a/src/test/modules/test_lfind/test_lfind.c b/src/test/modules/test_lfind/test_lfind.c
index c04bc2f6b4..92266e644f 100644
--- a/src/test/modules/test_lfind/test_lfind.c
+++ b/src/test/modules/test_lfind/test_lfind.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "port/pg_lfind.h"
 
diff --git a/src/test/modules/test_oat_hooks/test_oat_hooks.c b/src/test/modules/test_oat_hooks/test_oat_hooks.c
index 05c3371225..8834a9f215 100644
--- a/src/test/modules/test_oat_hooks/test_oat_hooks.c
+++ b/src/test/modules/test_oat_hooks/test_oat_hooks.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/parallel.h"
 #include "catalog/dependency.h"
 #include "catalog/objectaccess.h"
diff --git a/src/test/modules/test_parser/test_parser.c b/src/test/modules/test_parser/test_parser.c
index f3de7d8abf..dedfb11187 100644
--- a/src/test/modules/test_parser/test_parser.c
+++ b/src/test/modules/test_parser/test_parser.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 
 PG_MODULE_MAGIC;
diff --git a/src/test/modules/test_predtest/test_predtest.c b/src/test/modules/test_predtest/test_predtest.c
index eaf006c649..4beef8fa49 100644
--- a/src/test/modules/test_predtest/test_predtest.c
+++ b/src/test/modules/test_predtest/test_predtest.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
 #include "executor/spi.h"
diff --git a/src/test/modules/test_radixtree/test_radixtree.c b/src/test/modules/test_radixtree/test_radixtree.c
index 1d9165a3a2..3e79bf14ab 100644
--- a/src/test/modules/test_radixtree/test_radixtree.c
+++ b/src/test/modules/test_radixtree/test_radixtree.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "common/int.h"
 #include "common/pg_prng.h"
 #include "fmgr.h"
diff --git a/src/test/modules/test_rbtree/test_rbtree.c b/src/test/modules/test_rbtree/test_rbtree.c
index 3e76d27bf1..8234724bb5 100644
--- a/src/test/modules/test_rbtree/test_rbtree.c
+++ b/src/test/modules/test_rbtree/test_rbtree.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "common/pg_prng.h"
 #include "fmgr.h"
 #include "lib/rbtree.h"
diff --git a/src/test/modules/test_regex/test_regex.c b/src/test/modules/test_regex/test_regex.c
index c4ef15d742..5ce8d1b30a 100644
--- a/src/test/modules/test_regex/test_regex.c
+++ b/src/test/modules/test_regex/test_regex.c
@@ -14,6 +14,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "funcapi.h"
 #include "miscadmin.h"
 #include "regex/regex.h"
diff --git a/src/test/modules/test_resowner/test_resowner_basic.c b/src/test/modules/test_resowner/test_resowner_basic.c
index 558cf1de56..664cfa8ffc 100644
--- a/src/test/modules/test_resowner/test_resowner_basic.c
+++ b/src/test/modules/test_resowner/test_resowner_basic.c
@@ -12,6 +12,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "lib/ilist.h"
 #include "utils/memutils.h"
diff --git a/src/test/modules/test_rls_hooks/test_rls_hooks.c b/src/test/modules/test_rls_hooks/test_rls_hooks.c
index 358e28edb4..c123731941 100644
--- a/src/test/modules/test_rls_hooks/test_rls_hooks.c
+++ b/src/test/modules/test_rls_hooks/test_rls_hooks.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "catalog/pg_type.h"
 #include "fmgr.h"
 #include "miscadmin.h"
diff --git a/src/test/modules/test_shm_mq/test.c b/src/test/modules/test_shm_mq/test.c
index 3d235568b8..591b7dcebd 100644
--- a/src/test/modules/test_shm_mq/test.c
+++ b/src/test/modules/test_shm_mq/test.c
@@ -13,6 +13,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "fmgr.h"
 #include "miscadmin.h"
 #include "pgstat.h"
diff --git a/src/test/modules/test_slru/test_slru.c b/src/test/modules/test_slru/test_slru.c
index d227b06703..e73a11e202 100644
--- a/src/test/modules/test_slru/test_slru.c
+++ b/src/test/modules/test_slru/test_slru.c
@@ -14,6 +14,8 @@
 
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/slru.h"
 #include "access/transam.h"
 #include "miscadmin.h"
diff --git a/src/test/modules/test_tidstore/test_tidstore.c b/src/test/modules/test_tidstore/test_tidstore.c
index 5417163407..2a8db7cf39 100644
--- a/src/test/modules/test_tidstore/test_tidstore.c
+++ b/src/test/modules/test_tidstore/test_tidstore.c
@@ -16,6 +16,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/tidstore.h"
 #include "fmgr.h"
 #include "funcapi.h"
diff --git a/src/test/modules/worker_spi/worker_spi.c b/src/test/modules/worker_spi/worker_spi.c
index 7e1042f4ab..68d6b6c6f4 100644
--- a/src/test/modules/worker_spi/worker_spi.c
+++ b/src/test/modules/worker_spi/worker_spi.c
@@ -22,6 +22,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 /* These are always necessary for a bgworker */
 #include "miscadmin.h"
 #include "postmaster/bgworker.h"
diff --git a/src/test/modules/xid_wraparound/xid_wraparound.c b/src/test/modules/xid_wraparound/xid_wraparound.c
index dce81c0c6d..740a39df5c 100644
--- a/src/test/modules/xid_wraparound/xid_wraparound.c
+++ b/src/test/modules/xid_wraparound/xid_wraparound.c
@@ -14,6 +14,8 @@
  */
 #include "postgres.h"
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/xact.h"
 #include "miscadmin.h"
 #include "storage/proc.h"
diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c
index deffaea578..9bbe88cabb 100644
--- a/src/test/regress/regress.c
+++ b/src/test/regress/regress.c
@@ -19,6 +19,8 @@
 #include <math.h>
 #include <signal.h>
 
+#define PG_THREADSAFE_EXTENSION		1
+
 #include "access/detoast.h"
 #include "access/htup_details.h"
 #include "access/transam.h"
-- 
2.39.3 (Apple Git-145)

