Work with sessions

This document describes how to create, use, terminate, and list yoursessions.

Before you complete these steps, ensure you have the necessarypermissions.

Create a session

If you would like to capture a group of your SQL activities, create aBigQuery session. After creating a session, you can runinteractive queries in your session until the sessionterminates. All queries in the sessionare run (processed) in the location where the session was created.

Console

In the Google Cloud console, each session is assigned to an editor tab.

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

    Go to BigQuery

  2. ClickCompose new query. Anew editor tab opens.

  3. ClickMore> Query settings. TheQuery settings panelappears.

  4. In theSession management section, clickUse session mode toenable the session mode.

  5. InAdditional settings> Data location, select thelocation. After the session is created, all queries in the session arerestricted to this location and the location cannot be changed.

  6. ClickSave.

  7. Write a query in the editor taband run it. The new session is created after this first query is run.

bq

Open theCloud Shelland enter the followingbq query command:

bqquery\--nouse_legacy_sql\--create_session[--location'SESSION_LOCATION']\'SQL_STATEMENT'

where:

  • SESSION_LOCATION: Bind the session to aphysical location. Restrict all queries inthe session to this location. Optional.
  • SQL_STATEMENT: The first SQL statement for your session.

Your session ID is returned with the results of the query.

API

Call thejobs.query method withthe following parameters:

{"query":"SQL_STATEMENT","createSession":true,["location":"SESSION_LOCATION"]}

where:

  • SQL_STATEMENT: The first SQL statement for your session.
  • SESSION_LOCATION: Bind the session to aphysical location. Restrict all queries inthe session to this location. Optional.

The response body is similar to the following:

{"jobReference":{"projectId":"myProject","jobId":"job_123"},"statistics":{"sessionInfo":{"sessionId":"CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZ"}}}

Run a query in a session

After you create a session, you can run queries in that session:

Console

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

    Go to BigQuery

  2. Click the editor tab that contains the session.

  3. Add your query to the session and clickRun.

bq

Open theCloud Shelland enter the followingbq query command:

bqquery\--nouse_legacy_sql\--session_id=SESSION_ID\'SQL_STATEMENT'

where:

  • SESSION_ID: Replace this with theID of the session you want to work with.
  • SQL_STATEMENT: A SQL statement to run in your session.

The results of the query are followed by your session ID.

If you are going to run lots of queries with the Cloud Shell,you can add your session ID to[query] in.bigqueryrcso that you don't need to copy and paste the session ID into each command.

This is what a session ID looks like in.bigqueryrc:

[query]--session_id=CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZ

After you've added the session ID to.bigqueryrc, you can omit the--session_id flag from thebq query command. If you want to use adifferent session or if a sessionterminates, you must update your.bigqueryrc file.

API

Call thejobs.query method withthe following parameters:

{"query":"SQL_STATEMENT","connectionProperties":[{"key":"session_id","value":"SESSION_ID"}]}

where:

  • SQL_STATEMENT: The first SQL statement for your session.
  • SESSION_ID: TheID of the session.

Terminate a session

A session can be terminated manually or automatically.The history of a terminated session is available for 20 days after termination.

Auto-terminate a session

A session is terminatedautomatically after 24 hours of inactivity or after 7 days, whichever happensfirst.

Terminate the current session

You can terminate your current session with a SQL statement or in theGoogle Cloud console, if the session was created there.

Console

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

    Go to BigQuery

  2. Find the editor tab that contains your session and close it. The session is terminated.

SQL

Do the following to terminate your session:

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

    Go to BigQuery

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

    CALLBQ.ABORT_SESSION();

  3. ClickRun.

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

Terminate a session by ID

You can terminate a session using its ID. You don't need to bein the session to terminate it this way.

Get the session ID, and then run the following statement:

CALLBQ.ABORT_SESSION(SESSION_ID);

ReplaceSESSION_ID with the ID of the session to terminate.

Get the ID of your active session

In some situations, you need to reference a session to continue working withinit. For example, if you are working with the Cloud Shell, you must includethe session ID each time you run a command for that session.

Console

You don't need to provide the session ID to run a new query inside a sessionin the Google Cloud console. You can just continue working in theeditor tab that contains the session. However, if you would like to referenceyour session in the Cloud Shell or an API call, you need to know the IDfor the session you created in the console.

Before you complete these steps, make sure that that you have run at least onequery in an active session.

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

    Go to BigQuery

  2. Click the editor tab that contains the session.

  3. InQuery results, clickJob information.

  4. In theJob information list, search for the session ID:

    Session ID: CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZ

bq

To run query commands in a session within the Cloud Shell, you need toinclude the session ID in the command. You can get the session ID when youcreate a session or bylisting your sessions.

When you create a session with the Cloud Shell, the session ID that isreturned is similar to the following:

