LightDB快速入門


LightDB是什么?

  LightDB是一款基於PostgreSQL的關系型數據庫,99%兼容PostgreSQL,主要針對金融業務場景優化。不同於國內其他基於PostgreSQL的數據庫為了修改而修改,導致無法持續向后兼容新版本的PostgreSQL,進而無法享受社區帶來的改進。LightDB采用完全擁抱開源的模式,在開源PostgreSQL基礎上進行增強如:支持開源PostgreSQL不支持的分區定義語法、運行時被剪除的分區不顯示、實現了pg_stat_monitor&pg_profile等監控沒有包含的等待事件(這是DBA優化數據庫最重要的分析信息)、類似Oracle的自動參數優化、默認主機免密登錄&遠程md5認證等等。針對Linux平台優化(不支持也無計划支持Windows),並持續以patch模式回饋社區,采用和Percona Server、EnterpriseDB一樣的運作模式。

  不同於國內要么僅提供github開源、要么必須先走銷售后提供使用包的模式,LightDB選擇不開源,但是無功能、容量限制180天下載試用的機制,所以開發人員無需擔心被雙標。

快速入門

1、安裝

  用戶可以從LightDB官網https://www.hs.net/lightdb下載LightDB對應版本,當前提供x86、arm兩個版本(Q3末將提供麒麟OS、歐拉版本),分單機與高可用版(Q3末高可用和單機版安裝包將合二為一,同時支持GUI和CLI安裝),其中高可用版當前僅支持x86。下面以x86為例,先下載LightDB-X13.3-21.1-el7.x86_64.bin(注意:需要目標操作系統為redhat/centos 7.x版本,6和8可能會有依賴問題)。

 

   下載好之后,上傳到linux,如下:

[root@hs-10-20-30-127 ~]# ll | grep LightDB-X13.3-21.1-el7.x86_64.bin 
-rw-r--r--   1 root root  96927077 Aug 21 17:56 LightDB-X13.3-21.1-el7.x86_64.bin

  可以使用root或lightdb用戶安裝LightDB數據庫,如果未預創建lightdb,則安裝過程中會自動創建。如下:

[root@hs-10-20-30-127 ~]# chmod a+x LightDB-X13.3-21.1-el7.x86_64.bin 
[root@hs-10-20-30-127 ~]# ./LightDB-X13.3-21.1-el7.x86_64.bin install
Do you want to install LightDB?[Y/N]:Y
Do you need to add user 'lightdb' and group 'lightdb'?[Y/N]:Y
Which directory do you want to install to? [/home/lightdb]:Y
Please enter the correct directory absolute path:
Please enter the correct directory absolute path:/home/lightdb
Install to directory '/home/lightdb'?[Y/N]:Y
lightdb install path is: '/home/lightdb/lightdb13.3-21.1'
lightdb bin directory is: '/home/lightdb/lightdb13.3-21.1/lightdb-x/bin'
uuid-1.6.2-26.el7.x86_64
Install db ...
lightdb_after_install.sh is not exists,now set environment
configuring environment variable of lightdb ... config path done
configuring lib environment variable of lightdb ... config libpath done
lightdb installed success,next step to init default instance,Do you want to create default cluster?[Y/N]:Y
Now configure instance parameters ...
 INFO  modify effective_cache_size successfully
 INFO  modify wal_buffers successfully
 INFO  modify max_wal_size successfully
 INFO  modify min_wal_size successfully
 INFO  modify random_page_cost successfully
 INFO  modify max_parallel_maintenance_workers successfully
 INFO  modify default_statistics_target successfully
 INFO  modify min_parallel_index_scan_size successfully
 INFO  modify max_worker_processes successfully
 INFO  modify auto_explain.log_min_duration successfully
 INFO  modify max_parallel_workers successfully
 INFO  modify logging_collector successfully
 INFO  modify enable_partitionwise_aggregate successfully
 INFO  modify log_min_duration_statement successfully
 INFO  modify shared_buffers successfully
 INFO  modify parallel_setup_cost successfully
 INFO  modify track_io_timing successfully
 INFO  modify log_min_messages successfully
 INFO  modify temp_buffers successfully
 INFO  modify commit_siblings successfully
 INFO  modify min_parallel_table_scan_size successfully
init default instance sucess,We will start this install by default port 5432,please remember your password
wJqnP9HlWz   -- lightdb用戶默認初始密碼
waiting for server to start.... done
server started
LightDB install finish,welcome to use LightDB
[root@hs-10-20-30-127 ~]# lsof -i:5432
COMMAND   PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
lightdb 20025 lightdb    6u  IPv4 329909      0t0  TCP *:postgres (LISTEN)

  不用一分鍾,很簡單,就安裝好了。

  卸載同樣簡單,install子目錄下包含了一個uninstall.sh腳本,可用於一鍵干凈卸載。

