BigQuery · Metadata view
BigQuery INFORMATION_SCHEMA
INFORMATION_SCHEMA is BigQuery's set of read-only metadata views for tables, columns, partitions, storage and jobs. Query mydataset.INFORMATION_SCHEMA.TABLES or region-us.INFORMATION_SCHEMA.TABLE_STORAGE to audit schemas, find large tables and estimate storage cost. Each on-demand query bills at least 10 MB.
INFORMATION_SCHEMA views are system views you query with ordinary SQL. Every reference needs a qualifier. A dataset qualifier (mydataset.INFORMATION_SCHEMA.TABLES) limits results to one dataset and works for TABLES, COLUMNS, COLUMN_FIELD_PATHS, PARTITIONS, VIEWS, MATERIALIZED_VIEWS, TABLE_OPTIONS, ROUTINES, ROUTINE_OPTIONS, PARAMETERS and ROW_ACCESS_POLICIES. A region qualifier (region-us.INFORMATION_SCHEMA.TABLES) covers every dataset of the project in that location and is required for project-wide views such as JOBS and TABLE_STORAGE. A project prefix in front of either reads another project. With no qualifier at all, the query fails.
The views you will use most: TABLES (one row per table, view or snapshot, including a ddl column with the statement that recreates it), COLUMNS (types, nullability, is_partitioning_column and clustering_ordinal_position), PARTITIONS (rows, bytes, last_modified_time and storage_tier per partition), TABLE_STORAGE (logical, physical, time-travel and fail-safe bytes per table) and INFORMATION_SCHEMA.JOBS (job history with bytes billed and slot time).
Region qualifiers are not hierarchical: region-us is the US multi-region and does not include us-central1, and region-eu does not include europe-west3. The query must run in the same location as the view, and one query cannot join views from two regions. View names are case-sensitive, so write INFORMATION_SCHEMA in capitals.
Metadata is not always instant. Schema changes can take a moment to appear in TABLES and COLUMNS (bq show reads the table directly), TABLE_STORAGE lags by seconds to minutes, and changes caused by table or partition expiration can take up to a day to show there. PARTITIONS is in Preview, accepts only a dataset qualifier and reads at most 1,000 tables per query.
Syntax
SELECT ... FROM [project_id.]dataset_id.INFORMATION_SCHEMA.view_name
SELECT ... FROM [project_id.]`region-REGION`.INFORMATION_SCHEMA.view_nameArguments and return value
| Argument | Description |
|---|---|
project_id | Optional. The project to read. Defaults to the project that runs the query. With a project prefix, put a dashed project ID and the region in backticks. |
dataset_id | Dataset qualifier: limits the view to one dataset. Supported by dataset-level views such as TABLES, COLUMNS, PARTITIONS and VIEWS. |
REGION | Region qualifier written as region-us, region-eu or region-europe-west3. Required for JOBS, TABLE_STORAGE and other project-level views. Not supported by PARTITIONS. |
view_name | The view, in capitals: TABLES, COLUMNS, PARTITIONS, TABLE_STORAGE, JOBS, SCHEMATA, VIEWS, ROUTINES and others. |
Returns: A result set of metadata rows, one per object the view describes (table, column, partition, job), limited to the objects you have permission to list.
Examples
List the tables in a dataset with the DDL to recreate them
table_type is BASE TABLE, VIEW, MATERIALIZED VIEW, EXTERNAL, SNAPSHOT or CLONE. The ddl column is a quick way to copy a table definition into version control.
SELECT table_name, table_type, creation_time, ddl
FROM project.dataset.INFORMATION_SCHEMA.TABLES
ORDER BY creation_time DESC;Find partitioning and clustering columns across a region
Every dataset of the project in the US multi-region. Tables with no row here are neither partitioned nor clustered, which makes them the first suspects when a query scans too much.
SELECT
table_schema,
table_name,
column_name,
data_type,
is_partitioning_column,
clustering_ordinal_position
FROM `region-us`.INFORMATION_SCHEMA.COLUMNS
WHERE is_partitioning_column = 'YES'
OR clustering_ordinal_position IS NOT NULL
ORDER BY table_schema, table_name, clustering_ordinal_position;Largest tables and their monthly storage cost under each billing model
US list prices per GiB-month: logical $0.02 active and $0.01 long-term, physical $0.04 active and $0.02 long-term. active_physical_bytes already includes time-travel bytes, so only fail-safe bytes are added. The estimate ignores the free first 10 GiB and is a snapshot of today's bytes, not the prorated bill.
SELECT
table_schema,
table_name,
ROUND(total_logical_bytes / POW(1024, 3), 1) AS logical_gib,
ROUND(total_physical_bytes / POW(1024, 3), 1) AS physical_gib,
ROUND(active_logical_bytes / POW(1024, 3) * 0.02
+ long_term_logical_bytes / POW(1024, 3) * 0.01, 2) AS logical_billing_usd,
ROUND((active_physical_bytes + fail_safe_physical_bytes) / POW(1024, 3) * 0.04
+ long_term_physical_bytes / POW(1024, 3) * 0.02, 2) AS physical_billing_usd
FROM `region-us`.INFORMATION_SCHEMA.TABLE_STORAGE
ORDER BY total_logical_bytes DESC
LIMIT 20;Partition sizes and storage tier for one table
storage_tier is LONG_TERM for partitions not modified for 90 days. partition_id is __NULL__ for rows with a NULL partitioning value and __UNPARTITIONED__ for rows outside the partition range.
SELECT
partition_id,
total_rows,
ROUND(total_logical_bytes / POW(1024, 3), 2) AS logical_gib,
last_modified_time,
storage_tier
FROM project.dataset.INFORMATION_SCHEMA.PARTITIONS
WHERE table_name = 'events'
ORDER BY partition_id DESC
LIMIT 30;Common mistakes
- Leaving out the qualifier.
SELECT * FROM INFORMATION_SCHEMA.TABLESfails with Table "INFORMATION_SCHEMA.TABLES" must be qualified with a dataset. Add a dataset (analytics.INFORMATION_SCHEMA.TABLES) or a region (region-us.INFORMATION_SCHEMA.TABLES). - Expecting
region-usto cover all US locations. It is the US multi-region only. Datasets inus-central1orus-east4needregion-us-central1orregion-us-east4, in separate queries; a mismatch fails with a Not found error naming the location. - Writing view names in lowercase.
region-us.information_schema.tablesis read as an ordinary dataset and table and fails with Access Denied. UseINFORMATION_SCHEMAand the view name in capitals. - Querying
PARTITIONSfor a whole region. It only accepts a dataset qualifier and stops at 1,000 tables with "INFORMATION_SCHEMA.PARTITIONS query attempted to read too many tables". Filter ontable_name, or query dataset by dataset. - Adding
time_travel_physical_bytestoactive_physical_bytes. Active physical bytes already include time travel, so the sum double counts. Fail-safe bytes are separate and are the only ones to add for physical billing.
Cost and performance
- On-demand, every
INFORMATION_SCHEMAquery bills at least 10 MB and is never served from cache, so the same metadata query run every five minutes is billed every five minutes. On capacity pricing these queries consume your reservation's slots. The views carry no storage charge. - Loading, streaming or running DML that modifies a partition restarts its 90-day timer and returns it to active storage, which costs twice the long-term rate. Querying, exporting or copying the table does not. Check
storage_tierinPARTITIONSbefore a backfill rewrites years ofLONG_TERMpartitions. TABLE_STORAGEreports bytes at this moment. Storage is billed in GiB-months, prorated over time, so a table created yesterday costs a fraction of what the snapshot suggests for this month. Use it to rank tables and pick a billing model, then confirm in Cloud Billing.
FAQ
How do I list all tables in a BigQuery dataset?
SELECT table_name, table_type FROM mydataset.INFORMATION_SCHEMA.TABLES. For every dataset in a location, use the region qualifier instead: region-us.INFORMATION_SCHEMA.TABLES, which adds the table_schema (dataset) column you can group by.
How do I get the size of a BigQuery table?
Read total_logical_bytes, total_physical_bytes and total_rows from region-us.INFORMATION_SCHEMA.TABLE_STORAGE, or per partition from dataset.INFORMATION_SCHEMA.PARTITIONS. Both are metadata queries and do not scan the table.
Does querying INFORMATION_SCHEMA cost money?
Yes on on-demand pricing: at least 10 MB per query, with no caching. On capacity pricing it uses slots from your reservation. See BigQuery pricing.
How do I find which column a table is partitioned or clustered by?
In INFORMATION_SCHEMA.COLUMNS, is_partitioning_column = 'YES' marks the partitioning column and clustering_ordinal_position gives the order of clustering columns (1 is the first). The ddl column of TABLES shows both in the PARTITION BY and CLUSTER BY clauses.
Related
Errors you may hit
All BigQuery INFORMATION_SCHEMA · All statements and clauses · BigQuery error messages
