Avoid circular header file dependency
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2025-04-26T03:20:09Z
Lists: pgsql-hackers
Attachments
Hi hackers,
While working on wait events I faced some compilation issues due to circular
header file dependency (introduced in fa88928470b5) between wait_event.h and
wait_event_types.h. Those files have include guards but could still lead to
compilation issues in some cases due to the circular dependency.
Currently, on HEAD, this doesn't cause any issues but I think it's better to
avoid circular header file dependencies (harder to maintain and understand).
Please find attached a patch to $SUBJECT between those 2 header files: it
extracts (in a new header file) from wait_event.h what is strictly needed in
wait_event_types.h.
Out of curiosity, I ran clang-tidy with misc-header-include-cycle ([1]) and it
also reports:
../src/pl/plpython/plpy_util.h:9:10: warning: circular header file dependency detected while including 'plpython.h'
This one worries me less because plpy_util.h only contains simple external
function declarations.
I was surprised that clang-tidy does report only the plpy_util.h one (in addition
to the wait_event_types.h one) so I wondered if misc-header-include-cycle was
failing to discover circular dependencies in nested cases.
I did a quick test with: a.h → c.h → b.h → d.h → a.h and it found it:
"
../d.h:6:10: warning: circular header file dependency detected while including 'a.h'
6 | #include "a.h"
| ^
../b.h:6:10: note: 'd.h' included from here
6 | #include "d.h"
| ^
../c.h:6:10: note: 'b.h' included from here
6 | #include "b.h"
| ^
../a.h:6:10: note: 'c.h' included from here
6 | #include "c.h"
| ^
../main.c:2:10: note: 'a.h' included from here
2 | #include "a.h"
"
So it looks like that our code base only contains those 2: plpy_util.h and
wait_event_types.h cases.
Thoughts?
[1]: https://clang.llvm.org/extra/clang-tidy/checks/misc/header-include-cycle.html
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
Commits
-
Remove circular #include's between wait_event.h and wait_event_types.h
- b225c5e76ed1 18.0 landed
-
Remove circular #include's between plpython.h and plpy_util.h.
- 2311f193ea7d 18.0 landed