Database partition; pgAdmin operation pgsql partition; modify pgsql database name

Table of contents

Partition

what is partition

Advantages of Partitioning

pgAdmin operates pgsql partition

create parent table

 create partition

 Data storage partition

Extend (create partition script by day)

Modify the database name

 Link


Partition

what is partition

Refers to dividing a large table or index into multiple small, independent parts, each part is called a partition, in order to better manage and process data. Partitions are logical, and different partitions can be physically stored on different disks or in different locations on the same disk. Database partitioning can be done automatically by the database management system without manually splitting data tables or indexes.

For example: I have a table with a large amount of data, with tens of millions of data per day. In order to facilitate management, statistics and query, I can create partitions by day and put the data into the partitions corresponding to the time. In the case of creating partitions below, there will be Implementation.

Generally speaking, according to the partition strategy, the data is distributed into different sub-tables, and the association relationship is established through the parent table, so as to realize the physical partition of the data.

Advantages of Partitioning

Database partitioning is a technical means to efficiently manage massive data. It can improve the performance, availability and maintainability of the database, and greatly facilitates the development and maintenance of the database.

After the partition table, different tables can be placed in different physical spaces, so that cold data can be placed on cheap physical machines, and hot data can be placed on machines with strong performance.

In terms of performance, the efficiency of querying data through the parent table of the partition table is lower than that of ordinary full data tables. Querying data in a directly partitioned table is more efficient than querying data in a full table.

Compared to creating a single table without partitioning, I think it is more convenient to aggregate statistics by partition.

pgAdmin operates pgsql partition

create parent table

find create table

 Enter the name, open the partition (you will be prompted to specify the partition table key, ignore it)

 Write table attributes must contain a partition logic field, here I partition by time (date), (note that the primary key index cannot appear, otherwise an error will be reported)

 Find partitions, you can see that there are three partition types here I use list

  • Range partitioning: A table is partitioned into "ranges" defined by key columns or sets of columns, with no overlap between ranges of values ​​assigned to different partitions. For example: Partitioning can be done by a date range or by an identifier range for a specific business object.
  • List Partitioning: Partition the table by explicitly listing which key values ​​appear in each partition.
  • Hash (Hash) partition: (HASH strategy has been provided since PG11) to partition the table by specifying the modulus and remainder for each partition. Each partition will hold rows, and dividing the hash of the partition key by the specified modulus will yield the specified remainder.

There are two types of keyType: column and expression, I choose column

Collation sorting rules (used to specify the sorting method of string data in table columns) I did not choose

operator class operator class I did not choose

The partitions box below is to create specific partitions. Let’s not create them first, but create the parent table first.

 You can preview the sql, after all, you can’t only use pgadmin to visualize the operation, save the end

 create partition

Find the parent table just created, open the properties, and find the partitions. Because of the type of list we use, just specify the in field

 After saving, you can see that the partition has appeared

 Data storage partition

very simple case

When inserting, you must specify your partition column. For example, the partition I created above is in=230512, so when I want to insert or query this partition, I must carry the condition of partition column=230512, and the rest will be left to the database for your logic. Insert or query.

Extend (create partition script by day)

In fact, when we create a partition, click the sql column on the far right, and we have already given our sql

Then combine pgagent to write timing tasks, the method is as follows

declare 
    currentDate varchar;
  
BEGIN
  SELECT INTO currentDate to_char(current_date+interval '1 d', 'yymmdd');
  execute 'CREATE TABLE public.message'||currentDate||' PARTITION OF public.message FOR VALUES IN ('||currentDate||')';
  return currentDate;
END;

If you don’t know pgagent, you can search and view it in my article

Modify the database name

After finishing everything, the leader suddenly said that my database name is not very good, I directly right-click to modify the database name and get an error

 This is why, although the option of modification is clearly given, but the modification is not allowed, isn't this bullying honest people?

In fact, it is very simple to think about it. If this database is used by others and there is a connection, what should you do if you change it.

So we go directly to pgsql with the command line and find the pid of the active connection in the database

SELECT
  pid,
  usename,
  application_name
FROM
  pg_stat_activity
WHERE
  datname = 'name';

Immediately afterwards, we directly disconnected their connections (don’t dare to play like this in a formal environment, use it with caution), and kicked all the pids

 SELECT pg_terminate_backend(pid);

 Finally execute the statement to modify the database name

ALTER DATABASE test_db RENAME TO test_new_db;

 Link

PostgreSQL tutorial (sjkjc.com)

Guess you like

Origin blog.csdn.net/wai_58934/article/details/130638117