In session: CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZ

API

To pass SQL commands into a session with an API call, you need to includethe session ID in the API call. You can get the session ID when youcreate a session or bylisting your sessions.

When you create a session with an API call, the session ID in the responselooks similar to the following:

sessionId: CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZ

List active and inactive sessions

To get session IDs of active and inactive sessions, follow these steps:

Console

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

    Go to BigQuery

  2. In the left pane, clickExplorer:

    Highlighted button for the Explorer pane.

    If you don't see the left pane, clickExpand left pane to open the pane.

  3. In theExplorer pane, clickJob history.

  4. Select the type of job history:

    • To display information of your recent jobs, clickPersonal history.
    • To display information of recent jobs in your project, clickProjecthistory.
  5. In theSession ID column, you can view session IDs for your jobs.

    Session ID in job history

SQL

To get a list of your three most recent sessions including the active andterminated sessions, run the following query in the editor tab:

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

    Go to BigQuery

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

    SELECTsession_id,MAX(creation_time)ASlast_modified_timeFROMregion-us.INFORMATION_SCHEMA.VIEWWHEREsession_idISNOTNULLANDcreation_time >TIMESTAMP_SUB(CURRENT_TIMESTAMP(),INTERVAL20DAY)GROUPBYsession_idORDERBYlast_modified_timeDESC;

    Replace the following:

    • VIEW: theINFORMATION_SCHEMA view:
      • JOBS_BY_USER: returns only the jobs created by the current user in the current project
      • SESSIONS_BY_USER: returns only the sessions created by the current user in the current project
      • SESSIONS_BY_PROJECT: returns all sessions in the current project

  3. ClickRun.

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

The result is similar to the following:

+-------------------------------------------------------------------------+| session_id                                        | last_modified_time  |+-------------------------------------------------------------------------+| CgwKCmZhbGl1LXRlc3QQARokMGQ5YWWYzZmE0YjhkMDBm     | 2021-06-01 23:04:26 || CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZTczwZjA1NDc2 | 2021-05-30 22:43:02 || CgwKCmZhbGl1LXRlc3QQY2MzLTg4ZDEtYzVhOWZiYmM5NzZk  | 2021-04-07 22:31:21 |+-------------------------------------------------------------------------+

View the history of a session

A session captures your SQL activities within a timeframe. This information isstored in the session's history. Session history lets you track changes you'vemade in the session. If a job fails or succeeds, it is recorded in thesession history so you can go back later and see what you did.

Console

To view the history of a session in the Google Cloud console, you can filteryourPersonal History orProject History by session ID to view allSQL queries run in a specific session.

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

    Go to BigQuery

  2. In the left pane, clickExplorer:

    Highlighted button for the Explorer pane.

  3. In theExplorer pane, clickJob history.

  4. Select the type of job history you want to view:

    • To display information of your recent jobs, clickPersonal history.
    • To display information of recent jobs in your project, clickProject history.
  5. ClickFilter and thenselectSession ID.

  6. In theSession ID field, search for the session ID:

    Session ID: CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0OWQtZ

SQL

To view historical data for a specific session, firstget your session ID, then follow thesesteps:

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

    Go to BigQuery

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

    SELECT*FROMregion-us.INFORMATION_SCHEMA.VIEWWHEREsession_info.session_id='SESSION_ID';

    Replace the following:

    • VIEW: theINFORMATION_SCHEMA view to work with

      Select one of the following views:

      • JOBS_BY_USER: returns only the jobs created by the current user in the current project
      • SESSIONS_BY_USER: returns only the sessions created by the current user in the current project
      • SESSIONS_BY_PROJECT: returns all sessions in the current project
    • SESSION_ID: the ID of the session for which to retrieve historical data

  3. ClickRun.

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

Example

The following returns the history for a session that has the session IDCgwKCmZhbGl1LXRlc3QQARokMDAzYjI0. You can replace this session ID with yourown.

SELECTcreation_time,queryFROMregion-us.INFORMATION_SCHEMA.JOBS_BY_USERWHEREsession_info.session_id='CgwKCmZhbGl1LXRlc3QQARokMDAzYjI0'ANDcreation_time>TIMESTAMP_SUB(CURRENT_TIMESTAMP(),INTERVAL20DAY);

The result is similar to the following:

+---------------------+------------------------------------------------------------------------------------------+|    creation_time    |                                          query                                           |+---------------------+------------------------------------------------------------------------------------------+| 2021-06-01 23:04:26 | SELECT * FROM Purchases;                                                                 || 2021-06-01 23:02:51 | CREATE TEMP TABLE Purchases(total INT64) AS SELECT * FROM UNNEST([10,23,3,14,55]) AS a;  |+---------------------+------------------------------------------------------------------------------------------+

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.