Skip to content

Add Structured Error Format and New Verbosity Levels in libpq#4947

Merged
rahim-kanji merged 5 commits intov3.0from
v3.0_structured_libpq_error_message
May 22, 2025
Merged

Add Structured Error Format and New Verbosity Levels in libpq#4947
rahim-kanji merged 5 commits intov3.0from
v3.0_structured_libpq_error_message

Conversation

@rahim-kanji
Copy link
Collaborator

@rahim-kanji rahim-kanji commented May 14, 2025

  • Deprecating PQgetResultFromPGconn() API.
  • Introducing a structured, length-prefixed error format for all errors returned via PQerrorMessage().
  • Adding two new error verbosity levels: PSERRORS_FORMATTED_DEFAULT and PSERRORS_FORMATTED_VERBOSE

Closes #4948

…rror information:

PSERRORS_FORMATTED_DEFAULT – Default formatted error message
PSERRORS_FORMATTED_VERBOSE – Verbose formatted error message
Implemented a parser to handle the new format.
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @rahim-kanji, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request focuses on enhancing error reporting in libpq, the PostgreSQL client library. I'm deprecating the PQgetResultFromPGconn() API and introducing a structured, length-prefixed error format for errors returned via PQerrorMessage(). Additionally, I'm adding two new error verbosity levels: PSERRORS_FORMATTED_DEFAULT and PSERRORS_FORMATTED_VERBOSE. The changes modify the Makefile to include a patch file, and update startup.c, fe-protocol3.c, libpq-fe.h, PgSQL_Connection.h, and PgSQL_Connection.cpp to implement the new error formatting and verbosity levels.

Highlights

  • Deprecation: PQgetResultFromPGconn() API is being deprecated.
  • Error Formatting: Introducing a structured, length-prefixed error format for PQerrorMessage().
  • New Verbosity Levels: Adding PSERRORS_FORMATTED_DEFAULT and PSERRORS_FORMATTED_VERBOSE error verbosity levels.
  • Error Handling: The pull request replaces direct calls to set_error_from_result with set_error_from_PQerrorMessage in PgSQL_Connection.cpp to leverage the new error formatting.

Changelog

Click here to see the changelog
  • deps/Makefile
    • Added fmt_err_msg.patch to the list of patches to be applied during the build process.
  • include/PgSQL_Connection.h
    • Added a declaration for set_error_from_PQerrorMessage() method.
    • Added declarations for is_valid_formatted_pq_error_header and parse_pq_error_message methods.
  • lib/PgSQL_Connection.cpp
    • Replaced calls to set_error_from_result with set_error_from_PQerrorMessage to use the new error formatting.
    • Implemented set_error_from_PQerrorMessage() to parse the formatted error message from PQerrorMessage() and populate the error information.
    • Implemented helper functions is_valid_formatted_pq_error_header and parse_pq_error_message to parse the new error message format.
  • src/bin/psql/startup.c
    • Added formatted_default and formatted_verbose as valid options for the VERBOSITY setting in psql.
  • src/interfaces/libpq/fe-protocol3.c
    • Implemented the logic for formatting error messages based on the PSERRORS_FORMATTED_DEFAULT and PSERRORS_FORMATTED_VERBOSE verbosity levels.
    • The formatted error message includes fields like severity, SQLSTATE, and primary message, with verbose mode adding detail, hint, context, etc.
  • src/interfaces/libpq/libpq-fe.h
    • Added PSERRORS_FORMATTED_DEFAULT and PSERRORS_FORMATTED_VERBOSE to the PGVerbosity enum.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Errors in the code,
A structured format now,
Verbose details bloom.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces structured error formatting and new verbosity levels in libpq, along with deprecating the PQgetResultFromPGconn() API. The changes seem well-structured and address the stated goals. However, there are a few areas that could benefit from further review and potential adjustments, as identified in the review comments.

Summary of Findings

  • Error Handling Consistency: Ensure that the new set_error_from_PQerrorMessage() function provides equivalent or better error information compared to the deprecated PQgetResultFromPGconn() and set_error_from_result() in all scenarios.
  • Code Clarity and Documentation: Add comments to explain the purpose and meaning of variables like libErr and the logic behind specific code sections, such as the integer overflow check in parse_pq_error_message.
  • Potential Out-of-Bounds Access: Address the potential out-of-bounds access in is_valid_formatted_pq_error_header by adding a check to ensure size_end is within the string bounds before accessing s[size_end].

Merge Readiness

The pull request introduces valuable improvements to error handling and verbosity in libpq. However, before merging, it's recommended to address the identified issues related to error handling consistency, code clarity, potential out-of-bounds access, and configuration flexibility. Addressing these points will improve the robustness and maintainability of the code. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging. At a minimum, the HIGH severity comment should be addressed before merging.

@renecannao
Copy link
Contributor

Can one of the admins verify this patch?

@rahim-kanji rahim-kanji force-pushed the v3.0_structured_libpq_error_message branch from 6e544c4 to 7c46f6c Compare May 15, 2025 06:29
@rahim-kanji rahim-kanji merged commit d29bf8c into v3.0 May 22, 2025
28 of 161 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate PQgetResultFromPGconn() and Enhance Error Handling Using PQerrorMessage()

2 participants