Quickly implement vacuum full for system tables in PostgreSQL

Source: Internet
Author: User
Tags psql

Vacuum full locks the table and is very inefficient. In practice, vacuum cannot be used to narrow down the pg_class, resulting in a long downtime.
In fact, the simplest way to implement vacuum full is to copy a table again, Create Table B as select * From A; then use table B instead of Table.
Since pg_class is the basis of all tables, we cannot replace it even if we copy it. In this way, we can replace the underlying data file in another way. Because pg_class has a system column, OID, we cannot directly copy this column, So we adopt a roundabout method.
1. Create a new table with OID, create table cxf with oids as select * From pg_class limit 0;
Create the same index as pg_class in this table, because if we replace the underlying data file with the old index file, it will be messy.
2. Export the entire pg_class into text using the Copy command (using the with oids parameter) to export the oId
3. Then, run the copy with oids command to save the data file to the table created in step 1.
4. Stop the database (Write All cached data to a file)
5. Replace the underlying data file and index file
6. Restart the database.

 

Using this method, the data file size of pg_class is changed from 1.4g to 63 m, and pg_attribute is changed from 1.5g to 602 m, achieving the vacuum full effect. The specific steps are as follows:

1. Check the table and index information about pg_class.
Aligputf8 = # select OID, relname, relfilenode from pg_class where relname like '% pg_class % ';
Oid | relname | relfilenode
---------- + ---------------------------- + -------------
1259 | pg_class | 1259
2662 | pg_class_oid_index | 15687137
2663 | pg_class_relname_nsp_index | 15687138

2. view these files in the database base directory.
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> ll-H 1259 1259.*15687137 15687137.*15687138 15687138 .*
Ls: 15687137. *: no such file or directory
Ls: 15687138. *: no such file or directory
-RW ------- 1 gpadmin 1.0g Dec 11 1259
-RW ------- 1 gpadmin 395 M Dec 11 1259.1
-RW ------- 1 gpadmin 20 m Dec 11 20:16 15687137
-RW ------- 1 gpadmin 83 m Dec 11 20:16 15687138

3. Create a table with the same structure as pg_class. The with oids must be added during table creation.
Aligputf8 = # create table cxf with oids as select * From pg_class limit 0;
Select 0
Aligputf8 = # create index cxf_pg_class_oid_index on cxf (OID );
Create Index
Aligputf8 = # create index cxf_pg_class_relname_nsp_index on cxf (relname, relnamespace );
Create Index
Create an index. when starting the database, he will go to pg_class and search for records through the index. Therefore, we need to re-create the index and overwrite the underlying file together.
Oid | relname | relfilenode
---------- + -------------------------------- + -------------
19317362 | cxf | 19317362
19317367 | cxf_pg_class_oid_index | 19317367
19317368 | cxf_pg_class_relname_nsp_index | 19317368
(3 rows)

We can see that the field information of the two tables is consistent with the field content.
Aligputf8 = # select count (*) from pg_attribute where attrelid = 19317362;
Count
-------
38
(1 row)

Aligputf8 = # select count (*) from pg_attribute where attrelid = 1259;
Count
-------
38
(1 row)

4. view the current data volume of pg_class
Aligputf8 = # select count (*) from pg_class;
Count
--------
331799
(1 row)

5. Export pg_class to a file and then import it to cxf.
Aligputf8 = # copy pg_class to '/tmp/pg_class_cxf' with null as ''delimiter E'/5' oids;
Copy 1, 331799
Aligputf8 = # copy cxf from '/tmp/pg_class_cxf' with null as ''delimiter E'/5' oids;
Copy 1, 331799

6. shut down the database, back up the existing pg_class data file and index file to avoid exceptions, and then replace the underlying data file (the database must be closed if the database is not closed, the copied information may not have been flushed to the hard disk. It may be a problem to overwrite the original file. I tried it before. As a result, the pg_class table cannot be found due to data loss, the entire database is unavailable ).
$ Gphome/bin/pg_ctl-w-D/home/gpadmin/cxf/aligp-1/-o "-e-I-P 5132 -- silent-mode = true" Stop
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> ll-H 19317362 19317367 19317368
-RW ------- 1 gpadmin 63 m Dec 11 20:39 19317362
-RW ------- 1 gpadmin 9.8 m Dec 11 19317367
-RW ------- 1 gpadmin 47 M Dec 11 20:39 19317368

Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> MV 1259 1259.bak
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> MV 1259.1 1259.1.bak
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> MV 15687137 15687137.bak
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> MV 15687138 15687138.bak
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> CP 19317362 1259
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> CP 19317367 15687137
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> CP 19317368 15687138
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384>

7. Restart the database and verify
Gpadmin @ hadoop5:/home/gpadmin/cxf/aligp-1/base/16384> pgoptions = "-C gp_session_role = utility" Psql-e
Psql (8.2.13)
Type "help" for help.

Aligputf8 = # select count (*) from pg_class;
Count
--------
331799
(1 row)

Aligputf8 = # explain select * From pg_class where OID = 1259;
Query plan
Bytes ---------------------------------------------------------------------------------------
Index scan using pg_class_oid_index on pg_class (cost = 0. 00 .. 200.58 rows = 1 width = 268)
Index cond: oid = 1259: oid
(2 rows)

8. Use the same method for pg_attribute. In this case, insert directly. You do not need to copy the data to an external table because the table has no oid.
Data Volume changed from 602 GB to MB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.