Work with slot reservations

The BigQuery Reservation API lets you purchase dedicated slots (called commitments ), create pools of slots (called reservations ), and assign projects, folders, and organizations to those reservations.

Reservations allow you to assign a dedicated number of slots to a workload. For example, you might not want a production workload to compete with test workloads for slots. You could create a reservation named prod and assign your production workloads to this reservation. For more information, see Reservations .

Create reservations

Required permissions

To create a reservation, you need the following Identity and Access Management (IAM) permission:

Each of the following predefined IAM roles includes this permission:

  • BigQuery Resource Editor
  • BigQuery Resource Admin

For more information about IAM roles in BigQuery, see Predefined roles and permissions .

Create a reservation with dedicated slots

Select one of the following options:

Console

  1. In the Google Cloud console, go to the BigQuery page.

    Go to BigQuery

  2. In the navigation panel, go to the Capacity managementsection, and then click Create reservation.

  3. In the Reservation namefield, enter a name for the reservation.

  4. In the Locationdrop-down list, select the location. If you select a BigQuery Omni location , your edition option is limited to the Enterprise edition.

  5. In the Editionlist, select the edition. Autoscaling is only available within an edition. For more information, see Introduction to BigQuery editions .

  6. In the Max reservation size selectorlist, select the maximum reservation size.

  7. Optional: In the Baseline slotsfield, enter the number of baseline slots for the reservation.

    The number of available autoscaling slots is determined by subtracting the Baseline slotsvalue from the Max reservation size. For example, if you create a reservation with 100 baseline slots and a max reservation size of 400, your reservation has 300 autoscaling slots. For more information about baseline slots, see Using reservations with baseline and autoscaling slots .

  8. To disable idle slot sharing and use only the specified slot capacity, click the Ignore idle slotstoggle.

  9. To expand the Advanced settingssection, click the expander arrow.

  10. Optional: To set the target job concurrency, click the Override automatic target job concurrencytoggle to on and enter the Target Job Concurrency.

  11. The breakdown of slots is displayed in the Cost estimatetable. A summary of the reservation is displayed in the Capacity summarytable.

  12. Click Save.

The new reservation is visible in the Slot reservationstab.

SQL

To create a reservation, use the CREATE RESERVATION DDL statement .

  1. In the Google Cloud console, go to the BigQuerypage.

    Go to BigQuery

  2. In the query editor, enter the following statement:

     CREATE 
      
      RESERVATION 
     
      
    `  ADMIN_PROJECT_ID 
     
     . 
     region 
     - 
      LOCATION 
     
     . 
      RESERVATION_NAME 
     
    ` OPTIONS 
      
     ( 
      
     slot_capacity 
      
     = 
      
      NUMBER_OF_BASELINE_SLOTS 
     
     , 
      
     edition 
      
     = 
      
      EDITION 
     
     , 
      
     autoscale_max_slots 
      
     = 
      
      NUMBER_OF_AUTOSCALING_SLOTS 
     
     ); 
    

    Replace the following:

    • ADMIN_PROJECT_ID : the project ID of the administration project that owns the reservation resource
    • LOCATION : the location of the reservation. If you select a BigQuery Omni location , your edition option is limited to the Enterprise edition.
    • RESERVATION_NAME : the name of the reservation

      It must start and end with a lowercase letter or a number and contain only lowercase letters, numbers, and dashes.

    • NUMBER_OF_BASELINE_SLOTS : the number baseline of slots to allocate to the reservation. You cannot set the slot_capacity option and the edition option in the same reservation.
    • EDITION : the edition of the reservation. Assigning a reservation to an edition comes with feature and pricing changes. For more information, see Introduction to BigQuery editions .
    • NUMBER_OF_AUTOSCALING_SLOTS : the number of autoscaling slots assigned to the reservation. This is equal to the value of the max reservation size minus the number of baseline slots.
  3. Click Run.

For more information about how to run queries, see Run an interactive query .

bq

To create a reservation, use the bq mk command with the --reservation flag:

bq mk \
    --project_id= ADMIN_PROJECT_ID 
\
    --location= LOCATION 
\
    --reservation \
    --slots= NUMBER_OF_BASELINE_SLOTS 
\
    --ignore_idle_slots=false \
    --edition= EDITION 
\
    --autoscale_max_slots= NUMBER_OF_AUTOSCALING_SLOTS 
\ RESERVATION_NAME 

