Remove circular #include's between plpython.h and plpy_util.h.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 27 Apr 2025 15:43:02 +0000 (11:43 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 27 Apr 2025 15:43:02 +0000 (11:43 -0400)
plpython.h included plpy_util.h, simply on the grounds that "it's
easier to just include it everywhere".  However, plpy_util.h must
include plpython.h, or it won't pass headerscheck.  While the
resulting circularity doesn't have any immediate bad effect,
it's poor design.  We have seen serious messes arise in the past
from overly-broad inclusion footprints created by such circularities,
so let's establish a project policy against it.

To fix, just replace *.c files' inclusions of plpython.h with
plpy_util.h.  They'll pull in plpython.h indirectly; indeed, almost
all have already done so via inclusions of other plpy_xxx.h headers.
(Any extensions using plpython.h can do likewise without breaking
the compatibility of their code with prior Postgres versions.)

Reported-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Discussion: https://postgr.es/m/aAxQ6fcY5QQV1lo3@ip-10-97-1-34.eu-west-3.compute.internal

16 files changed:
contrib/hstore_plpython/hstore_plpython.c
contrib/jsonb_plpython/jsonb_plpython.c
contrib/ltree_plpython/ltree_plpython.c
src/pl/plpython/plpy_cursorobject.c
src/pl/plpython/plpy_elog.c
src/pl/plpython/plpy_exec.c
src/pl/plpython/plpy_main.c
src/pl/plpython/plpy_planobject.c
src/pl/plpython/plpy_plpymodule.c
src/pl/plpython/plpy_procedure.c
src/pl/plpython/plpy_resultobject.c
src/pl/plpython/plpy_spi.c
src/pl/plpython/plpy_subxactobject.c
src/pl/plpython/plpy_typeio.c
src/pl/plpython/plpy_util.c
src/pl/plpython/plpython.h

index 8812fb3f3e445c24e861940a3583ac17fd74ea60..e2bfc6da38e18230cef9f62b33783786090bf5cb 100644 (file)
@@ -3,7 +3,7 @@
 #include "fmgr.h"
 #include "hstore/hstore.h"
 #include "plpy_typeio.h"