[lightdb@hs-10-20-30-127 ~]$ ll
total 192
drwxrwxr-x 3 lightdb lightdb     28 Aug 21 18:19 data
drwxr-xr-x 5 lightdb lightdb     52 Aug 21 18:18 lightdb13.3-21.1
-rw-rw-r-- 1 lightdb lightdb 194232 Aug 21 19:40 pwr_1_10.html
[lightdb@hs-10-20-30-127 ~]$ cd lightdb13.3-21.1/
[lightdb@hs-10-20-30-127 lightdb13.3-21.1]$ ll
total 0
drwxrwxr-x 3 lightdb lightdb 106 Aug 21 18:19 install
drwxrwxr-x 7 lightdb lightdb  69 Aug 17 21:43 lightdb-x
drwxrwxr-x 7 lightdb lightdb  67 Aug  3 15:57 pgpool
[lightdb@hs-10-20-30-127 lightdb13.3-21.1]$ cd install/
[lightdb@hs-10-20-30-127 install]$ ll
total 20
-rw-rw-r-- 1 lightdb lightdb 6109 Aug 21 18:19 install_2021_08_21_181825.log
-rwxrwxr-x 1 lightdb lightdb 3246 Aug 21 18:18 lightdb-uninstall.sh
-rw-rw-r-- 1 lightdb lightdb 4671 Aug 21 18:18 README.md
drwxr-xr-x 2 lightdb lightdb   42 Aug 21 18:18 system-lib
[lightdb@hs-10-20-30-127 install]$ pwd
/home/lightdb/lightdb13.3-21.1/install

2、簡單使用

  在主機端,可以使用ltsql(類似於mysql客戶端或oracle sql*plus)登錄LightDB數據庫(一般來說,除了運維不得已以及一些特殊場景,大部分開發人員都會通過navicat或dbeaver訪問LightDB,因為LightDB兼容PostgreSQL協議,因此和PostgreSQL一樣的方式去連接即可)。如下:

[lightdb@hs-10-20-30-127 ~]$ ltsql
ltsql (13.3-21.1)
Type "help" for help.

[lightdb@hs-10-20-30-127 ~]$ ltsql
ltsql (13.3-21.1)
Type "help" for help.

postgres=# \l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 postgres  | lightdb | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | lightdb | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/lightdb         +
           |         |          |             |             | lightdb=CTc/lightdb
 template1 | lightdb | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/lightdb         +
           |         |          |             |             | lightdb=CTc/lightdb
(3 rows)

postgres=# create database my_lightdb;
CREATE DATABASE
postgres=# \c my_lightdb;
You are now connected to database "my_lightdb" as user "lightdb".
my_lightdb=# create table my_tab(id serial/*類似於mysql auto_increment,oracle序列+序列作為默認值*/,name varchar(100),desc1 varchar(1000));
CREATE TABLE
my_lightdb=# insert into my_tab(name,desc1) values('name1','desc1');
INSERT 0 1

my_lightdb=# insert into my_tab(name,desc1) select uuid_ns_oid(),uuid_ns_oid() from generate_series(1,1000);   -- 批量創建數據,類似於oracle的connect by level,比mysql方便的多
INSERT 0 1000
my_lightdb=# select * from my_tab limit 1;
 id | name  | desc1 
----+-------+-------
  1 | name1 | desc1
(1 row)

my_lightdb=# select * from my_tab limit 2;
 id |                 name                 |                desc1                 
----+--------------------------------------+--------------------------------------
  1 | name1                                | desc1
  2 | 6ba7b812-9dad-11d1-80b4-00c04fd430c8 | 6ba7b812-9dad-11d1-80b4-00c04fd430c8
(2 rows)

  查看SQL語句的執行時間,可以打開ltsql客戶端的\timing選項,如下:

my_lightdb=# \timing on
Timing is on.
my_lightdb=# select count(1) from my_tab;
 count 
-------
  1001
(1 row)

Time: 0.420 ms
my_lightdb=# select count(1) from my_tab;
 count 
-------
  1001
(1 row)

Time: 0.151 ms

  查看當前數據庫下的對象及其大小,可以執行ltsql命令\dS+,如下:

my_lightdb=# \dS+
                                             List of relations
   Schema   |              Name               |   Type   |  Owner  | Persistence |    Size    | Description 
