Skip to content

Conversation

@JavierJF
Copy link
Collaborator

@JavierJF JavierJF commented May 21, 2025

PR 4960: Comprehensive MySQL/PostgreSQL Module Control

Main Purpose

This PR implements fine-grained control over ProxySQL's MySQL and PostgreSQL modules, allowing users to selectively enable or disable specific components via CLI arguments or configuration file settings. This addresses issue #4951 and provides better resource management and customization capabilities.

New CLI Arguments Added

  • --mysql-workers <true|false>: Enable/disable MySQL worker threads
  • --pgsql-workers <true|false>: Enable/disable PostgreSQL worker threads
  • --mysql-admin <true|false>: Enable/disable MySQL admin interface
  • --pgsql-admin <true|false>: Enable/disable PostgreSQL admin interface
  • --mysql-monitor <true|false>: Enable/disable MySQL monitor module
  • --pgsql-monitor <true|false>: Enable/disable PostgreSQL monitor module

Configuration File Support

Corresponding boolean settings can be specified in the ProxySQL configuration file:

mysql-workers=true
mysql-admin=true  
mysql-monitor=true
pgsql-workers=true
pgsql-admin=true
pgsql-monitor=true

Core Implementation Features

1. Selective Module Startup

  • Worker Threads: Controls whether MySQL and/or PostgreSQL worker threads are created
  • Admin Interfaces: Manages which admin interfaces (MySQL and/or PostgreSQL) listen for connections
  • Monitor Modules: Controls whether MySQL and/or PostgreSQL monitoring threads are started

2. Intelligent Thread Management

  • MySQL Workers: When disabled, no MySQL worker threads are created, reducing resource usage
  • PostgreSQL Workers: When disabled, no PostgreSQL worker threads are created
  • Admin Interfaces: Each interface can be independently enabled/disabled
  • Monitor Modules: Each monitor runs independently based on its setting

3. Admin Interface Variable Synchronization

  • Critical Fix: Added synchronization between internal global variables and admin interface variables
  • mysql-monitor_enabled: Now accurately reflects --mysql-monitor CLI argument state
  • pgsql-monitor_enabled: Now accurately reflects --pgsql-monitor CLI argument state
  • Ensures admin interface queries return correct module status

Resource Management Benefits

  • Reduced Memory Usage: Disabled modules don't allocate memory for threads and data structures
  • Lower CPU Usage: No background processing for disabled modules
  • Faster Startup: Skip initialization of disabled components
  • Customizable Deployments: Run only the modules you need

Use Cases Enabled

  1. MySQL-Only Deployments: Disable all PostgreSQL modules for pure MySQL environments
  2. PostgreSQL-Only Deployments: Disable all MySQL modules for pure PostgreSQL environments
  3. Admin-Only Mode: Run admin interfaces without worker threads for configuration management
  4. Monitor-Only Mode: Run monitoring without proxying capabilities
  5. Resource-Constrained Environments: Disable unnecessary modules to save resources

Comprehensive Testing Coverage

Worker/Admin Module Tests (reg_test_4960_modules_startup-t)

  • 16 test combinations covering all possible enable/disable states
  • Port binding verification ensures interfaces start/stop correctly
  • CLI argument and config file validation
  • Tests each combination: MySQL workers, PgSQL workers, MySQL admin, PgSQL admin

Monitor Module Tests (reg_test_4960_monitor_modules-t)

  • 4 test combinations for monitor module states
  • Database validation via admin interface variables
  • Synchronization verification between CLI args and runtime state

Technical Implementation Details

  • Global Variables: Added mysql_workers, pgsql_workers, mysql_admin, pgsql_admin to ProxySQL_GlobalVariables
  • Thread Control Logic: Modified thread creation loops to respect module enable/disable flags
  • Admin Interface Management: Updated interface initialization to check module flags
  • Configuration Processing: Added CLI argument parsing and config file reading
  • Coherence Checks: Added logging for module state consistency
  • Variable Synchronization: Fixed admin interface to reflect actual module state

Backward Compatibility

  • All modules are enabled by default, maintaining existing behavior
  • No breaking changes to existing configurations
  • Graceful degradation when modules are disabled
  • Clear error messages and logging for troubleshooting

Documentation and Monitoring

  • Informative Logging: Module status logged during startup
  • Admin Interface Visibility: Module status queryable via global_variables table
  • Runtime Status: Monitor module state accessible through admin interface

