Project: YugabyteDB (App Definition and Development / Database)
File: func_yb_server_region.md

title: ybserverregion() function [YSQL] headerTitle: ybserverregion() linkTitle: ybserverregion() description: Returns the region of the currently connected node menu: v2.18: identifier: api-ysql-exprs-ybserverregion parent: geo-partitioning-helper-functions type: docs `ybserverregion()` returns the region of the currently connected node. Call `ybserverregion()` ```plpgsql yugabyte=# SELECT ybserverregion(); ``` ```output.sql ybserverregion -- us-west-1 (1 row) ``` This function is primarily helpful while implementing , as it can significantly simplify inserting rows from the user's connected node. Do the following to create a 3-node multi-region cluster and a geo-partitioned table using tablespaces: Create a cluster spread across 3 regions us-west-1, us-east-1, us-east-2 using yugabyted: ```sh ./bin/yugabyted start \ --base_dir=/home/yugabyte/<IP1>/yugabyte-data \ --listen=<IP1> \ --masterflags "placementcloud=aws,placementregion=us-west-1,placementzone=us-west-1c" \ --tserverflags "placementcloud=aws,placementregion=us-west-1,placementzone=us-west-1c" ./bin/yugabyted start \ --base_dir=/home/yugabyte/<IP2>/yugabyte-data \ --listen=<IP2> \ --join=<IP1> \ --masterflags "placementcloud=aws,placementregion=us-east-2,placementzone=us-east-2c" \ --tserverflags "placementcloud=aws,placementregion=us-east-2,placementzone=us-east-2c" ./bin/yugabyted start \ --base_dir=/home/yugabyte/<IP3>/yugabyte-data \ --listen=<IP3> \ --join=<IP1> \ --masterflags "placementcloud=aws,placementregion=us-east-1,placementzone=us-east-1a" \ --tserverflags "placementcloud=aws,placementregion=us-east-1,placementzone=us-east-1a" ``` Use to specify the placement configuration to be used by the cluster: ```sh ./bin/yb-admin -masteraddresses <IP1>:7100 modifyplacement_info aws.us-west-1.us-west-1c:1,aws.us-east-1.us-east-1a:1,aws.us-east-2.us-east-2c:1 3 ``` Create tablespaces corresponding to the regions used by the cluster created above : ```sql CREATE TABLESPACE uswesttablespace WITH (replicaplacement=' {"numreplicas":1,"placementblocks":[{"cloud":"aws","region":"us-west-1","zone":"us-west-1c","minnum_replicas":1}]}'); CREATE TABLESPACE useast1tablespace WITH (replicaplacement=' {"numreplicas":1,"placementblocks":[{"cloud":"aws","region":"us-east-1","zone":"us-east-1a","minnum_replicas":1}]}'); CREATE TABLESPACE useast2tablespace WITH (replicaplacement=' {"numreplicas":1,"placementblocks":[{"cloud":"aws","region":"us-east-2","zone":"us-east-2c","minnum_replicas":1}]}'); ``` For more information on how to set up a cluster with or with corresponding tablespaces, see . Using the tablespaces, you can create a geo-partitioned table as follows. This is a partitioned table with 3 partitions, where each partition is pinned to a different location based on the regions. The geopartition column value is default to be the currently connected region as in `ybserver_region()`. ```sql CREATE TABLE users(user_id INTEGER NOT NULL, user_info VARCHAR NOT NULL, geopartition VARCHAR DEFAULT ybserver_region(), PRIMARY KEY(userid, geopartition)) PARTITION BY LIST(geo_partition); CREATE TABLE useruswest PARTITION OF users FOR VALUES IN ('us-west-1') TABLESPACE uswesttablespace; CREATE TABLE useruseast1 PARTITION OF users FOR VALUES IN ('us-east-1') TABLESPACE useast1tablespace; CREATE TABLE useruseast2 PARTITION OF users FOR VALUES IN ('us-east-2') TABLESPACE useast2tablespace; ``` Insert data to the `users` table: If your server is connected to region `us-west-1`, you can insert rows into the `users` table without having to specify the `geo_partition` column value. ```sql INSERT INTO users VALUES(1, 'US West user'); ``` If your server is connected to region `us-west-1` and you want to insert rows into another region's partitioned table, you can still insert the rows normally. ```sql INSERT INTO users VALUES(2, 'US East 1 user', 'us-east-1'); ``` In a partitioned setup, if there are no `WHERE` clause restrictions on the partition key, note that every query on a partitioned table gets fanned out to all of its child partitions: ```sql EXPLAIN (COSTS OFF) SELECT * FROM users; ``` ```output QUERY PLAN Append -> Seq Scan on useruseast2 -> Seq Scan on useruseast1 -> Seq Scan on useruswest (4 rows) ``` Assuming that the client is in the `us-west-1` region, using `ybserverregion()` in the `WHERE` clause causes YSQL to only scan the `ususerwest` table: ```sql EXPLAIN (COSTS OFF) SELECT * FROM users WHERE geopartition=ybserver_region(); ``` ```output QUERY PLAN -- Append Subplans Removed: 2 -> Seq Scan on useruswest Filter: ((geopartition)::text = (ybserver_region())::text) (4 rows) ``` In other words, using `ybserverregion()` in the `WHERE` clause automatically returns only values from your current region. ```sql SELECT * FROM users WHERE geopartition=ybserver_region(); ``` ```output userid | userinfo | geo_partition +--+ 1 | US West user | us-west-1 (1 row) ``` {{< note title="Note" >}} If you didn't set the placementzone flag at node startup, ybserver_zone() returns NULL. {{< /note >}}