Skip to content

fix: fixed Postgres driver CPU 100%#2229

Merged
lvca merged 1 commit intomainfrom
postgres-cpu100
May 30, 2025
Merged

fix: fixed Postgres driver CPU 100%#2229
lvca merged 1 commit intomainfrom
postgres-cpu100

Conversation

@lvca
Copy link
Contributor

@lvca lvca commented May 30, 2025

This fixes the issue with CPU 100% when the postgres driver has an open connection.

The issue was with the read from the socket: if no data is present, a spin lock like behavior happens inside the read. Now if the data is not present, sleep 100ms and check again.

The idea would be implementing a multiplexing poll, this is a quick fix.

@lvca lvca self-assigned this May 30, 2025
@lvca lvca added this to the 25.5.1 milestone May 30, 2025
@lvca lvca requested a review from robfrank May 30, 2025 04:51
Copy link
Contributor

@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 @lvca, 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

Hello, gemini-code-assist here! This pull request by @lvca addresses a critical issue where the Postgres driver could consume 100% CPU when an open connection was idle. The core fix involves modifying the message reading loop to avoid busy-waiting when no data is available on the network channel. Additionally, a minor change in a test file disables Postgres debug logging by default.

Highlights

  • Fix CPU Usage: Implemented a check in the readMessage method of the PostgresNetworkExecutor to prevent a tight loop when no data is available on the channel. If no data is present, the thread now sleeps for 100ms, resolving the 100% CPU usage issue.
  • Test Configuration Update: Changed the default value for GlobalConfiguration.POSTGRES_DEBUG to false in the PostgresWJdbcTest class, disabling debug logging by default in tests.

Changelog

  • postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java
    • Added import for com.arcadedb.utility.CodeUtils (line 46).
    • In the readMessage method, added a check if (!channel.inputHasData()) (line 1078).
    • If no input data is available, the method now calls CodeUtils.sleep(100) (line 1079) and returns false (line 1080), preventing a busy-wait loop.
  • postgresw/src/test/java/com/arcadedb/postgres/PostgresWJdbcTest.java
    • Changed the default value of GlobalConfiguration.POSTGRES_DEBUG from true to false (line 58) in the setTestConfiguration method.
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.


Channel waits for data,
Looping fast, CPU hot,
Now it sleeps a bit.

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
Contributor

@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 a fix for a high CPU usage issue in the Postgres driver by adding a sleep interval when the input channel has no data. This is a good, direct approach to prevent busy-waiting. The change in the test file to disable debug logging is also a sensible cleanup.

Overall, the main change effectively addresses the reported problem. I have one suggestion regarding the use of a magic number for the sleep duration to improve maintainability.

Summary of Findings

  • Magic Number for Sleep Duration: The sleep duration of 100 ms in PostgresNetworkExecutor.java is a magic number. It's recommended to define it as a named constant (e.g., NETWORK_IDLE_SLEEP_MS) for better readability and maintainability.

Merge Readiness

The pull request effectively addresses the high CPU usage issue. There's one medium-severity suggestion to improve maintainability by replacing a magic number with a named constant. Addressing this would be beneficial before merging.

As an AI reviewer, I am not authorized to approve pull requests. Please ensure other human reviewers inspect and approve these changes.

@codacy-production
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
-0.05% 100.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (0deebdd) 72348 46148 63.79%
Head commit (52da3d1) 72351 (+3) 46113 (-35) 63.74% (-0.05%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#2229) 3 3 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Copy link
Collaborator

@robfrank robfrank left a comment

Choose a reason for hiding this comment

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

Ooook, maybe in the future we can do better :)

@lvca lvca merged commit 439f878 into main May 30, 2025
17 of 19 checks passed
robfrank pushed a commit that referenced this pull request Jun 17, 2025
@robfrank robfrank deleted the postgres-cpu100 branch January 14, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants