Skip to content

Session parameter/variable tracking for PostgreSQL - v3.0#4799

Merged
renecannao merged 49 commits intov3.0from
v3.0_session_parameter_tracking
Mar 13, 2025
Merged

Session parameter/variable tracking for PostgreSQL - v3.0#4799
renecannao merged 49 commits intov3.0from
v3.0_session_parameter_tracking

Conversation

@rahim-kanji
Copy link
Collaborator

@rahim-kanji rahim-kanji commented Jan 22, 2025

Description

Implemented PostgreSQL session parameters tracking. It significantly improves connection multiplexing capabilities.
This enhancement allows ProxySQL to intelligently reuse connections and properly configure session parameters on that connection, reducing the overhead of creating new connections for each client session.

How It Works

The feature maintains two parameter trackers:

  • One for the client session
  • One for each backend connection

When a new query is received, ProxySQL:

  • Searches the connection pool for the optimal connection
  • Prioritizes connections where session parameters match the client's requirements
  • If no perfect match exists, selects the most suitable connection or creates a new one
  • Automatically executes the necessary SET statements to align connection parameters with session requirements
  • Then executes the user's query on the properly configured connection

Key Benefits

  • Reduced Resource Consumption: Minimizes connection overhead on both ProxySQL and PostgreSQL servers
  • Improved Performance: Avoids the cost of establishing new connections
  • Efficient Multiplexing: Maximizes connection reuse while maintaining correct session settings
  • Proactive Validation: Mimics PostgreSQL's validation behavior for parameter values, catching invalid settings before they reach the backend

Currently tracks following session parameters:

  • client_encoding
  • datestyle
  • intervalstyle
  • standard_conforming_strings
  • timezone
  • allow_in_place_tablespaces
  • bytea_output
  • client_min_messages
  • enable_bitmapscan
  • enable_hashjoin
  • enable_indexscan
  • enable_nestloop
  • enable_seqscan
  • enable_sort
  • escape_string_warning
  • extra_float_digits
  • maintenance_work_mem
  • synchronous_commit

Note:
If a client sets any other session parameter or provides non-literal value, that connection becomes locked to its specific host group to prevent any inconsistent behavior.

Added handling of RESET command and DEFAULT parameter values

When executed, run-time parameters are restored to their default values. These defaults are determined in similar manner as PostgreSQL

  • When a parameter is RESET or set to DEFAULT, ProxySQL now first checks if a value is provided in the connection options.
  • If a connection value is specified, it is applied; otherwise, the parameter falls back to the value defined by the corresponding pgsql-default_* variable.

Special handling of DateStyle

DateStyle consists of two values: a format and an order (e.g., ISO, MDY). Unlike other parameters, DateStyle can be set with one or both values, and the missing value retains its previous setting.

Example:
SET DateStyle TO 'ISO, MDY' → Format: ISO, Order: MDY
SET DateStyle TO 'ISO' → Format: ISO, Order remains unchanged i.e MDY
SET DateStyle TO 'DMY' → Format remains unchanged i,e ISO, Order: DMY
SET DateStyle TO 'Postgres' → Format: Postgres, Order remains unchanged i.e DMY

Extra:

  • Added support for COPY ... FROM STDOUT, functioning similarly to COPY ... FROM STDIN

Closes#4837
Closes#4865
Closes#4776

SET statements on the backend.
* Added set_charset: Manage character set configuration.
* Added connect_start_SetCharset: Enables character set
initialization during the connection start process.
* client_encoding/names
* datestyle
* timezone/time zone
* standard_conforming_strings
@renecannao
Copy link
Contributor

Can one of the admins verify this patch?

@rahim-kanji rahim-kanji force-pushed the v3.0_session_parameter_tracking branch from 2f2ac6f to da166ac Compare February 5, 2025 09:52
- DateStyle consists of two values: a format and an order (e.g., ISO,
MDY). Unlike other parameters, DateStyle can be set with one or both
values, and the missing value retains its previous setting.

Examples:
SET DateStyle TO 'ISO, MDY' → Format: ISO, Order: MDY
SET DateStyle TO 'ISO' → Format: ISO, Order remains unchanged i.e MDY
SET DateStyle TO 'DMY' → Format remains unchanged i,e ISO, Order: DMY
SET DateStyle TO 'Postgres' → Format: Postgres, Order remains unchanged
i.e DMY

Improved RESET statements and DEFAULT parameter values handling

- Enhance RESET and DEFAULT value handling:
    • When a parameter is RESET or set to DEFAULT, ProxySQL now first
    checks if a value is provided in the connection options.
    • If a connection value is specified, it is applied; otherwise,
	the parameter falls back to the value defined by the
	corresponding pgsql-default_* variable.
enable_bitmapscan
enable_indexscan
enable_seqscan
maintenance_work_mem
* Removed async_set_names
* Removed async_set_option
* Removed dead code
* Removed async_set_autocommit
PgSQL_Connection_Placeholder to PgSQL_Connection
Removed dead code
Removed MySQL Prepare Statement related code
lib/PgSQL_Session.cpp # # Untracked files: #	.gitattributes #
.vs/ #	PR_proxysql_postg.txt #	include/query_cache - Copy.hpp #
lib/Query_Cache - Copy.cpp #	tash qqq #
test/tap/tests/pgsql-basic_tests-t - Copy.cpp #
test/tap/tests/pgsql-connection_test-t.cpp #
test/tap/tests/pgsql-set_parameter_test-t.cpp #
@rahim-kanji rahim-kanji changed the title Session parameter/variable tracking for PostgreSQL - v3.0 [WIP] Session parameter/variable tracking for PostgreSQL - v3.0 Mar 12, 2025
@renecannao renecannao merged commit 8f51df7 into v3.0 Mar 13, 2025
5 of 6 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.

2 participants