------------+---------------------------------+----------+---------+-------------+------------+-------------
 pg_catalog | pg_aggregate                    | table    | lightdb | permanent   | 56 kB      | 
 pg_catalog | pg_am                           | table    | lightdb | permanent   | 40 kB      | 
 pg_catalog | pg_amop                         | table    | lightdb | permanent   | 80 kB      | 
 pg_catalog | pg_amproc                       | table    | lightdb | permanent   | 64 kB      | 
......
......
 public     | dual                            | view     | lightdb | permanent   | 0 bytes    | 
 public     | my_tab                          | table    | lightdb | permanent   | 152 kB     | 
 public     | my_tab_id_seq                   | sequence | lightdb | permanent   | 8192 bytes | 
 public     | pg_stat_statements              | view     | lightdb | permanent   | 0 bytes    | 
 public     | pg_wait_sampling_current        | view     | lightdb | permanent   | 0 bytes    | 
 public     | pg_wait_sampling_history        | view     | lightdb | permanent   | 0 bytes    | 
 public     | pg_wait_sampling_profile        | view     | lightdb | permanent   | 0 bytes    | 
(136 rows)

  查看特定表的定義:

my_lightdb=# \dS+ my_tab;
                                                        Table "public.my_tab"
 Column |          Type           | Collation | Nullable |              Default               | Storage  | Stats target | Description 
--------+-------------------------+-----------+----------+------------------------------------+----------+--------------+-------------
 id     | integer                 |           | not null | nextval('my_tab_id_seq'::regclass) | plain    |              | 
 name   | character varying(100)  |           |          |                                    | extended |              | 
 desc1  | character varying(1000) |           |          |                                    | extended |              | 
Access method: heap

  查看SQL語句的執行計划可以使用explain,如下:

my_lightdb=# explain select * from my_tab where id = 100;
                       QUERY PLAN                       
--------------------------------------------------------
 Seq Scan on my_tab  (cost=0.00..26.51 rows=1 width=76)
   Filter: (id = 100)
(2 rows)

Time: 0.289 ms
my_lightdb=# explain (analyze,verbose) select * from my_tab where id = 100;
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Seq Scan on public.my_tab  (cost=0.00..26.51 rows=1 width=76) (actual time=0.020..0.088 rows=1 loops=1)
   Output: id, name, desc1
   Filter: (my_tab.id = 100)
   Rows Removed by Filter: 1000
 Planning Time: 0.048 ms
 Execution Time: 0.110 ms
(6 rows)

Time: 0.386 ms
my_lightdb=# explain (analyze,verbose,buffers/* buffers選項能夠查看每個行源rowsource的邏輯和物理讀次數,使得分析更加直接*/) select * from my_tab where id = 100;
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Seq Scan on public.my_tab  (cost=0.00..26.51 rows=1 width=76) (actual time=0.019..0.076 rows=1 loops=1)
   Output: id, name, desc1
   Filter: (my_tab.id = 100)
   Rows Removed by Filter: 1000
   Buffers: shared hit=14
 Planning Time: 0.050 ms
 Execution Time: 0.094 ms
(7 rows)

Time: 0.385 ms

3、文件導入導出

  文本文件是日常需求之一,LightDB支持客戶端和服務端導入導出,\copy(ltsql命令)是客戶端導入導出,copy(SQL語句)是服務端導入導出。

  就導入而言,\copy性能一般,不適用於生產大數據量導入導出,LightDB下個版本將包含提供類似於oracle sqlloader的大數據加載工具。

  就導出而言,\copy可以開多個會話並行導出,同一個表的不同分區也可以並行導出,因此可以滿足生產需求。

my_lightdb=# \copy (select * from ora_datatype) to '/home/lightdb/t.csv' delimiter '|' csv header;
COPY 5
my_lightdb=# exit
[lightdb@hs-10-20-30-127 install]$ cd
[lightdb@hs-10-20-30-127 ~]$ more t.csv 
id|name|create_date
1|ff|2021-08-21
2|should b|2021-08-21
3|xx|2021-08-21
4|baba|2021-08-21
5|baba|2020-12-21
[lightdb@hs-10-20-30-127 ~]$ ltsql
ltsql (13.3-21.1)
Type "help" for help.

postgres=# \c my_lightdb 
You are now connected to database "my_lightdb" as user "lightdb".

my_lightdb=# create table ora_datatype_2 (like ora_datatype);  -- 默認值、序列、索引都不包含
CREATE TABLE
my_lightdb=# create table ora_datatype_3 (like ora_datatype INCLUDING INDEXES INCLUDING COMMENTS);  -- 不包含序列定義
CREATE TABLE
my_lightdb=# create table ora_datatype_4 (like ora_datatype INCLUDING DEFAULTS);  -- 包含了序列定義,不合適
CREATE TABLE

