polar_sql_mapping

Updated at:

The polar_sql_mapping plug-in redirects SQL statements at the database level — without modifying your application code. When a SQL statement matches a configured pattern, PolarDB for PostgreSQL (Oracle-Compatible) automatically executes the mapped replacement instead.

Use this plug-in in two scenarios:

  • Fix broken SQL: An application sends a SQL statement that fails (for example, querying a table that does not exist), and you cannot patch the application immediately.

  • Replace working SQL: A SQL statement runs without errors but has a semantic or performance issue. Use pattern-based matching to capture and replace it.

Prerequisites

Before you begin, ensure that:

  • The polar_sql_mapping plug-in exists in your database. New databases include it automatically. For older databases, create it manually:

    set default_with_rowids to off;
    create extension polar_sql_mapping;

Limits

  • After you configure a mapping, all SQL statements matching the specified pattern are redirected to the target SQL — not just the first match. Review your patterns carefully before enabling them in production.

  • polar_sql_mapping.max_num limits how many distinct SQL statements can be recorded. The default is 10. Statements beyond the limit are silently ignored. Changes to this parameter take effect after a database restart.

Fix broken SQL

Use this workflow when an application sends a SQL statement that returns an error.

Step 1: Enable SQL statement mapping

alter system set polar_sql_mapping.use_sql_mapping=on;

The default value is off.

Step 2: Enable error recording

alter system set polar_sql_mapping.record_error_sql=on;

The default value is off.

Disable error recording when you no longer need it to minimize performance impact:
alter system set polar_sql_mapping.record_error_sql=off;

Step 3: Trigger and capture the incorrect SQL statement

Run the incorrect SQL statement to generate an error:

select * from emp;

Expected output:

ERROR:  relation "emp" does not exist

Step 4: Query the recorded error

Incorrect SQL statements are recorded in polar_sql_mapping.error_sql_info. Query it to get the statement ID:

select * from polar_sql_mapping.error_sql_info;

Expected output:

 id  |       query        |           emessage            | calls
-----+--------------------+-------------------------------+-------
   1 | select * from emp; | relation "emp" does not exist |     1

Step 5: Create the mapping

Use polar_sql_mapping.insert_mapping_id(id, 'target_sql') to map the incorrect statement to a replacement. The first argument is the ID from error_sql_info, and the second is the SQL to execute instead:

select polar_sql_mapping.insert_mapping_id(1, ' select 1');

Step 6: Verify the mapping

Run the original incorrect statement again:

select * from emp;

Expected output after mapping:

 ?column?
----------
        1
(1 row)

PolarDB intercepted select * from emp and executed select 1 instead. To confirm the active mapping rules, query polar_sql_mapping.polar_sql_mapping_table:

select * from polar_sql_mapping.polar_sql_mapping_table;

The output lists all active mappings, including the source_sql and target_sql for each entry.

Use placeholders in mappings

Most applications use prepared statements. When mapping parameterized queries, use $n placeholders in both the source and target SQL — this follows PostgreSQL prepared statement syntax. Do not use ?.

For example, the following mapping entry redirects a parameterized query that filters by a single bind parameter:

 id |            source_sql            |           target_sql
----+----------------------------------+---------------------------------
  4 | select 1 from dual where a = $1; | select 1 from dual where 1 = $1
(1 row)

$1 corresponds to the first bind parameter sent by the application. The mapping applies regardless of the parameter value.

Replace working SQL

Use this workflow when an application sends a SQL statement that runs without errors but needs to be replaced — for example, to fix a semantic issue or improve query performance.

Step 1: Enable SQL statement mapping

alter system set polar_sql_mapping.use_sql_mapping=on;

Step 2: Enable error recording

alter system set polar_sql_mapping.record_error_sql=on;
Disable error recording when you no longer need it to minimize performance impact:
alter system set polar_sql_mapping.record_error_sql=off;

Step 3: Set a match pattern

Configure error_pattern using LIKE syntax. All SQL statements that match are captured into error_sql_info.

The following example captures all statements referencing test_table:

set polar_sql_mapping.error_pattern to '%test_table%';
set polar_sql_mapping.record_error_sql to true;

Step 4: Run the target SQL statements

select * from test_table;
select a from test_table;
select max(a) from test_table;

Step 5: Query the captured statements

select * from polar_sql_mapping.error_sql_info;

Expected output:

 id  |             query              |          emessage          | calls
-----+--------------------------------+----------------------------+-------
   1 | select * from test_table;      | Error Pattern Force Record |     1
   2 | select a from test_table;      | Error Pattern Force Record |     1
   3 | select max(a) from test_table; | Error Pattern Force Record |     1
(3 rows)

Step 6: Create the mappings

Map each captured statement to its replacement:

select polar_sql_mapping.insert_mapping_id(x, 'text');

Replace x with the ID from error_sql_info and text with the target SQL statement.

Step 7: Clear the match pattern

After creating the mappings, clear the pattern to stop capturing:

reset polar_sql_mapping.error_pattern;

Parameters and functions

Parameter / FunctionDescriptionDefault
polar_sql_mapping.use_sql_mappingEnables or disables SQL statement mapping.off
polar_sql_mapping.record_error_sqlEnables automatic recording of incorrect SQL statements or pattern-matched statements.off
polar_sql_mapping.max_numMaximum number of distinct SQL statements that can be recorded. Statements beyond this limit are ignored. Changes take effect after a database restart.10
polar_sql_mapping.error_sql_info_clear()Clears all recorded entries in error_sql_info.—