Check out our free resources and eBooks like: "Effective Indexing in Postgres"

Step 1: Create Monitoring User

We recommend creating a separate monitoring user on your PostgreSQL database for pganalyze.

Connect to the database you will be monitoring as the avnadmin user. Then run the following to create a new user (we've generated a random password for you, but you can replace it with one of your choosing):

CREATE USER pganalyze WITH PASSWORD '[could not generate password]' CONNECTION LIMIT 5;

CREATE SCHEMA pganalyze;
GRANT USAGE ON SCHEMA pganalyze TO pganalyze;
GRANT USAGE ON SCHEMA public TO pganalyze;

CREATE OR REPLACE FUNCTION pganalyze.get_stat_replication() RETURNS SETOF pg_stat_replication AS
$$
  /* pganalyze-collector */ SELECT * FROM pg_catalog.pg_stat_replication;
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;

Then, connect to each database that you plan to monitor on this server as avnadmin and run the following to enable the collection of additional column statistics:

CREATE SCHEMA IF NOT EXISTS pganalyze;
GRANT USAGE ON SCHEMA pganalyze TO pganalyze;
CREATE OR REPLACE FUNCTION pganalyze.get_column_stats() RETURNS SETOF pg_stats AS
$$
  /* pganalyze-collector */ SELECT schemaname, tablename, attname, inherited, null_frac, avg_width,
    n_distinct, NULL::anyarray, most_common_freqs, NULL::anyarray, correlation, NULL::anyarray,
    most_common_elem_freqs, elem_count_histogram
  FROM pg_catalog.pg_stats;
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;

Note: We never collect actual table data through this method (see the NULL values in the function), but we do collect statistics about the distribution of values in your tables. You can skip creating the get_column_stats helper function if the database contains highly sensitive information and statistics about it should not be collected. This will impact the accuracy of Index Advisor recommendations.

Note that it is important you run these as the avnadmin user in order to pass down the full access to statistics tables.

Write down the username and password of the monitoring user, we will need it in later steps of this guide.


Next we continue by enabling pg_stat_statements:

Proceed to Step 2: Enabling pg_stat_statements

Couldn't find what you were looking for or want to talk about something specific?
Start a conversation with us →