my_lightdb=# \copy ora_datatype_2 from '/home/lightdb/t.csv' with (format csv,delimiter '|',header true);
COPY 5
my_lightdb=# select * from ora_datatype_2
my_lightdb-# ;
 id |   name   | create_date 
----+----------+-------------
  1 | ff       | 2021-08-21
  2 | should b | 2021-08-21
  3 | xx       | 2021-08-21
  4 | baba     | 2021-08-21
  5 | baba     | 2020-12-21
(5 rows)

   在后續系列,我們將陸續推出從Oracle、MySQL到LightDB的遷移示例。心急的小伙伴也可以現在就訪問https://www.hs.net/lightdb,右下角包含了pdf版本的遷移指南。

  注:對於非text/varchar類型的字段,如果CSV文件中有潛在的null值(使用navicat導出mysql或SELECT INTO…OUTFILE的時候就可能會有這種值),需要在with選項中通過FORCE_NULL(col1,col2)列出可能為NULL的字段,如下:

postgres=# copy tsys_user_right from '/home/zjh/test_data/tsys_user_right.csv' with (FORMAT csv, DELIMITER ',',HEADER false,FORCE_NULL(create_date));

  否則會提示“ERROR:  invalid input syntax for type bigint: ""”,如下:

postgres=# copy tsys_user_right from '/home/zjh/test_data/tsys_user_right.csv' with (FORMAT csv, DELIMITER ',',HEADER false);
ERROR:  invalid input syntax for type bigint: ""
CONTEXT:  COPY tsys_user_right, line 280, column create_date: ""

  除了copy導入導出外,LightDB還支持外部表的該表,其通過標准的FDW實現(不過其效率較低,不適合生產使用)。

4、監控

  對於任何數據庫來說,監控是很重要的基礎工具,LightDB也不例外,自帶pgcenter用來實時監控LightDB實例的整體運行情況以及top sql,如下:

# 在下個版本,不帶地址選項的pgcenter默認將訪問本地LightDB實例
[lightdb@hs-10-20-30-127 ~]$ pgcenter top -h 127.0.0.1 -p 5432 postgres

 

  相比pgAdmin,pgcenter更加直觀。除了top命令,pgcenter還包含profile命令用於跟蹤某個特定PID,record命令用來跟蹤LightDB實例一段時間的運行情況。用戶可輸入pgcenter --help查看完整的功能。

  當前除了使用pgcenter監控外,用戶也可以通過promethous監控。后續,LightDB將提供數據庫領域監控平台LightDB-EM。

5、PWR報告

  筆者以前做架構時,但凡誰來推薦數據庫,首先問的是有沒有類似一樣開箱即用的AWR報告。所以,在LightDB中,自然不能錯過,LightDB在pg_profile中補充了最重要的等待事件部分(下個版本將完全重構為PWR和PSH),且默認開箱即用的10分鍾自動執行一次快照。如下:

postgres=# select show_samples();   -- 查詢當前快照
           show_samples            
-----------------------------------
 (1,"2021-08-21 18:20:00+08",t,,,)
 (2,"2021-08-21 18:30:00+08",t,,,)
 (3,"2021-08-21 18:40:00+08",t,,,)
 (4,"2021-08-21 18:50:00+08",t,,,)
 (5,"2021-08-21 19:00:00+08",t,,,)
 (6,"2021-08-21 19:10:00+08",t,,,)
 (7,"2021-08-21 19:20:00+08",t,,,)
(7 rows)
postgres=# select take_sample();  -- 創建快照
      take_sample       
------------------------
 (local,OK,00:00:00.17)
(1 row)
[lightdb@hs-10-20-30-127 ~]$ ltsql -Aqtc "SELECT get_report(1,10)" -o pwr_1_10.html
[lightdb@hs-10-20-30-127 ~]$ ll | grep pwr_1_10.html 
-rw-rw-r-- 1 lightdb lightdb 194232 Aug 21 19:40 pwr_1_10.html

  

 

   

  

  在下個版本,PWR中將包含主機的信息,TOP SQL語句中包含SQL語句的前200個字符提高易讀性等,等待事件進一步細分為Idle和非Idle。

  關於PWR每部分的解釋可以參見https://www.hs.net/r/cms/www/itn/forPrd/html/pgprofile.html#id-1.11.7.37.10

6、Oracle兼容

  LightDB兼容部分Oracle函數、特性及數據類型,例如to_number,to_char,dual,decode,varchar2,date。

postgres=# select to_number(x) from (select '123.323' x) x;
 to_number 
-----------
   123.323
