Considerations for using extensions and modules in Azure HorizonDB

This article describes some special considerations that you must be aware of when using certain extensions or modules in an Azure HorizonDB instance.

Generic considerations for extensions

To use an extension in your Azure HorizonDB instance, you need to:

If you get any error while executing the CREATE EXTENSION, ALTER EXTENSION, DROP EXTENSION, or COMMENT ON EXTENSION commands on your Azure HorizonDB instance, see the list of possible errors, and what could be the cause of each of those errors.

Generic considerations for modules

To use a module in your Azure HorizonDB instance, add it to the shared_preload_libraries parameter as described in load libraries.

Modules don't need to be allowlisted. That's an exclusive requirement for extensions.

Extensions with specific considerations

The following list enumerates all the supported extensions that require specific considerations when used in an Azure HorizonDB instance:

  • AGE
  • dblink
  • pg_buffercache
  • pg_cron
  • pg_hint_plan
  • pg_prewarm
  • pg_repack
  • pg_stat_statements
  • postgres_fdw
  • pgstattuple

AGE

The Apache AGE extension is a graph extension for PostgreSQL supported by Azure HorizonDB. It provides graph database functionalities, openCypher query support, and the ability to execute complex queries on graph data stored in PostgreSQL. 'Apache AGE' is an open-source project released under the Apache License 2.0.

Install AGE

To use AGE, add the extension to the allow list, load its library, and install the extension in the database where you plan to use its functionality.

The dblink extension allows you to connect from one Azure HorizonDB instance to another or another database in the same server. Azure HorizonDB supports both incoming and outgoing connections to any PostgreSQL server. The sending server needs to allow outbound connections to the receiving server. Similarly, the receiving server needs to allow connections from the sending server.

pg_buffercache

Use the pg_buffercache extension to examine the contents of shared_buffers. By using this extension, you can determine whether a specific relation is cached in shared_buffers. This extension can help you troubleshoot caching-related performance problems.

This extension is part of the core installation of PostgreSQL, and it's easy to install.

CREATE EXTENSION pg_buffercache;

pg_cron

The pg_cron extension is a simple, cron-based job scheduler for PostgreSQL that runs inside the database as an extension. The pg_cron extension can run scheduled maintenance tasks within a PostgreSQL database. For example, you can run a periodic vacuum of a table or remove old data jobs.

The pg_cron extension can run multiple jobs in parallel, but it runs at most one instance of a job at a time. If a second run is supposed to start before the first one finishes, then the second run is queued and started as soon as the first run completes. In such a way, it ensures that jobs run precisely as many times as scheduled and don't run concurrently with themselves.

Make sure that the value to which shared_preload_libraries is set, includes pg_cron. This extension doesn't support loading the library as the effect of executing CREATE EXTENSION. Any attempt to run CREATE EXTENSION if the extension wasn't added to shared_preload_libraries, or the cluster wasn't restarted after it was added, results in an error whose text says pg_cron can only be loaded via shared_preload_libraries, and whose hint is Add pg_cron to the shared_preload_libraries configuration variable in postgresql.conf.

To use pg_cron, make sure you load its shared library upon server start, it's allowlisted, and it's installed in any database from which you want to interact with its functionality, using the SQL artifacts it creates.

Examples

  1. To delete old data on Saturday at 3:30 am (GMT).

    SELECT cron.schedule('30 3 * * 6', $$DELETE FROM events WHERE event_time < now() - interval '1 week'$$);
    
  2. To run the vacuum every day at 10:00 am (GMT) in the default database postgres.

    SELECT cron.schedule('0 10 * * *', 'VACUUM');
    
  3. To unschedule all tasks from pg_cron.

    SELECT cron.unschedule(jobid) FROM cron.job;
    
  4. To see all jobs currently scheduled with pg_cron.

    SELECT * FROM cron.job;
    
  5. To run the vacuum every day at 10:00 am (GMT) in the database test cron under the azure_pg_admin role account.

    SELECT cron.schedule_in_database('VACUUM',' 0 10 * * * ', 'VACUUM', 'testcron',null,TRUE);
    

More examples

Starting with pg_cron version 1.4, you can use the cron.schedule_in_database and cron.alter_job functions to schedule your job in a specific database and update an existing schedule, respectively.

The cron_schedule_in_database function allows for the user name as an optional parameter. Setting the username to a non-null value requires PostgreSQL superuser privilege and isn't supported for an Azure HorizonDB instance. Preceding examples show running this function with an optional user name parameter omitted or set to null, which runs the job in the context of the user scheduling the job, which should have azure_pg_admin role privileges.

  1. To delete old data on Saturday at 3:30 am (GMT) on database DBName.

    SELECT cron.schedule_in_database('JobName', '30 3 * * 6', $$DELETE FROM events WHERE event_time < now() - interval '1 week'$$,'DBName');
    
  2. To update or change the database name for the existing schedule

    SELECT cron.alter_job(job_id:=MyJobID,database:='NewDBName');
    

pg_hint_plan

The pg_hint_plan extension enables you to tweak PostgreSQL execution plans by using "hints" in SQL comments, such as:

/*+ SeqScan(a) */

The pg_hint_plan extension reads hinting phrases in a comment of the special form given with the target SQL statement. The particular form begins with the character sequence /*+ and ends with */. Hint phrases consist of hint names and following parameters enclosed by parentheses and delimited by spaces. New lines for readability can delimit each hinting phrase.

