After you enable data sharing for instances, you must execute SQL statements to create a share and authorize destination databases to consume the share. This topic describes SQL operations for data sharing in AnalyticDB for PostgreSQL in Serverless mode.

Precautions

  • The instances to which the source and destination databases belong must reside within the same region and have been added for data sharing. For more information about how to add instances for data sharing, see Enable or disable data sharing for instances.
  • Only tables can be shared.
  • A share can contain up to 1,000 tables.
  • If the source instance contains multiple shares, the destination database can subscribe to only a single share.
  • The destination database cannot perform write operations on shared tables.
  • DDL operations are not allowed on shared tables. To perform DDL operations on a shared table, you must cancel sharing of the table. For more information, see the "Remove a table" section of this topic.

Create a share

The following statement is used to create a share in the source database:

CREATE DATASHARE <share_name>;

The following table describes the parameter.

Parameter Description
share_name The name of the share to be created. The name must be 1 to 90 characters in length.

Example:

CREATE DATASHARE s01;

Delete a share

The following statement is used to delete a share from the source database. After you delete a share from the source database, the destination database can no longer access data of the share.

DROP DATASHARE <share_name>;

The following table describes the parameter.

Parameter Description
share_name The name of the share to be deleted.

Example:

DROP DATASHARE s01;

Grant permissions on a share

The following statement is used to grant subscription permissions on a share of the source database to the destination database. The destination database can subscribe to the share only after the permissions are granted.

GRANT USAGE ON DATASHARE <share_name> TO DATABASE <target_id>;

The following table describes the parameters.

Parameter Description
share_name The name of the share.
target_id The ID of the destination database. It must be in the universally unique identifier (UUID) format. For more information about how to obtain the database ID, see the "Query the ID of a database" section of this topic.

Example:

GRANT USAGE ON DATASHARE s01 TO DATABASE "c227516c-3802-4974-86e9-53**********";

Revoke permissions on a share

The following statement is used to revoke subscription permissions on a share of the source database from the destination database. After the permissions on a share are revoked, the destination database can no longer access data of the share.

REVOKE USAGE ON DATASHARE <share_name> FROM DATABASE <target_id>;

The following table describes the parameters.

Parameter Description
share_name The name of the share.
target_id The ID of the destination database. It musin the UUID format. For more information about how to obtain the database ID, see the "Query the ID of a database" section of this topic.

Example:

REVOKE USAGE ON DATASHARE s01 FROM DATABASE "c227516c-3802-4974-86e9-53**********";

Add a table

The following statement is used to add a table to a share. After you add a table to a share, you can query the table from the destination database that has subscribed to the share.

ALTER DATASHARE <share_name> ADD TABLE <table_name>;

The following table describes the parameters.

Parameter Description
share_name The name of the share.
table_name The name of the table to be added to the share.

Example:

ALTER DATASHARE s01 ADD TABLE t1_1;

Remove a table

The following statement is used to remove a table from a share:

ALTER DATASHARE <share_name> REMOVE TABLE <table_name>;

The following table describes the parameters.

Parameter Description
share_name The name of the share.
table_name The name of the table to be removed from the share.

Example:

ALTER DATASHARE s01 REMOVE TABLE t1_1;

Subscribe to a share

The following statement is used to subscribe to a share of the source database from the destination database. To query data of a share in the source destination, the destination database must obtain permissions on the share and subscribe to the share.

IMPORT DATASHARE <share_name> [AS <local_share_name>] FROM DATABASE <source_id>;

The following table describes the parameters.

Parameter Description
share_name The name of the share for subscription.
local_share_name The alias of the share set on the destination database.
source_id The ID of the source database. It must be in the UUID format. For more information about how to obtain the database ID, see the "Query the ID of a database" section of this topic.

Example:

IMPORT DATASHARE s01 AS s01a FROM DATABASE "c227516c-3802-4974-86e9-53**********";

Unsubscribe from a share

The following statement is used to unsubscribe from a share. After you unsubscribe from a share, the destination database can no longer access data of the share.

DROP SHARE <share_name>;

The following table describes the parameter.

Parameter Description
share_name The name of the share for unsubscription.

Example:

DROP SHARE s01;

Query data

The following statement is used to query data of a share from the destination database:

SELECT * FROM <share_name.schema_name.table_name>;

The following table describes the parameter.

Parameter Description
share_name.schema_name.table_name The name of the shared table to be queried from the destination database. It consists of the following fields:
  • share_name: the alias of the share set on the destination database. If no alias is set, it can be the name of the share.
  • schema_name: the name of the schema of the source database that contains the shared table.
  • table_name: the name of the shared table.

Example:

SELECT * FROM s01.public.t1_1;

Query the ID of a database

The following statement is used to query the ID of a database. The ID must be in the UUID format.

Note The database ID query statement can be used to query only the current database. To query the ID of another database, switch to the corresponding database and execute a query statement.
SELECT current_database_uuid();