forked from cockroachdb/cockroach
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: Add locality() builtin function
There is currently no easy way to programmatically inspect the locality of the current node. It appears in crdb_internal.gossip_nodes, but it's hard to work with because it's JSON, not constant-foldable, and keyed by node id. This commit adds a new locality builtin function that: Returns the hierarchical location of the current node as a tuple of labeled values, ordered from most inclusive to least inclusive. For example: `region=east,datacenter=us-east-1`. When building geo-distributed applications, this enables a very nice way to automatically assign the partition key, as illustrated below: CREATE TABLE charges ( region STRING NOT NULL DEFAULT (locality()).region, id UUID NOT NULL DEFAULT gen_random_uuid(), ... The DEFAULT expression for the region column automatically inserts the region value from the current node's locality. It also enables queries like this, that only touch rows in the current region (which is necessary to avoid cross-region hops): SELECT * FROM charges WHERE region = (locality()).region AND id = $1 The locality is constant, so the optimizer is able to fold column access to a constant value, which can then be used to select an optimal index. Resolves cockroachdb#37310 Release note (sql change): Adds a new locality builtin function that returns the hierarchical location of the current node as a tuple of labeled values, ordered from most inclusive to least inclusive.
- Loading branch information
1 parent
856ba91
commit fe7ded3
Showing
15 changed files
with
128 additions
and
18 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,19 @@ | ||
# LogicTest: 5node-dist 5node-dist-opt | ||
|
||
query T | ||
SELECT locality() | ||
---- | ||
(test,dc1) | ||
|
||
query T | ||
SELECT (locality()).region | ||
---- | ||
test | ||
|
||
query T | ||
SELECT (locality()).dc | ||
---- | ||
dc1 | ||
|
||
statement error could not identify column "unk" in tuple{string AS region, string AS dc} | ||
SELECT (locality()).unk |
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
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
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
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