Example:

/*+
 HashJoin(a b)
 SeqScan(a)
 */
    SELECT *
    FROM pgbench_branches b
    JOIN pgbench_accounts a ON b.bid = a.bid
    ORDER BY a.aid;

The preceding example causes the planner to use the results of a seqscan on table a to combine with table b as a hashjoin.

To use the pg_hint_plan extension, add the extension to the allow list, load its library, and install the extension in the database where you plan to use it.

pg_prewarm

The pg_prewarm extension loads relational data into the cache. When you prewarm your caches, your queries have better response times on their first run after a restart. The autoprewarm functionality for PostgreSQL isn't available in Azure HorizonDB.

pg_repack

First-time users of the pg_repack extension typically ask the following question: Is pg_repack an extension or a client-side executable like psql or pg_dump?

pg_repack is actually both. pg_repack/lib has the code for the extension, including the schema and SQL artifacts it creates, and the C library implementing the code of several of those functions.

On the other hand, pg_repack/bin has the code for the client application, which knows how to interact with the programmability elements implemented in the extension. This client application aims to ease the complexity of interacting with the different interfaces surfaced by the server-side extension. It offers the user some command-line options that are easier to understand. The client application is useless without the extension created on the database it's pointing to. The server-side extension on its own is fully functional, but it requires the user to understand a complicated interaction pattern. That pattern consists of executing queries to retrieve data that is used as input to functions implemented by the extension, and so on.

Permission denied for schema repack

Currently, because the extension grants permissions to the repack schema, you can only support running pg_repack functionality from the context of azure_pg_admin.

You might notice that if the owner of a table, who isn't azure_pg_admin, tries to run pg_repack, they receive the following error:

NOTICE: Setting up workers.conns
ERROR: pg_repack failed with error: ERROR:  permission denied for schema repack
LINE 1: select repack.version(), repack.version_sql()

To avoid that error, run pg_repack from the context of azure_pg_admin.

pg_stat_statements

The pg_stat_statements extension gives you a view of all the queries that run on your database. This information is useful for understanding your query workload performance on a production system.

The pg_stat_statements extension is preloaded in shared_preload_libraries on every Azure HorizonDB instance to provide a means of tracking SQL statement execution statistics.

For security reasons, you must add the pg_stat_statements extension to the allow list and install it using the CREATE EXTENSION command.

The pg_stat_statements.track setting controls which statements are tracked. Its allowed values are none, top, and all. Connect to the intended database endpoint and run SHOW pg_stat_statements.track; to check the effective value.

There's a tradeoff between the query execution information the pg_stat_statements extension provides and the server performance, as it logs each SQL statement. If you aren't actively using the pg_stat_statements extension, set pg_stat_statements.track to none. Some third-party monitoring services might rely on pg_stat_statements to deliver query performance insights, so confirm whether it's the case for you.

postgres_fdw

The postgres_fdw extension enables you to connect from one Azure HorizonDB instance to another instance or to another database in the same server. Azure HorizonDB supports both incoming and outgoing connections to any PostgreSQL server. The sending server needs to allow outbound connections to the receiving server. Similarly, the receiving server needs to allow connections from the sending server.

Confirm that your Azure HorizonDB cluster can reach the remote PostgreSQL server. Configure the required firewall rules and DNS resolution for the connection. For more information, see HorizonDB networking.

pgstattuple

In PostgreSQL 17, the pg_read_all_data role is automatically granted to azure_pg_admin, which allows pgstattuple to access objects in the pg_toast schema.

timescaleDB

The timescaleDB extension is a time-series database packaged as an extension for PostgreSQL. It provides time-oriented analytical functions and optimizations and scales Postgres for time-series workloads. Learn more about TimescaleDB, a registered trademark of Timescale, Inc. Azure HorizonDB provides the TimescaleDB Apache-2 edition.

Install TimescaleDB

To use timescaleDB, ensure that you add the extension to the allow list, load its library, and install the extension in the database where you plan to use its functionality.

You can now create a TimescaleDB hypertable from scratch or migrate existing time-series data in PostgreSQL.

For more information on restoring a Timescale database using pg_dump and pg_restore, see Timescale documentation.

Restore a Timescale database using timescaledb-backup

While running the SELECT timescaledb_post_restore() procedure, you might get permissions denied when updating timescaledb.restoring flag. The reason why you get this error is because of the limited ALTER DATABASE permission in Cloud PaaS database services. In this case, you can perform an alternative method using the timescaledb-backup tool to back up and restore the Timescale database. Timescaledb-backup is a program that makes dumping and restoring a TimescaleDB database simpler, less error-prone, and more performant.

To do so, follow these steps:

  1. Follow the TimescaleDB backup tool installation instructions.

  2. Create a target Azure HorizonDB instance and database.

  3. Enable Timescale extension.

  4. Grant the azure_pg_admin role to the user that is used by ts-restore.

  5. Run ts-restore to restore database.

For more information, see the TimescaleDB backup documentation.

Extensions and major version upgrade

The extensions anon, Apache AGE, dblink, orafce, postgres_fdw, and timescaledb aren't supported for all Azure HorizonDB instance versions when you use the in-place major version update feature.