Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql Performance

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:54 AM
Steve Eckmann
 
Posts: n/a
Default improving write performance for logging application

I have questions about how to improve the write performance of PostgreSQL for logging data from a real-time simulation. We found that MySQL 4.1.3 could log about 1480 objects/second using MyISAM tables or about 1225 objects/second using InnoDB tables, but PostgreSQL 8.0.3 could log only about 540 objects/second. (test system: quad-Itanium2, 8GB memory, SCSI RAID, GigE connection from simulation server, nothing running except system processes and database system under test)

We also found that we could improve MySQL performance significantly using MySQL's "INSERT" command extension allowing multiple value-list tuples in a single command; the rate for MyISAM tables improved to about 2600 objects/second. PostgreSQL doesn't support that language extension. Using the COPY command instead of INSERT might help, but since rows are being generated on the fly, I don't see how to use COPY without running a separate process that reads rows from the application and uses COPY to write to the database. The application currently has two processes: the simulation and a data collector that reads events from the sim (queued in shared memory) and writes them as rows to the database, buffering as needed to avoid lost data during periods of high activity. To use COPY I think we would have to split our data collector into two processes communicating via a pipe.

Query performance is not an issue: we found that when suitable indexes are added PostgreSQL is fast enough on the kinds of queries our users make. The crux is writing rows to the database fast enough to keep up with the simulation.

Are there general guidelines for tuning the PostgreSQL server for this kind of application? The suggestions I've found include disabling fsync (done), increasing the value of wal_buffers, and moving the WAL to a different disk, but these aren't likely to produce the 3x improvement that we need. On the client side I've found only two suggestions: disable autocommit and use COPY instead of INSERT. I think I've effectively disabled autocommit by batching up to several hundred INSERT commands in each PQexec() call, and it isn’t clear that COPY is worth the effort in our application.

Thanks.


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:54 AM
Tom Lane
 
Posts: n/a
Default Re: improving write performance for logging application

Steve Eckmann <eckmann@computer.org> writes:
> We also found that we could improve MySQL performance significantly
> using MySQL's "INSERT" command extension allowing multiple value-list
> tuples in a single command; the rate for MyISAM tables improved to
> about 2600 objects/second. PostgreSQL doesn't support that language
> extension. Using the COPY command instead of INSERT might help, but
> since rows are being generated on the fly, I don't see how to use COPY
> without running a separate process that reads rows from the
> application and uses COPY to write to the database.


Can you conveniently alter your application to batch INSERT commands
into transactions? Ie

BEGIN;
INSERT ...;
... maybe 100 or so inserts ...
COMMIT;
BEGIN;
... lather, rinse, repeat ...

This cuts down the transactional overhead quite a bit. A downside is
that you lose multiple rows if any INSERT fails, but then the same would
be true of multiple VALUES lists per INSERT.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 06:54 AM
Steinar H. Gunderson
 
Posts: n/a
Default Re: improving write performance for logging application

On Tue, Jan 03, 2006 at 04:44:28PM -0700, Steve Eckmann wrote:
> Are there general guidelines for tuning the PostgreSQL server for this kind
> of application? The suggestions I've found include disabling fsync (done),


Are you sure you really want this? The results could be catastrophic in case
of a crash.

> On the client side I've found only two suggestions: disable autocommit and
> use COPY instead of INSERT. I think I've effectively disabled autocommit by
> batching up to several hundred INSERT commands in each PQexec() call, and
> it isn’t clear that COPY is worth the effort in our application.


I'm a bit confused here: How can you batch multiple INSERTs into large
statements for MySQL, but not batch multiple INSERTs into COPY statements for
PostgreSQL?

Anyhow, putting it all inside one transaction (or a few) is likely to help
quite a lot, but of course less when you have fsync=false. Bunding multiple
statements in each PQexec() call won't really give you that; you'll have to
tell the database so explicitly.

/* Steinar */
--
Homepage: http://www.sesse.net/

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 06:54 AM
dlang
 
Posts: n/a
Default Re: improving write performance for logging application



On Tue, 3 Jan 2006, Tom Lane wrote:

> Steve Eckmann <eckmann@computer.org> writes:
> > We also found that we could improve MySQL performance significantly
> > using MySQL's "INSERT" command extension allowing multiple value-list
> > tuples in a single command; the rate for MyISAM tables improved to
> > about 2600 objects/second. PostgreSQL doesn't support that language
> > extension. Using the COPY command instead of INSERT might help, but
> > since rows are being generated on the fly, I don't see how to use COPY
> > without running a separate process that reads rows from the
> > application and uses COPY to write to the database.

