Internal Function
An optional read-only function that gives Datasentinel access to planner statistics and execution plans without granting SELECT on application tables.
The datasentinel_analytics function gives Datasentinel a safe, read-only window on information that the monitoring user cannot read on its own. No SELECT privilege on application tables is needed.
Objective
The function lets Datasentinel:
Read column statistics from the
pg_statsview.Read extended statistics definitions from the
pg_statistic_extcatalog.Produce the execution plan of a query, without ever running it.
Statistics are collected hourly, with both the agent-based and agentless methods. Before each collection, Datasentinel checks that the function is present in the database and that the monitoring user can execute it. The first hourly pass is not uploaded: the data appears on the platform after the second full hour.
What the function does
It supports three actions:
colstats
Column statistics from pg_stats, for all tables or for one table
extstats
Extended statistics definitions (CREATE STATISTICS), for all tables or for one table
explain
The execution plan of one statement, in text format
The explain action is locked down inside the function:
It always runs
EXPLAIN (ANALYZE off, FORMAT text). The statement is planned, never executed, and the caller cannot add any option.It accepts a single statement. A text with a second statement after a
;is rejected.A generic plan can be requested on PostgreSQL 16 and later.
The function is SECURITY DEFINER: it runs with the privileges of the role that installed it. For this reason, the SQL file revokes the execute privilege from PUBLIC, and the installer grants it only to the datasentinel role.
Deployment
The agent package ships three files in its root directory:
datasentinel_analytics.sql
The function definition and the REVOKE ... FROM PUBLIC
deploy_datasentinel_function.sh
Installs the function in every application database and grants EXECUTE to datasentinel
remove_datasentinel_function.sh
Removes the function from every application database
Use the files from the agent version you run: they are updated with the agent.
With agentless connections, no agent package is downloaded. The same three files are published in the datasentinel_analytics directory of the public Datasentinel toolkit repository on GitHub. On an on-premises platform, the toolkit is also installed in the home directory of the datasentinel user on the platform server.
Install
Run the installer on the PostgreSQL host, from the directory that holds the SQL file. The optional argument is the PostgreSQL port (default 5432).
The script connects through the local socket with psql, as the current OS user. Set PGHOST, PGUSER and the other PG* variables to connect differently, for example from a remote machine in an agentless setup:
On a cloud-managed instance, where no superuser is available, run the script as the master user.
Run the script again:
after creating a new database, which does not get the function otherwise;
after an agent upgrade that changes the SQL file.
The script uses CREATE OR REPLACE, so running it again is safe.
Check
In each database, run:
Expected result: one row, with prosecdef, ds_can_execute true and public_can_execute false.
Uninstall
The function is removed from every application database. Datasentinel detects its absence at the next hourly collection, and every other metric keeps working.
Last updated