This implementation provides ProxySQL administrators with unprecedented control over which components run, enabling more efficient resource utilization and customized deployment scenarios while maintaining full backward compatibility.

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 @JavierJF, 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 here, providing a summary of this pull request by @JavierJF. This PR introduces new configuration settings and command-line options to allow users to explicitly enable or disable the MySQL and PostgreSQL modules within ProxySQL. This provides more granular control over which database backends and associated functionalities (worker threads, admin interfaces, monitoring) are active, addressing issue #4951. Additionally, it includes minor fixes related to PostgreSQL replication table definitions and MySQL worker thread naming.

Highlights

  • New Configuration Settings: Adds new settings --mysql-workers, --pgsql-workers, --mysql-admin, --pgsql-admin, --mysql-monitor, and --pgsql-monitor (accessible via config file and command line) to control the startup of specific ProxySQL modules for MySQL and PostgreSQL. By default, all are enabled (true). Setting any of these to false or 0 will prevent the corresponding module/feature from starting.
  • PostgreSQL Replication Table Fix: Removes unsupported CHECK constraints related to innodb_read_only and super_read_only from the pgsql_replication_hostgroups table definition, as these are MySQL-specific concepts.
  • MySQL Worker Thread Naming Fix: Corrects the thread naming logic for MySQL idle worker threads.
  • Module Startup Logic Updates: Modifies the core startup logic for MySQL and PostgreSQL worker threads and admin interfaces to respect the new global configuration variables.

Changelog

Click here to see the changelog
  • include/PgSQL_HostGroups_Manager.h
    • Removed unsupported CHECK constraints (innodb_read_only, super_read_only, combinations) from the pgsql_replication_hostgroups table definition (line 49).
  • include/ProxySQL_Admin_Tables_Definitions.h
    • Removed unsupported CHECK constraints from the pgsql_replication_hostgroups table definition (line 279).
    • Removed unsupported CHECK constraints from the runtime_pgsql_replication_hostgroups table definition (line 290).
  • include/proxysql_glovars.hpp
    • Added new boolean global variables mysql_workers, pgsql_workers, mysql_admin, pgsql_admin to the ProxySQL_GlobalVariables struct (lines 91-94).
  • lib/MySQL_Thread.cpp
    • Modified set_variable function to respect the new mysql_workers global variable when setting the number of threads (lines 2063-2064).
    • Modified init function to respect the new mysql_workers global variable when determining the default number of threads (line 2410).
    • Corrected the thread name setting for MySQL idle threads to use mysql_threads_idles instead of mysql_threads (line 2465).
  • lib/PgSQL_Thread.cpp
    • Modified set_variable function to respect the new pgsql_workers global variable when setting the number of threads (lines 2000-2001).
    • Modified init function to respect the new pgsql_workers global variable when determining the default number of threads (line 2322).
  • lib/ProxySQL_Admin.cpp
    • Removed commented-out code related to client handling in admin_main_loop (lines 2261-2263, 2300-2302, 2415, 2456, 2474).
    • Adjusted the loop for starting MySQL admin interfaces to check the GloVars.global.mysql_admin flag (line 2387).
    • Adjusted the loop for starting PostgreSQL admin interfaces to check the GloVars.global.pgsql_admin flag (line 2427).
  • lib/ProxySQL_GloVars.cpp
    • Initialized the new global variables (mysql_workers, pgsql_workers, mysql_admin, pgsql_admin) to true by default (lines 200-203).
    • Added command-line options for the new settings (--mysql-monitor, --pgsql-monitor, --mysql-workers, --pgsql-workers, --mysql-admin, --pgsql-admin) (lines 268-273).
    • Added logic to process the new command-line options and update the corresponding global variables (lines 499-551).
  • src/main.cpp
    • Added a template helper function update_global_variable to simplify reading boolean settings from the config file (lines 675-688).
    • Used the new helper function to read the new module enable/disable settings from the config file (lines 747-754).
    • Added coherence checks to log messages if all admin interfaces, worker threads, or monitoring are disabled, or if PostgreSQL support is fully disabled (lines 891-903).
    • Modified the loop for creating MySQL worker threads to check the GloVars.global.mysql_workers flag (line 1004).
    • Modified the loop for creating PostgreSQL worker threads to check the GloVars.global.pgsql_workers flag (line 1028).
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.


Modules stand tall,
Or not, at your call.
Configured just right,
For day and for night.

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.

@renecannao
Copy link
Contributor

Can one of the admins verify this patch?

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

This pull request introduces valuable flexibility by allowing users to disable/enable MySQL and PostgreSQL modules (workers, admin interfaces, and monitors) via command-line switches and configuration files. The implementation is largely clean and effective, with good refactoring in src/main.cpp to handle global variable updates. The minor fixes for MySQL worker thread naming and PostgreSQL DDL are also welcome improvements.

One critical issue was found that needs addressing before merging. Other than that, the changes enhance the configurability and maintainability of ProxySQL.