>
> Can you conveniently alter your application to batch INSERT commands
> into transactions? Ie
>
> BEGIN;
> INSERT ...;
> ... maybe 100 or so inserts ...
> COMMIT;
> BEGIN;
> ... lather, rinse, repeat ...
>
> This cuts down the transactional overhead quite a bit. A downside is
> that you lose multiple rows if any INSERT fails, but then the same would
> be true of multiple VALUES lists per INSERT.


Steve, you mentioned that you data collector buffers the data before
sending it to the database, modify it so that each time it goes to send
things to the database you send all the data that's in the buffer as a
single transaction.

I am working on useing postgres to deal with log data and wrote a simple
perl script that read in the log files a line at a time, and then wrote
them 1000 at a time to the database. On a dual Opteron 240 box with 2G of
ram 1x 15krpm SCSI drive (and a untuned postgress install with the compile
time defaults) I was getting 5000-8000 lines/sec (I think this was with
fsync disabled, but I don't remember for sure). and postgres was
complaining that it was overrunning it's log sizes (which limits the speed
as it then has to pause to flush the logs)

the key thing is to send multiple lines with one transaction as tom shows
above.

David Lang


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-19-2008, 06:54 AM
Alexander Schreiber
 
Posts: n/a
Default Re: improving write performance for logging application

Steve Eckmann <eckmann@computer.org> wrote:
> I have questions about how to improve the write performance of PostgreSQL
> for logging data from a real-time simulation. We found that MySQL 4.1.3
> could log about 1480 objects/second using MyISAM tables or about 1225
> objects/second using InnoDB tables, but PostgreSQL 8.0.3 could log only
> about 540 objects/second. (test system: quad-Itanium2, 8GB memory, SCSI
> RAID, GigE connection from simulation server, nothing running except
> system processes and database system under test)
>
> to write to the database. The application currently has two processes:
> the simulation and a data collector that reads events from the sim
> (queued in shared memory) and writes them as rows to the database,
> buffering as needed to avoid lost data during periods of high activity.
> To use COPY I think we would have to split our data collector into two
> processes communicating via a pipe.
>
> Are there general guidelines for tuning the PostgreSQL server for this
> kind of application?


First suggestion: use transactions to batch your inserts together and
use large batches - write a few hundred rows per transaction. That
should speed things up seriously. I also suggest you test the effects of
using "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE" - in my application
it resulted in a further speedup.

I'm storing syslog and eventlog in a PostgreSQL database and using flat
tables I get in excess of 800 rows/s for eventlog and more than
1000 rows/s for syslog. This is with fsync enabled and several indexes
active in an application where getting data into the DB _fast_ is
everything and query performance is secondary.

This is also on a decidedly sub-optimal environment: data loading over a
100 MBit/s network, PostgreSQL host on a 4 CPU 1.9 GHz Xeon, database
filesystem on a 4 disk RAID5 with 20 MB/s throughput (linear writes)
through the FS.

> The suggestions I've found include disabling fsync
> (done),


This is _not_ a good idea. Disable fsync only if losing your entire
database or getting your database corrupted upon system crash/power
fail is acceptable.

> increasing the value of wal_buffers, and moving the WAL to a
> different disk, but these aren't likely to produce the 3x improvement
> that we need.


While you are stretching for higher hanging fruit you should also have
a look at the filesystem storing your database: ext3 ist _not_ a good
choice for this. In my benchmarks I found XFS to deliver the best
performance as a backing store FS for PostgreSQL.

And another one: switching from PostgreSQL 8.0.3 to PostgreSQL 8.1
provided a further performance boost.

> On the client side I've found only two suggestions:
> disable autocommit and use COPY instead of INSERT. I think I've
> effectively disabled autocommit by batching up to several hundred
> INSERT commands in each PQexec() call, and it isn’t clear that COPY
> is worth the effort in our application.


Regards,
Alex.
--
"Opportunity is missed by most people because it is dressed in overalls and
looks like work." -- Thomas A. Edison
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-19-2008, 06:54 AM
Ian Westmacott
 
Posts: n/a
Default Re: improving write performance for logging application

We have a similar application thats doing upwards of 2B inserts
per day. We have spent a lot of time optimizing this, and found the
following to be most beneficial:

1) use COPY (BINARY if possible)
2) don't use triggers or foreign keys
3) put WAL and tables on different spindles (channels if possible)
4) put as much as you can in each COPY, and put as many COPYs as
you can in a single transaction.
5) watch out for XID wraparound
6) tune checkpoint* and bgwriter* parameters for your I/O system