Replace the following:

  • ADMIN_PROJECT_ID : the project ID
  • LOCATION : the location of the reservation. If you select a BigQuery Omni location , your edition option is limited to the Enterprise edition.
  • NUMBER_OF_BASELINE_SLOTS : the number of baseline slots to allocate to the reservation

  • RESERVATION_NAME : the name of the reservation

  • EDITION : the edition of the reservation. Assigning a reservation to an edition comes with feature and pricing changes. For more information, see Introduction to BigQuery editions .

  • NUMBER_OF_AUTOSCALING_SLOTS : the number of autoscaling slots assigned to the reservation. This is equal to the value of the max reservation size minus the number of baseline slots.

For information about the --ignore_idle_slots flag, see Idle slots . The default value is false .

Terraform

Use the google_bigquery_reservation resource.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

The following example creates a reservation named my-reservation :

 resource "google_bigquery_reservation" "default" {
  name              = "my-reservation"
  location          = "us-central1"
  slot_capacity     = 100
  edition           = "ENTERPRISE"
  ignore_idle_slots = false # Use idle slots from other reservations
  concurrency       = 0     # Automatically adjust query concurrency based on available resources
  autoscale {
    max_slots = 200 # Allow the reservation to scale up to 300 slots (slot_capacity + max_slots) if needed
  }
} 

To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.

Prepare Cloud Shell

  1. Launch Cloud Shell .
  2. Set the default Google Cloud project where you want to apply your Terraform configurations.

    You only need to run this command once per project, and you can run it in any directory.

    export GOOGLE_CLOUD_PROJECT= PROJECT_ID 
    

    Environment variables are overridden if you set explicit values in the Terraform configuration file.

Prepare the directory

Each Terraform configuration file must have its own directory (also called a root module ).

  1. In Cloud Shell , create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf . In this tutorial, the file is referred to as main.tf .
    mkdir DIRECTORY 
    && cd DIRECTORY 
    && touch main.tf
  2. If you are following a tutorial, you can copy the sample code in each section or step.

    Copy the sample code into the newly created main.tf .

    Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.

  3. Review and modify the sample parameters to apply to your environment.
  4. Save your changes.
  5. Initialize Terraform. You only need to do this once per directory.
    terraform init

    Optionally, to use the latest Google provider version, include the -upgrade option:

    terraform init -upgrade

Apply the changes

  1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
    terraform plan

    Make corrections to the configuration as necessary.

  2. Apply the Terraform configuration by running the following command and entering yes at the prompt:
    terraform apply

    Wait until Terraform displays the "Apply complete!" message.

  3. Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.

Python

Install the google-cloud-bigquery-reservation package before using this code sample. Construct a ReservationServiceClient . Describe the reservation you'd like to create with a Reservation . Create the reservation with the create_reservation method.

  # TODO(developer): Set project_id to the project ID containing the 
 # reservation. 
 project_id 
 = 
" your 
 - 
 project 
 - 
 id 
" # TODO(developer): Set location to the location of the reservation. 
 # See: https://cloud.google.com/bigquery/docs/locations for a list of 
 # available locations. 
 location 
 = 
" US 
" # TODO(developer): Set reservation_id to a unique ID of the reservation. 
 reservation_id 
 = 
" sample 
 - 
 reservation 
" # TODO(developer): Set slot_capicity to the number of slots in the 
 # reservation. 
 slot_capacity 
 = 
 100 
 # TODO(developer): Choose a transport to use. Either 'grpc' or 'rest 
' transport 
 = 
" grpc 
" # ... 
 from 
 google.cloud.bigquery_reservation_v1.services 
 import 
 reservation_service 
 from 
 google.cloud.bigquery_reservation_v1.types 
 import 
 ( 
 reservation 
 as 
 reservation_types 
 , 
 ) 
 reservation_client 
 = 
 reservation_service 
 . 
 ReservationServiceClient 
 ( 
 transport 
 = 
 transport 
 ) 
 parent 
 = 
 reservation_client 
 . 
 common_location_path 
 ( 
 project_id 
 , 
 location 
 ) 
 reservation 
 = 
 reservation_types 
 . 
 Reservation 
 ( 
 slot_capacity 
 = 
 slot_capacity 
 ) 
 reservation 
 = 
 reservation_client 
 . 
 create_reservation 
 ( 
 parent 
 = 
 parent 
 , 
 reservation 
 = 
 reservation 
 , 
 reservation_id 
 = 
 reservation_id 
 , 
 ) 
 print 
 ( 
 f"Created 
 reservation 
 : 
 { 
 reservation 
 . 
 name 
 }")