Manage a tempdb database Stay organized with collections Save and categorize content based on your preferences.
This page describes management of the tempdb database in Cloud SQL.
The tempdb database is a system database that holds many objects, includingtemporary tables, stored procedures, and more. In your instances, you canperform common operations on this database.
A tempdb database is recreated each time an instance is restarted. To preventthe loss of user permissions, Cloud SQL provides permissions to thesqlserveruser after an instance is restarted.
Overview
Thesqlserveruserhas the ALTER permission for managing the tempdb database options.
For more information about managing this resource, see thetempdb databasepage.
Manage tempdb files
After youconnect to an instance, thesqlserveruser can manage the tempdb files.
Number of files
The user has ALTER permission on the tempdb database, which lets them controlsettings for the number of files and more. Some example operations include thefollowing:
ALTER DATABASE [tempdb] ADD FILEALTER DATABASE [tempdb] REMOVE
File size
The following sections describe methods used to control the size of files in thetempdb database.
For more information about these methods, seeShrink the tempdb database.
Change file sizes in tempdb
To control the size of files in the tempdb database, use theALTER DATABASEstatement. For more information, seeALTER DATABASE (Transact-SQL) File and Filegroup Options.
Shrink individual file size
msdb.dbo.gcloudsql_tempdb_shrinkfile is astored procedureyou can use to shrink an individual file in thetempdb database.
This stored procedure provides all the same benefits of theDBCC SHRINKFILEcommand.
DBCC SHRINKFILEdocumentation.The following are example uses of themsdb.dbo.gcloudsql_tempdb_shrinkfilestored procedure and its parameters, executed from theCloud SQL Studio:
Default option
msdb.dbo.gcloudsql_tempdb_shrinkfile@filename='FILENAME'
Where:
- FILENAME: the name of the file to be shrunk. For example,
tempdev.
This command executes the following SQL Server commands:
USE tempdbDBCC SHRINKFILE (@filename)
- FILENAME: the name of the file to be shrunk. For example,
EMPTYFILEmsdb.dbo.gcloudsql_tempdb_shrinkfile@filename='FILENAME',@empty_file=EMPTY_FILE_INT
Where:
- EMPTY_FILE_INT: an integer value that is either 0 or 1. If 1,then
EMPTYFILEis passed as an option. This option takes priority overother parameters.
This command executes the following SQL Server commands:
USE tempdbDBCC SHRINKFILE (@filename, EMPTYFILE)
- EMPTY_FILE_INT: an integer value that is either 0 or 1. If 1,then
Target size
msdb.dbo.gcloudsql_tempdb_shrinkfile@filename='FILENAME',@target_size=TARGET_SIZE_INT
Where:
- TARGET_SIZE_INT: an integer that represents the target size ofthe file in megabytes. Passes to the
DBCC SHRINKFILEcommand any valuethat is greater than or equal to zero. For example,10.
This command executes the following SQL Server commands. The integer 10 isincluded as an example:
USE tempdbDBCC SHRINKFILE (@filename, 10)
- TARGET_SIZE_INT: an integer that represents the target size ofthe file in megabytes. Passes to the
Target size and truncate only
msdb.dbo.gcloudsql_tempdb_shrinkfile@filename='FILENAME',@target_size=10,@truncateonly=TRUNCATE_ONLY_INT
Where:
- TRUNCATE_ONLY_INT: accepts an integer value of either 0 or 1.If set to 1, then
TRUNCATEONLYis passed as an option. Target size isignored ifTRUNCATEONLYis passed. This option takes priority overNOTRUNCATE.
This command executes the following SQL Server commands:
USE tempdbDBCC SHRINKFILE (@filename, 10, TRUNCATEONLY)
- TRUNCATE_ONLY_INT: accepts an integer value of either 0 or 1.If set to 1, then
Target size and no truncate option
msdb.dbo.gcloudsql_tempdb_shrinkfile@filename='FILENAME',@target_size=10,@no_truncate=NO_TRUNCATE_INT
Where:
- NO_TRUNCATE_INT: accepts an integer value of either 0 or 1.If set to 1, then
NOTRUNCATEis passed as an option.
This command executes the following SQL Server commands:
USE tempdbDBCC SHRINKFILE (@filename, 10, NOTRUNCATE)
- NO_TRUNCATE_INT: accepts an integer value of either 0 or 1.If set to 1, then
What's next
- Learn aboutInstance settings.
- Learn aboutMonitoring Cloud SQL instances.
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.