PostgreSQL的pg_bulkload工具

pg_bulkload 是一个针对 PostgreSQL 提供高性能批量数据加载的工具。相较于内置的 COPY 命令,pg_bulkload 更加灵活并且在许多情况下性能更高。它支持数据的强制加载、数据过滤、数据转换以及错误处理等多种功能,非常适合需要进行大量数据加载的应用场景。

安装 pg_bulkload

1 下载源代码

下载网址:https://github.com/ossc-db/pg_bulkload/tags

选择对应的版本下载。

2 编译并安装:

[pg16@test resource]$ tar -zxvf pg_bulkload-VERSION3_1_21.tar.gz

[pg16@test resource]$ cd pg_bulkload-VERSION3_1_21/

[pg16@test pg_bulkload-VERSION3_1_21]$ make

[pg16@test pg_bulkload-VERSION3_1_21]$ make install

3 创建extensions

[pg16@test ~]$ psql -p 5777

psql (16.2)

Type "help" for help.

postgres=# \l

List of databases

Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges

-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------

postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |

template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +

| | | | | | | | postgres=CTc/postgres

template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +

| | | | | | | | postgres=CTc/postgres

white | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |

white1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |

white2 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |

white3 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |

(7 rows)

postgres=# \c white postgres

You are now connected to database "white" as user "postgres".

white=# CREATE EXTENSION pg_bulkload;

CREATE EXTENSION

white=# select * from pg_extension;

oid | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition

-------+--------------------+----------+--------------+----------------+------------+-----------+--------------

14270 | plpgsql | 10 | 11 | f | 1.0 | |

17620 | pg_repack | 10 | 2200 | f | 1.5.0 | |

17659 | pg_stat_statements | 10 | 2200 | t | 1.10 | |

17739 | pgstattuple | 10 | 2200 | t | 1.5 | |

17840 | pg_bulkload | 10 | 2200 | f | 3.1.21 | |

(5 rows)

通过help查看帮助文档

[pg16@test ~]$ pg_bulkload --help

pg_bulkload is a bulk data loading tool for PostgreSQL

Usage:

Dataload: pg_bulkload [dataload options] control_file_path

Recovery: pg_bulkload -r [-D DATADIR]

Dataload options:

-i, --input=INPUT INPUT path or function

-O, --output=OUTPUT OUTPUT path or table

-l, --logfile=LOGFILE LOGFILE path

-P, --parse-badfile=* PARSE_BADFILE path

-u, --duplicate-badfile=* DUPLICATE_BADFILE path

-o, --option="key=val" additional option

Recovery options:

-r, --recovery execute recovery

-D, --pgdata=DATADIR database directory

Connection options:

-d, --dbname=DBNAME database to connect

-h, --host=HOSTNAME database server host or socket directory

-p, --port=PORT database server port

-U, --username=USERNAME user name to connect as

-w, --no-password never prompt for password

-W, --password force password prompt

Generic options:

-e, --echo echo queries

-E, --elevel=LEVEL set output message level

--help show this help, then exit

--version output version information, then exit

Read the website for details.

Report bugs to .

使用 pg_bulkload 加载数据

pg_bulkload 支持多种配置文件格式,可以通过配置文件指定加载选项。以下是一个基本的使用示例。

创建测试表 yewu1.test1

CREATE TABLE yewu1.test1 (

id SERIAL PRIMARY KEY,

name VARCHAR(100),

age INT,

city VARCHAR(100)

);

准备数据文件

准备一个数据文件 data.csv:

vi data.csv

1,John Doe,30,New York

2,Jane Smith,25,Los Angeles

3,Bob Johnson,40,Chicago

执行 pg_bulkload

[pg16@test ~]$ pg_bulkload -i data.csv -d white -O yewu1.test1 -l 1.log

NOTICE: BULK LOAD START

NOTICE: BULK LOAD END

0 Rows skipped.

3 Rows successfully loaded.

0 Rows not loaded due to parse errors.

0 Rows not loaded due to duplicate errors.

0 Rows replaced with new rows.

数据确认:

white=# select * from yewu1.test1;

id | name | age | city

----+-------------+-----+-------------

1 | John Doe | 30 | New York

2 | Jane Smith | 25 | Los Angeles

3 | Bob Johnson | 40 | Chicago

(3 rows)

总结

pg_bulkload 是一个强大的 PostgreSQL 批量加载工具,提供了比内置 COPY 命令更多的功能和更高的性能。通过灵活的配置文件,pg_bulkload 可以处理多种输入格式、执行数据转换和过滤、处理错误以及执行并行加载等。如果大量数据加载是您的主要需求,pg_bulkload 确实是一个值得探索的解决方案。

在使用和配置 pg_bulkload 时,建议先阅读官方文档和使用指南,以确保充分利用其各项特性。

Copyright © 2088 世界杯直播cctv5_世界杯阿根 - sunjianping.com All Rights Reserved.
友情链接
top