Summary of Findings

  • Correctness: Typo in PgSQL Thread Configuration: In lib/PgSQL_Thread.cpp, when determining if the number of PostgreSQL worker threads (intv) is valid, the condition incorrectly checks !GloVars.global.mysql_workers instead of !GloVars.global.pgsql_workers. This could lead to pgsql_workers being forced to 0 if mysql_workers is disabled, regardless of the pgsql_workers setting.
  • Maintainability: Refactoring Global Variable Updates: The introduction of the update_global_variable template function in src/main.cpp is a good refactoring choice, reducing code duplication when processing settings from the configuration file.
  • Feature Implementation: New Module Control Flags: The new command-line flags (--mysql-workers, --pgsql-workers, etc.) and corresponding global variables are clearly implemented, defaulting to true as described and allowing modules to be disabled effectively.
  • Bug Fix: MySQL Worker Thread Naming: The fix in lib/MySQL_Thread.cpp to use mysql_threads_idles[tn].thread_id for setting idle thread names appears to correct a previous issue.
  • Database Schema: PostgreSQL Replication Hostgroups Simplification: The DDL changes in include/PgSQL_HostGroups_Manager.h and include/ProxySQL_Admin_Tables_Definitions.h simplify the check_type constraint for pgsql_replication_hostgroups, which is a good cleanup if the removed options were unsupported.
  • Maintainability: Code Cleanup: Several instances of commented-out code were removed in lib/ProxySQL_Admin.cpp, improving code readability.
  • Maintainability: Command-line Option Processing (Not Commented due to Review Settings): In lib/ProxySQL_GloVars.cpp, the processing of new command-line options involves several similar if (opt->isSet(...)) { ... } blocks. While clear, this could potentially be refactored into a helper function if more options are added in the future, similar to the update_global_variable template used for config file parsing. This was not commented on directly due to the severity filter settings.

Merge Readiness

This pull request introduces significant and useful new functionality. However, there is a high severity issue in lib/PgSQL_Thread.cpp related to the configuration of PostgreSQL worker threads that must be addressed before merging. Once this issue is resolved, the PR should be in good shape. As an AI, I am not authorized to approve pull requests; please ensure further review and approval by authorized maintainers.

JavierJF added 4 commits May 21, 2025 15:23
…#4951

Accessible as config file and command line switches:

  + --(mysql|pgsql)-workers=(true|false)
  + --(mysql|pgsql)-admin=(true|false)
  + --(mysql|pgsql)-monitor=(true|false)

The default value for all is 'true'.
Since editors sometimes don't completely disable non-reachable code,
like macro-protected, it's cleaner too keep parenthesis open/close
outside this. Otherwise this can confuse editors scope matching.
@JavierJF JavierJF dismissed gemini-code-assist[bot]’s stale review May 21, 2025 13:31

Not picking up addressed changes.

@mirostauder
Copy link
Collaborator

retest this please

@renecannao renecannao requested a review from Copilot May 22, 2025 10:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds command-line and configuration settings to enable/disable MySQL and PostgreSQL modules while also applying minor fixes. Key changes include the introduction of a template helper function (update_global_variable) for setting globals, updates to command-line options and defaults for MySQL/PgSQL worker, admin, and monitor modules, and modifications to SQL table definitions to remove unsupported check constraints.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/main.cpp Introduces the update_global_variable template, uses it to update new globals, and adjusts thread creation.
lib/ProxySQL_GloVars.cpp Adds default values and new command-line options for MySQL/PgSQL modules; updates process_opts_post logic.
lib/ProxySQL_Admin.cpp Applies conditional loops based on new global admin flags and removes legacy commented code.
lib/PgSQL_Thread.cpp, lib/MySQL_Thread.cpp Adjust thread count logic to consider the new worker flags during configuration time.
include/proxysql_glovars.hpp Declares new global flags for module enablement.
include/ProxySQL_Admin_Tables_Definitions.h Updates the pgsql_replication_hostgroups table definition to only accept 'read_only' for check_type.
include/PgSQL_HostGroups_Manager.h Mirrors the changes of replication hostgroups definition for consistency.
Comments suppressed due to low confidence (1)

src/main.cpp:753

  • [nitpick] The configuration key 'mysql-monitor' does not clearly match the variable name 'my_monitor'; consider renaming either the key or the variable for improved consistency.
update_global_variable("mysql-monitor", GloVars.global.my_monitor);

@renecannao
Copy link
Contributor

renecannao commented May 22, 2025

When documenting these variables it is worth to mention that the Hostgroups Manager and Authentication Module are always enabled, no matter if Admin/Monitor/Workers are disabled.
As an example, either Monitor or Workers can access Hostgroups Manager even if the other module is disabled.

