### Start openGauss with Example Output Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/database_om_guide/start_stop_opengauss.md Demonstrates the command to start openGauss and its expected successful output. ```bash gs_om -t start Starting cluster. ========================================= ========================================= Successfully started. ``` -------------------------------- ### Example of OpenGauss Installation Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/tool_and_commandreference/gs_install.md This example demonstrates the execution of the gs_install script with a specified configuration file. It shows the typical output during the installation process, including parsing configuration, pre-installation checks, instance initialization, and final configuration. ```bash gs_install -X /opt/software/openGauss/clusterconfig.xml Parsing the configuration file. Check preinstall on every node. Successfully checked preinstall on every node. Creating the backup directory. Successfully created the backup directory. begin deploy.. Installing the cluster. begin prepare Install Cluster.. Checking the InstallationGuide environment on all nodes. begin install Cluster.. Installing applications on all nodes. Successfully installed APP. begin init Instance.. encrypt cipher and rand files for database. Please enter password for database: Please repeat for database: begin to create CA cert files The sslcert will be generated in /opt/gaussdb/cluster/app/share/sslcert/om Cluster InstallationGuide is completed. Configuring. Deleting instances from all nodes. Successfully deleted instances from all nodes. Checking node configuration on all nodes. Initializing instances on all nodes. Updating instance configuration on all nodes. Check consistence of memCheck and coresCheck on DN nodes. Successful check consistence of memCheck and coresCheck on all nodes. Configuring pg_hba on all nodes. Configuration is completed. Successfully started cluster. Successfully installed application. ``` -------------------------------- ### Get Bad Block Information Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/sql_reference/statistics_information_functions.md Obtains damage information about pages or CUs after the current node starts. Example: select * from pg_stat_bad_block(); ```sql select * from pg_stat_bad_block(); ``` -------------------------------- ### Create Table and Show Create Table Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-show-create-table.md This example demonstrates creating a simple table and then using SHOW CREATE TABLE to view its definition. The output includes the table name, its columns, constraints, and storage options. ```sql openGauss=# CREATE TABLE t1 (c1 INT PRIMARY KEY); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1" CREATE TABLE openGauss=# show create table t1; Table | Create Table -------+--------------------------------------------------------- t1 | SET search_path = public; + | CREATE TABLE t1 ( | c1 integer NOT NULL | ) | WITH (orientation=row, compression=no); | ALTER TABLE t1 ADD CONSTRAINT t1_pkey PRIMARY KEY (c1); (1 row) ``` -------------------------------- ### Example: Modify Sequence Start Value Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/alter_sequence.md Changes the start value of the 'serial' sequence to 90. ```sql --修改序列起始值为90 openGauss=# ALTER SEQUENCE serial START 90; ``` -------------------------------- ### Create Table and Show Create Table Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/extension_reference/dolphin-SHOW-CREATE-TABLE.md This example demonstrates creating a simple table and then using SHOW CREATE TABLE to view its definition. ```sql openGauss=# CREATE TABLE t1 (c1 INT PRIMARY KEY); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1" CREATE TABLE openGauss=# show create table t1; Table | Create Table -------+--------------------------------------------------------- t1 | SET search_path = public; + | CREATE TABLE t1 ( | c1 integer NOT NULL + | ) + | WITH (orientation=row, compression=no); + | ALTER TABLE t1 ADD CONSTRAINT t1_pkey PRIMARY KEY (c1); (1 row) ``` -------------------------------- ### Install openGauss Lite (Standalone) Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/getting_started/installation.md Installs openGauss Lite in standalone mode. This command sets the data directory, installation directory, and starts the cluster. ```bash echo password | sh ./install.sh --mode single -D ~/openGauss/data -R ~/openGauss/install --start ``` -------------------------------- ### Create a Server Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/zh/sql_reference/create_server.md This example demonstrates the basic syntax for creating a server using the `log_fdw` foreign data wrapper. Ensure the foreign data wrapper is already installed and configured. ```sql openGauss=* create server my_server foreign data wrapper log_fdw; ``` -------------------------------- ### Get Server Start Time Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/sql_reference/system_information_functions.md Retrieves the timestamp with time zone indicating when the server process started. ```sql openGauss=# SELECT pg_postmaster_start_time(); pg_postmaster_start_time ------------------------------ 2017-08-30 16:02:54.99854+08 (1 row) ``` -------------------------------- ### Create Schema, Set Search Path, and Tables/Views Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-show-tables.md This snippet demonstrates the setup required before using SHOW TABLES, including creating a schema, setting the search path, and defining tables and views. ```sql openGauss=# CREATE SCHEMA tst_schema; openGauss=# SET SEARCH_PATH TO tst_schema; openGauss=# CREATE TABLE tst_t1 openGauss-# ( openGauss(# id int primary key, openGauss(# name varchar(20) NOT NULL, openGauss(# addr text COLLATE "de_DE", openGauss(# phone text COLLATE "es_ES", openGauss(# addr_code text openGauss(# ); openGauss=# CREATE VIEW tst_v1 AS SELECT * FROM tst_t1; openGauss=# CREATE TABLE t_t2(id int); ``` -------------------------------- ### Get Current Timestamp at Statement Start Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/sql_reference/date_and_time_processing_functions_and_operators.md Specifies the current date and time at the start of the current statement. ```sql openGauss=# SELECT statement_timestamp(); statement_timestamp ------------------------------- 2017-09-01 17:04:39.119267+08 (1 row) ``` -------------------------------- ### HLL 'Hello World' Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/sql_reference/brief_tutorial/data-types.md A basic 'Hello World' example demonstrating the creation of a table with an HLL column, insertion of an empty HLL, adding hashed integers and strings, and retrieving the cardinality. ```sql -- Create a table with the HLL type. openGauss=# create table helloworld (id integer, set hll); ``` ```sql -- Insert an empty HLL to the table. openGauss=# insert into helloworld(id, set) values (1, hll_empty()); ``` ```sql -- Add a hashed integer to the HLL. openGauss=# update helloworld set set = hll_add(set, hll_hash_integer(12345)) where id = 1; ``` ```sql -- Add a hashed string to the HLL. openGauss=# update helloworld set set = hll_add(set, hll_hash_text('hello world')) where id = 1; ``` ```sql -- Obtain the number of distinct values of the HLL. openGauss=# select hll_cardinality(set) from helloworld where id = 1; hll_cardinality ----------------- 2 (1 row) ``` ```sql -- Delete the table. openGauss=# drop table helloworld; ``` -------------------------------- ### OpenGauss ODBC Connection and Data Retrieval Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/developer_guide/example_odbc.md This C code snippet illustrates how to establish an ODBC connection to OpenGauss, perform DDL and DML operations, and retrieve data. Ensure you have the necessary ODBC libraries and drivers installed. The connection string and credentials should be adjusted based on your OpenGauss setup. ```c #include #include #include #ifdef WIN32 #include #endif SQLHENV V_OD_Env; // Handle ODBC environment SQLHSTMT V_OD_hstmt; // Handle statement SQLHDBC V_OD_hdbc; // Handle connection char typename[100]; SQLINTEGER value = 100; SQLINTEGER V_OD_erg,V_OD_buffer,V_OD_err,V_OD_id; int main(int argc,char *argv[]) { // 1. Allocate an environment handle. V_OD_erg = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env); if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) { printf("Error AllocHandle\n"); exit(0); } // 2. Set environment attributes (version information). SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); // 3. Allocate a connection handle. V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc); if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) { SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); exit(0); } // 4. Set connection attributes. SQLSetConnectAttr(V_OD_hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON, 0); // 5. Connect to the data source. userName and password indicate the username and password for connecting to the database. Set them as needed. // If the username and password have been set in the odbc.ini file, you do not need to set userName or password here, retaining "" for them. However, you are not advised to do so because the username and password will be disclosed if the permission for odbc.ini is abused. V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "gaussdb", SQL_NTS, (SQLCHAR*) "userName", SQL_NTS, (SQLCHAR*) "password", SQL_NTS); if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) { printf("Error SQLConnect %d\n",V_OD_erg); SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); exit(0); } printf("Connected !\n"); // 6. Set statement attributes. SQLSetStmtAttr(V_OD_hstmt,SQL_ATTR_QUERY_TIMEOUT,(SQLPOINTER *)3,0); // 7. Allocate a statement handle. SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt); // 8. Run SQL statements. SQLExecDirect(V_OD_hstmt,"drop table IF EXISTS customer_t1",SQL_NTS); SQLExecDirect(V_OD_hstmt,"CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));",SQL_NTS); SQLExecDirect(V_OD_hstmt,"insert into customer_t1 values(25,li)",SQL_NTS); // 9. Prepare for execution. SQLPrepare(V_OD_hstmt,"insert into customer_t1 values(?)",SQL_NTS); // 10. Bind parameters. SQLBindParameter(V_OD_hstmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0, &value,0,NULL); // 11. Run prepared statements. SQLExecute(V_OD_hstmt); SQLExecDirect(V_OD_hstmt,"select id from testtable",SQL_NTS); // 12. Obtain attributes of a specific column in the result set. SQLColAttribute(V_OD_hstmt,1,SQL_DESC_TYPE,typename,100,NULL,NULL); printf("SQLColAtrribute %s\n",typename); // 13. Bind the result set. SQLBindCol(V_OD_hstmt,1,SQL_C_SLONG, (SQLPOINTER)&V_OD_buffer,150, (SQLLEN *)&V_OD_err); // 14. Obtain data in the result set by executing SQLFetch. V_OD_erg=SQLFetch(V_OD_hstmt); // 15. Obtain and return data by executing SQLGetData. while(V_OD_erg != SQL_NO_DATA) { SQLGetData(V_OD_hstmt,1,SQL_C_SLONG,(SQLPOINTER)&V_OD_id,0,NULL); printf("SQLGetData ----ID = %d\n",V_OD_id); V_OD_erg=SQLFetch(V_OD_hstmt); }; printf("Done !\n"); // 16. Disconnect data source connections and release handles. SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt); SQLDisconnect(V_OD_hdbc); SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc); SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); return(0); } ``` -------------------------------- ### Example: Display Server Version Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/database_administration_guide/viewing_parameter_values.md This example demonstrates the output when viewing the server version using the SHOW command. ```sql openGauss=# SHOW server_version; server_version ---------------- 9.2.4 (1 row) ``` -------------------------------- ### Install openGauss Lite (Primary Node) Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/getting_started/installation.md Installs openGauss Lite on the primary node for a primary/standby configuration. It specifies the installation mode, data and installation paths, replication connection information, and starts the cluster. ```bash echo password | sh ./install.sh --mode primary -D ~/openGauss/data -R ~/openGauss/install -C "replconninfo1='localhost=ip1 localport=port1 remotehost=ip2 remoteport=port2'" --start ``` -------------------------------- ### Create and Show View Definition Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-show-create-view.md This example demonstrates how to create a view and then use SHOW CREATE VIEW to retrieve its definition. The output includes the view name, the CREATE VIEW statement, and session-specific client and connection settings. ```sql --Create a view openGauss=# create view tt19v as openGauss-# select 'foo'::text = any(array['abc','def','foo']::text[]) c1, openGauss-# 'foo'::text = any((select array['abc','def','foo']::text[])::text[]) c2; CREATE VIEW --Query a statement for creating a view. openGauss=# show create view tt19v; View | Create View | character_set_client | collation_connection -------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+---------------------- tt19v | CREATE OR REPLACE VIEW public.tt19v AS +| UTF8 | en_US.UTF-8 | SELECT ('foo'::text = ANY (ARRAY['abc'::text, 'def'::text, 'foo'::text])) AS c1, ('foo'::text = ANY ((SELECT ARRAY['abc'::text, 'def'::text, 'foo'::text] AS "array")::text[])) AS c2; | | (1 row) ``` -------------------------------- ### Example: Display Server Version Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/database_administration_guide/viewing_parameter_values.md This example demonstrates the output when viewing the 'server_version' parameter using the SHOW command. ```sql postgres=# SHOW server_version; server_version ---------------- 9.2.4 (1 row) ``` -------------------------------- ### Start openGauss Database Service Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/installation_guide/deb_installation.md Starts the openGauss database service after installation. This command assumes the service name is 'opengauss'. ```shell service opengauss start ``` -------------------------------- ### Create Index and Table, then View Index Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-show-index.md This example demonstrates creating a schema, setting the search path, creating a table with a primary key and a separate index on a column, and then using the SHOW INDEX command to view the created indexes. ```sql --Create an index and a table. openGauss=# CREATE SCHEMA tst_schema; openGauss=# SET SEARCH_PATH TO tst_schema; openGauss=# CREATE TABLE tst_t1 openGauss-# ( openGauss(# id int primary key, openGauss(# name varchar(20) NOT NULL openGauss(# ); openGauss=# CREATE INDEX tst_t1_name_ind on tst_t1(name); --View the index of a table. openGauss=# show index from tst_t1 ; table | non_unique | key_name | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment --------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+--------------- tst_t1 | t | tst_t1_name_ind | 1 | name | A | | | | | btree | | tst_t1 | f | tst_t1_pkey | 1 | id | A | | | | | btree | | (2 rows) ``` -------------------------------- ### Commit Transaction Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/brief_tutorial/transaction.md This example shows starting a transaction, deleting a row, and then committing the changes, making the deletion permanent. ```sql --开启一个事务,设置事务的隔离级别为READ COMMITTED,访问模式为READ ONLY。 openGauss=# BEGIN; openGauss=# DELETE FROM customer_t1 WHERE amount = 1000; openGauss=# COMMIT; ``` -------------------------------- ### HLL 'Hello World' Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/sql_reference/hll.md A basic example demonstrating the creation of a table with an HLL column, adding hashed values, and retrieving the cardinality. ```sql -- Create a table with the HLL type: openGauss=# create table helloworld (id integer, set hll); -- Insert an empty HLL to the table: openGauss=# insert into helloworld(id, set) values (1, hll_empty()); -- Add a hashed integer to the HLL: openGauss=# update helloworld set set = hll_add(set, hll_hash_integer(12345)) where id = 1; -- Add a hashed string to the HLL: openGauss=# update helloworld set set = hll_add(set, hll_hash_text('hello world')) where id = 1; -- Obtain the number of distinct values of the HLL: openGauss=# select hll_cardinality(set) from helloworld where id = 1; hll_cardinality ----------------- 2 (1 row) -- Delete the table. openGauss=# drop table helloworld; ``` -------------------------------- ### Start Disaster Recovery (DR) Setup Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/tool_and_commandreference/gs_ddr.md Initiates the DR setup process. This command must be executed on both the primary and standby clusters. The `--disaster_type` parameter is only required during the initial setup. ```bash gs_ddr -t start -m [primary|disaster_standby] --disaster_type [dorado|stream] [-X XMLFILE] [--json JSONFILE] [--time-out=SECS] [-l LOGFILE] ``` -------------------------------- ### Database Connection and Setup Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/developer_guide/example_libpq.md Establishes a connection to a PostgreSQL database and sets the search path to an empty string for security. Includes error handling for connection and configuration commands. ```c static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main(int argc, char **argv) { char *database, *in_filename, *out_filename, *out_filename2; Oid lobjOid; PGconn *conn; PGresult *res; if (argc != 5) { fprintf(stderr, "Usage: %s database_name in_filename out_filename out_filename2\n", argv[0]); exit(1); } database = argv[1]; in_filename = argv[2]; out_filename = argv[3]; out_filename2 = argv[4]; /* * set up the connection */ conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /* Set always-secure search path, so malicous users can't take control. */ res = PQexec(conn, "SELECT pg_catalog.set_config('search_path', '', false)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SET failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "begin"); PQclear(res); ``` -------------------------------- ### Start Kafka Service Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/data_migration_guide/data_check.md From the Kafka installation directory, start the Kafka server in daemon mode using the server properties file. ```bash bin/kafka-server-start -daemon etc/kafka/server.properties ``` -------------------------------- ### Database Connection and Setup Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/developer_guide/example_libpq.md This code snippet demonstrates establishing a connection to a PostgreSQL database using PQsetdb, checking the connection status, and setting the search path to an empty string for security. It also begins a transaction. ```c static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main(int argc, char **argv) { char *database, *in_filename, *out_filename, *out_filename2; Oid lobjOid; PGconn *conn; PGresult *res; if (argc != 5) { fprintf(stderr, "Usage: %s database_name in_filename out_filename out_filename2\n", argv[0]); exit(1); } database = argv[1]; in_filename = argv[2]; out_filename = argv[3]; out_filename2 = argv[4]; /* * set up the connection */ conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /* Set always-secure search path, so malicous users can't take control. */ res = PQexec(conn, "SELECT pg_catalog.set_config('search_path', '', false)"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SET failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "begin"); PQclear(res); return 0; } ``` -------------------------------- ### Backend Transaction Start Time Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/sql_reference/statistics-information-functions.md Get the start time of the current transaction for a server process, subject to user privileges and configuration. ```APIDOC ## pg_stat_get_backend_xact_start(integer) ### Description Specifies the time when the given server process's currently executing transaction is started only if the current user is the system administrator or the user of the session being queried and track_activities is enabled. ### Return Type timestamp with time zone ``` -------------------------------- ### Example: Using GET DIAGNOSTICS in a Stored Procedure Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/get_diagnostics.md This example demonstrates the use of GET CURRENT DIAGNOSTICS within a stored procedure's CONTINUE HANDLER to capture error details like MYSQL_ERRNO and MESSAGE_TEXT during an insert operation that violates a NOT NULL constraint. ```sql DROP TABLE IF EXISTS t1 ; NOTICE: table "t1" does not exist, skipping CREATE TABLE t1(c1 TEXT NOT NULL); CREATE OR REPLACE PROCEDURE prc() AS DECLARE num INT; DECLARE errcount INT; DECLARE errno INT; DECLARE msg TEXT; BEGIN DECLARE CONTINUE HANDLER FOR 23502 BEGIN -- Here the current DA is nonempty because no prior statements -- executing within the handler have cleared it GET CURRENT DIAGNOSTICS CONDITION 1 errno = MYSQL_ERRNO, msg = MESSAGE_TEXT; RAISE NOTICE 'current DA before mapped insert , error = % , msg = %', errno, msg; ``` -------------------------------- ### HLL 'Hello World' Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/brief_tutorial/data_types.md A basic example demonstrating the creation of a table with an HLL type, inserting data, and retrieving cardinality. ```sql -- Create a table with the HLL type. openGauss=# create table helloworld (id integer, set hll); -- Insert an empty HLL to the table. openGauss=# insert into helloworld(id, set) values (1, hll_empty()); -- Add a hashed integer to the HLL. openGauss=# update helloworld set set = hll_add(set, hll_hash_integer(12345)) where id = 1; -- Add a hashed string to the HLL. openGauss=# update helloworld set set = hll_add(set, hll_hash_text('hello world')) where id = 1; -- Obtain the number of distinct values of the HLL. openGauss=# select hll_cardinality(set) from helloworld where id = 1; hll_cardinality ----------------- 2 (1 row) -- Delete the table. openGauss=# drop table helloworld; ``` -------------------------------- ### Start ZooKeeper Service Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/data_migration_guide/data_check.md Navigate to the Kafka installation directory and start the ZooKeeper server in daemon mode using the provided properties file. ```bash cd /data/kafka/confluent-7.2.0 bin/zookeeper-server-start -daemon etc/kafka/zookeeper.properties ``` -------------------------------- ### HLL 'Hello World' Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/sql_reference/hll_data_type.md A basic example demonstrating the creation of a table with an HLL column, inserting data, and querying distinct values. ```sql -- Create a table with the HLL type: openGauss=# create table helloworld (id integer, set hll); ``` ```sql -- Insert an empty HLL to the table: openGauss=# insert into helloworld(id, set) values (1, hll_empty()); ``` ```sql -- Add a hashed integer to the HLL: openGauss=# update helloworld set set = hll_add(set, hll_hash_integer(12345)) where id = 1; ``` ```sql -- Add a hashed string to the HLL: openGauss=# update helloworld set set = hll_add(set, hll_hash_text('hello world')) where id = 1; ``` ```sql -- Obtain the number of distinct values of the HLL: openGauss=# select hll_cardinality(set) from helloworld where id = 1; ``` ```sql -- Delete the table. openGauss=# drop table helloworld; ``` -------------------------------- ### HLL 'Hello World' Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/zh/brief_tutorial/data_types.md A basic example demonstrating the creation of an HLL table, inserting empty HLL objects, adding integer and text values, and retrieving the distinct count. ```sql -- 创建带有hll类型的表 openGauss=# create table helloworld (id integer, set hll); -- 向表中插入空的hll openGauss=# insert into helloworld(id, set) values (1, hll_empty()); -- 把整数经过哈希计算加入到hll中 openGauss=# update helloworld set set = hll_add(set, hll_hash_integer(12345)) where id = 1; -- 把字符串经过哈希计算加入到hll中 openGauss=# update helloworld set set = hll_add(set, hll_hash_text('hello world')) where id = 1; -- 得到hll中的distinct值 openGauss=# select hll_cardinality(set) from helloworld where id = 1; hll_cardinality ----------------- 2 (1 row) -- 删除表 openGauss=# drop table helloworld; ``` -------------------------------- ### Backend Activity Start Time Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/sql_reference/statistics-information-functions.md Get the start time of the currently executing query for a server process, subject to user privileges and configuration. ```APIDOC ## pg_stat_get_backend_activity_start(integer) ### Description Specifies the time when the given server process's currently executing query is started only if the current user is the system administrator or the user of the session being queried and track_activities is enabled. ### Return Type timestamp with time zone ``` -------------------------------- ### Example: Query Slow SQLs from Statement History Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/statistics_function.md Example of querying slow SQL records from the statement history on the primary server using a start time point. The query retrieves database name, schema name, SQL query, start time, and finish time. ```sql openGauss=# select db_name, schema_name, query, start_time, finish_time from dbe_perf.get_statement_history('2025-08-01', NULL); ``` -------------------------------- ### Create and Drop Directory Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/drop_directory.md This example demonstrates how to first create a directory object and then subsequently drop it. Ensure the directory path exists on the server. ```sql --创建目录。 openGauss=# CREATE OR REPLACE DIRECTORY dir as '/tmp/'; --删除目录。 openGauss=# DROP DIRECTORY dir; ``` -------------------------------- ### Create a data source with TYPE, VERSION, and OPTIONS Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/sql_reference/create_data_source.md This example shows how to create a data source by providing all possible parameters: type, version, and various connection options including username and password. ```sql openGauss=# CREATE DATA SOURCE ds_test4 TYPE 'unknown' VERSION '11.2.3' OPTIONS (dsn 'openGauss', username 'userid', password 'xxxxxx', encoding ''); ``` -------------------------------- ### Start Incremental Migration Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/data_migration_guide/incremental_migration.md Use the gs_replicate.sh start command to initiate the incremental migration process. Specify the workspace ID if you used one during installation. ```bash sh gs_replicate.sh start mysql-opengauss workspace.id ``` -------------------------------- ### Example: Creating Tables and Index, then Purging Table Source: https://github.com/opengauss-mirror/docs/blob/master/docs-lite/en/sql_reference/purge.md This example demonstrates creating a tablespace, creating tables within it, creating an index, dropping tables, and then purging a specific table from the recycle bin. It also shows how to view the recycle bin contents before and after the purge. ```sql -- Create the reason_table_space tablespace. openGauss=# CREATE TABLESPACE REASON_TABLE_SPACE1 owner tpcds RELATIVE location 'tablespace/tsp_reason1'; -- Create the tpcds.reason_t1 table in the tablespace. openGauss=# CREATE TABLE tpcds.reason_t1 ( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) ) tablespace reason_table_space1; -- Create the tpcds.reason_t2 table in the tablespace. openGauss=# CREATE TABLE tpcds.reason_t2 ( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) ) tablespace reason_table_space1; -- Create the tpcds.reason_t3 table in the tablespace. openGauss=# CREATE TABLE tpcds.reason_t3 ( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) ) tablespace reason_table_space1; -- Create an index on the tpcds.reason_t1 table. openGauss=# CREATE INDEX index_t1 on tpcds.reason_t1(r_reason_id); openGauss=# DROP TABLE tpcds.reason_t1; openGauss=# DROP TABLE tpcds.reason_t2; openGauss=# DROP TABLE tpcds.reason_t3; -- View the recycle bin. openGauss=# SELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN; rcyname | rcyoriginname | rcytablespace -----------------------+---------------+--------------- BIN$16409$2CEE988==$0 | reason_t1 | 16408 BIN$16412$2CF2188==$0 | reason_t2 | 16408 BIN$16415$2CF2EC8==$0 | reason_t3 | 16408 BIN$16418$2CF3EC8==$0 | index_t1 | 0 (4 rows) -- Purge the table. openGauss=# PURGE TABLE tpcds.reason_t3; openGauss=# SELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN; rcyname | rcyoriginname | rcytablespace -----------------------+---------------+--------------- BIN$16409$2CEE988==$0 | reason_t1 | 16408 BIN$16412$2CF2188==$0 | reason_t2 | 16408 BIN$16418$2CF3EC8==$0 | index_t1 | 0 (3 rows) ``` -------------------------------- ### Full Example with Error Handling Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/get_diagnostics.md A comprehensive example demonstrating the use of GET DIAGNOSTICS in conjunction with error handling and retrieving various diagnostic items. ```APIDOC ## Full Example with Error Handling This example demonstrates how to use GET DIAGNOSTICS to capture detailed error information after an operation fails, and how to retrieve stacked diagnostics. ```sql -- Enable specific behavior for diagnostics SET enable_set_variable_b_format = ON; SET b_format_behavior_compat_options = 'diagnostics'; -- Initialize variables to store diagnostic information SET @class_origin='',@subclass_origin='',@returned_sqlstate='',@message_text= '',@mysql_errno='',@constraint_catalog='',@constraint_schema='',@constraint_name='',@catalog_name='',@schema_name='',@table_name='',@column_name='',@cursor_name=''; -- Attempt an operation that will fail DROP TABLE IF EXISTS xx; -- Expected ERROR: table "xx" does not exist -- Retrieve statement information (number of conditions and row count) GET diagnostics @num = NUMBER, @row = ROW_COUNT; -- Retrieve detailed condition information for the first condition GET diagnostics condition @num @class_origin=CLASS_ORIGIN,@subclass_origin=SUBCLASS_ORIGIN,@returned_sqlstate=RETURNED_SQLSTATE,@message_text= MESSAGE_TEXT,@mysql_errno=MYSQL_ERRNO,@constraint_catalog=CONSTRAINT_CATALOG,@constraint_schema=CONSTRAINT_SCHEMA,@constraint_name=CONSTRAINT_NAME,@catalog_name=CATALOG_NAME,@schema_name=SCHEMA_NAME,@table_name=TABLE_NAME,@column_name=COLUMN_NAME,@cursor_name=CURSOR_NAME; -- Attempt to retrieve information for a non-existent condition number (condition 2) GET diagnostics condition 2 @class_origin=CLASS_ORIGIN; -- Show errors to verify the captured information SHOW ERRORS; -- Display the retrieved statement information SELECT @num, @row; -- Display the detailed condition information SELECT @class_origin,@subclass_origin,@returned_sqlstate,@message_text,@mysql_errno,@constraint_catalog,@constraint_schema,@constraint_name,@catalog_name,@schema_name,@table_name,@column_name,@cursor_name; -- Attempt to retrieve stacked diagnostics (will error if handler not active) GET stacked diagnostics @num = NUMBER, @row = ROW_COUNT; ERROR: GET STACKED DIAGNOSTICS when handler not active. ``` ``` -------------------------------- ### Install Go SDK and Import Package Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/datavec/integration_go.md Install the Go SDK for openGauss using 'go get'. Import the necessary package in your Go project. ```go go get gitcode.com/opengauss/openGauss-connector-go-pq@master import ( "database/sql" _ "gitcode.com/opengauss/openGauss-connector-go-pq" ) ``` -------------------------------- ### Create a Table for EXPLAIN Examples Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-explain.md This command creates a copy of the 'customer_address' table for use in EXPLAIN examples. ```sql --Create the tpcds.customer_address_p1 table. openGauss=# CREATE TABLE tpcds.customer_address_p1 AS TABLE tpcds.customer_address; ``` -------------------------------- ### Example: Create CMK and CEK in GSQL Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/characteristic_description/advanced_features/fully-encrypted-database.md This example demonstrates the process of creating a CMK and a CEK in the GSQL environment, including user creation and connection setup. ```bash -- Create the $GAUSSHOME/etc/localkms/ directory. mkdir -p $GAUSSHOME/etc/localkms/ -- Use a privileged account to create a common user named alice. openGauss=# CREATE USER alice PASSWORD '********'; -- Use the account of common user alice to connect to the fully-encrypted database and execute the syntax. gsql -p 57101 postgres -U alice -r -C -- Create a CMK object. openGauss=> CREATE CLIENT MASTER KEY alice_cmk WITH (KEY_STORE = localkms , KEY_PATH = "key_path_value", ALGORITHM = RSA_2048); -- Create a CEK object. openGauss=> CREATE COLUMN ENCRYPTION KEY ImgCEK WITH VALUES (CLIENT_MASTER_KEY = alice_cmk, ALGORITHM = AEAD_AES_256_CBC_HMAC_SHA256); ``` -------------------------------- ### Create and Show View Definition Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/extension_reference/dolphin-SHOW-CREATE-VIEW.md This example demonstrates how to create a view and then use SHOW CREATE VIEW to retrieve its definition. Note the session variables character_set_client and collation_connection are also displayed. ```sql --创建视图 openGauss=# create view tt19v as openGauss-# select 'foo'::text = any(array['abc','def','foo']::text[]) c1, openGauss-# 'foo'::text = any((select array['abc','def','foo']::text[])::text[]) c2; CREATE VIEW --查询视图创建语句 openGauss=# show create view tt19v; View | Create View | character_set_client | collation_connection -------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+---------------------- tt19v | CREATE OR REPLACE VIEW public.tt19v AS +| UTF8 | en_US.UTF-8 | SELECT ('foo'::text = ANY (ARRAY['abc'::text, 'def'::text, 'foo'::text])) AS c1, ('foo'::text = ANY ((SELECT ARRAY['abc'::text, 'def'::text, 'foo'::text] AS "array")::text[])) AS c2; | | (1 row) ``` -------------------------------- ### Example: Create User Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/extension_reference/dolphin-ALTER-TABLESPACE.md Demonstrates the creation of a user role. ```sql openGauss=# CREATE ROLE joe IDENTIFIED BY 'xxxxxxxxx'; ``` ```sql openGauss=# CREATE ROLE jay IDENTIFIED BY 'xxxxxxxxx'; ``` -------------------------------- ### Get current date and time based on statement start time Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/date_and_time_processing_functions_and_operators.md These functions return the time at the start of the current statement. `transaction_timestamp()` is equivalent to `CURRENT_TIMESTAMP(precision)`, `statement_timestamp()` returns the start time of the current statement, and `now()` is an alias for `transaction_timestamp()`. ```sql transaction_timestamp() statement_timestamp() now() ``` -------------------------------- ### Example: Create Tablespace Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/extension_reference/dolphin-ALTER-TABLESPACE.md Demonstrates the creation of a tablespace. ```sql openGauss=# CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/tablespace_1'; ``` -------------------------------- ### Get Current Timestamp with Precision Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-date-and-time-processing-functions-and-operators.md Returns the timestamp when the statement starts to be executed with specified precision. The maximum precision is 6. Use this to get timestamp with fractional seconds. ```sql openGauss=# select current_timestamp(3); current_timestamp(3) ------------------------- 2022-07-21 17:00:41.251 (1 row) openGauss=# select current_timestamp(); current_timestamp() --------------------- 2022-07-21 17:06:06 (1 row) ``` -------------------------------- ### Get Current Time with Precision Source: https://github.com/opengauss-mirror/docs/blob/master/docs/en/extension_reference/dolphin-date-and-time-processing-functions-and-operators.md Returns the time when a statement starts to be executed with specified precision. The maximum precision is 6. Use this to get time with fractional seconds. ```sql openGauss=# select current_time(3); current_time(3) ----------------- 16:57:23.255 (1 row) openGauss=# select current_time(); current_time() ---------------- 17:05:01 (1 row) ``` -------------------------------- ### Schema Creation Examples Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/character_set_and_collation.md Examples demonstrating how to create schemas with different character set and collation configurations. ```sql -- 仅设置字符集,字符序为字符集的默认字符序 openGauss=# create schema test charset utf8; ``` ```sql -- 仅设置字符序,字符集为字符序关联的字符集 openGauss=# create schema test collate utf8_bin; ``` ```sql -- 同时设置字符集与字符序,字符集和字符序需对应 openGauss=# create schema test charset utf8 collate utf8_bin; ``` -------------------------------- ### Example: Creating Tables and Index, then Purging Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/purge.md This example demonstrates the process of creating a tablespace, creating tables and an index within it, dropping them (which moves them to the recycle bin), and then selectively purging them. ```sql -- 创建表空间reason_table_space openGauss=# CREATE TABLESPACE REASON_TABLE_SPACE1 owner tpcds RELATIVE location 'tablespace/tsp_reason1'; -- 在表空间创建表tpcds.reason_t1 openGauss=# CREATE TABLE tpcds.reason_t1 ( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) ) tablespace reason_table_space1; -- 在表空间创建表tpcds.reason_t2 openGauss=# CREATE TABLE tpcds.reason_t2 ( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) ) tablespace reason_table_space1; -- 在表空间创建表tpcds.reason_t3 openGauss=# CREATE TABLE tpcds.reason_t3 ( r_reason_sk integer, r_reason_id character(16), r_reason_desc character(100) ) tablespace reason_table_space1; -- 对表tpcds.reason_t1创建索引 openGauss=# CREATE INDEX index_t1 on tpcds.reason_t1(r_reason_id); openGauss=# DROP TABLE tpcds.reason_t1; openGauss=# DROP TABLE tpcds.reason_t2; openGauss=# DROP TABLE tpcds.reason_t3; --查看回收站 openGauss=# SELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN; rcyname | rcyoriginname | rcytablespace -----------------------+---------------+--------------- BIN$16409$2CEE988==$0 | reason_t1 | 16408 BIN$16412$2CF2188==$0 | reason_t2 | 16408 BIN$16415$2CF2EC8==$0 | reason_t3 | 16408 BIN$16418$2CF3EC8==$0 | index_t1 | 0 (4 rows) --PURGE清除表 openGauss=# PURGE TABLE tpcds.reason_t3; openGauss=# SELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN; rcyname | rcyoriginname | rcytablespace -----------------------+---------------+--------------- BIN$16409$2CEE988==$0 | reason_t1 | 16408 BIN$16412$2CF2188==$0 | reason_t2 | 16408 BIN$16418$2CF3EC8==$0 | index_t1 | 0 (3 rows) --PURGE清除索引 openGauss=# PURGE INDEX tindex_t1; openGauss=# SELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN; rcyname | rcyoriginname | rcytablespace -----------------------+---------------+--------------- BIN$16409$2CEE988==$0 | reason_t1 | 16408 BIN$16412$2CF2188==$0 | reason_t2 | 16408 (2 rows) --PURGE清除回收站所有对象 openGauss=# PURGE recyclebin; openGauss=# SELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN; rcyname | rcyoriginname | rcytablespace -----------------------+---------------+--------------- (0 rows) ``` -------------------------------- ### UNION Clause Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/sql_reference/select.md Demonstrates the `UNION` clause, which combines the result sets of two or more SELECT statements. This example merges results from `tpcds.reason` where `r_reason_desc` starts with 'W' or 'N'. ```sql --UNION子句示例:将表tpcds.reason里r_reason_desc字段中的内容以W开头和以N开头的进行合并。 openGauss=# SELECT r_reason_sk, tpcds.reason.r_reason_desc FROM tpcds.reason WHERE tpcds.reason.r_reason_desc LIKE 'W%' UNION SELECT r_reason_sk, tpcds.reason.r_reason_desc FROM tpcds.reason WHERE tpcds.reason.r_reason_desc LIKE 'N%'; ``` -------------------------------- ### Range Partitioning Example Source: https://github.com/opengauss-mirror/docs/blob/master/docs/zh/extension_reference/dolphin-CREATE-TABLE-PARTITION.md Demonstrates inserting data into a range-partitioned table and handling cases where the partition key does not map to any existing partition. It also shows how to query data from specific partitions. ```sql { FUNCEXPR :funcid 1397 :funcresulttype 23 :funcresulttype_orig -1 :funcretset false :funcformat 0 :funccollid 0 :inputcollid 0 :args ({OPEXPR :opno 514 :opfuncid 141 :opresulttype 23 :opretset false :opcollid 0 :inputcollid 0 :args ({VAR :varno 1 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnoold 1 :varoattno 1 :location 64} {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 :constbyval true :constisnull false :ismaxvalue false :location 66 :constvalue 4 [ 2 0 0 0 0 0 0 0 ] :cursor_data :row_count 0 :cur_dno -1 :is_open false :found false :not_found false :null_open false :null_fetch false}) :location 65}) :location 60 :refSynOid 0} (1 row) openGauss=# insert into testrangepart values(-51,1),(49,2); INSERT 0 2 openGauss=# insert into testrangepart values(-101,1); ERROR: inserted partition key does not map to any table partition openGauss=# select * from testrangepart partition(p0); a | b ----+--- 49 | 2 (1 row) openGauss=# select * from testrangepart partition(p1); a | b ----+ -51 | 1 (1 row) openGauss=# select * from testrangepart where a = -51; a | b ----+ -51 | 1 (1 row) ```