Authorized routines
Authorized routines let you share query results with specific users or groupswithout giving them access to the underlying tables that generated the results.For example, an authorized routine can compute an aggregationover data or look up a table value and use that value in a computation.
By default, if a user invokes a routine, the user must have access to read thedata in the table. As an alternative, you canauthorize the routineto access the dataset that contains the referenced table. An authorized routinecan query the tables in the dataset, even if the user who calls the routinecan't query those tables directly.
The following types of routines can be authorized:
Caution: Stored procedures authorized as routines have DDL and DML access.These procedures can create, modify, and delete database objects. Principalswith access to authorized stored procedures can bypassIdentity and Access Management (IAM) permissions and perform actions that are normallydenied to them. Only grant authorized stored procedure access to principalsthat you trust to run the procedure in its entirety.Authorize routines
To authorize a routine, use the Google Cloud console, the bq command-line tool, or the REST API:
Console
Go to the BigQuery page in the Google Cloud console.
In the left pane, clickExplorer:

If you don't see the left pane, clickExpand left pane to open the pane.
In theExplorer pane, expand your project, clickDatasets, andthen select a dataset.
In the details pane, clickShare> Authorize Routines.
In theAuthorized routines page, in theAuthorize routinesection, select theProject,Dataset, andRoutine for theroutine that you want to authorize.
ClickAdd authorization.
bq
Use the
bq showcommand to get the JSON representation of the datasetthat you want the routine to access. The output from the command is aJSON representation of theDatasetresource.Save the result to a local file.bqshow--format=prettyjsonTARGET_DATASET >dataset.json
Replace
TARGET_DATASETwith the name of the dataset thatthe routine can access.Edit the file to add the following JSON object to the
accessarray intheDatasetresource:{"routine":{"datasetId":"DATASET_NAME","projectId":"PROJECT_ID","routineId":"ROUTINE_NAME"}}
Replace the following:
DATASET_NAME: the name of the dataset thatcontains the routine.PROJECT_ID: the project ID of the project thatcontains the routine.ROUTINE_NAME: the name of the routine.
Optional: If you are authorizing astored procedure, attach an IAM role. This role restrictsaccess to the authorized procedure based on its permissions. To do this,add
"role"to the JSON object:{"role":"ROLE_NAME","routine":{"datasetId":"DATASET_NAME","projectId":"PROJECT_ID","routineId":"ROUTINE_NAME"}}
Replace
ROLE_NAMEwith the name of the role thatyou want to attach. You can attach the following roles to a storedprocedure:- BigQuery Routine Metadata Viewer (
roles/bigquery.routineMetadataViewer) - BigQuery Routine Data Viewer (
roles/bigquery.routineDataViewer) - BigQuery Routine Data Editor (
roles/bigquery.routineDataEditor) - BigQuery Routine Admin (
roles/bigquery.routineAdmin)
- BigQuery Routine Metadata Viewer (
Use the
bq updatecommand to update the dataset:bqupdate--sourcedataset.jsonTARGET_DATASET
API
Call the
datasets.getmethod to fetch the dataset that you want the routine to access. Theresponse body contains a representation of theDatasetresource.Add the following JSON object to the
accessarray in theDatasetresource:{"routine":{"datasetId":"DATASET_NAME","projectId":"PROJECT_ID","routineId":"ROUTINE_NAME"}}
Replace the following:
DATASET_NAME: the name of the dataset thatcontains the UDF.PROJECT_ID: the project ID of the project thatcontains the UDF.ROUTINE_NAME: the name of the routine.
Optional: If you are authorizing a stored procedure, attach anIAM role. This role restricts access to the authorizedprocedure based on its permissions. To do this, add
"role"to the JSONobject:{"role":"ROLE_NAME","routine":{"datasetId":"DATASET_NAME","projectId":"PROJECT_ID","routineId":"ROUTINE_NAME"}}
Replace
ROLE_NAMEwith the name of the role thatyou want to attach. You can attach the following roles to a storedprocedure:- BigQuery Routine Metadata Viewer (
roles/bigquery.routineMetadataViewer) - BigQuery Routine Data Viewer (
roles/bigquery.routineDataViewer) - BigQuery Routine Data Editor (
roles/bigquery.routineDataEditor) - BigQuery Routine Admin (
roles/bigquery.routineAdmin)
- BigQuery Routine Metadata Viewer (
Call the
dataset.updatemethod with the modifiedDatasetrepresentation.
CREATE OR REPLACE statement(CREATE OR REPLACE FUNCTION,CREATE OR REPLACE PROCEDURE,CREATE OR REPLACE TABLE FUNCTION),or by calling theroutines.update method, then you must re-authorize the routine.Quotas and limits
Authorized routines are subject to dataset limits. For more information, seeDataset limits.
Authorized routine example
The following is an end-to-end example of creating and using an authorized UDF.
Create two datasets named
private_datasetandpublic_dataset. For moreinformation about creating a dataset, seeCreating a dataset.Run the following statement to create a table named
private_tableinprivate_dataset:CREATEORREPLACETABLEprivate_dataset.private_tableASSELECTkeyFROMUNNEST(['key1','key1','key2','key3'])key;Run the following statement to create a UDF named
count_keyinpublic_dataset. The UDF includes aSELECTstatement onprivate_table.CREATEORREPLACEFUNCTIONpublic_dataset.count_key(input_keySTRING)RETURNSINT64AS((SELECTCOUNT(1)FROMprivate_dataset.private_tabletWHEREt.key=input_key));Grant the
Note: Instead of using a built-in role, consider creating a custom rolewith minimal permissions. For more information, seeCreating and managing custom roles.bigquery.dataViewerrole to a user on thepublic_datasetdataset. This role includes thebigquery.routines.getpermission, whichlets the user call the routine.For information about how to assign access controls to a dataset, seeControlling access to datasets.At this point, the user has permission to call the
count_keyroutinebut cannot access the table inprivate_dataset. If the user tries tocall the routine, they get an error message similar to the following:Access Denied: Table myproject:private_dataset.private_table: User doesnot have permission to query table myproject:private_dataset.private_table.
Using the bq command-line tool, run the
showcommand as follows:bqshow--format=prettyjsonprivate_dataset >dataset.json
The output is saved to a local file named
dataset.json.Edit
dataset.jsonto add the following JSON object to theaccessarray:{"routine":{"datasetId":"public_dataset","projectId":"PROJECT_ID","routineId":"count_key"}}
Replace
PROJECT_IDwith the project ID forpublic_dataset.Using the bq command-line tool, run the
updatecommand as follows:bqupdate--sourcedataset.jsonprivate_dataset
To verify that the UDF has access to
private_dataset, the user can run thefollowing query:SELECTpublic_dataset.count_key('key1');
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.