### Install PostgreSQL SDK Source: https://www.volcengine.com/docs/6438/1257453 Commands to install or upgrade the SDK for specific or all users. ```bash python setup.py install -- ``` ```bash sudo python setup.py install ``` ```bash pip install --upgrade volcengine ``` -------------------------------- ### CreateDBInstance Request Example Source: https://www.volcengine.com/docs/6438/1159933?lang=zh An example of the HTTP POST request structure for creating a new database instance. ```json POST /?Action=CreateDBInstance&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z ``` -------------------------------- ### Example Command Line Connection to PostgreSQL Source: https://www.volcengine.com/docs/6438/79237?lang=zh An example of the psql command with specific connection details for a PostgreSQL instance. ```bash psql -h postgresbcc087e73689****.rds-pg.ivolces.com -p 5432 -U testuser -d db01 ``` -------------------------------- ### Create hypopg Extension Source: https://www.volcengine.com/docs/6438/2222195?lang=zh Use this SQL command to install the hypopg plugin. Verify the installation by querying the pg_extension view. ```sql --创建 create extension hypopg; --查看是否创建成功 SELECT * FROM pg_extension WHERE extname = 'hypopg'; ``` -------------------------------- ### Install Node.js Source: https://www.volcengine.com/docs/6438/1130800?lang=zh Install Node.js on a Debian-based system using the NodeSource repository. ```shell sudo apt-get update sudo apt-get install -y ca-certificates curl gnupg sudo mkdir -p /etc/apt/keyrings curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg NODE_MAJOR=16 echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list sudo apt-get update sudo apt-get install nodejs -y ``` -------------------------------- ### Install pg_stat_kcache Plugin Source: https://www.volcengine.com/docs/6438/1126563?lang=zh Commands to install the required extensions for tracking statistics. ```APIDOC ## SQL CREATE EXTENSION ### Description Installs the necessary extensions to enable file system read/write statistics tracking. ### Method SQL ### Request Example ```sql CREATE EXTENSION pg_stat_statements; CREATE EXTENSION pg_stat_kcache; ``` ``` -------------------------------- ### Example POST Request to DescribeDBInstances Source: https://www.volcengine.com/docs/6438/1159935?lang=zh This is an example of a POST request to the DescribeDBInstances API. Ensure the 'Host' and 'Authorization' headers are correctly formatted. ```http POST /?Action=DescribeDBInstances&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z Authorization: HMAC-SHA256 Credential=AKLTN2I0MmFiNzMxNWE5NDgzMzk4MmVjMTVkODlkZTZ****/20211202/cn-beijing/rds_postgresql/request,SignedHeaders=x-date, Signature=71d31fc7bcf990142851c9833c5656391486cda0ae2e0b8ada733b7c6458**** { "PageSize": 10, "PageNumber": 1, "ChargeType": "PostPaid", "InstanceStatus": "Running", "CreateTimeStart": "2024-07-24T09:05:36.000Z", "TagFilters": [ { "Key": "project", "Value": "default" } ] } ``` -------------------------------- ### Create Allow List Response Example Source: https://www.volcengine.com/docs/6438/1257390 This is an example of a successful response from the CreateAllowList API, returning the ID of the newly created whitelist. ```json { "ResponseMetadata": { "RequestId": "2024042319123272910D61D1EA53CF50B2-0a580d", "Action": "CreateAllowList", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-guangzhou" }, "Result": { "AllowListId": "acl-2b7d64502b0a4210a24c58e739de****" } } ``` -------------------------------- ### DescribeBackups API Request Example Source: https://www.volcengine.com/docs/6438/1158713 This is an example of a POST request to the DescribeBackups API action. Ensure the Host, X-Date, and Authorization headers are correctly configured for your environment. ```json POST /?Action=DescribeBackups&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z Authorization: HMAC-SHA256 Credential=AKLTN2I0MmFiNzMxNWE5NDgzMzk4MmVjMTVkODlkZTZ****/20211202/cn-beijing/rds_postgresql/request,SignedHeaders=x-date, Signature=71d31fc7bcf990142851c9833c5656391486cda0ae2e0b8ada733b7c6458**** { "InstanceId": "postgres-a3e7eb2f****" } ``` -------------------------------- ### Create Allow List Request Example Source: https://www.volcengine.com/docs/6438/1257390 This is an example of a POST request to create a whitelist using the CreateAllowList API. Ensure the Content-Type is application/json and include necessary headers like X-Date and Authorization. ```http POST /?Action=CreateAllowList&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z Authorization: HMAC-SHA256 Credential=AKLTN2I0MmFiNzMxNWE5NDgzMzk4MmVjMTVkODlkZTZ****/20211202/cn-beijing/rds_postgresql/request,SignedHeaders=x-date, Signature=71d31fc7bcf990142851c9833c5656391486cda0ae2e0b8ada733b7c6458**** { "AllowListDesc": "这是一个测试白名单", "AllowListName": "测试白名单", "AllowList": "10.1.1.1,10.2.3.0/24", "SecurityGroupIds": [ "sg-13fuw9bo2hlhc3n6nu4g****" ] } ``` -------------------------------- ### DescribeDBInstanceProxyParameters Response Example Source: https://www.volcengine.com/docs/6438/1491538 This is an example JSON response from the DescribeDBInstanceProxyParameters API, detailing the instance ID, database engine version, parameter count, and a list of proxy parameters with their configurations. ```json { "ResponseMetadata": { "RequestId": "2025031919422828B3EE5B6F629AFF0F74-bb2104", "Action": "DescribeDBInstanceProxyParameters", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": { "DBEngineVersion": "PostgreSQL_17", "InstanceId": "postgres-f511b775****", "ParameterCount": "1", "Parameters": [ { "CheckingCode": "[20-200]", "DefaultValue": "200", "Description": "After the endpoint opens the read-write split, the corresponding proxy connection number parameter.", "DescriptionZH": "终端开了读写分离后,其所对应的代理的连接数参数。", "ForceRestart": true, "Name": "ReadWriteProxyConnection", "Type": "integer", "Value": "20" } ] } } ``` -------------------------------- ### Initialize PostgreSQL Client Source: https://www.volcengine.com/docs/6438/1257453 Setup the configuration with AK, SK, region, and host to instantiate the API client. ```python import volcenginesdkcore import volcenginesdkrdspostgresql as pg from volcenginesdkcore.rest import ApiException if __name__ == "__main__": configuration = volcenginesdkcore.Configuration() # 设置 AKSK 验证信息及 Region configuration.ak = "Your AK" configuration.sk = "Your SK" configuration.region = "Your Region" configuration.host = "Your Host" try: # 创建 PostgreSQL SDK 客户端,接口调用均需要通过此客户端执行 api_instance = pg.RDSPOSTGRESQLApi(volcenginesdkcore.ApiClient(configuration)) except ApiException as e: print("Exception: %s\n" % e) ``` -------------------------------- ### Install TypeScript Dependencies Source: https://www.volcengine.com/docs/6438/1130800?lang=zh Install project dependencies and the tsx runner. ```shell pnpm run setup pnpm install tsx ``` -------------------------------- ### Install pnpm Source: https://www.volcengine.com/docs/6438/1130800?lang=zh Install the pnpm package manager on your Linux environment. ```shell curl -fsSL https://get.pnpm.io/install.sh | sh - ``` -------------------------------- ### Create PostgreSQL Instance Source: https://www.volcengine.com/docs/6438/1257453 Example of creating a new database instance with specific engine version, node configuration, and storage settings. ```python from pprint import pprint import volcenginesdkcore import volcenginesdkrdspostgresql as pg from volcenginesdkcore.rest import ApiException if __name__ == "__main__": configuration = volcenginesdkcore.Configuration() configuration.ak = "Your AK" configuration.sk = "Your SK" configuration.region = "Your Region" configuration.host = "Your Host" try: api_instance = pg.RDSPOSTGRESQLApi(volcenginesdkcore.ApiClient(configuration)) resp = api_instance.create_db_instance(pg.CreateDBInstanceRequest( db_engine_version="PostgreSQL_12", node_info=[ pg.NodeInfoForCreateDBInstanceInput( zone_id="cn-beijing-a", node_type="Primary", node_spec="rds.postgres.1c2g", node_operate_type="Create" ), pg.NodeInfoForCreateDBInstanceInput( zone_id="cn-beijing-a", node_type="Secondary", node_spec="rds.postgres.1c2g", node_operate_type="Create" ), ], storage_type="LocalSSD", storage_space=20, vpc_id="vpc-2bzo8w6m87v282dx0eemg****", subnet_id="subnet-2d6b6byrhtts058ozfenq****", charge_info=pg.ChargeInfoForCreateDBInstanceInput(charge_type="PostPaid"), instance_name="testname", )) pprint(resp) except ApiException as e: print("Exception: %s\n" % e) ``` -------------------------------- ### 创建 Server 数据库连接 Source: https://www.volcengine.com/docs/6438/1456338?lang=zh 在 'profile' 数据库中执行此命令,创建一个指向 'server' 数据库的连接。'create_server' 函数用于配置连接名、地址、端口、数据库名、用户名和密码。 ```sql profile=> SELECT create_server('server', 'host=$server数据库所在实例的连接地址 dbname=server port=$server数据库所在实例的端口号 user=$server数据库的高权限账号 password=$账号对应的密码'); ``` -------------------------------- ### CreateSchema API Request and Response Examples Source: https://www.volcengine.com/docs/6438/1158733?lang=zh Examples of the HTTP request and JSON response for the CreateSchema operation. ```json POST /?Action=CreateSchema&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z Authorization: HMAC-SHA256 Credential=AKLTN2I0MmFiNzMxNWE5NDgzMzk4MmVjMTVkODlkZTZ****/20211202/cn-beijing/rds_postgresql/request,SignedHeaders=x-date, Signature=71d31fc7bcf990142851c9833c5656391486cda0ae2e0b8ada733b7c6458**** { "InstanceId": "postgres-ca7b7019****", "DBName": "testdb1", "SchemaName": "testschema", "Owner": "User01" } ``` ```json { "ResponseMetadata": { "RequestId": "202308041039157B21482F51ABF9040FDC", "Action": "CreateSchema", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": null } ``` -------------------------------- ### 初始化 SysBench 测试数据 Source: https://www.volcengine.com/docs/6438/104932?lang=zh 使用 prepare 命令根据目标库大小初始化测试数据。 ```bash sysbench ./tests/include/oltp_legacy/oltp.lua \ --db-driver=pgsql \ --pgsql-db=sbtest \ --pgsql-user= \ --pgsql-password= \ --pgsql-port=5432 \ --pgsql-host= \ --oltp-tables-count=64 \ --oltp-table-size=10000000 \ --time=180 \ --max-requests=0 \ --threads=20 \ --report-interval=5 \ --forced-shutdown=1 \ prepare ``` -------------------------------- ### 创建全文索引 Source: https://www.volcengine.com/docs/6438/1386382 为 'test' 表的 'introduce' 字段创建全文索引。该索引使用 GIN 类型,并结合了 'testzhcfg' 配置和 `upper()` 函数来提高搜索的准确性和效率。示例中包含了创建测试表、插入数据以及创建索引的 SQL 语句。 ```sql -- 创建测试表 CREATE TABLE test(introduce text); INSERT INTO test VALUES ('使用RDS for PostgreSQL数据库,客户无需再关心底层硬件资源,降低使用成本,提高整体效率'); INSERT INTO test VALUES ('兼容多种开源的插件,用户自由化管理,不同的数据类型及索引类型支持多种应用场景'); INSERT INTO test VALUES ('按需开通,即开即用,灵活升降配置,与PostgreSQL原生接口一致,无需应用改造,全托管,免运维'); -- 创建索引 CREATE INDEX idx ON test USING gin (to_tsvector('testzhcfg',upper(introduce))); ``` -------------------------------- ### 配置 PostgreSQL 客户端环境变量 Source: https://www.volcengine.com/docs/6438/907280?lang=zh 配置 pg_repack 客户端所需的环境变量,包括 PG_HOME、PG_CONFIG 以及相关的 include 和 library 路径。这些变量对于编译和运行 pg_repack 至关重要。 ```bash export PG_HOME=/usr/local/pgsql-12.13 export PG_CONFIG=${PG_HOME}/bin/pg_config export PATH=${PG_HOME}/bin:${PATH} export PG_INCLUDE_DIR=$(${PG_HOME}/bin/pg_config --includedir) export PG_PKG_INCLUDE_DIR=$(${PG_HOME}/bin/pg_config --pkgincludedir) export PG_SERVER_INCLUDE_DIR=$(${PG_HOME}/bin/pg_config --includedir-server) export PG_LIB_DIR=$(${PG_HOME}/bin/pg_config --libdir) export PG_PKG_LIB_DIR=$(${PG_HOME}/bin/pg_config --pkglibdir) export LD_LIBRARY_PATH=${PG_LIB_DIR}:${PG_PKG_LIB_DIR}${LD_LIBRARY_PATH+:${LD_LIBRARY_PATH}} export C_INCLUDE_PATH=${PG_INCLUDE_DIR}:${PG_PKG_INCLUDE_DIR}:${PG_SERVER_INCLUDE_DIR}${C_INCLUDE_PATH+:${C_INCLUDE_PATH}} export CPLUS_INCLUDE_PATH=${PG_INCLUDE_DIR}:${PG_PKG_INCLUDE_DIR}:${PG_SERVER_INCLUDE_DIR}${CPLUS_INCLUDE_PATH+:${CPLUS_INCLUDE_PATH}} ``` -------------------------------- ### DeleteSlot API Response Example Source: https://www.volcengine.com/docs/6438/1323548 This is an example of a successful response from the DeleteSlot API, showing the ResponseMetadata and a null Result. ```json { "ResponseMetadata": { "RequestId": "202407181609044F08B2D5162A85A75434-9bd837", "Action": "DeleteSlot", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": null } ``` -------------------------------- ### Example: Creating and Using HNSW Index Source: https://www.volcengine.com/docs/6438/1126562?lang=zh Demonstrates creating an HNSW index with parallel workers and adjusting search accuracy by setting the `hnsw.ef_search` parameter. ```SQL -- Create an HNSW index with parallel workers and specific construction parameters. SET maintenance_work_mem = '1GB'; SET max_parallel_workers = 4; SET max_parallel_maintenance_workers = 4; DROP INDEX IF EXISTS tbl_vector_tc1_idx; CREATE INDEX tbl_vector_tc1_idx ON tbl_vector USING hnsw (tc1 vector_l2_ops) WITH (m = 10, ef_construction = 20); -- Example: Searching with HNSW index (Euclidean distance). -- High recall scenario (higher ef_search): DROP INDEX IF EXISTS tbl_vector_tc1_idx; CREATE INDEX tbl_vector_tc1_idx ON tbl_vector USING hnsw (tc1 vector_l2_ops) WITH (m = 10, ef_construction = 20); SET hnsw.ef_search = 100; -- Higher ef_search for potentially better recall. SELECT * FROM tbl_vector ORDER BY tc1 <-> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Low recall scenario (lower ef_search): DROP INDEX IF EXISTS tbl_vector_tc1_idx; CREATE INDEX tbl_vector_tc1_idx ON tbl_vector USING hnsw (tc1 vector_l2_ops) WITH (m = 5 , ef_construction = 10); SET hnsw.ef_search = 10; -- Lower ef_search for potentially faster but less accurate results. SELECT * FROM tbl_vector ORDER BY tc1 <-> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Example: Searching with HNSW index (Cosine similarity). -- High recall scenario: -- (SQL for cosine similarity search with HNSW would follow a similar pattern to L2, adjusting the operator and potentially parameters.) ``` -------------------------------- ### ModifyParameterTemplate API Response Example Source: https://www.volcengine.com/docs/6438/1585051?lang=zh This is an example of a successful response from the ModifyParameterTemplate API. The 'Result' field is null upon successful modification. ```json { "ResponseMetadata": { "RequestId": "20250507155747117C127A35D4369CA572-7113d4", "Action": "ModifyParameterTemplate", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": null } ``` -------------------------------- ### 创建 PostgreSQL 实例 Source: https://www.volcengine.com/docs/6438/1257454?lang=zh 使用 CreateDBInstance 接口创建 PostgreSQL 实例。 ```go package rdspostgresql import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/rdspostgresql" "testing" "github.com/volcengine/volcengine-go-sdk/volcengine" ) func TestRDSPostgreSQL_CreateDBInstanceCommon(t *testing.T) { InitConfig() resp, err := Client.CreateDBInstance(&rdspostgresql.CreateDBInstanceInput{ DBEngineVersion: volcengine.String("PostgreSQL_12"), NodeInfo: []*rdspostgresql.NodeInfoForCreateDBInstanceInput{ { NodeOperateType: volcengine.String("Create"), NodeSpec: volcengine.String("rds.postgres.1c2g"), NodeType: volcengine.String("Primary"), ZoneId: volcengine.String("cn-beijing-c"), }, { NodeOperateType: volcengine.String("Create"), NodeSpec: volcengine.String("rds.postgres.1c2g"), NodeType: volcengine.String("Secondary"), ZoneId: volcengine.String("cn-beijing-c"), }, { NodeOperateType: volcengine.String("Create"), NodeSpec: volcengine.String("rds.postgres.1c2g"), NodeType: volcengine.String("ReadOnly"), ZoneId: volcengine.String("cn-beijing-c"), }, }, StorageSpace: volcengine.Int32(30), StorageType: volcengine.String("LocalSSD"), VpcId: volcengine.String("vpc-2bzo8w6m87v282dx0eemg****"), SubnetId: volcengine.String("subnet-2d6b6byrhtts058ozfenq****"), ChargeInfo: &rdspostgresql.ChargeInfoForCreateDBInstanceInput{ ChargeType: volcengine.String("PrePaid"), AutoRenew: volcengine.Bool(false), PeriodUnit: volcengine.String("Month"), Period: volcengine.Int32(2), Number: volcengine.Int32(1), }, InstanceName: volcengine.String("pg-test-sdk-go"), ProjectName: volcengine.String("default"), Tags: []*rdspostgresql.TagForCreateDBInstanceInput{ { Key: volcengine.String("test1"), Value: volcengine.String("test1"), }, { Key: volcengine.String("test2"), Value: volcengine.String("test2"), }, }, }) if err != nil { panic(err) } fmt.Println(resp) } ``` -------------------------------- ### Create PostgreSQL Instance Source: https://www.volcengine.com/docs/6438/1456231?lang=zh This snippet demonstrates how to create a new PostgreSQL instance with specified configurations, including node information, storage, VPC settings, and billing. ```APIDOC ## Create PostgreSQL Instance ### Description Creates a new Cloud Database PostgreSQL instance with specified configurations. ### Method POST (Implied by SDK method `createDBInstance`) ### Endpoint N/A (SDK method call) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **InstanceName** (string) - Required - The name of the instance. - **DBEngineVersion** (string) - Required - The compatible database engine version (e.g., "PostgreSQL_12"). - **StorageType** (string) - Required - The storage type for the instance (e.g., "LocalSSD"). - **StorageSpace** (integer) - Required - The storage space allocated for the instance in GB. - **VpcId** (string) - Required - The ID of the Virtual Private Cloud (VPC). - **SubnetId** (string) - Required - The ID of the subnet within the VPC. - **NodeInfo** (array of objects) - Required - Configuration for the instance nodes. - **NodeType** (string) - Required - Type of the node (e.g., "Primary", "Secondary"). - **ZoneId** (string) - Required - The availability zone ID (e.g., "cn-beijing-c"). - **NodeSpec** (string) - Required - The specification of the node (e.g., "rds.postgres.1c2g"). - **ChargeInfo** (object) - Required - Billing information for the instance. - **ChargeType** (string) - Required - The charge type (e.g., "PostPaid"). ### Request Example ```java package org.example; import com.volcengine.*; import com.volcengine.sign.Credentials; import com.volcengine.rdspostgresql.RdsPostgresqlApi; import com.volcengine.rdspostgresql.model.*; import java.util.ArrayList; import java.util.List; public class Main { // Set AKSK verification information and Region public static String ak = "Your AK"; public static String sk = "Your SK"; public static String region = "Your Region"; public static String host = "Your Host"; // Create an RDS PostgreSQL SDK client, which can be used to call API to perform task requests public static ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region) .setEndpoint(host) .setConnectTimeout(10); public static RdsPostgresqlApi rdsPostgresql = new RdsPostgresqlApi(apiClient); // Create an interface input parameter and configure it according to the interface parameter description public static void main(String[] args) throws Exception { List nodesInfo = new ArrayList<>(); NodeInfoForCreateDBInstanceInput primaryNodeInfo = new NodeInfoForCreateDBInstanceInput(); primaryNodeInfo.setNodeType("Primary"); primaryNodeInfo.setZoneId("cn-beijing-c"); primaryNodeInfo.setNodeSpec("rds.postgres.1c2g"); NodeInfoForCreateDBInstanceInput secondaryNodeInfo = new NodeInfoForCreateDBInstanceInput(); secondaryNodeInfo.setNodeType("Secondary"); secondaryNodeInfo.setZoneId("cn-beijing-c"); secondaryNodeInfo.setNodeSpec("rds.postgres.1c2g"); nodesInfo.add(primaryNodeInfo); nodesInfo.add(secondaryNodeInfo); ChargeInfoForCreateDBInstanceInput chargeInfo = new ChargeInfoForCreateDBInstanceInput(); chargeInfo.setChargeType("PostPaid"); CreateDBInstanceRequest request = new CreateDBInstanceRequest(); request.setInstanceName("pg-test-sdk-java"); request.setDbEngineVersion("PostgreSQL_12"); request.setStorageType("LocalSSD"); request.setStorageSpace(30); request.setVpcId("vpc-2bzo8w6m87v282dx0eemg****"); request.setSubnetId("subnet-2d6b6byrhtts058ozfenq****"); request.setNodeInfo(nodesInfo); request.setChargeInfo(chargeInfo); // Call the interface to complete the operation corresponding to the interface try { CreateDBInstanceResponse response = rdsPostgresql.createDBInstance(request); System.out.println(response); } catch (ApiException e) { System.out.println(e.getResponseBody()); } } } ``` ### Response #### Success Response (200) - **response** (object) - The response object containing details of the created instance. #### Response Example ```json { "example": "response body" } ``` ``` -------------------------------- ### 使用 pgbench 进行数据库压测 Source: https://www.volcengine.com/docs/6438/1456338?lang=zh 在客户端命令行窗口执行此命令,使用 pgbench 工具对 'server' 数据库进行基准测试。参数包括连接地址、端口、用户、并发客户端数、线程数、压测时长、数据规模因子和目标数据库。 ```bash pgbench -h postgresxxxxxx.rds-pg.ivolces.com -p 5432 -U test_account -c 100 -j 2 -T 300 -s 100 -i server --压测 100 客户端,每个客户端 2 个线程,压测 300s, 数据量为 100 个规模因子 ``` -------------------------------- ### Install Script Dependencies Source: https://www.volcengine.com/docs/6438/1130800?lang=zh Install the required Python packages for the script to function. This includes libraries for PostgreSQL interaction and OpenAI API calls. ```shell pip install psycopg2-binary pip install openai pip install 'openai[datalib]' ``` -------------------------------- ### Example: Creating and Using IVFFlat Index Source: https://www.volcengine.com/docs/6438/1126562?lang=zh Demonstrates creating an IVFFlat index with different similarity metrics (L2, Cosine, Inner Product) and adjusting recall by setting the `ivfflat.probes` parameter. ```SQL -- Create an IVFFlat index using vector_l2_ops for Euclidean distance similarity search. -- High recall scenario: DROP TABLE IF EXISTS tbl_vector; CREATE TABLE tbl_vector(id serial, tc1 vector(5)); INSERT INTO tbl_vector (tc1) SELECT array_agg(random())::vector(5) FROM generate_series(1.0,5.0); CREATE INDEX tbl_vector_tc1_idx ON tbl_vector USING ivfflat (tc1) WITH (lists = 4); SET ivfflat.probes = 4; -- Set ivfflat.probes equal to the index's lists value to scan all lists. SELECT * FROM tbl_vector ORDER BY tc1 <-> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Low recall scenario: SET ivfflat.probes = 1; -- Set ivfflat.probes less than the index's lists value to scan a subset of lists. SELECT * FROM tbl_vector ORDER BY tc1 <-> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Create an IVFFlat index using vector_cosine_ops for cosine similarity search. -- High recall scenario: DROP INDEX tbl_vector_tc1_idx; CREATE INDEX tbl_vector_tc1_idx ON tbl_vector USING ivfflat (tc1 vector_cosine_ops) WITH (lists = 4); SET ivfflat.probes = 4; SELECT * FROM tbl_vector ORDER BY tc1 <=> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Low recall scenario: SET ivfflat.probes = 1; SELECT * FROM tbl_vector ORDER BY tc1 <=> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Create an IVFFlat index using vector_ip_ops for inner product similarity search. -- High recall scenario: DROP INDEX tbl_vector_tc1_idx; CREATE INDEX tbl_vector_tc1_idx ON tbl_vector USING ivfflat (tc1 vector_ip_ops) WITH (lists = 4); SET ivfflat.probes = 4; SELECT * FROM tbl_vector ORDER BY tc1 <#> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; -- Low recall scenario: SET ivfflat.probes = 1; SELECT * FROM tbl_vector ORDER BY tc1 <#> '[0.559782,0.194308,0.454407,0.0176121,0.442676]'; ``` -------------------------------- ### DeleteSlot API Request Example Source: https://www.volcengine.com/docs/6438/1323548 This is an example of a POST request to the DeleteSlot API, including necessary headers and the JSON body with InstanceId and SlotName. ```http POST /?Action=DeleteSlot&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z Authorization: HMAC-SHA256 Credential=AKLTN2I0MmFiNzMxNWE5NDgzMzk4MmVjMTVkODlkZTZ****/20211202/cn-beijing/rds_postgresql/request,SignedHeaders=x-date, Signature=71d31fc7bcf990142851c9833c5656391486cda0ae2e0b8ada733b7c6458**** { "InstanceId": "postgres-b99fc497****", "SlotName": "SlotToBeDeleted" } ``` -------------------------------- ### POST /?Action=CreateDBInstance Source: https://www.volcengine.com/docs/6438/1159933?lang=zh Creates a new PostgreSQL database instance. This is an asynchronous operation. ```APIDOC ## POST /?Action=CreateDBInstance&Version=2022-01-01 ### Description Calls the CreateDBInstance interface to create a new PostgreSQL database instance. This is an asynchronous request. ### Method POST ### Endpoint /?Action=CreateDBInstance&Version=2022-01-01 ### Parameters #### Request Body - **DBEngineVersion** (String) - Required - Compatible version (e.g., PostgreSQL_12, PostgreSQL_13, etc.) - **NodeInfo** (Array of NodeInfoObject) - Required - Instance specification configuration. - **StorageType** (String) - Required - Instance storage type, fixed value: LocalSSD. - **StorageSpace** (Integer) - Optional - Instance storage space in GB. - **VpcId** (String) - Required - Private network ID. - **SubnetId** (String) - Required - Subnet ID. - **AllowListIds** (Array of String) - Optional - List of whitelist IDs. - **InstanceName** (String) - Optional - Instance name. - **MaintenanceWindow** (Object of MaintenanceWindowObject) - Optional - Maintenance window settings. - **ProjectName** (String) - Optional - Project name. - **Tags** (Array of TagObject) - Optional - Tags for the instance. - **ChargeInfo** (Object of ChargeInfoObject) - Required - Billing information. ### Request Example { "DBEngineVersion": "PostgreSQL_12", "NodeInfo": [{"ZoneId": "cn-beijing-a", "NodeType": "Primary", "NodeSpec": "rds.postgres.1c2g", "NodeOperateType": "Create"}], "StorageType": "LocalSSD", "VpcId": "vpc-2gdgzrrl5icjk50ztyz6b****", "SubnetId": "subnet-30uhx4w39n75s7r2qr0lq****", "ChargeInfo": {"ChargeType": "PostPaid"} } ### Response #### Success Response (200) - **InstanceId** (String) - The ID of the created instance. - **OrderId** (String) - The ID of the order generated. #### Response Example { "InstanceId": "postgres-21a3333b****", "OrderId": "Order634971667632428****" } ``` -------------------------------- ### DescribeBackups API Response Example Source: https://www.volcengine.com/docs/6438/1158713 This is an example response from the DescribeBackups API. It includes metadata about the request and a list of backup details for the specified instance. ```json { "ResponseMetadata": { "RequestId": "202308041100439000B4A38D7086052659", "Action": "DescribeBackups", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": { "Backups": [ { "BackupDataSize": 16785668, "BackupDescription": "", "BackupEndTime": "2025-05-12T12:21:15.000Z", "BackupFileName": "20250512-194637F_20250512-202111I", "BackupFileSize": 16785668, "BackupId": "20250512-194637F_20250512-202111I", "BackupMethod": "Physical", "BackupProgress": 100, "BackupScope": "Instance", "BackupStartTime": "2025-05-12T12:21:11.000Z", "BackupStatus": "Success", "BackupType": "Increment", "CreateType": "System", "DownloadStatus": "NotAllowed", "InstanceInfo": { "AllowListIds": [ "acl-fdd6293f300143f2a4932bb483c8****", "acl-ded4c0b707e44efd8f0dbc4e88f5****" ], "ChargeDetail": { "AutoRenew": false, "ChargeEndTime": "", "ChargeStartTime": "2023-09-11T06:43:34.000Z", "ChargeStatus": "Normal", "ChargeType": "PostPaid", "Number": 1, "OverdueReclaimTime": "", "OverdueTime": "", "Period": 1, "PeriodUnit": "Month" }, "DBEngineVersion": "PostgreSQL_12", "InstanceId": "postgres-da8f12d3****", "InstanceName": "测试实例", "InstanceStatus": "Running", "InstanceType": "HA", "Memory": 2, "Nodes": [ { "InstanceId": "postgres-da8f12d3****", "NodeId": "postgres-da8f12d3****", "NodeSpec": "rds.postgres.1c2g", "NodeType": "Primary", "ZoneId": "cn-beijing-b" }, { "InstanceId": "postgres-da8f12d3****", "NodeId": "postgres-da8f12d3****-fkoh", "NodeSpec": "rds.postgres.1c2g", "NodeType": "Secondary", "ZoneId": "cn-beijing-a" } ], "ProjectName": "rds-pg", "StorageSpace": 20, "StorageType": "LocalSSD", "SubnetId": "subnet-30uhx4w39n75s7r2qr0lq****", "VCPU": 1, "VpcId": "vpc-2gdgzrrl5icjk50ztyz6b****", "ZoneId": "cn-beijing-b" } } ], "Total": 1 } } ``` -------------------------------- ### POST /?Action=DownloadBackup Source: https://www.volcengine.com/docs/6438/1585058?lang=zh Initiates the download process for a specific logical backup of a PostgreSQL instance. ```APIDOC ## POST /?Action=DownloadBackup ### Description Initiates the download process for a logical backup. This is an asynchronous operation. After calling this, you must call the GetBackupDownloadLink interface to obtain the download link for the backup file. ### Method POST ### Endpoint /?Action=DownloadBackup&Version=2022-01-01 ### Parameters #### Request Body - **InstanceId** (String) - Required - The ID of the PostgreSQL instance. - **BackupId** (String) - Required - The ID of the logical backup to be downloaded. ### Request Example { "InstanceId": "postgres-a3e7eb2f****", "BackupId": "20250512-194654-3836LI" } ### Response #### Success Response (200) - **ResponseMetadata** (Object) - Contains request metadata including RequestId, Action, Version, Service, and Region. - **Result** (null) - The result field is null for this operation. #### Response Example { "ResponseMetadata": { "RequestId": "20250512220201C4DF75445E6A5077C0C9-02b7b8", "Action": "DownloadBackup", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": null } ``` -------------------------------- ### Install pgjwt Extension Source: https://www.volcengine.com/docs/6438/2208442 Install the pgjwt extension in your PostgreSQL database. The `CASCADE` option can be used to automatically handle dependencies if pgcrypto is not already enabled. ```sql CREATE EXTENSION pgjwt; -- or use CASCADE: CREATE EXTENSION pgjwt cascade; ``` -------------------------------- ### DeleteAllowList API Response Example Source: https://www.volcengine.com/docs/6438/1257394?lang=zh This is an example of a successful response from the DeleteAllowList API. It includes metadata about the request and a null result, indicating the operation was successful. ```json { "ResponseMetadata": { "RequestId": "20240423194839054AF272DDAED3F70BEA-e6e5fa", "Action": "DeleteAllowList", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-guangzhou" }, "Result": null } ``` -------------------------------- ### CreateDBInstance Source: https://www.volcengine.com/docs/6438/1158704 Creates a new PostgreSQL database instance. ```APIDOC ## POST CreateDBInstance ### Description Creates a new PostgreSQL database instance within the specified configuration. ### Method POST ### Endpoint CreateDBInstance ``` -------------------------------- ### Example DescribeDBInstances Response Source: https://www.volcengine.com/docs/6438/1159935?lang=zh This is an example response from the DescribeDBInstances API, showing instance details including address objects. The 'ResponseMetadata' contains request tracking information. ```json { "ResponseMetadata": { "RequestId": "20251014123704ABBB743EDFA13A979A78-62ee28", "Action": "DescribeDBInstances", "Version": "2022-01-01", "Service": "rds_postgresql", "Region": "cn-beijing" }, "Result": { "Instances": [ { "AddressObject": [ { "DNSVisibility": false, "Domain": "postgres-d9ace5c3****-public.rds-pg.cn-beijing.volces.com", "EipId": "eip-1a1k0eh4m8mio8nvepkxv****", ``` -------------------------------- ### 执行 SysBench 性能测试 Source: https://www.volcengine.com/docs/6438/104932?lang=zh 运行 run 命令启动性能测试,需根据实际环境替换数据库连接参数。 ```bash sysbench ./tests/include/oltp_legacy/oltp.lua \ --db-driver=pgsql \ --pgsql-db=sbtest \ --pgsql-user= \ --pgsql-password= \ --pgsql-port=5432 \ --pgsql-host= \ --oltp-tables-count=64 \ --oltp-table-size=10000000 \ --time=180 \ --max-requests=0 \ --threads=64 \ --report-interval=5 \ --forced-shutdown=1 \ run ``` -------------------------------- ### HTTP Request Example for DeleteParameterTemplate Source: https://www.volcengine.com/docs/6438/1585052?lang=zh This example shows the HTTP request structure for calling the DeleteParameterTemplate API. It includes necessary headers and the JSON body containing the TemplateId. ```http POST /?Action=DeleteParameterTemplate&Version=2022-01-01 HTTP/1.1 Content-Type: application/json Host: rds-postgresql.cn-beijing.volcengineapi.com X-Date: 20231002T101010Z Authorization: HMAC-SHA256 Credential=AKLTN2I0MmFiNzMxNWE5NDgzMzk4MmVjMTVkODlkZTZ****/20211202/cn-beijing/rds_postgresql/request,SignedHeaders=x-date, Signature=71d31fc7bcf990142851c9833c5656391486cda0ae2e0b8ada733b7c6458**** { "TemplateId": "postgresql-6d57281c3313****" } ``` -------------------------------- ### Describe DB Instance Price Difference Response Example Source: https://www.volcengine.com/docs/6438/1159939 This response shows the price details for a DB instance, including original price, discount price, and payable price for different charge items. ```json {  "ResponseMetadata": {  "RequestId": "202306192156254A900086CBECA123D60F",  "Action": "DescribeDBInstancePriceDifference",  "Version": "2022-01-01",  "Service": "rds_postgresql",  "Region": "cn-beijing"  },  "Result": {  "ChargeItemPrices": [  {  "ChargeItemKey": "rds.pg.d1.1c2g",  "ChargeItemType": "Primary",  "ChargeItemValue": 1,  "DiscountPrice": 120.345,  "OriginalPrice": 120.345,  "PayablePrice": 120.345  },  {  "ChargeItemKey": "rds.pg.d1.1c2g",  "ChargeItemType": "Secondary",  "ChargeItemValue": 1,  "DiscountPrice": 120.345,  "OriginalPrice": 120.345,  "PayablePrice": 120.345  },  {  "ChargeItemKey": "rds.pg.d1.localssd",  "ChargeItemType": "Storage",  "ChargeItemValue": 100,  "DiscountPrice": 0.05368034,  "NodeNumPerInstance": 2,  "OriginalPrice": 0.25,  "PayablePrice": 0.05368034  }  ],  "Currency": "人民币",  "DiscountPrice": -17.5,  "OriginalPrice": -17.5,  "PayablePrice": -17.5  }  }  ```