当前位置:网站首页>Automatically create partition tables by time and month in PostgreSQL
Automatically create partition tables by time and month in PostgreSQL
2022-07-25 01:50:00 【Xiao Guangbiao】
PostgreSQL Automatically create partition tables by time and month
Preface
In my work, I encountered ten million level single table data query optimization , Bloggers think of table partition for the first time , The process of solving the problem is hereby recorded , Avoid digging holes .
Database table partition divides a large physical table into several small physical tables , And these small physical tables can be logically used as a table .
PostgreSQL Partition the table in the database , And 11g Previous Oracle similar , You need to create partition tables manually 、 Index, etc. . By inheritance , For example, by time , Create a table partition every month , Data is recorded in the corresponding partition .
1. Create the main table
Create main table structure , The name of the table three_height_data_value, The name of the time field : data_time
CREATE TABLE "public"."three_height_data_value" (
"data_id" varchar(64) COLLATE "pg_catalog"."default" NOT NULL,
"data_time" timestamp(6) NOT NULL,
"data_value" float8 NOT NULL
);
2. Create stored procedure
Create stored procedure , Usage dynamics SQL, Automate the operation and maintenance of large table partitions .
CREATE OR REPLACE FUNCTION auto_insert_into_three_height_data_value()
RETURNS trigger AS
$BODY$
DECLARE
time_column_name text ; -- The name of the time field used for partitioning in the parent table [ Must first initialize !!]
curMM varchar(6); -- 'YYYYMM' String , Used as the suffix of partition sub table
isExist boolean; -- Partition sub table , Does it already exist
startTime text;
endTime text;
strSQL text;
BEGIN
-- Before calling , Must first initialize ( Time field name ):time_column_name [ Get... Directly from the call parameters !!]
time_column_name := TG_ARGV[0];
-- Determine the corresponding partition table Does it already exist ?
EXECUTE 'SELECT $1.'||time_column_name INTO strSQL USING NEW;
curMM := to_char( strSQL::timestamp , 'YYYYMM' );
select count(*) INTO isExist from pg_class where relname = (TG_RELNAME||'_'||curMM);
-- If it does not exist , Before insertion Create a sub partition first
IF ( isExist = false ) THEN
-- Create a sub partition table
startTime := curMM||'01 00:00:00';
endTime := to_char( startTime::timestamp + interval '1 month', 'YYYY-MM-DD HH24:MI:SS');
strSQL := 'CREATE TABLE IF NOT EXISTS '||TG_RELNAME||'_'||curMM||
' ( CHECK('||time_column_name||'>='''|| startTime ||''' AND '
||time_column_name||'< '''|| endTime ||''' )
) INHERITS ('||TG_RELNAME||') ;' ;
EXECUTE strSQL;
-- Create index
strSQL := 'CREATE INDEX '||TG_RELNAME||'_'||curMM||'_INDEX_'||time_column_name||' ON '
||TG_RELNAME||'_'||curMM||' ('||time_column_name||');' ;
EXECUTE strSQL;
END IF;
-- Insert data into sub partition !
strSQL := 'INSERT INTO '||TG_RELNAME||'_'||curMM||' SELECT $1.*' ;
EXECUTE strSQL USING NEW;
RETURN NULL;
END
$BODY$
LANGUAGE plpgsql;
explain :
(1) The code uses TG_ARGV[0] To get the parameters passed in during the call : The name of the time field used for the partition .
(2) In the code , Through built-in parameters TG_RELNAME Get the table name of the parent table .
(3) First, according to the insertion time , Judge whether the corresponding partition table exists ? If exist , Directly insert the corresponding partition sub table
(4) If the partition table does not exist , First create partitioned sub tables and indexes , Then insert the data into the created sub table .
Above code , stay PostgreSQL v9.4 Debugging through . Theoretically , v8.4 All of the above are supported .
3. Create trigger
CREATE TRIGGER insert_three_height_data_value_trigger
BEFORE INSERT
ON three_height_data_value
FOR EACH ROW
EXECUTE PROCEDURE auto_insert_into_three_height_data_value('data_time');
4. The pit of tread
(1)constraint_exclusion Property settings
Make sure postgresql.conf Medium constraint_exclusion Configuration item is not disable . This is very important , If this parameter item is disable, Query performance based on partitioned tables cannot be optimized , Even lower performance than using indexes directly without using partitioned tables .
(2)linux How to execute postgresql Of sql Script
Method 1 : First, through psql Connect to the corresponding db:
psql -d db1 -U userA
Then enter the password , After entering the database , Input :
\i /pathA/xxx.sql
Method 2 : Directly through psql Command execution SQL file
In this way, there is no need to log in to the database first , Just use one command
psql -d db1 -U userA -f /pathA/xxx.sql
(3) How to query table partitions
– Query the specified partition table
SELECT nmsp_parent.nspname AS parent_schema , parent.relname AS parent , nmsp_child.nspname AS child , child.relname AS child_schema FROM pg_inherits JOIN pg_class parent ON pg_inherits.inhparent = parent.oid JOIN pg_class child ON pg_inherits.inhrelid = child.oid JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace WHERE parent.relname = 'plc_import_data_value';
Query all zone tables
SELECT nspname , relname ,COUNT(*) AS partition_num FROM pg_class c , pg_namespace n , pg_inherits i WHERE c.oid = i.inhparent AND c.relnamespace = n.oid AND c.relhassubclass AND c.relkind = 'r' GROUP BY 1,2 ORDER BY partition_num DESC;
If it helps , please “ give the thumbs-up ”、“ Focus on ”、“ Collection ” Ha !
边栏推荐
- Custom type
- 【Appium】Failed to create session. An unknown server-side error occurred while processing the command
- Chip sold at sand price: Lei Jun's dream was "ruined" by this company
- Speed comparison between 64 bit width and 32 bit width of arm64 memory
- [27. Expression evaluation (infix expression)]
- Thinkphp5.0.24 deserialization chain analysis
- Standard transfer function
- Actf questions (dropper+master_of_dns)
- Mongodb security cluster construction
- Jsonp solves cross domain plug-ins (JS, TS)
猜你喜欢

Take the first place in the International Olympic Games in mathematics, physics and chemistry, and win all the gold medals. Netizen: the Chinese team is too good

Example analysis of recombinant monoclonal antibody prosci CD154 antibody

"Nowadays, more than 99.9% of the code is garbage!"

Talk about resume optimization and interview skills of algorithm post!

Opengauss kernel analysis: query rewriting

Pursue and kill "wallet Assassin" all over the network

Eolink - quickly develop interfaces through document driven

Kubernetes creates a user with dashboard read-only permission (with exec permission)

Synchronization primitive: lock

Kernel structure and design
随机推荐
Gerrit statistics script
Green low-carbon Tianyi cloud, a new engine of digital economy!
Jsonp solves cross domain plug-ins (JS, TS)
Hcip - BGP - border gateway protocol
2022.7.20 linear table
Cloud native platform, let edge applications play out!
Luo min cannot become Dong Yuhui
Scientific data center resources and user access control system
[28. Maximum XOR pair]
Hbuilderx developed by uni app connects to night God simulator
Ecosystem long-term observation data product system
10 commonly used data visualization tool software
About the relationship between parent process and child process (UAC bypass idea)
The introduction of 23 Filipino doctors for 18million was a hot topic, and the school teacher responded: expedient
CSRF attack principle scenario
G024-db-gs-ins-01 openeuler deployment opengauss (single instance)
Take C language from 0 to 1 - program structure and use examples
When does MySQL use table locks and row locks?
Interpretation of video generation paper of fed shot video to video (neurips 2019)
Speed comparison between 64 bit width and 32 bit width of arm64 memory