Serverless resources are charged based on consumption and duration of use. This guide explains how to view your serverless computing resource usage and estimate associated costs.
Notes on billing
Serverless Computing bills each SQL job based on CU-hours (Compute Units × Hours). This metric measures resource consumption by multiplying the amount of CU used by the job's duration. Each hour, usage from the previous hour is aggregated, bills are generated, and fees are automatically deducted. For more billing information, see Billing overview.
Serverless Computing has been commercially available and billable since July 1, 2024 (UTC+8).
Refer to the Serverless Computing guide for information on supported regions and zones.
Both
hologres.hg_query_logandhologres.hg_serverless_computing_query_logretain data for the last 30 days.
Only successfully executed SQL jobs incur charges. Failed jobs are not billed.
Query resource usage
Starting with Hologres V2.1.18, query the hologres.hg_serverless_computing_query_log view to calculate the resource usage of Serverless Computing tasks and estimate costs. For more information, see Query and analyze slow query logs.
In versions earlier than Hologres V2.2.7, slow query logs record only successful serverless computing tasks lasting more than 100 ms and all failed tasks. In Hologres V2.2.7 and later versions, all serverless computing tasks are available in slow query logs.
Slow query logs record the resource usage per SQL statement. During bill generation, processes such as data aggregation and unit conversion may cause minor discrepancies between the data in the logs and the final bill.
Required permissions
Querying hologres.hg_serverless_computing_query_log requires the following permissions:
Query data scans of all databases in an instance
You have the
superuserrole:-- A superuser can execute this command. Replace "Account ID" with the actual Alibaba Cloud account ID. For a RAM user, prefix the ID with "p4_". ALTER USER "Account ID" SUPERUSER;Alternatively, you are a regular user added to the
pg_read_all_statsuser group:NoteTo view all slow query logs as a regular user, ask an instance superuser to add you to this group.
GRANT pg_read_all_stats TO "Alibaba Cloud account ID";-- Standard PostgreSQL authorization model CALL spm_grant('pg_read_all_stats', 'Alibaba Cloud account ID'); -- Simple permission model (SPM) CALL slpm_grant('pg_read_all_stats', 'Alibaba Cloud account ID'); -- Schema-level permission model (SLPM)
View data scans of the current database
Enable the simple permission model (SPM) or the schema-level permission model (SLPM) for the database and add the user to the
db_adminuser group:CALL spm_grant('<db_name>_admin', 'Alibaba Cloud account ID'); -- SPM CALL slpm_grant('<db_name>.admin', 'Alibaba Cloud account ID'); -- SLPMRegular users can query the data scan volume for their own queries in the current database.
For more information, see Permission management overview.
Query the resource usage of a single SQL statement
Example:
SELECT
*,
queue_time_ms, -- The duration of the SQL statement queueing for serverless computing resources, in milliseconds (ms).
serverless_allocated_cores, -- The number of serverless CUs allocated to the SQL statement.
serverless_resource_used_time_ms, -- The time that serverless computing resources were in use by the SQL statement, in ms.
(serverless_allocated_cores::DECIMAL(38, 4)) * (serverless_resource_used_time_ms::DECIMAL(38, 4)) AS serverless_cums
FROM
hologres.hg_serverless_computing_query_log;Query the resource usage within a time period
Example:
SELECT
(SUM((serverless_allocated_cores::DECIMAL(38, 4)) * (serverless_resource_used_time_ms::DECIMAL(38, 4))) / 1000 / 60 / 60)::bigint AS serverless_cuh
FROM
hologres.hg_serverless_computing_query_log
WHERE
status = 'SUCCESS'
AND serverless_allocated_cores IS NOT NULL
AND serverless_resource_used_time_ms IS NOT NULL
AND query_end BETWEEN '2024-05-01 00:00:00' AND '2024-05-31 24:00:00';-- Specify the time period.Estimate resource usage for SQL statements
Before enabling Serverless Computing, estimate the resource requirements for your SQL statements by executing them on your existing Hologres instance. The hologres.hg_query_log system table captures cpu_time_ms (CPU time in milliseconds), reflecting the resource consumption of each SQL task. Estimate serverless resource usage (CU-hours) using this formula: cpu_time_ms / 1000 / 60 / 60. Query cpu_time_ms:
SELECT cpu_time_ms FROM hologres.hg_query_log WHERE query_id = 'xxx';Estimating serverless resource usage by running SQL statements on instance reserved resources is approximate. This is because instance reserved resources can be impacted by other workloads. Additionally, the capacities of these two resource types may differ. For a more accurate assessment, we recommend selecting key SQL tasks and testing their actual resource usage directly on serverless resources. This testing process runs independently and will not affect other tasks on your instance. For more information, see the Serverless Computing user guide.
Monitor resource usage
Set daily quota
Hologres V3.1.5+ allows a superuser to limit the daily usage of serverless computing resources using the following command:
-- Set a resource quota for a DB.
ALTER DATABASE <db_name> SET hg_serverless_computing_daily_max_cuh_usage_threshold = xx;
-- Set a resource quota for a user.
ALTER USER <user_name> SET hg_serverless_computing_daily_max_cuh_usage_threshold = xx;Notes
The default value of
hg_serverless_computing_daily_max_cuh_usage_thresholdis-1, which means no limit is enforced.Unit: CU-hour.
Hologres updates the total serverless computing resource usage for an instance every 10 minutes. Consequently, enforcement of limits may experience a delay of up to 10 minutes.
Before executing an SQL statement, Hologres compares the serverless computing resources used that day with the daily resource quota for the current user or database. If the limit is reached, the SQL statement falls back to instance reserved resources.
Hologres calculates serverless computing resource consumption based on the calendar day in the database's default time zone. You can query the database's default time zone by running the following statements within the same session:
-- Reset the time zone to avoid effects from the client time zone or the user's default time zone. RESET timezone; -- Query the default time zone of the current DB. SHOW timezone;
Suggestions
For long-term users: To determine a suitable daily quota for serverless computing resources, analyze your resource usage over a specific time period.
For new users: We recommend using the feature extensively in a stable production environment first. After observing your usage patterns, you can then decide whether to set a daily cumulative usage limit.
To prevent SQL statements from falling back to instance reserved resources when the Serverless Computing usage limit is reached, run the following commands as a superuser. This configuration will cause queries to fail and return an error.
-- Set at the DB level.
ALTER DATABASE <db_name> SET hg_serverless_computing_enable_fallback_when_exceed_cuh_threshold = false;
-- Set at the user level.
ALTER USER <user_name> SET hg_serverless_computing_enable_fallback_when_exceed_cuh_threshold = false;The default value of
hg_serverless_computing_enable_fallback_when_exceed_cuh_thresholdistrue. This means once the limit is reached, the SQL statement automatically runs on the instance reserved resources.If you set it to
falseand the limit is reached, the SQL statement fails and returns the following error:serverless computing is not available due to exceeding cuh usage threshold, please adjust the threshold or trun off serverless computing for current query.
Analyze costs
Monitor and analyze the costs of Serverless Computing resources in the Expenses and Costs console.
Log on to Expenses and Costs. In the left navigation pane, click .
On the Cost Analysis page, in the Conditions section on the right, set Category to Billing Item and set Cost Type and Time Unit.
In the Filters section, set the filter items as needed.
For more information, see Cost Analysis.
Set cost alerts
In the Expenses and Costs console, you can manage budgets and set alerts for pay-as-you-go billing items. For more information, see Cost alerts.
Create alerts for long queries
Hologres supports monitoring Serverless Computing metrics. We recommend that you create alert rules for Serverless Computing to prevent unexpected costs.
For example, for the Serverless Computing Longest Active Query Time metric, a recommended alert rule is as follows:
Warn: The value of the "Serverless Computing / Serverless Computing Longest Active Query Time" metric is >= 3,600,000 milliseconds for 5 Consecutive Cycles (1 Cycle = 1 Minute).

For more information, see Configure alert rules.