JavierJF added 3 commits July 1, 2025 17:54
…s' definition"

This reverts commit 8058162. Since the
table changes require additional code due to the standard table
migration procedure, these changes shall be move to their own PR.
Should be complemented with more details in the official doc.
@JavierJF JavierJF requested a review from renecannao July 1, 2025 16:33
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jul 1, 2025

@renecannao renecannao added this to the Release 3.0.4 milestone Oct 31, 2025
renecannao and others added 8 commits November 22, 2025 21:31
- Tests all 16 combinations of MySQL/PgSQL worker and admin module configurations
- Verifies both admin and worker port binding behavior
- Includes aggressive cleanup to prevent interference between test cases
- Tests both CLI arguments and config file settings
- Uses dedicated port range (13750-13813) to avoid conflicts
- Includes prerequisite checking for required system tools
- Correct test case expectations to match CLI arguments behavior
- Fix binary naming vs actual module enable/disable logic
- Ensure admin and worker modules are tested independently
- Resolve unused result warnings from system() calls
- Remove debug logging to clean up test output
- Validate that all module combinations work correctly
- Remove redundant sleep(1) between port check attempts
- Port checking with netcat completes instantly
- Reduces test execution time by approximately 60+ seconds
- Maintains same functionality while improving performance
Add comprehensive TAP test for verifying MySQL and PostgreSQL monitor
modules can be enabled/disabled via CLI arguments. Test validates monitor
status by querying global_variables table for mysql-monitor_enabled and
pgsql-monitor_enabled variables.
Modify monitor test to redirect ProxySQL stdout and stderr to
proxysql.log files in each test's datadir for cleaner console output
and better debugging capabilities.
Add synchronization code to ensure mysql-monitor_enabled and pgsql-monitor_enabled
variables in global_variables table accurately reflect the state of CLI arguments
--mysql-monitor and --pgsql-monitor. Previously, CLI arguments correctly disabled
monitor modules but admin interface variables remained true, causing inconsistent
state and test failures.

The fix adds synchronization after admin database initialization to update
admin interface variables based on internal global variables, ensuring proper
reflection of monitor module state.
Add reg_test_4960_modules_startup-t and reg_test_4960_monitor_modules-t to the
default-g1 test group to ensure comprehensive testing of the module enable/disable
functionality. Tests run only in default group to avoid duplication across multiple
test groups.
@sonarqubecloud
Copy link

Please retry analysis of this Pull-Request directly on SonarQube Cloud

@renecannao
Copy link
Contributor

Enhanced PR description with comprehensive feature overview including CLI arguments, configuration options, use cases, testing coverage, and implementation details.

@renecannao
Copy link
Contributor

⚠️ Important Dependency Notice

This PR has potential dependencies with PR 5217 (Fix issue 5186: Prevent admin query crashes after PROXYSQL STOP/START cycles). Both PRs should be tested together before merge.

Overlap Areas:

  1. lib/ProxySQL_Admin.cpp - Both PRs modify this file

    • This PR: Fixes admin interface variable synchronization
    • PR 5217: Adds null pointer protection in save_*_from_runtime() functions
  2. Module Lifecycle Management

    • This PR: Allows selective module disable/enable at startup
    • PR 5217: Implements STOP/START cycles assuming all modules are present
  3. src/main.cpp - Both PRs modify startup behavior

  4. Global Variable Handling

    • This PR: Adds module-specific flags (mysql_workers, pgsql_workers, etc.)
    • PR 5217: Uses GloVars.global.nostart for restart sequences

Verification Needed:

  • Test all module enable/disable combinations with PROXYSQL STOP/START
  • Verify null pointer protection works with disabled modules
  • Check admin interface variable accuracy in mixed states
  • Validate no crashes during STOP/START with selective modules

Tracking Issue: #5218 has been created to coordinate this verification.

Please do not merge until dependencies are verified.

renecannao added a commit that referenced this pull request Nov 24, 2025


Implements shared test framework to verify dependencies between:
- PR #5217: PROXYSQL STOP/START crash fixes
- PR #4960: MySQL/PostgreSQL module enable/disable functionality

Key changes:
- Create shared header test_proxysql_stop_query_handling.hpp with centralized test count constant
- Refactor original test to use shared header (simpler wrapper)
- Extend reg_test_4960_modules_startup-t.cpp: 8 MySQL admin cases get STOP/START testing
- Extend reg_test_4960_monitor_modules-t.cpp: all 4 monitor cases get STOP/START testing
- Add PROXYSQL_STOP_START_TEST_COUNT constant for maintainability

Test coverage now validates STOP/START behavior across 12 different module configurations,
addressing the core dependency concerns from issue #5218.
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.

4 participants