Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1050 from knz/name-resolution
Tell a bit more about SQL name resolution.
- Loading branch information
Showing
4 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
title: Name Resolution | ||
summary: Table and function names can exist in multiple places. Resolution decides which one to use. | ||
toc: false | ||
--- | ||
|
||
A SQL client can have access to multiple databases side-by-side. The | ||
same table name (e.g., `orders`) can exist in multiple | ||
databases. When a query specifies a table name without a database | ||
name (e.g., `select * from orders`), how does CockroachDB know | ||
which `orders` table is being considered? | ||
|
||
This page details how CockroachDB performs *name resolution* to answer | ||
this question. | ||
|
||
<div id="toc"></div> | ||
|
||
## Overview | ||
|
||
The following *name resolution algorithm* is used both to determine | ||
table names in [table expressions](table-expressions.html) and | ||
function names in [value expressions](sql-expressions.html): | ||
|
||
- If the name is *qualified* (i.e., the name already tells where to look), use this information. | ||
For example, `SELECT * FROM db1.orders` will look up "`orders`" only in `db1`. | ||
- If the name is *unqualified*: | ||
- Try to find the name in the "default database" as set by [`SET DATABASE`](set-database.html). | ||
- Try to find the name using the [search path](#search-path). | ||
- If the name is not found, produce an error. | ||
|
||
## Search path | ||
|
||
In addition to the default database configurable via [`SET DATABASE`](set-database.html), | ||
unqualified names are also looked up in the current session's *search path*. | ||
|
||
The search path is a session variable containing a list of databases, | ||
or *name spaces*, where names are looked up. | ||
|
||
The current search path can be inspected using the statement `SHOW | ||
SEARCH_PATH`, or [`SHOW ALL`](show-all.html). | ||
|
||
By default, the search path for new columns includes just | ||
`pg_catalog`, so that queries can use PostgreSQL compatibility | ||
functions and virtual tables in that namespace without the need to | ||
prefix them with "`pg_catalog.`" every time. | ||
|
||
## See also | ||
|
||
- [List of predefined databases](predefined-namespaces.html) | ||
- [`SET DATABASE`](set-database.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters