更新时间:2026-07-28 GMT+08:00
分享

操作指导

示例

  • JDBC用例
    1. 准备数据。
      gaussdb=> CREATE TABLE test_gpc_jdbc(col_a int, col_b int);
      CREATE TABLE
      gaussdb=> INSERT INTO test_gpc_jdbc VALUES(generate_series(1,1000), generate_series(1,1000));
      INSERT 0 1000
    2. 执行JDBC。
      // 以下用例以gsjdbc4.jar驱动为例,如果使用其他驱动包,仅需修改驱动类名和数据库连接的url前缀。  
      // gsjdbc4.jar: 主类名为“org.postgresql.Driver”,数据库连接的url前缀为“jdbc:postgresql”。  
      
      public static void main(String[] args) throws IOException {  
          // 链接字符串中prepareThreshold = 1,表示从第一个语句开始就是named PBE 。
          String urls = "jdbc:postgresql://localhost:48100/postgres?prepareThreshold=1";  
          // 在环境变量USER、PASSWORD配置用户名和密码  
          String username = System.getenv("USER");  
          String passwd = System.getenv("PASSWORD");  
          Properties urlProps = new Properties();  
          urlProps.setProperty("user", username);  
          urlProps.setProperty("password", passwd);  
      
          try {  
              Connection con = DriverManager.getConnection(urls, urlProps);  
              con.setAutoCommit(true);  
              Statement stmt = con.createStatement();  
              // 为方便快速查看计划存入缓存的情况,设置plan_cache_mode=force_generic_plan。  
              stmt.execute("set plan_cache_mode = 'force_generic_plan';");  
              // 调用同一个Connection接口创建prepareStatement并执行语句。  
              String selectsql = "select * from test_gpc_jdbc where col_a = ? and col_b = ?;";  
              PreparedStatement pstmt = con.prepareStatement(selectsql);  
              pstmt.setInt(1, 100);  
              pstmt.setInt(2, 200);  
              pstmt.execute();  
      
              stmt.close();  
              pstmt.close();  
              con.close();  
          } catch (SQLException e) {  
              e.printStackTrace();  
              System.exit(1);  
          }  
      }
    3. 查看GPC视图,可查询到刚执行的语句,最后通过系统视图清理。
      gaussdb=> SELECT query, refcount, valid FROM dbe_perf.global_plancache_status WHERE query LIKE '%test_gpc_jdbc%';
                                  query                            | refcount | valid 
      -------------------------------------------------------------+----------+-------
       select * from test_gpc_jdbc where col_a = $1 and col_b = $2 |        0 | t
      (1 row)
      
      gaussdb=> SELECT * FROM dbe_perf.global_plancache_clean;
       plancache_clean 
      -----------------
       t
      (1 row)
      
      gaussdb=> SELECT query, refcount, valid FROM dbe_perf.global_plancache_status WHERE query LIKE '%test_gpc_jdbc%';
       query | refcount | valid 
      -------+----------+-------
      (0 rows)
    4. 删除表。
      gaussdb=> DROP TABLE test_gpc_jdbc;
      DROP TABLE
  • 函数用例
    1. 准备数据和函数。
      gaussdb=> CREATE TABLE test_gpc_proc(col_a int);
      CREATE TABLE
      gaussdb=> INSERT INTO test_gpc_proc VALUES(generate_series(1, 1000));
      INSERT 0 1000
      gaussdb=> CREATE OR REPLACE FUNCTION count_beyond
      gaussdb-> (
      gaussdb(> threshold int
      gaussdb(> )
      gaussdb-> RETURNS int
      gaussdb-> AS $$
      gaussdb$> DECLARE
      gaussdb$> count_number int;
      gaussdb$> BEGIN 
      gaussdb$>     select count(*) from test_gpc_proc where col_a > threshold into count_number;
      gaussdb$>     return count_number; 
      gaussdb$> END;
      gaussdb$> $$ LANGUAGE 'plpgsql';
      CREATE FUNCTION
      gaussdb=> SET plan_cache_mode='force_generic_plan';
      SET
    2. 调用函数并查看GPC视图。
      gaussdb=> SELECT count_beyond(200);
       count_beyond 
      --------------
                800
      (1 row)
      
      gaussdb=> SELECT query, refcount, valid FROM dbe_perf.global_plancache_status WHERE query LIKE '%test_gpc_proc%';
                                 query                            | refcount | valid 
      ------------------------------------------------------------+----------+-------
       select count(*) from test_gpc_proc where col_a > threshold |        1 | t
      (1 row)
    3. 删除函数及数据。
      gaussdb=> DROP FUNCTION count_beyond;
      DROP FUNCTION
      gaussdb=> DROP TABLE test_gpc_proc;
      DROP TABLE

相关文档