Skip to content
Use aiven_extras for Superuser Operations

Use aiven_extras for Superuser Operations

Run the operations that PostgreSQL reserves to a superuser, such as a FOR ALL TABLES publication or loading auto_explain, through the functions of the aiven_extras extension.

Why This Extension Exists

The administrative user avnadmin is not a superuser, see Limits and Quotas. Most administration works without superuser rights. For the few operations that do not, aiven_extras provides wrapper functions owned by the platform’s superuser.

Check the version offered on your service:

SELECT name, default_version
FROM pg_available_extensions
WHERE name = 'aiven_extras';

Enable the Extension

In each database that needs it:

CREATE EXTENSION aiven_extras;

The extension has no dependency and installs into its own aiven_extras schema. Call its functions with the schema prefix, aiven_extras..

List the functions, and see which ones run with the owner’s rights (security_definer = t) rather than with yours:

SELECT proname, prosecdef AS security_definer
FROM pg_proc
WHERE pronamespace = 'aiven_extras'::regnamespace
ORDER BY proname;

What You Need It For

OperationWithout aiven_extrasWith aiven_extras
Publication for all tablesCREATE PUBLICATION ... FOR ALL TABLES fails with must be superuser to create FOR ALL TABLES publicationpg_create_publication_for_all_tables()
Load auto_explainLOAD 'auto_explain' fails with access to library "auto_explain" is not allowedauto_explain_load() and the set_auto_explain_*() functions
Take ownership of publicpublic is owned by pg_database_ownerclaim_public_schema_ownership()
Switch session_replication_role for a role other than avnadminpermission denied to set parametersession_replication_role(), see Disable Constraints During a Bulk Load

The other functions duplicate statements that avnadmin can run directly on PostgreSQL 16 and later: publications on given tables, subscriptions, replication slots, pgaudit settings. They are described below so that you recognize them in existing scripts.

Create a Publication for All Tables

SELECT aiven_extras.pg_create_publication_for_all_tables(
    'PUBLICATION_NAME',
    'INSERT,UPDATE,DELETE'
);

Parameters:

  • PUBLICATION_NAME: name of the publication.
  • The second argument lists the published operations, any of INSERT, UPDATE, DELETE and TRUNCATE, separated by commas.

The publication is owned by avnadmin. Check it, and drop it with plain SQL:

SELECT pubname, puballtables, pubinsert, pubupdate, pubdelete, pubtruncate
FROM pg_publication;
DROP PUBLICATION PUBLICATION_NAME;

For a publication on given tables, the plain statement works for tables owned by avnadmin or by a service user:

CREATE PUBLICATION PUBLICATION_NAME FOR TABLE public.orders, public.order_lines;

The wrapper aiven_extras.pg_create_publication('PUBLICATION_NAME', 'INSERT,UPDATE,DELETE', 'public.orders', 'public.order_lines') does the same. It needs schema-qualified table names: an unqualified orders fails with relation "orders" does not exist.

Load auto_explain

auto_explain writes the plan of slow statements to the service logs. Load it into the current session and set its parameters:

SELECT aiven_extras.auto_explain_load();
SELECT aiven_extras.set_auto_explain_log_min_duration('1000');

The value is in milliseconds. The other setters are set_auto_explain_log_analyze(), set_auto_explain_log_buffers(), set_auto_explain_log_format(), set_auto_explain_log_nested_statements(), set_auto_explain_log_timing() and set_auto_explain_log_verbose(). Each takes its value as text, for example 'on' or 'json'. Setting the parameters with SET fails with permission denied to set parameter.

Statements of that session that run longer than the threshold appear in the service logs with LOG: duration: ... plan:, followed by the query text and the plan. Read the logs with:

exo dbaas logs SERVICE_NAME -z ZONE -l 100

Parameters:

  • SERVICE_NAME: name of the service.
  • ZONE: zone of the service, for example ch-gva-2.

The setting lasts for the session only. See Identify slow queries for service-wide options.

To get the plan of one statement as JSON, without logging, use explain_statement(). It runs with the privileges of the caller:

SELECT aiven_extras.explain_statement('SELECT count(*) FROM public.orders');

Take Ownership of the public Schema

SELECT aiven_extras.claim_public_schema_ownership();

After the call, \dn public shows avnadmin as the owner. See Claim public schema ownership for when a tool needs this.

Subscriptions and Replication Slots

On PostgreSQL 16 and later, avnadmin can run CREATE SUBSCRIPTION, ALTER SUBSCRIPTION and DROP SUBSCRIPTION directly, and create or drop replication slots with pg_create_logical_replication_slot() and pg_drop_replication_slot(). Prefer the plain statements; see Set up logical replication.

The wrappers still work, which matters for scripts written for PostgreSQL 15:

WrapperPlain statement
pg_create_subscription(name, conninfo, publication, slot, slot_create, copy_data, origin)CREATE SUBSCRIPTION
pg_alter_subscription_enable(name), pg_alter_subscription_disable(name)ALTER SUBSCRIPTION ... ENABLE or DISABLE
pg_alter_subscription_refresh_publication(name, copy_data)ALTER SUBSCRIPTION ... REFRESH PUBLICATION
pg_drop_subscription(name, drop_slot)DROP SUBSCRIPTION
dblink_slot_create_or_drop(conninfo, slot, 'create' or 'drop')pg_create_logical_replication_slot() on the publisher

pg_create_subscription() defaults to slot_create => false, copy_data => true and origin => 'any', and warns: WARNING: This function is deprecated, use the standard CREATE SUBSCRIPTION statement instead.

aiven_extras.pg_list_all_subscriptions() returns the subscriptions with their connection string, password included. Do not grant it to roles that must not see remote credentials.

A service user that connects as a replication client needs the replication attribute. Set it when creating the user with exo dbaas user create SERVICE_NAME USERNAME -z ZONE --pg-allow-replication, or change it later through the API:

echo '{"allow-replication":true}' | \
  exo api update-dbaas-postgres-allow-replication SERVICE_NAME USERNAME -z ZONE

Parameters:

  • USERNAME: the service user. Send false to remove the attribute.

Check it in SQL with SELECT rolname, rolreplication FROM pg_roles WHERE rolname = 'USERNAME';.

Replication Statistics

avnadmin reads pg_stat_replication directly. The aiven_extras.pg_stat_replication view only returns rows whose usename is the current session user, so as avnadmin it is empty.

On every service, pg_stat_replication has a row with application_name = pghoard: it is the backup process of the service, not a replica.

Functions That Are Deprecated

  • set_pgaudit_parameter() and set_pgaudit_role_parameter() still work but warn This function is deprecated, changing superuser-reserved GUC is now grantable to roles. Use ALTER DATABASE ... SET pgaudit.log or ALTER ROLE ... SET pgaudit.log. See Collect pgaudit logs.
  • pg_create_subscription(), see above.

There is no wrapper for pg_stat_statements_reset(): avnadmin can call it directly in a database where pg_stat_statements is installed.

Let Another Role Use a Function

The functions are granted to avnadmin with the right to pass them on. Grant only what a role needs:

GRANT USAGE ON SCHEMA aiven_extras TO ROLE_NAME;
GRANT EXECUTE ON FUNCTION aiven_extras.session_replication_role(text) TO ROLE_NAME;

Parameters:

  • ROLE_NAME: the role that needs the function.

Without the EXECUTE grant, the role gets permission denied for function. A few functions, such as the subscription enable and disable wrappers and explain_statement(), are executable by every role; they run with the caller’s own privileges.

Related Pages

Last updated on