On Tue, 2006-01-03 at 16:44 -0700, Steve Eckmann wrote:
> I have questions about how to improve the write performance of PostgreSQL for logging data from a real-time simulation. We found that MySQL 4.1.3 could log about 1480 objects/second using MyISAM tables or about 1225 objects/second using InnoDB tables, but PostgreSQL 8.0.3 could log only about 540 objects/second. (test system: quad-Itanium2, 8GB memory, SCSI RAID, GigE connection from simulation server, nothing running except system processes and database system under test)
>
> We also found that we could improve MySQL performance significantly using MySQL's "INSERT" command extension allowing multiple value-list tuples in a single command; the rate for MyISAM tables improved to about 2600 objects/second. PostgreSQL doesn't support that language extension. Using the COPY command instead of INSERT might help, but since rows are being generated on the fly, I don't see how to use COPY without running a separate process that reads rows from the application and uses COPY to write to the database. The application currently has two processes: the simulation and a data collector that reads events from the sim (queued in shared memory) and writes them as rows to the database, buffering as needed to avoid lost data during periods of high activity. To use COPY I think we would have to split our data collector into two processes communicating via a pipe.
>
> Query performance is not an issue: we found that when suitable indexes are added PostgreSQL is fast enough on the kinds of queries our users make. The crux is writing rows to the database fast enough to keep up with the simulation.
>
> Are there general guidelines for tuning the PostgreSQL server for this kind of application? The suggestions I've found include disabling fsync (done), increasing the value of wal_buffers, and moving the WAL to a different disk, but these aren't likely to produce the 3x improvement that we need. On the client side I've found only two suggestions: disable autocommit and use COPY instead of INSERT. I think I've effectively disabled autocommit by batching up to several hundred INSERT commands in each PQexec() call, and it isn’t clear that COPY is worth the effort in our application.
>
> Thanks.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster

--
Ian Westmacott <ianw@intellivid.com>
Intellivid Corp.


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-19-2008, 06:54 AM
Steve Eckmann
 
Posts: n/a
Default Re: improving write performance for logging application

Tom Lane wrote:

>Steve Eckmann <eckmann@computer.org> writes:
>
>
>>We also found that we could improve MySQL performance significantly
>>using MySQL's "INSERT" command extension allowing multiple value-list
>>tuples in a single command; the rate for MyISAM tables improved to
>>about 2600 objects/second. PostgreSQL doesn't support that language
>>extension. Using the COPY command instead of INSERT might help, but
>>since rows are being generated on the fly, I don't see how to use COPY
>>without running a separate process that reads rows from the
>>application and uses COPY to write to the database.
>>
>>

>
>Can you conveniently alter your application to batch INSERT commands
>into transactions? Ie
>
> BEGIN;
> INSERT ...;
> ... maybe 100 or so inserts ...
> COMMIT;
> BEGIN;
> ... lather, rinse, repeat ...
>
>This cuts down the transactional overhead quite a bit. A downside is
>that you lose multiple rows if any INSERT fails, but then the same would
>be true of multiple VALUES lists per INSERT.
>
> regards, tom lane
>
>

Thanks for the suggestion, Tom. Yes, I think I could do that. But I
thought what I was doing now was effectively the same, because the
PostgreSQL 8.0.0 Documentation says (section 27.3.1): "It is allowed to
include multiple SQL commands (separated by semicolons) in the command
string. Multiple queries sent in a single PQexec call are processed in a
single transaction...." Our simulation application has nearly 400 event
types, each of which is a C++ class for which we have a corresponding
database table. So every thousand events or so I issue one PQexec() call
for each event type that has unlogged instances, sending INSERT commands
for all instances. For example,

