Procedures
Table of Contents
Dolt SQL Procedures
Dolt provides native stored procedures to allow access to dolt CLI commands from within a SQL session. Each procedure is named after the dolt command line command it matches, and takes arguments in an identical form.
For example, dolt checkout -b feature-branch is equivalent to executing the following SQL statement:
SQL procedures are provided for all imperative CLI commands. For commands that inspect the state of the database and print some information, (dolt diff, dolt log, etc.) system tables are provided instead.
One important note: all procedures modify state only for the current session, not for all clients. So for example, whereas running dolt checkout feature-branch will change the working HEAD for anyone who subsequently runs a command from the same dolt database directory, running CALL DOLT_CHECKOUT('feature-branch') only changes the working HEAD for that database session. The right way to think of this is that the command line environment is effectively a session, one that happens to be shared with whomever runs CLI commands from that directory.
DOLT_ADD()
DOLT_ADD()Adds working changes to staged for this session. Works exactly like dolt add on the CLI, and takes the same arguments.
After adding tables to the staged area, they can be committed with DOLT_COMMIT().
Options
table: Table(s) to add to the list tables staged to be committed. The abbreviation '.' can be used to add all tables.
-A: Stages all tables with changes.
Output Schema
Example
DOLT_BACKUP()
DOLT_BACKUP()Add or remove a configured backup, sync with a configured backup, sync a backup to a remote URL, restore a remote URL backup as a new database.
Output Schema
To sync the current database to configured backup:
To sync with a remote URL which is not configured as a backup:
To add and remove a configured backup:
To restore a backup:
Example
DOLT_BRANCH()
DOLT_BRANCH()Create, delete, and rename branches.
To list branches, use the DOLT_BRANCHES system table, instead of the DOLT_BRANCH() stored procedure.
To look up the current branch, use the @@<dbname>_head_ref system variable, or the active_branch() SQL function, as shown in the examples section below.
WARNING: In a multi-session server environment, Dolt will prevent you from deleting or renaming a branch in use in another session. You can force renaming or deletion by passing the --force option, but be aware that active clients on other sessions will no longer be able to execute statements after their active branch is removed and will need to end their session and reconnect.
Notes
Branch names have a few restrictions which are similar to the constraints Git puts on branch names. Dolt's branches are a little more restrictive, as ASCII characters are required. Rules are as follows:
All characters must be ASCII (7 Bit)
May not start with '.' (period)
May not contain '..' (two periods)
May not contain '@{'
May not contain ASCII control characters
May not contain characters: ':', '?', '[', '\', '^', '~', '*'
May not contain whitespace (spaces, tabs, newlines)
May not end with '/'
May not end with '.lock'
May not be HEAD (case insensitive)
May not be indistinguishable from a commit hash. 32 characters, where all characters are 0-9 or a-z (case sensitive)
The dolt_branch() procedure implicitly commits the current transaction and begins a new one.
Options
-c, --copy: Create a copy of a branch. Must be followed by the name of the source branch to copy and the name of the new branch to create. Without the --force option, the copy will fail if the new branch already exists.
-m, --move: Move/rename a branch. Must be followed by the current name of an existing branch and a new name for that branch. Without the --force option, renaming a branch in use on another server session will fail. Be aware that forcibly renaming or deleting a branch in use in another session will require that session to disconnect and reconnect before it can execute statements again.
-d, --delete: Delete a branch. Must be followed by the name of an existing branch to delete. Without the --force option, deleting a branch in use on another server session will fail. Be aware that forcibly renaming or deleting a branch in use in another session will require that session to disconnect and reconnect before it can execute statements again.
-f, --force: When used with the --copy option, allows for recreating a branch from another branch, even if the branch already exists. When used with the --move or --delete options, force will allow you to rename or delete branches in use in other active server sessions, but be aware that this will require those other sessions to disconnect and reconnect before they can execute statements again.
-D: Shortcut for --delete --force.
Output Schema
Examples
DOLT_CHECKOUT()
DOLT_CHECKOUT()Switches this session to a different branch.
With table names as arguments, restores those tables to their contents in the current HEAD.
Note, unlike the Git command-line, if you have a modified working set, those changes remain on the branch you modified after a DOLT_CHECKOUT(). Uncommitted changes in the working set do not transfer to the checked out branch as on the command line. We modified this behavior in the SQL context because multiple users may be connected to the same branch. Having one user bring changes from various other branches with them when they switch branches is too disruptive in the multi-tenant SQL context.
Notes
DOLT_CHECKOUT() with a branch argument has two side effects on your session state:
The session's current database, as returned by
SELECT DATABASE(), is now the unqualified database name.For the remainder of this session, references to the unqualified name of this database will resolve to the branch checked out.
See the comments after the statements below for an example of this behavior, and also read Using Branches
Options
-b: Create a new branch with the given name.
-B: Similar to -b, but will move a branch if it already exists.
-t: When creating a new branch, set up 'upstream' configuration.
Output Schema
Example
DOLT_CHERRY_PICK()
DOLT_CHERRY_PICK()Apply the changes introduced by an existing commit.
Apply changes from existing commit and creates a new commit from the current HEAD.
Works exactly like dolt cherry-pick command on the CLI, and has the same notes and limitations.
Options
--abort: Abort the current conflict resolution process, and revert all changes from the in-process cherry-pick operation.
--allow-empty: Allow empty commits to be cherry-picked. Note that use of this option only keeps commits that were initially empty. Commits which become empty, due to a previous commit, will cause cherry-pick to fail.
Output Schema
Example
For the below example consider the following set up of main and mybranch branches:
We want to cherry-pick only the change introduced in commit hash 'k318tpmqn4l97ofpaerato9c3m70lc14', which inserts 1 and 2 to the table. Specifying 'mybranch~1' instead of the commit hash also works.
DOLT_CLEAN()
DOLT_CLEAN()Deletes untracked tables in the working set.
Deletes only specified untracked tables if table names passed as arguments.
With --dry-run flag, tests whether removing untracked tables will return with zero status.
Options
--dry-run: Test removing untracked tables from working set.
Output Schema
Example
DOLT_CLONE()
DOLT_CLONE()Clones an existing Dolt database into a new database within the current Dolt environment. The existing database must be specified as an argument, either as a file URL that points to an existing Dolt database on disk, or a doltremote URL for remote hosted database (e.g. a database hosted on DoltHub or DoltLab), or a <org>/<database> (e.g. dolthub/us-jails) as a shorthand for a database hosted on DoltHub. An additional argument can optionally be supplied to specify the name of the new, cloned database, otherwise the current name of the existing database will be used.
NOTE: When cloning from a file URL, you must currently include the .dolt/noms subdirectories. For more details see the GitHub tracking issue, dolt#1860.
Options
--remote: Name of the remote to be added to the new, cloned database. The default is 'origin'.
-b, --branch: The branch to be cloned. If not specified all branches will be cloned.
--depth: Clone a single branch and limit history to the given commit depth.
Output Schema
Examples
DOLT_COMMIT()
DOLT_COMMIT()Commits staged tables to HEAD. Works like dolt commit with each value directly following the flag. The one difference is that the default commit author is the authenticated SQL user (e.g. root@localhost) instead of the user.name and user.email properties configured via dolt config.
DOLT_COMMIT() also commits the current transaction.
Options
-m, --message: Use the given <msg> as the commit message. Required
-a, --all: Stages all modified tables (but not newly created tables) before committing.
-A, --ALL: Stages all tables (including new tables) before committing.
--allow-empty: Allow recording a commit that has the exact same data as its sole parent. This is usually a mistake, so it is disabled by default. This option bypasses that safety.
--skip-empty: Record a commit only if there are changes to be committed. The commit operation will be a no-op, instead of an error, if there are no changes staged to commit. An error will be thrown if --skip-empty is used with --allow-empty.
--date: Specify the date used in the commit. If not specified, the current system time is used.
--author: Specify an explicit author using the standard "A U Thor [email protected]" format. Note that unlike dolt commit on the CLI, when invoking the dolt_commit() stored procedure, the default commit author is the authenticated SQL user (e.g. root@localhost).
Output Schema
Examples
DOLT_CONFLICTS_RESOLVE()
DOLT_CONFLICTS_RESOLVE()When a merge finds conflicting changes, it documents them in the dolt_conflicts table. A conflict is between two versions: ours (the rows at the destination branch head) and theirs (the rows at the source branch head). dolt conflicts resolve will automatically resolve the conflicts by taking either the ours or theirs versions for each row.
Options
<table>: List of tables to be resolved. '.' can be used to resolve all tables.
--ours: For all conflicts, take the version from our branch and resolve the conflict.
--theirs: For all conflicts, take the version from their branch and resolve the conflict.
Output Schema
Examples
DOLT_FETCH()
DOLT_FETCH()Fetch refs, along with the objects necessary to complete their histories and update remote-tracking branches. Works exactly like dolt fetch on the CLI, and takes the same arguments.
Options
--prune, -p: After fetching, remove any remote-tracking references that don't exist on the remote.
Output Schema
Example
Notes
Dropping the second argument, or passing NULL, will result is using the default refspec.
DOLT_GC()
DOLT_GC()Cleans up unreferenced data from the database. Running the dolt_gc procedure on a Dolt sql-server will block all writes while garbage collection is in progress.
Options
--shallow Performs a faster but less thorough garbage collection.
Output Schema
Notes
To prevent concurrent writes potentially referencing garbage collected chunks, running call dolt_gc() will break all open connections to the running server. In flight queries on those connections may fail and must be retried. Re-establishing connections after they are broken is safe.
At the end of the run, the connection which ran call dolt_gc() will be left open in order to deliver the results of the operation itself. The connection will be left in a terminally broken state where any attempt to run a query on it will result in the following error:
The connection should be closed. In some connection pools it can be awkward to cause a single connection to actually close. If you need to run call dolt_gc() programmatically, one work around is to use a separate connection pool with a size of 1 which can be closed after the run is successful.
DOLT_MERGE()
DOLT_MERGE()Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. Works exactly like dolt merge on the CLI, and takes the same arguments.
Any resulting merge conflicts must be resolved before the transaction can be committed or a new Dolt commit created. DOLT_MERGE() creates a new commit for any successful merge with auto-generated commit message if not defined.
Options
--no-ff: Create a merge commit even when the merge resolves as a fast-forward.
--squash: Merges changes to the working set without updating the commit history
-m <msg>, --message=<msg>: Use the given as the commit message. This is only useful for --non-ff commits.
--abort: Abort the current conflict resolution process, and try to reconstruct the pre-merge state.
--author: Specify an explicit author using the standard A U Thor <[email protected]> format.
When merging a branch, your session state must be clean. COMMIT orROLLBACK any changes, then DOLT_COMMIT() to create a new dolt commit on the target branch.
If the merge causes conflicts or constraint violations, you must resolve them using the dolt_conflicts system tables before the transaction can be committed. See Dolt system tables for details.
Output Schema
Example
DOLT_PULL()
DOLT_PULL()Fetch from and integrate with another database or a local branch. In its default mode, dolt pull is shorthand for dolt fetch followed by dolt merge <remote>/<branch>. Works exactly like dolt pull on the CLI, and takes the same arguments.
Any resulting merge conflicts must be resolved before the transaction can be committed or a new Dolt commit created.
Options
--no-ff: Create a merge commit even when the merge resolves as a fast-forward.
--squash: Merges changes to the working set without updating the commit history
--force: Ignores any foreign key warnings and proceeds with the commit.
--prune, -p: After fetching, remove any remote-tracking references that don't exist on the remote.
When merging a branch, your session state must be clean. COMMIT orROLLBACK any changes, then DOLT_COMMIT() to create a new dolt commit on the target branch.
If the merge causes conflicts or constraint violations, you must resolve them using the dolt_conflicts system tables before the transaction can be committed. See Dolt system tables for details.
Output Schema
Example
DOLT_PURGE_DROPPED_DATABASES()
DOLT_PURGE_DROPPED_DATABASES()Permanently deletes any dropped databases that are being held in a temporary holding area. When a Dolt database is dropped, it is moved to a temporary holding area where the dolt_undrop() stored procedure can restore it. The dolt_purge_dropped_databases() stored procedure clears this holding area and permanently deletes any data from those databases. This action is not reversible, so callers should be cautious about using it. The main benefit of using this function is to reclaim disk space used by the temporary holding area. Because this is a destructive operation, callers must have SUPER privileges in order to execute it.
Example
DOLT_PUSH()
DOLT_PUSH()Updates remote refs using local refs, while sending objects necessary to complete the given refs. Works exactly like dolt push on the CLI, and takes the same arguments.
Options
--force: Update the remote with local history, overwriting any conflicting history in the remote.
Output Schema
Example
DOLT_REBASE()
DOLT_REBASE()Rewrites commit history for the current branch by replaying commits, allowing the commits to be reordered, squashed, or dropped. The commits included in the rebase plan are the commits reachable by the current branch, but NOT reachable from the branch specified as the argument when starting a rebase (also known as the upstream branch). This is the same as Git and Dolt's "two dot log" syntax, or |upstreamBranch|..|currentBranch|.
For example, consider the commit graph below, where a feature branch has branched off of a main branch, and both branches have added commits:
If we rebase from the feature branch using the main branch as our upstream, the default rebase plan will include commits G, H, and I, since those commits are reachable from our current branch, but NOT reachable from the upstream branch. By default, the changes from those same commits will be reapplied, in the same order, to the tip of the upstream branch main. The resulting commit graph will then look like:
Rebasing is useful to clean and organize your commit history, especially before merging a feature branch back to a shared branch. For example, you can drop commits that contain debugging or test changes, or squash or fixup small commits into a single commit, or reorder commits so that related changes are adjacent in the new commit history.
Limitations
Currently only interactive rebases are supported. Conflict resolution for data conflicts is supported through Dolt's standard conflict resolution process, but conflict resolution for schema conflicts is not supported. If a rebase encounters a schema conflict, the rebase will be automatically aborted.
Options
--interactive or -i: Start an interactive rebase. Currently only interactive rebases are supported, so this option is required.
--continue: Continue an interactive rebase after adjusting the rebase plan stored in dolt_rebase.
--abort: Abort a rebase in progress.
--empty: How to handle commits that are not empty to start, but which become empty after rebasing. Valid values are: drop (default) or keep. This option may only be specified when starting a rebase, and is not valid when continuing a rebase.
Output Schema
Example
DOLT_REMOTE()
DOLT_REMOTE()Adds a remote for a database at given url, or removes an existing remote with its remote-tracking branches and configuration settings. Similar to dolt remote command on the CLI, with the exception of cloud provider flags. To list existing remotes, use the dolt_remotes system table.
Output Schema
Example
DOLT_RESET()
DOLT_RESET()Default mode resets staged tables to their HEAD state. Can also be used to reset a database to a specific commit. Works exactly like dolt reset on the CLI, and takes the same arguments.
Like other data modifications, after a reset you must COMMIT the transaction for any changes to affected tables to be visible to other clients.
Notes
With the
--hardoption, thedolt_reset()procedure implicitly commits the current transaction and begins a new one.
Options
--hard: Resets the working tables and staged tables. Any changes to tracked tables in the working tree since are discarded.
--soft: Does not touch the working tables, but removes all tables staged to be committed. This is the default behavior.
Output Schema
Example
DOLT_REVERT()
DOLT_REVERT()Reverts the changes introduced in a commit, or set of commits. Creates a new commit from the current HEAD that reverses the changes in all the specified commits. If multiple commits are given, they are applied in the order given.
Options
--author=<author>: Specify an explicit author using the standard A U Thor <[email protected]> format.
Output Schema
Example
DOLT_RM()
DOLT_RM()Default mode removes tables from the staging area and working directory. Works exactly like dolt rm on the CLI, and takes the same arguments. When used with the --cached flag, removes tables only from the staging area while leaving the working directory unchanged.
Options
--cached: Use this option to unstage and remove tables only from the staging area. Working tree tables, whether modified or not, will be left alone.
Output Schema
Example
DOLT_STASH()
DOLT_STASH()Manage temporary saves of uncommitted changes. Changes can be saved, restored, or removed without affecting the commit history. Similar to the dolt stash command on the cli. An important exception is that the procedure requires a push subcommand, and cannot be called without arguments to stash away changes. To list existing stashes, use the dolt_stashes system table.
Subcommands
Push (Save changes)
Saves current working directory and staged changes to a named stash. By default, only tracks changes to already-tracked tables.
--include-untracked,-u: Include untracked tables in the stash--all,-a: Include all changes (tracked, untracked, and ignored tables)
Pop (Restore and remove)
Applies the changes from the specified stash to the working directory and removes the stash. If conflicts occur, the operation is aborted.
Drop
Removes the specified stash without applying the changes. If no number is specified, removes most recent stash for the given name.
Clear
Removes all stashes for the specified stash name.
Examples
DOLT_TAG()
DOLT_TAG()Creates a new tag that points at specified commit ref, or deletes an existing tag. Works exactly like dolt tag command on the CLI, and takes the same arguments except for listing tags. To list existing tags, use dolt_tags system table.
Options
-m: Use the given message as the tag message.
-d: Delete a tag.
--author: Specify an explicit author using the standard "A U Thor [email protected]" format.
Output Schema
Example
DOLT_UNDROP()
DOLT_UNDROP()Restores a dropped database. See the dolt_purge_dropped_databases() stored procedure for info on how to permanently remove dropped databases.
Options
dolt_undrop() takes a single argument – the name of the dropped database to restore. When called without any arguments, dolt_undrop() returns an error message that contains a list of all dropped databases that are available to be restored.
Example
Usage Notes
Dropped databases are moved to the .dolt_dropped_databases directory in the Dolt data directory. If a database with the same name is dropped multiple times, the previous copy will be renamed to <database_name>.backup.<timestamp>. This enables you to restore a previously dropped database, even if it was recreated and dropped again. To restore a previous version, rename the backup directory to the original database name and then call dolt_undrop('<database_name>'). If you do not rename the directory and use the name with the timestamp when you call dolt_undrop(), then the database will be restored with the timestamp in the name.
DOLT_UPDATE_COLUMN_TAG()
DOLT_UPDATE_COLUMN_TAG()Updates a column's internal identifier. Most users will never need to know about column tags, but there are some rare cases where a column tag collision can occur during a merge. In those cases, it can be useful to manually update a column's tag. This is an advanced operation, so use with caution and reach out to the Dolt team for questions or guidance on Discord or GitHub.
Note that the dolt_update_column_tag() stored procedure updates a column's tag in the working set, so users must call dolt_commit() after updating the tag to commit the changes to the HEAD of the current branch. Column tag changes do not currently show up in working set status or diffs, so users should be careful to commit the changes immediately after updating the tag to avoid confusion around a dirty working set without a visible diff.
Arguments and Options
<table>: The table containing the column to update.
<column>: The name of the column to update.
<tag>: An integer value to set for the column's new tag.
Output Schema
Example
DOLT_VERIFY_CONSTRAINTS()
DOLT_VERIFY_CONSTRAINTS()Verifies that working set changes (inserts, updates, and/or deletes) satisfy the defined table constraints. If any constraints are violated they are written to the DOLT_CONSTRAINT_VIOLATIONS table.
DOLT_VERIFY_CONSTRAINTS by default does not detect constraints for row changes that have been previously committed. The --all option can be specified if you wish to validate all rows in the database. If FOREIGN_KEY_CHECKS has been disabled in prior commits, you may want to use the --all option to ensure that the current state is consistent and no violated constraints are missed.
Arguments and Options
<table>: The table(s) to check constraints on. If omitted, checks all tables.
-a, --all: Verifies constraints against every row.
-o, --output-only: Disables writing results to the DOLT_CONSTRAINT_VIOLATIONS system table.
Output Schema
Example
For the below examples consider the following schema:
A simple case:
Using --all to verify all rows:
Checking specific tables only:
Statistics Updates
Control functions are used to start and stop background thread activity related to statistics updates. See stats documentation for more information.
dolt_stats_restart()
dolt_stats_restart()If no thread is active for the current database, start a new update thread with the current session's parameters (dolt_stats_memory_only, dolt_stats_job_interval, dolt_stats_gc_enabled). If a thread is already active for this database, the thread is stopped and started with the new parameters.
dolt_stats_stop()
dolt_stats_stop()Clears the work queue and stops the thread (otherwise no-op).
dolt_stats_purge()
dolt_stats_purge()Deletes the stats cache from memory and the filesystem. Also clearing working queue and stop the update thread.
dolt_stats_once():
dolt_stats_once():This command collects statistics once. It should be used when no background thread is running (ex: in dolt sql and when we do not wish to run a background thread).
dolt_stats_wait():
dolt_stats_wait():Blocks on a full queue cycle. In practice it takes at least one cycle for stats to reflect the contents of the database stats in the blocking session.
dolt_stats_gc():
dolt_stats_gc():Blocks waiting for a GC signal. Garbage collection finalizes in the same cadence as new statistic updates.
dolt_stats_flush():
dolt_stats_flush():Blocks waiting on a flush signal. Flushes occur after new statistic updates.
dolt_stats_info():
dolt_stats_info():Returns the current state of the stats provider (optional '-short' flag).
Access Control
Dolt stored procedures are access controlled using the GRANT permissions system. MySQL database permissions trickle down to tables and procedures, someone who has Execute permission on a database would have Execute permission on all procedures related to that database. Dolt deviates moderately from this behavior for sensitive operations. See Administrative Procedures below.
Users who need common Dolt capability such as adding and committing to a branch will need Execute permission granted on the database in question. As a privileged user, you can grant access with the following command:
This will give the user, pat, the ability run all stored procedures on the database. This includes Dolt procedures as well as user defined procedures. If you need to use fine grained permissions, you can grant them individually:
If you need to remove access for a particular capability, REVOKE as follows:
Administrative Procedures
The follow procedures are considered administrative, and as a result users are required to have explicit grants to use them.
dolt_backup
dolt_clone
dolt_fetch
dolt_undrop
dolt_purge_dropped_databases
dolt_gc
dolt_pull
dolt_push
dolt_remote
For example, if a service account requires the ability to start dolt_gc, then it must have specific permissions to do so:
dolt_push(), dolt_fetch(), and dolt_pull() are considered administrative operations currently because they all use a shared credential to talk to remote servers. User level access to remotes, and the ability to store user level credentials for them is on our roadmap.
The root user, or any other user with super privileges is allowed to call all procedures.
Last updated
Was this helpful?