-#include "plpython.h"
+#include "plpy_util.h"
 
 PG_MODULE_MAGIC_EXT(
                    .name = "hstore_plpython",
index 680445a006fb12dd73f621812add4167c6918e01..9383615abbfa32ae230bd7da5d3b212bbcff6154 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "plpy_elog.h"
 #include "plpy_typeio.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/fmgrprotos.h"
 #include "utils/jsonb.h"
 #include "utils/numeric.h"
index ba5926b8be6788a2358c1d03312b03dc2cfdfb70..0493aeb2423cc9ad6accff299b8ac4cae68b40ec 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "fmgr.h"
 #include "ltree/ltree.h"
-#include "plpython.h"
+#include "plpy_util.h"
 
 PG_MODULE_MAGIC_EXT(
                    .name = "ltree_plpython",
index 1c6be7561200746ea9e5acbfb62efa414d18c875..37d7efca77ce5cbaf34c231e1d45437cf1ea9c58 100644 (file)
@@ -16,7 +16,7 @@
 #include "plpy_planobject.h"
 #include "plpy_resultobject.h"
 #include "plpy_spi.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/memutils.h"
 
 static PyObject *PLy_cursor_query(const char *query);
index 70de5ba13d74b28ceb159b6f203433d352ad5139..ddf3573f0e70b8f389b883864f831f75e1cb0c2f 100644 (file)
@@ -10,7 +10,7 @@
 #include "plpy_elog.h"
 #include "plpy_main.h"
 #include "plpy_procedure.h"
-#include "plpython.h"
+#include "plpy_util.h"
 
 PyObject   *PLy_exc_error = NULL;
 PyObject   *PLy_exc_fatal = NULL;
index 00747bb811b551a0f8354fd3629b71c5c4f82061..28fbd443b98c9a52799318920e16c9c9dcc09894 100644 (file)
@@ -17,7 +17,7 @@
 #include "plpy_main.h"
 #include "plpy_procedure.h"
 #include "plpy_subxactobject.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/fmgrprotos.h"
 #include "utils/rel.h"
 
index 8f56155f006c9fdb28b29a41122316c814a828ca..f36eadbadc66d7835f7d2a47104c9b48844f6c10 100644 (file)
@@ -18,7 +18,7 @@
 #include "plpy_plpymodule.h"
 #include "plpy_procedure.h"
 #include "plpy_subxactobject.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
index 3e385555e5e08d2bdc917e1b2dbdffeea6a229be..6044893afdd1373456ea1f88484e1e2a982b62d3 100644 (file)
@@ -9,7 +9,7 @@
 #include "plpy_cursorobject.h"
 #include "plpy_planobject.h"
 #include "plpy_spi.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/memutils.h"
 
 static void PLy_plan_dealloc(PLyPlanObject *self);
index ea06d9a52b10856bf09df732b5c144cab17ab67e..1f980b44b2a31a24fcb1788d81f4b95fcc4d96f6 100644 (file)
@@ -14,7 +14,7 @@
 #include "plpy_resultobject.h"
 #include "plpy_spi.h"
 #include "plpy_subxactobject.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/builtins.h"
 
 HTAB      *PLy_spi_exceptions = NULL;
index b494eeb474f36e0f2d51747b0564fe299eb88e48..c176d24e801183c1bd4b7c688be13bd0df9911eb 100644 (file)
@@ -13,7 +13,7 @@
 #include "plpy_elog.h"
 #include "plpy_main.h"
 #include "plpy_procedure.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/builtins.h"
 #include "utils/hsearch.h"
 #include "utils/memutils.h"
index 80fa1d7accc74729b66bf58300d35b39b5387337..0d9997cbaa32cd0872bd28d593236a537a38f948 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "plpy_elog.h"
 #include "plpy_resultobject.h"
-#include "plpython.h"
+#include "plpy_util.h"
 
 static void PLy_result_dealloc(PLyResultObject *self);
 static PyObject *PLy_result_colnames(PyObject *self, PyObject *unused);
index 77fbfd6c86819852e1cbbae33e4a9abc996eca9b..1e386aadcca2bc4de841ee21ce51b873e7ba3acf 100644 (file)
@@ -19,7 +19,7 @@
 #include "plpy_plpymodule.h"
 #include "plpy_resultobject.h"
 #include "plpy_spi.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/memutils.h"
 
 static PyObject *PLy_spi_execute_query(char *query, long limit);
index ad24a9fc4b8c680de06ac4d279d87ec2ebf08312..c2484a99b4ae31dd31a215d957de18d28b3702a0 100644 (file)
@@ -9,7 +9,7 @@
 #include "access/xact.h"
 #include "plpy_elog.h"
 #include "plpy_subxactobject.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/memutils.h"
 
 List      *explicit_subtransactions = NIL;
index 1d127ae3ffe37c7c697f4344d6854459523ba22e..f6509a419028208793ad783b37cc44e0ffd4dd9a 100644 (file)
@@ -14,7 +14,7 @@
 #include "plpy_elog.h"
 #include "plpy_main.h"
 #include "plpy_typeio.h"
-#include "plpython.h"
+#include "plpy_util.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
index 6d89b1cb60a659b3f0ec794ec988a8870f315d89..ef710aa371eac33861cd47ad8e9258732b5b56f2 100644 (file)
@@ -9,7 +9,6 @@
 #include "mb/pg_wchar.h"
 #include "plpy_elog.h"
 #include "plpy_util.h"
-#include "plpython.h"
 
 /*
  * Convert a Python unicode object to a Python string/bytes object in
index 06fc1a5440fef09e6bc40565bcfd25b61639535b..118b3100840f6934fdd9eef1df82594ae4209d6f 100644 (file)
@@ -2,6 +2,10 @@
  *
  * plpython.h - Python as a procedural language for PostgreSQL
  *
+ * Note: this file is #include'd by each of the sub-module header files
+ * (plpy_elog.h, etc).  It's therefore unnecessary for any plpython *.c
+ * files to include it directly.
+ *
  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
@@ -38,9 +42,4 @@
 #undef TEXTDOMAIN
 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
 
-/*
- * Used throughout, so it's easier to just include it everywhere.
- */
-#include "plpy_util.h"
-
 #endif                         /* PLPYTHON_H */