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, 08:10 AM
Tom Lane
 
Posts: n/a
Default elog(FATAL) vs shared memory

In this thread:
http://archives.postgresql.org/pgsql...3/msg00145.php
we eventually determined that the reported lockup had three components:

(1) something (still not sure what --- Martin and Mark, I'd really like
to know) was issuing random SIGTERMs to various postgres processes
including autovacuum.

(2) if a SIGTERM happens to arrive while btbulkdelete is running,
the next CHECK_FOR_INTERRUPTS will do elog(FATAL), causing elog.c
to do proc_exit(0), leaving the vacuum still recorded as active in
the shared memory array maintained by _bt_start_vacuum/_bt_end_vacuum.
The PG_TRY block in btbulkdelete doesn't get a chance to clean up.

(3) eventually, either we try to re-vacuum the same index or
accumulation of bogus active entries overflows the array.
Either way, _bt_start_vacuum throws an error, which btbulkdelete
PG_CATCHes, leading to_bt_end_vacuum trying to re-acquire the LWLock
already taken by _bt_start_vacuum, meaning that the process hangs up.
And then so does anything else that needs to take that LWLock...

Point (3) is already fixed in CVS, but point (2) is a lot nastier.
What it essentially says is that trying to clean up shared-memory
state in a PG_TRY block is unsafe: you can't be certain you'll
get to do it. Now this is not a big deal during normal SIGTERM or
SIGQUIT database shutdown, because we're going to abandon the shared
memory segment anyway. However, if we ever want to support individual
session kill via SIGTERM, it's a problem. Even if we were not
interested in someday considering that a supported feature, it seems
that dealing with random SIGTERMs is needed for robustness in at least
some environments.

AFAICS, there are basically two ways we might try to approach this:

Plan A: establish the rule that you mustn't try to clean up shared
memory state in a PG_CATCH block. Anything you need to do like that
has to be handled by an on_shmem_exit hook function, so it will be
called during a FATAL exit. (Or maybe you can do it in PG_CATCH for
normal ERROR cases, but you need a backing on_shmem_exit hook to
clean up for FATAL.)

Plan B: change the handling of FATAL errors so that they are thrown
like normal errors, and the proc_exit call happens only when we get
out to the outermost control level in postgres.c. This would mean
that PG_CATCH blocks get a chance to clean up before the FATAL exit
happens. The problem with that is that a non-cooperative PG_CATCH
block might think it could "recover" from the error, and then the exit
does not happen at all. We'd need a coding rule that PG_CATCH blocks
*must* re-throw FATAL errors, which seems at least as ugly as Plan A.
In particular, all three of the external-interpreter PLs are willing
to return errors into the external interpreter, and AFAICS we'd be
entirely at the mercy of the user-written Perl or Python or Tcl code
whether it re-throws the error or not.

So Plan B seems unacceptably fragile. Does anyone see a way to fix it,
or perhaps a Plan C with a totally different idea? Plan A seems pretty
ugly but it's the best I can come up with.

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
  #2 (permalink)  
Old 04-12-2008, 08:12 AM
Mark Shuttleworth
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

Tom Lane wrote:
> (1) something (still not sure what --- Martin and Mark, I'd really like
> to know) was issuing random SIGTERMs to various postgres processes
> including autovacuum.
>


This may be a misfeature in our test harness - I'll ask Stuart Bishop to
comment.

Mark

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 08:12 AM
Stuart Bishop
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

Mark Shuttleworth wrote:
> Tom Lane wrote:
>> (1) something (still not sure what --- Martin and Mark, I'd really like
>> to know) was issuing random SIGTERMs to various postgres processes
>> including autovacuum.
>>

>
> This may be a misfeature in our test harness - I'll ask Stuart Bishop to
> comment.


After a test is run, the test harness kills any outstanding connections so
we can drop the test database. Without this, a failing test could leave open
connections dangling causing the drop database to block.

CREATE OR REPLACE FUNCTION _killall_backends(text)
RETURNS Boolean AS $$
import os
from signal import SIGTERM

plan = plpy.prepare(
"SELECT procpid FROM pg_stat_activity WHERE datname=$1", ['text']
)
success = True
for row in plpy.execute(plan, args):
try:
plpy.info("Killing %d" % row['procpid'])
os.kill(row['procpid'], SIGTERM)
except OSError:
success = False

return success
$$ LANGUAGE plpythonu;

--
Stuart Bishop <stuart.bishop@canonical.com> http://www.canonical.com/
Canonical Ltd. http://www.ubuntu.com/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGFM4NAfqZj7rGN0oRAklGAKCBk4o8gyprHzWNPTLREO Zs48LZqQCgnSPw
IvZRsEONoAGW8ZzIiQo6nXo=
=qFbS
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 08:12 AM
Tom Lane
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

Stuart Bishop <stuart.bishop@canonical.com> writes:
> After a test is run, the test harness kills any outstanding connections so
> we can drop the test database. Without this, a failing test could leave open
> connections dangling causing the drop database to block.


Just to make it perfectly clear: we don't consider SIGTERMing individual
backends to be a supported operation (maybe someday, but not today).
That's why you had to resort to plpythonu to do this. I hope you don't
have anything analogous in your production databases ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 08:14 AM
Mark Shuttleworth
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

Tom Lane wrote:
> Stuart Bishop <stuart.bishop@canonical.com> writes:
>
>> After a test is run, the test harness kills any outstanding connections so
>> we can drop the test database. Without this, a failing test could leave open
>> connections dangling causing the drop database to block.
>>

>
> Just to make it perfectly clear: we don't consider SIGTERMing individual
> backends to be a supported operation (maybe someday, but not today).
> That's why you had to resort to plpythonu to do this. I hope you don't
> have anything analogous in your production databases ...
>

Ah, that could explain it. With the recent patches it seems to be
working OK, but I guess we should find a more standard way to rejig the
db during the test runs.

Mark

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-12-2008, 08:14 AM
Stuart Bishop
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

Tom Lane wrote:
> Stuart Bishop <stuart.bishop@canonical.com> writes:
>> After a test is run, the test harness kills any outstanding connections so
>> we can drop the test database. Without this, a failing test could leave open
>> connections dangling causing the drop database to block.

>
> Just to make it perfectly clear: we don't consider SIGTERMing individual
> backends to be a supported operation (maybe someday, but not today).
> That's why you had to resort to plpythonu to do this. I hope you don't
> have anything analogous in your production databases ...


No - just the test suite. It seems the only way to terminate any open
connections, which is a requirement for hooking PostgreSQL up to a test
suite or any other situation where you need to drop a database *now* rather
than when your clients decide to disconnect (well... unless we refactor to
start a dedicated postgres instance for each test, but our overheads are
already pretty huge).

--
Stuart Bishop <stuart.bishop@canonical.com> http://www.canonical.com/
Canonical Ltd. http://www.ubuntu.com/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGF6smAfqZj7rGN0oRAuJ/AJ0dqPvrVb3vgZHuOV3AwQOM9Nyw+ACdF2Uh
afuoBOQ5iEuuJyc+NyA3WD0=
=wTGl
-----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-12-2008, 08:16 AM
Jim Nasby
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

FWIW, you might want to put some safeguards in there so that you
don't try to inadvertently kill the backend that's running that
function... unfortunately I don't think there's a built-in function
to tell you the PID of the backend you're connected to; if you're
connecting via TCP you could use inet_client_addr() and
inet_client_port(), but that won't work if you're using the socket to
connect.

On Apr 5, 2007, at 6:23 AM, Stuart Bishop wrote:

> Mark Shuttleworth wrote:
>> Tom Lane wrote:
>>> (1) something (still not sure what --- Martin and Mark, I'd
>>> really like
>>> to know) was issuing random SIGTERMs to various postgres processes
>>> including autovacuum.
>>>

>>
>> This may be a misfeature in our test harness - I'll ask Stuart
>> Bishop to
>> comment.

>
> After a test is run, the test harness kills any outstanding
> connections so
> we can drop the test database. Without this, a failing test could
> leave open
> connections dangling causing the drop database to block.
>
> CREATE OR REPLACE FUNCTION _killall_backends(text)
> RETURNS Boolean AS $$
> import os
> from signal import SIGTERM
>
> plan = plpy.prepare(
> "SELECT procpid FROM pg_stat_activity WHERE datname=$1",
> ['text']
> )
> success = True
> for row in plpy.execute(plan, args):
> try:
> plpy.info("Killing %d" % row['procpid'])
> os.kill(row['procpid'], SIGTERM)
> except OSError:
> success = False
>
> return success
> $$ LANGUAGE plpythonu;
>
> --
> Stuart Bishop <stuart.bishop@canonical.com> http://
> www.canonical.com/
> Canonical Ltd. http://www.ubuntu.com/
>


--
Jim Nasby jim@nasby.net
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)



---------------------------(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, 08:16 AM
Heikki Linnakangas
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

Tom Lane wrote:
> 2) if a SIGTERM happens to arrive while btbulkdelete is running,
> the next CHECK_FOR_INTERRUPTS will do elog(FATAL), causing elog.c
> to do proc_exit(0), leaving the vacuum still recorded as active in
> the shared memory array maintained by _bt_start_vacuum/_bt_end_vacuum.
> The PG_TRY block in btbulkdelete doesn't get a chance to clean up.