PQexec(dbConn, "INSERT INTO FlyingObjectState VALUES (...); INSERT
INTO FlyingObjectState VALUES (...); ...");

My thought was that this would be a good compromise between minimizing
transactions (one per event class per buffering interval instead of one
per event) and minimizing disk seeking (since all queries in a single
transaction insert rows into the same table). Am I overlooking something
here? One thing I haven't tried is increasing the buffering interval
from 1000 events to, say, 10,000. It turns out that 1000 is a good
number for Versant, the object database system we're replacing, and for
MySQL, so I assumed it would be a good number for PostgreSQL, too.

Regards, Steve


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-19-2008, 06:54 AM
Steve Eckmann
 
Posts: n/a
Default Re: improving write performance for logging application

Steinar H. Gunderson wrote:

>On Tue, Jan 03, 2006 at 04:44:28PM -0700, Steve Eckmann wrote:
>
>
>>Are there general guidelines for tuning the PostgreSQL server for this kind
>>of application? The suggestions I've found include disabling fsync (done),
>>
>>

>
>Are you sure you really want this? The results could be catastrophic in case
>of a crash.
>
>
>
>>On the client side I've found only two suggestions: disable autocommit and
>>use COPY instead of INSERT. I think I've effectively disabled autocommit by
>>batching up to several hundred INSERT commands in each PQexec() call, and
>>it isn’t clear that COPY is worth the effort in our application.
>>
>>

>
>I'm a bit confused here: How can you batch multiple INSERTs into large
>statements for MySQL, but not batch multiple INSERTs into COPY statements for
>PostgreSQL?
>
>Anyhow, putting it all inside one transaction (or a few) is likely to help
>quite a lot, but of course less when you have fsync=false. Bunding multiple
>statements in each PQexec() call won't really give you that; you'll have to
>tell the database so explicitly.
>
>/* Steinar */
>
>

Thanks, Steinar. I don't think we would really run with fsync off, but I
need to document the performance tradeoffs. You're right that my
explanation was confusing; probably because I'm confused about how to
use COPY! I could batch multiple INSERTS using COPY statements, I just
don't see how to do it without adding another process to read from
STDIN, since the application that is currently the database client is
constructing rows on the fly. I would need to get those rows into some
process's STDIN stream or into a server-side file before COPY could be
used, right?

You're comment about bundling multiple statements in each PQexec() call
seems to disagree with a statement in 27.3.1 that I interpret as saying
each PQexec() call corresponds to a single transaction. Are you sure my
interpretation is wrong?

Regards, Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-19-2008, 06:54 AM
Steve Eckmann
 
Posts: n/a
Default Re: improving write performance for logging application

dlang wrote:

>On Tue, 3 Jan 2006, Tom Lane wrote:
>
>
>
>>Steve Eckmann <eckmann@computer.org> writes:
>>
>>
>>>We also found that we could improve MySQL performance significantly
>>>using MySQL's "INSERT" command extension allowing multiple value-list
>>>tuples in a single command; the rate for MyISAM tables improved to
>>>about 2600 objects/second. PostgreSQL doesn't support that language
>>>extension. Using the COPY command instead of INSERT might help, but
>>>since rows are being generated on the fly, I don't see how to use COPY
>>>without running a separate process that reads rows from the
>>>application and uses COPY to write to the database.
>>>
>>>

>>Can you conveniently alter your application to batch INSERT commands
>>into transactions? Ie
>>
>> BEGIN;
>> INSERT ...;
>> ... maybe 100 or so inserts ...
>> COMMIT;
>> BEGIN;
>> ... lather, rinse, repeat ...
>>
>>This cuts down the transactional overhead quite a bit. A downside is
>>that you lose multiple rows if any INSERT fails, but then the same would
>>be true of multiple VALUES lists per INSERT.
>>
>>

>
>Steve, you mentioned that you data collector buffers the data before
>sending it to the database, modify it so that each time it goes to send
>things to the database you send all the data that's in the buffer as a
>single transaction.
>
>I am working on useing postgres to deal with log data and wrote a simple
>perl script that read in the log files a line at a time, and then wrote
>them 1000 at a time to the database. On a dual Opteron 240 box with 2G of
>ram 1x 15krpm SCSI drive (and a untuned postgress install with the compile
>time defaults) I was getting 5000-8000 lines/sec (I think this was with
>fsync disabled, but I don't remember for sure). and postgres was
>complaining that it was overrunning it's log sizes (which limits the speed
>as it then has to pause to flush the logs)
>
>the key thing is to send multiple lines with one transaction as tom shows
>above.
>
>David Lang
>

Thanks, David. I will look more carefully at how to batch multiple rows
per PQexec() call. Regards, Steve.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-19-2008, 06:54 AM
Steve Eckmann
 
Posts: n/a
Default Re: improving write performance for logging application

Ian Westmacott wrote:

>We have a similar application thats doing upwards of 2B inserts
>per day. We have spent a lot of time optimizing this, and found the
>following to be most beneficial:
>
>1) use COPY (BINARY if possible)
>2) don't use triggers or foreign keys
>3) put WAL and tables on different spindles (channels if possible)
>4) put as much as you can in each COPY, and put as many COPYs as
> you can in a single transaction.
>5) watch out for XID wraparound
>6) tune checkpoint* and bgwriter* parameters for your I/O system
>

Thanks, Ian. I will look at how to implement your suggestions.

Regards, Steve

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:44 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433