Skip to content

Fix NPE in PostgreSQL plugin when executing statements prepared with generated keys - #840

Merged
wu-sheng merged 1 commit into
apache:mainfrom
carteruu:fix-postgresql-prepared-statement-npe
Oct 15, 2026
Merged

wu-sheng merged 1 commit into
apache:mainfrom
carteruu:fix-postgresql-prepared-statement-npe

Conversation

@carteruu

Copy link
Copy Markdown
Contributor

Fix the NullPointerException in the PostgreSQL plugin for statements prepared with generated keys (apache/skywalking#14138)

  • Add a unit test to verify that the fix works.
  • Explain briefly why the bug exists and how to fix it.

PgConnection#prepareStatement(String, String[]) is intercepted by JDBCPrepareStatementWithStringArrayInterceptor, which wraps the result in SWPreparedStatement but does not set the dynamic field of the real PgPreparedStatement. pgjdbc 42.x constructs that statement directly instead of going through the intercepted 4-argument prepareStatement, so its dynamic field stays null. prepareStatement(String, int) with Statement.RETURN_GENERATED_KEYS delegates to this method, and Hibernate and MyBatis use it for generated keys.

PgPreparedStatement is still enhanced, so when SWPreparedStatement calls the real executeUpdate (and the nested executeWithFlags), PreparedStatementExecuteMethodsInterceptor dereferences the null dynamic field. The agent logs 4 NPEs for every such insert, while the SQL span is still recorded by SWPreparedStatement.

This PR skips the interceptor when the dynamic field or its ConnectionInfo is null, which is the same guard the MySQL plugin's PreparedStatementExecuteMethodsInterceptor already has. Tracing of this path is unchanged. The new unit test fails with the NPE from the issue before this change.

@wu-sheng wu-sheng added this to the 9.9.0 milestone Oct 15, 2026
@wu-sheng wu-sheng added the bug Something isn't working label Oct 15, 2026
@wu-sheng
wu-sheng merged commit 19615e6 into apache:main Oct 15, 2026
253 of 261 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] PostgreSQL plugin throws NPE when executing statements prepared with RETURN_GENERATED_KEYS

2 participants