I skimmed through all users of PG_TRY/CATCH in the backend to check if
there's other problems like that looming. There's one that looks
dangerous in pg_start_backup() in xlog.c. forcePageWrites flag in shared
memory is cleared in a PG_CATCH block. It's not as severe, though, as it
can be cleared manually by calling pg_stop_backup(), and only leads to
degraded performance.

> (3) eventually, either we try to re-vacuum the same index or
> accumulation of bogus active entries overflows the array.
> Either way, _bt_start_vacuum throws an error, which btbulkdelete
> PG_CATCHes, leading to_bt_end_vacuum trying to re-acquire the LWLock
> already taken by _bt_start_vacuum, meaning that the process hangs up.
> And then so does anything else that needs to take that LWLock...


I also looked for other occurances of point (3), but couldn't find any,
so I guess we're now safe from it.

> Point (3) is already fixed in CVS, but point (2) is a lot nastier.
> What it essentially says is that trying to clean up shared-memory
> state in a PG_TRY block is unsafe: you can't be certain you'll
> get to do it. Now this is not a big deal during normal SIGTERM or
> SIGQUIT database shutdown, because we're going to abandon the shared
> memory segment anyway. However, if we ever want to support individual
> session kill via SIGTERM, it's a problem. Even if we were not
> interested in someday considering that a supported feature, it seems
> that dealing with random SIGTERMs is needed for robustness in at least
> some environments.


Agreed. We should do our best to be safe from SIGTERMs, even if we don't
consider it supported.

> AFAICS, there are basically two ways we might try to approach this:
>
> Plan A: establish the rule that you mustn't try to clean up shared
> memory state in a PG_CATCH block. Anything you need to do like that
> has to be handled by an on_shmem_exit hook function, so it will be
> called during a FATAL exit. (Or maybe you can do it in PG_CATCH for
> normal ERROR cases, but you need a backing on_shmem_exit hook to
> clean up for FATAL.)
>
> Plan B: change the handling of FATAL errors so that they are thrown
> like normal errors, and the proc_exit call happens only when we get
> out to the outermost control level in postgres.c. This would mean
> that PG_CATCH blocks get a chance to clean up before the FATAL exit
> happens. The problem with that is that a non-cooperative PG_CATCH
> block might think it could "recover" from the error, and then the exit
> does not happen at all. We'd need a coding rule that PG_CATCH blocks
> *must* re-throw FATAL errors, which seems at least as ugly as Plan A.
> In particular, all three of the external-interpreter PLs are willing
> to return errors into the external interpreter, and AFAICS we'd be
> entirely at the mercy of the user-written Perl or Python or Tcl code
> whether it re-throws the error or not.
>
> So Plan B seems unacceptably fragile. Does anyone see a way to fix it,
> or perhaps a Plan C with a totally different idea? Plan A seems pretty
> ugly but it's the best I can come up with.


Yeah, plan A seems like the way to go.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

---------------------------(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
  #9 (permalink)  
Old 04-12-2008, 08:16 AM
Jim Nasby
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory

On Apr 11, 2007, at 6:23 PM, Jim Nasby wrote:
> FWIW, you might want to put some safeguards in there so that you
> don't try to inadvertently kill the backend that's running that
> function... unfortunately I don't think there's a built-in function
> to tell you the PID of the backend you're connected to; if you're
> connecting via TCP you could use inet_client_addr() and
> inet_client_port(), but that won't work if you're using the socket
> to connect.


*wipes egg off face*

There is a pg_backend_pid() function, even if it's not documented
with the other functions (it's in the stats function stuff for some
reason).
--
Jim Nasby jim@nasby.net
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)



---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-12-2008, 08:16 AM
Gregory Stark
 
Posts: n/a
Default Re: elog(FATAL) vs shared memory


"Heikki Linnakangas" <heikki@enterprisedb.com> writes:

> Tom Lane wrote:
>
>> AFAICS, there are basically two ways we might try to approach this:
>>
>> Plan A: establish the rule that you mustn't try to clean up shared
>> memory state in a PG_CATCH block. Anything you need to do like that
>> has to be handled by an on_shmem_exit hook function, so it will be
>> called during a FATAL exit. (Or maybe you can do it in PG_CATCH for
>> normal ERROR cases, but you need a backing on_shmem_exit hook to
>> clean up for FATAL.)
>>...
>> So Plan B seems unacceptably fragile. Does anyone see a way to fix it,
>> or perhaps a Plan C with a totally different idea? Plan A seems pretty
>> ugly but it's the best I can come up with.

>
> Yeah, plan A seems like the way to go.


The alternative is that instead of a general purpose shmem hook you note the
pid of the process that is expecting to handle the cleanup. So for instance
something like pg_start_backup instead of setting a flag would store its pid.
Then someone else who comes along and finds the field set has to double check
if the pid is actually still around and if not it has to clean it up itself.

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


---------------------------(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
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:43 PM.


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 53