(1 row)
postgres=# select to_char(123.45) from dual;
 to_char 
---------
 123.45
(1 row)

postgres=# select to_number('123.45') from dual;
 to_number 
-----------
    123.45
(1 row)
my_lightdb=# create table ora_datatype(id serial primary key,name varchar2(100),create_date date /*注:date部分不包含時間*/);
CREATE TABLE

my_lightdb=# insert into ora_datatype(name,create_date) values('ff',current_date);
INSERT 0 1
my_lightdb=# select * from ora_datatype;
 id | name | create_date 
----+------+-------------
  1 | ff   | 2021-08-21
(1 row)

my_lightdb=# insert into ora_datatype(name,create_date) values(decode('abc'::text,'a','a','should b'),current_date);
INSERT 0 1
my_lightdb=# select * from ora_datatype;
 id |   name   | create_date 
----+----------+-------------
  1 | ff       | 2021-08-21
  2 | should b | 2021-08-21
(2 rows)

my_lightdb=# select nvl(decode(name,'ff',null,name),'cast to null') from ora_datatype;
     nvl      
--------------
 cast to null
 should b
(2 rows)

  listagg聚合函數:

my_lightdb=# select * from ora_datatype;
 id |   name   | create_date 
----+----------+-------------
  1 | ff       | 2021-08-21
  2 | should b | 2021-08-21
  3 | xx       | 2021-08-21
  4 | baba     | 2021-08-21
  5 | baba     | 2020-12-21
(5 rows)

my_lightdb=# select create_date,listagg(name,',') from ora_datatype group by create_date;
 create_date |       listagg       
-------------+---------------------
 2020-12-21  | baba
 2021-08-21  | ff,should b,xx,baba
(2 rows)
my_lightdb=# select add_months(date'2016/05/01',2) from dual;
 add_months 
------------
 2016-07-01
(1 row)

my_lightdb=# select add_months(date'2016/05/01',-2) from dual;
 add_months 
------------
 2016-03-01
(1 row)

my_lightdb=# select last_day(current_timestamp::date) from dual;
  last_day  
------------
 2021-08-31
(1 row)

my_lightdb=# SELECT MONTHS_BETWEEN(DATE'2016/03/15', DATE'2015/11/15') FROM DUAL;
 months_between 
----------------
              4
(1 row)

my_lightdb=# select to_date('2021/12/12','yyyy/mm/dd')
my_lightdb-# ;
  to_date   
------------
 2021-12-12
(1 row)

7、定時任務

  LightDB包含開箱即用的定時任務功能,定時任務功能需要在postgres數據庫下執行:

 

postgres=# SELECT cron.schedule('nightly-vacuum', '0 10 * * *', 'VACUUM');
 schedule 
----------
        2
(1 row)
-- 查詢所有定時任務
postgres=# select * from cron.job;
 jobid |   schedule   |       command        | nodename  | nodeport | database | username | active |         jobname         
-------+--------------+----------------------+-----------+----------+----------+----------+--------+-------------------------
     1 | */10 * * * * | SELECT take_sample() | localhost |     5432 | postgres | lightdb  | t      | take_sample_every_10min
     2 | 0 10 * * *   | VACUUM               | localhost |     5432 | postgres | lightdb  | t      | nightly-vacuum
(2 rows)

 

  定時任務的格式遵循cron表達式規則,如下:

 ┌───────────── min (0 - 59)
 │ ┌────────────── hour (0 - 23)
 │ │ ┌─────────────── day of month (1 - 31)
 │ │ │ ┌──────────────── month (1 - 12)
 │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
 │ │ │ │ │                  Saturday, or use names; 7 is also Sunday)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *
cron表達式可以基於https://crontab.guru/在線網站生成。
值得注意的是,如果LightDB運行在hot standby模式,定時任務是不會執行的。一旦hot standby服務器被promote,定時任務就會自動觸發。這也是符合業務的預期行為。

 

  詳細兼容明細可以參考https://www.hs.net/r/cms/www/itn/forPrd/html/orafce.html#id-1.11.7.30.4,后續版本將兼容rownum,rowid,sysdate。

===========================

  入門系列就到此為止,足以帶用戶上路了。

  除了上面這些功能外,LightDB還包含了很多其它開箱即用功能,有興趣的小伙伴和用戶可以下載體驗。

   最后,如果用戶希望部署高可用的話,可參考LightDB官網的《LightDB數據庫安裝手冊》,其安裝在單機基礎上增加了ssh互通和一個文本格式的主機列表配置,除此之外,無需額外配置即可完成高可用配置,應用通過VIP訪問LightDB。下一篇我們介紹LightDB高可用的自動切換使用。

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM