Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-12-2008, 05:06 AM
Zdenek Kotala
 
Posts: n/a
Default PG qsort vs. Solaris

Postgres has own implementation of qsort. It is used only for Solaris,
because in some cases Solaris implementation was terrible slow.

Now, New qsort is present in the Solaris from version 9 update 6 and I
performed some quick test and the speed is very similarly with pg
implementation see bellow. The Solaris qsort only does not have test for
preordered array.

Is it time to "remove" PG qsort and use libc version for solaris 9, 10...?

There some useful links:
solaris qsort implementation
http://cvs.opensolaris.org/source/xr...n/util/qsort.c
discuss about qsort
http://momjian.postgresql.org/cgi-bin/pgtodo?qsort



Regards Zdenek


PS: Test program is located on
http://bugs.opensolaris.org/bugdatab...bug_id=4489885

There is test result:

mode 1 2 3 4 5 6 7 8
pg 3.440 54.259 42.251 40.967 38.214 29.730 21.668 39.142
pg2 39.492 53.598 44.697 40.546 38.027 29.572 21.598 38.756
solaris 41.207 41.957 41.873 41.616 35.895 29.502 26.906 39.492


Pg2 test is without sort array prechecking.



---------------------------(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
  #2 (permalink)  
Old 04-12-2008, 05:06 AM
Tom Lane
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Zdenek Kotala <Zdenek.Kotala@Sun.COM> writes:
> Is it time to "remove" PG qsort and use libc version for solaris 9, 10...?


I have no particular desire to introduce a version number check until we
have to. If you can show that the newer versions have a qsort that
substantially *out-performs* ours, it would be worth doing that, but
merely being competitive isn't enough to make it worth the trouble.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 05:06 AM
Neil Conway
 
Posts: n/a
Default Re: PG qsort vs. Solaris

On Tue, 2006-10-03 at 10:48 -0400, Tom Lane wrote:
> I have no particular desire to introduce a version number check until we
> have to. If you can show that the newer versions have a qsort that
> substantially *out-performs* ours


Are there any platform-local variants of qsort() that substantially
outperform our implementation? (I don't remember hearing of one, but I
might have missed it.) Given the time that has been spent working around
the braindamaged behavior of qsort() on various platforms, I would be
more inclined to *always* use our qsort() instead of the platform's
version. That way we'd get the same behavior across all platforms, and
we can at least verify that our implementation behaves reasonably for
the special cases we're interested in (presorted input, many-equal-keys,
etc.), and doesn't do crazy stuff like randomly switch to merge sort for
certain inputs.

-Neil



---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 05:06 AM
Tom Lane
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Neil Conway <neilc@samurai.com> writes:
> Given the time that has been spent working around
> the braindamaged behavior of qsort() on various platforms, I would be
> more inclined to *always* use our qsort() instead of the platform's
> version.


I've been heard to argue against that in the past, but I'm beginning to
see the merit of the idea. One good reason for doing it is that we
could stop worrying about the possibility of large-scale memory leaks
due to erroring out of glibc's qsort --- in particular it would be OK
to add CHECK_FOR_INTERRUPTS into the comparison callback as was
requested recently.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 05:06 AM
Tom Lane
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Neil Conway <neilc@samurai.com> writes:
> Given the time that has been spent working around
> the braindamaged behavior of qsort() on various platforms, I would be
> more inclined to *always* use our qsort() instead of the platform's
> version.


I spent a bit of time looking into why we hadn't chosen to do this already.
The remaining uncertainty was expressed by Greg Stark: glibc's mergesort
has a small advantage over quicksort in terms of the average number of
calls of the comparison function, and considering that we tend to use
pretty heavyweight comparison functions, that seems like it ought to
favor the mergesort. Nobody bothered to check this out back in March
when the last discussion died off.

I made a small hack in tuplesort.c to actually count the
comparison-function calls, and then ran this test case with both our
qsort and glibc's (from Fedora Core 5 current glibc):

set trace_sort TO 1;
set client_min_messages TO log;
set work_mem TO '200MB';
select count(*) from
(select random()::text from generate_series(1,1000000) order by 1) ss;

In C locale the text comparison is relatively quick, and I see results
like

glibc:
LOG: begin tuple sort: nkeys = 1, workMem = 204800, randomAccess = f
LOG: performsort starting: CPU 0.15s/2.39u sec elapsed 2.54 sec
LOG: performsort done: CPU 0.18s/7.09u sec elapsed 7.27 sec
LOG: internal sort ended, 102701 KB used, 18674655 comparisons: CPU 0.18s/7.38u sec elapsed 7.56 sec
ours:
LOG: begin tuple sort: nkeys = 1, workMem = 204800, randomAccess = f
LOG: performsort starting: CPU 0.18s/2.34u sec elapsed 2.51 sec
LOG: performsort done: CPU 0.18s/5.18u sec elapsed 5.36 sec
LOG: internal sort ended, 102701 KB used, 21277970 comparisons: CPU 0.18s/5.46u sec elapsed 5.64 sec

In en_US.utf8 locale, strcoll is pretty slow, but:

glibc:
LOG: begin tuple sort: nkeys = 1, workMem = 204800, randomAccess = f
LOG: performsort starting: CPU 0.17s/2.35u sec elapsed 2.52 sec
LOG: performsort done: CPU 0.19s/15.94u sec elapsed 16.13 sec
LOG: internal sort ended, 102701 KB used, 18674910 comparisons: CPU 0.19s/16.23u sec elapsed 16.43 sec
ours:
LOG: begin tuple sort: nkeys = 1, workMem = 204800, randomAccess = f
LOG: performsort starting: CPU 0.18s/2.30u sec elapsed 2.49 sec
LOG: performsort done: CPU 0.18s/15.30u sec elapsed 15.48 sec
LOG: internal sort ended, 102701 KB used, 20972345 comparisons: CPU 0.18s/15.58u sec elapsed 15.76 sec

If you're sorting integer or float keys it's a lot worse:

postgres=# select count(*) from (select random() from generate_series(1,1000000) order by 1) ss;

glibc:
LOG: begin tuple sort: nkeys = 1, workMem = 204800, randomAccess = f
LOG: performsort starting: CPU 0.16s/0.70u sec elapsed 0.86 sec
LOG: performsort done: CPU 0.18s/5.10u sec elapsed 5.28 sec
LOG: internal sort ended, 71452 KB used, 18674509 comparisons: CPU 0.18s/5.38u sec elapsed 5.56 sec
ours:
LOG: begin tuple sort: nkeys = 1, workMem = 204800, randomAccess = f
LOG: performsort starting: CPU 0.11s/0.74u sec elapsed 0.86 sec
LOG: performsort done: CPU 0.11s/3.22u sec elapsed 3.33 sec
LOG: internal sort ended, 71452 KB used, 21123160 comparisons: CPU 0.11s/3.50u sec elapsed 3.62 sec

So basically, glibc's qsort is bad enough that even a
10%-more-comparisons advantage doesn't save it.

I propose that we do the following:

1. Switch to using port/qsort.c all the time.
2. Add a "qsort_arg" function that is identical to qsort except it also
passes a void pointer through to the comparison function. This will
allow us to get rid of the non-reentrant static variable and extra
level of function call in tuplesort.c.
3. Insert a CHECK_FOR_INTERRUPTS() call as was requested back in July.
With glibc out of the way, there's no longer a reason to fear memory
leakage from cancelling a sort.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-12-2008, 05:06 AM
Zdenek Kotala
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Tom Lane wrote:
> Zdenek Kotala <Zdenek.Kotala@Sun.COM> writes:
>> Is it time to "remove" PG qsort and use libc version for solaris 9, 10...?

>
> I have no particular desire to introduce a version number check until we
> have to. If you can show that the newer versions have a qsort that
> substantially *out-performs* ours, it would be worth doing that, but
> merely being competitive isn't enough to make it worth the trouble.
>


The implementation in the solaris uses same ideas like postgres
implementation exclude sort array detection. There are small difference
with threshold when median uses 9 items and threshold for insertion
sort. Performance is similarly - no winer (only on sorted array).

Zdenek

---------------------------(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
  #7 (permalink)  
Old 04-12-2008, 05:07 AM
Zdenek Kotala
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Tom Lane wrote:
> Neil Conway <neilc@samurai.com> writes:
>> Given the time that has been spent working around
>> the braindamaged behavior of qsort() on various platforms, I would be
>> more inclined to *always* use our qsort() instead of the platform's
>> version.

>


<snip>

> I propose that we do the following:
>
> 1. Switch to using port/qsort.c all the time.


1.5 Move it to another directory - e.g. backend/utils/sort?

> 2. Add a "qsort_arg" function that is identical to qsort except it also
> passes a void pointer through to the comparison function. This will
> allow us to get rid of the non-reentrant static variable and extra
> level of function call in tuplesort.c.
> 3. Insert a CHECK_FOR_INTERRUPTS() call as was requested back in July.
> With glibc out of the way, there's no longer a reason to fear memory
> leakage from cancelling a sort.


4. replace KR function definition by the ANSI style :-)




regards Zdenek

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-12-2008, 05:07 AM
Neil Conway
 
Posts: n/a
Default Re: PG qsort vs. Solaris

On Tue, 2006-10-03 at 15:44 -0400, Tom Lane wrote:
> I propose that we do the following:
>
> 1. Switch to using port/qsort.c all the time.
> 2. Add a "qsort_arg" function that is identical to qsort except it also
> passes a void pointer through to the comparison function. This will
> allow us to get rid of the non-reentrant static variable and extra
> level of function call in tuplesort.c.
> 3. Insert a CHECK_FOR_INTERRUPTS() call as was requested back in July.
> With glibc out of the way, there's no longer a reason to fear memory
> leakage from cancelling a sort.


+1 from me.

I can implement this (for 8.3, naturally), unless you'd prefer to do it
yourself.

-Neil



---------------------------(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
  #9 (permalink)  
Old 04-12-2008, 05:07 AM
Tom Lane
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Neil Conway <neilc@samurai.com> writes:
> On Tue, 2006-10-03 at 15:44 -0400, Tom Lane wrote:
>> 1. Switch to using port/qsort.c all the time.
>> 2. Add a "qsort_arg" function that is identical to qsort except it also
>> passes a void pointer through to the comparison function. This will
>> allow us to get rid of the non-reentrant static variable and extra
>> level of function call in tuplesort.c.
>> 3. Insert a CHECK_FOR_INTERRUPTS() call as was requested back in July.
>> With glibc out of the way, there's no longer a reason to fear memory
>> leakage from cancelling a sort.


> +1 from me.


> I can implement this (for 8.3, naturally), unless you'd prefer to do it
> yourself.


I was planning to do it right now, on the grounds that #2 and #3 are bug
fixes, and that fixing the existing memory leakage hazard is a good
thing too.

regards, tom lane

---------------------------(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
  #10 (permalink)  
Old 04-12-2008, 05:07 AM
Gregory Stark
 
Posts: n/a
Default Re: PG qsort vs. Solaris

Tom Lane <tgl@sss.pgh.pa.us> writes:

> So basically, glibc's qsort is bad enough that even a
> 10%-more-comparisons advantage doesn't save it.


Actually what I was more concerned about was things like on data structures
with complex comparison routines. Things like sorting on arrays or ROWs.

For that matter it seems to me that sorting on a single column is a pretty
unrealistic scenario too. Most of the time I find queries have long lists of
columns in the ORDER BY clause.

Do those numbers look very different if you have lots of columns or if you're
sorting on something like an array or a ROW?

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

---------------------------(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
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 11:03 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618