polar_sql_inception (SQL Audit)
In team collaboration or automated release processes, inconsistent SQL standards and unaudited change operations can cause data errors, performance bottlenecks, or even failures in the production environment. The polar_sql_inception plugin addresses this pain point by providing an automated SQL audit engine that integrates auditing and execution. You can customize rules to automatically check SQL during development, testing, and release phases, enforce coding standards, and block high-risk operations—ensuring the quality and security of database changes.
Applicable Scope
The supported version of PolarDB for PostgreSQL (Compatible with Oracle) is Oracle syntax compatible 2.0, with a minor engine version of 2.0.14.19.38.0 or later.
You can check the minor engine version number in the console View the minor engine version number, or use the SHOW polardb_version; statement. If your minor engine version does not meet the requirement, upgrade the minor engine version Upgrade the minor engine version.
Quick Start
This section uses a simple example to demonstrate a core feature of polar_sql_inception: how to block a non-compliant `CREATE TABLE` statement in under three minutes.
Install the
polar_sql_inceptionplugin by running the following command in your database.CREATE EXTENSION polar_sql_inception;Set an audit rule to require a primary key for all new tables. Run the
SETcommand to enable this rule in the current session.SET polar_sql_inception.table_rule_check_primary_key = ON;Call the audit function to check a CREATE TABLE statement that does not include a primary key. Run the following query and set the
executeparameter toFALSEto perform audit only, without execution.SELECT * FROM polar_sql_inception( sql_statements := 'CREATE TABLE users (id INT, name TEXT);', execute := FALSE );Expected result: The audit fails and returns a clear error message. The
error_levelfield iserror, and theerror_messagefield clearly states the violation:set a primary key. This confirms that the plugin successfully blocked the non-compliant SQL.sql_id | sql_statement | stage | error_level | error_message | affected_rows --------+----------------------------------------+---------+-------------+-------------------+--------------- 1 | CREATE TABLE users (id INT, name TEXT) | checked | warning | set a primary key+| 0 | | | | |
How it works
Configuration Modes
Choose the configuration method that best fits your needs.
Use the
SETcommand to configure parameters for the current database. This method is flexible and convenient, requires no cluster restart, and applies only to the current application scope.Session-level configuration:
-- Only effective in the current session SET polar_sql_inception.dml_rule_check_dml_where = ON;User level:
-- Only effective for the current user ALTER username SET polar_sql_inception.dml_rule_check_dml_where = ON;Database-level configuration:
-- Only effective in the current database ALTER databasename SET polar_sql_inception.dml_rule_check_dml_where = ON;
Execution Model
When you call the polar_sql_inception function and pass multiple SQL statements, it follows this processing flow:
Serial processing: The plugin processes SQL statements one by one in order. The flow is: audit statement A → (if passed and
executeisTRUE) execute statement A → audit statement B → (if passed andexecuteisTRUE) execute statement B → …Fault interruption: During auditing or execution, if a statement triggers an
error-level fault, or triggers awarning-level fault and warnings are not configured to be ignored (ignore_warning_when_executing = OFF), the execution flow for all subsequent statements is interrupted. However, the plugin continues to audit all remaining statements and returns the complete audit results.
Execute SQL Audit and Changes
This section describes how to use the polar_sql_inception function to audit SQL statements and execute changes as needed.
Function Definition
polar_sql_inception(
sql_statements TEXT,
execute BOOLEAN DEFAULT FALSE,
schema TEXT DEFAULT NULL
)Parameter description
Parameter | Description | Applicable Scope |
| One or more SQL statements to audit. | Same as the applicable scope of the feature. |
| Execute SQL statements after a successful audit. The default is |
|
| Specify the default schema that SQL statements rely on for execution. Note This parameter is equivalent to internally and temporarily running | Oracle syntax compatible 2.0, with a minor engine version of 2.0.14.20.41.0 or later. |
Operation Examples
Scenario 1: Audit Multiple SQL Statements Only
Check whether two CREATE TABLE statements comply with the rule requiring a primary key, without actually creating the tables.
-- Enable the "table must have a primary key" rule
SET polar_sql_inception.table_rule_check_primary_key = ON;
-- Call the function to audit
SELECT * FROM polar_sql_inception(
sql_statements := $$
-- Error example: no primary key
CREATE TABLE t1 (id INT);
-- Correct example: with primary key
CREATE TABLE t2 (id INT PRIMARY KEY);
$$,
execute := FALSE
);Returned result: The first SQL statement reports an error because it violates the rule. The second SQL statement passes the audit.
sql_id | sql_statement | stage | error_level | error_message | affected_rows
--------+----------------------------------------------+---------+-------------+---------------------+---------------
1 | +| checked | warning | set a primary key +| 0
| -- Error example: no primary key +| | | |
| CREATE TABLE t1 (id INT) | | | |
2 | +| checked | success | no violations found | 0
| +| | | |
| -- Correct example: with primary key | | | |
| CREATE TABLE t2 (id INT PRIMARY KEY) | | | | Scenario 2: Audit and Execute After Passing
Audit an UPDATE statement to verify compliance with the rule requiring a WHERE clause for DML statements. If it complies, execute it.
-- Prepare test data
CREATE TABLE products (id INT, stock INT);
INSERT INTO products VALUES (1, 100);
-- Enable the "DML must have WHERE" rule
SET polar_sql_inception.dml_rule_check_dml_where = ON;
-- Call the function to audit and execute
SELECT * FROM polar_sql_inception(
sql_statements := 'UPDATE products SET stock = 99 WHERE id = 1;',
execute := TRUE
);Returned result: The SQL statement passes the audit and executes successfully (stage: executed and error_level: success). The affected_rows field shows that one row was updated.
sql_id | sql_statement | stage | error_level | error_message | affected_rows
--------+---------------------------------------------+----------+-------------+---------------------+---------------
1 | UPDATE products SET stock = 99 WHERE id = 1 | executed | success | no violations found | 1Result set details
The polar_sql_inception function returns a result set. Each row corresponds to the audit and execution result of one SQL statement you provided.
Field | Description |
| The ordinal number of the SQL statement, starting from 1. |
| The original text of the SQL statement being processed. |
| The current processing stage of the SQL statement:
|
| The severity level of the result:
|
| Detailed audit feedback or error message. If multiple violations exist, they are separated by line feeds. When the audit passes, it returns |
| The number of affected rows.
|
Configuration templates
To help you configure rule parameters in batch, PolarDB provides the following configuration templates. Select and apply the rules that match your business needs.
-- Runtime configuration
SET polar_sql_inception.get_real_affected_rows = OFF;
SET polar_sql_inception.enable_utility_parse_analysis = ON;
SET polar_sql_inception.ignore_warning_when_executing = OFF;
-- Table-related rules
SET polar_sql_inception.table_rule_enable_partition = ON;
SET polar_sql_inception.table_rule_check_primary_key = OFF;
SET polar_sql_inception.table_rule_enable_foreign_key = ON;
SET polar_sql_inception.table_rule_merge_alter_table = OFF;
SET polar_sql_inception.table_rule_must_have_columns = 'column1,column2,column3';
-- Column-related rules
SET polar_sql_inception.column_rule_max_char_length = 0;
SET polar_sql_inception.column_rule_enable_text_type = ON;
SET polar_sql_inception.column_rule_enable_json_type = ON;
SET polar_sql_inception.column_rule_check_not_null = OFF;
SET polar_sql_inception.column_rule_enable_timestamp_type = ON;
SET polar_sql_inception.column_rule_check_timestamp_default = OFF;
SET polar_sql_inception.column_rule_check_timestamp_count = OFF;
SET polar_sql_inception.column_rule_check_default_value = OFF;
-- Index-related rules
SET polar_sql_inception.index_rule_enable_null_index_name = ON;
SET polar_sql_inception.index_rule_max_key_parts = 0;
SET polar_sql_inception.index_rule_max_primary_key_parts = 0;
SET polar_sql_inception.index_rule_check_pk_columns_only_int = OFF;
SET polar_sql_inception.index_rule_max_keys = 0;
-- Naming-related rules
SET polar_sql_inception.naming_rule_check_char = OFF;
SET polar_sql_inception.naming_rule_check_keyword = OFF;
-- DML-related rules
SET polar_sql_inception.dml_rule_check_insert_field = OFF;
SET polar_sql_inception.dml_rule_check_dml_where = OFF;
SET polar_sql_inception.dml_rule_enable_select_star = ON;
SET polar_sql_inception.dml_rule_enable_orderby_rand = ON;
SET polar_sql_inception.dml_rule_max_update_rows = 0;
SET polar_sql_inception.dml_rule_max_insert_rows = 0;
SET polar_sql_inception.dml_rule_max_delete_rows = 0;
-- Other rules
SET polar_sql_inception.check_schema_consistency = OFF;Configuration reference
Runtime configuration
Parameter | Default value | Description |
|
| During the |
|
| During the |
|
| During the Note Also supported: Oracle syntax compatibility 2.0, with minor engine version 2.0.14.19.40.0 or later. |
Rule configuration
Table-related rules
Rule name | Parameter | Default value | Trigger scenario |
Tables must have a primary key |
|
|
|
Partitioned tables are not allowed |
|
|
|
Foreign keys are not allowed |
|
|
|
Tables must contain specified columns |
|
|
|
Merge multiple ALTER TABLE statements |
|
|
|
Table does not exist | Default rule | - |
|
Only one primary key is allowed | Default rule | - |
|
Table referenced by LIKE does not exist | Default rule | - |
|
User must have permission | Default rule | - |
|
Column-related rules
Rule name | Parameter | Default value | Trigger scenario |
CHAR length limit |
|
|
|
TEXT type is not allowed |
|
|
|
JSON type is not allowed |
|
|
|
Columns must have a NOT NULL constraint |
|
|
|
TIMESTAMP type is not allowed |
|
|
|
TIMESTAMP columns must have a default value |
|
|
|
Only one |
|
|
|
All columns must define a default value, except for |
|
|
|
Duplicate column names are not allowed | Default rule | - |
|
Index-related rules
Rule name | Parameter | Default value | Trigger scenario |
Indexes must have a name |
|
|
|
Maximum number of columns in a regular index |
|
|
|
Maximum number of columns in a primary key index |
|
|
|
Primary key columns must be integer type |
|
|
|
Maximum number of indexes per single table |
|
|
|
Columns specified when creating an index must exist | Default rule | - |
|
You can create an index only for a column that already exists. | Default rule | - |
|
Index names must not be duplicated | Default rule | - |
|
User must have permission | Default rule | - |
|
Naming rules
Rule name | Parameter | Default value | Trigger scenario |
Check name character set |
|
|
|
Check for keywords |
|
|
|
DML rules
Rule name | Parameter | Default value | Trigger scenario |
Insert list must be specified |
|
|
|
DML statements must include a WHERE clause |
|
|
|
|
|
|
|
|
|
|
|
Limit number of rows updated |
|
|
|
Limit number of rows inserted |
|
|
|
Limit number of rows deleted |
|
|
|
Tables and columns must exist | Default rule | - | DML statement references a non-existent table or column. |
User must have permission | Default rule | - | DML statement lacks permission on the table. |
Other rules
Rule name | Parameter | Default value | Trigger scenario |
Check schema consistency |
|
| Triggered when the schema explicitly specified in a DML or DDL statement differs from the schema parameter specified in the polar_sql_inception function. Note Also supported: Oracle syntax compatibility 2.0, with minor engine version 2.0.14.20.41.0 or later. |