当少量写入Postgresql

时间:2018-11-24 23:26:18

标签: postgresql optimization database-connection libpqxx

我是Postgresql的大一新生,所以任何提示或技巧都值得赞赏。

我的程序很小。它从套接字接收数据,然后将它们插入没有索引/外键/约束/文本字段的表中。实际上,它只有几个数字字段。

我来自网络的数据以每秒约100到1000条记录的速度连续不断地传输。我的Postgresql根本没有优化,并且所有东西都以默认值运行。

麻烦是 每个数据库插入的时间消耗在很大的范围内变化。单个插入的最大时间可能是10s,最小插入时间可能是100us,而平均插入时间是900us。< / p>

尽管我可以增加缓冲区的长度,但是为了避免在等待太长的插入时间时缓冲区耗尽,我认为这不是正确的解决方案,因为无论缓冲区长度有多长,我们都可以遇到一个更慢的插入。 我猜在Postgresql的世界中这是不正常的(写一个小的普通表需要10s),但是我不知道要解决它,所以我在这里问它是希望有人指出我的错误或某些错误。优化方法。

env:Centos 7.5 64位,Postgresql 10,gcc 8.2.0和最新的pqxx lib。

这是我的pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust

# IPv4 incoming connections:
host    all             all             all                     password

# IPv6 local connections:
host    all             all             ::1/128                 ident

# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident

和postgresql.conf

listen_addresses = '*'          # what IP address(es) to listen on;
max_connections = 100           # (change requires restart)
shared_buffers = 128MB          # min 128kB
dynamic_shared_memory_type = posix  # the default is the first option
log_destination = 'stderr'      # Valid values are combinations of
logging_collector = on          # Enable capturing of stderr and csvlog
log_directory = 'log'           # directory where log files are written,
log_filename = 'postgresql-%a.log'  # log file name pattern,
log_truncate_on_rotation = on       # If on, an existing log file with the
log_rotation_age = 1d           # Automatic rotation of logfiles will
log_rotation_size = 0           # Automatic rotation of logfiles will
log_line_prefix = '%m [%p] '        # special values:
log_timezone = 'PRC'
datestyle = 'iso, ymd'
timezone = 'PRC'
lc_messages = 'zh_CN.UTF-8'         # locale for system error message
lc_monetary = 'zh_CN.UTF-8'         # locale for monetary formatting
lc_numeric = 'zh_CN.UTF-8'          # locale for number formatting
lc_time = 'zh_CN.UTF-8'             # locale for time formatting
default_text_search_config = 'pg_catalog.simple'

对不起,我是英语,我是中国人。感谢您耐心阅读我可怜的英语。 谢谢!

0 个答案:

没有答案