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, 07:29 AM
Florian G. Pflug
 
Posts: n/a
Default Updated propsoal for read-only queries on PITR slaves (SoC 2007)

Hi

I've updated (or rather rewritten) my proposal for implementing
read-onyl queries on PITR slaves as a "Summer of Code 2007" project.

I've added are more details description of how I plan implement
a read-only mode suitable for PITR slaves, and put in a few
possible enhancements to the "Big, Global R/W lock" idea for
serializing WAL replay and queries.

I'm looking forward to any kind of suggestions, ideas, or
critism - I'd like my proposal to be as detailed as
possible before I submit it to SoC, so that if
I get a chance to work on it, I can be reasonable sure
that people here are happy with the way I approach the problem.

greetings, Florian Pflug

Implementing support for read-only queries on PITR slaves
================================================== =======

Submitter: Florian Pflug <fgp@phlo.org>

Abstract:
---------
The support for PITR (Point-In-Time-Recovery) in postgres can be used to built
a simple form a master-slave replication. Currently, no queries can be
executed on the slave, though - it only replays WAL (Write-Ahead-Log) segments
it receives from the master. I want to implement support for running read-onyl
queries on such a PITR slave, making PITR usefull not only for disaster
recovery, but also for loadbalancing.

Course overview of the proposed implementation:
-----------------------------------------------
Currently, postgres does WAL replay soley during the startup of the database,
before all subsystems are fully initialized, and before backend are allowed to
connect. To support read-only queries on PITR slaves, while still guaranteeing
that the database is in a consistens state, the WAL replay will be split into
two parts. The first will replay only enough wal to guarantee a consistens
state, and will run during startup. If read-only mode is disabled, the next
step will be run immediatly after the first. If, however, read-only mode is
enabled, then the database will be brought online in read-only mode after
completing recovery, and the second step will be lauched as a seperate
process. Clients are allowed to connect, and to execute read-only queries as
soon as the database is online, even though WAL replay is still being done in
the background.

Implementation of a read-only mode suitable for PITR slaves
-----------------------------------------------------------
Since replication via PITR runs asynchrously, and runs one-way (master-slave),
queries running on the slave are of course not allowed to insert, update or
delete data, nor to change the schema in any way. But there are still write
operations in the datadir that they _are_ allowed to do.
Those are
.) Creating temporary files for on-disk sorting and spilling out
tuplestores
.) Setting XMIN_COMMITTED and XMAX_COMITTED on heap tuples
.) Setting LP_DELETE on index tuples.
Note that creating temporary tables is not allowed. This is necessary
because temporary tables have associated entries in pg_class,
which obviously can't be created on PITR slaves.

Postgres already supports "set transaction read only" during normal operation.
On a read-only PITR slave every transaction will automatically be flagged
read-only, which results in nice error messages (like "ERROR: transaction is
read-only") if a user tries to execute insert/updates/deletes or
schema-changing operations. Also any command that has to be execute outside of
a transaction block (VACUUM) is disallowed on PITR slaved. As an additional
protection, a global variable read_only_mode is introduced. If in read-only
mode, this is set to true for all backends except the WAL replay process, and
the following checks are added.
.) MarkBufferDirty() is changed to throw an error if read_only_mode == 1
Hint bit updates already use SetBufferCommitInfoNeedsSave() instead of
MarkBufferDirty(), which just suits use fine.
.) XLogInsert() and XLogWrite() throws if read_only_mode == 1
.) SlruPhysicalWritePage() and SimpleLruWritePage() throws if read_only_mode == 1
This prevents creating or changing multixacts, subtrans and clog entries.
.) EndPrepare() and FinishPreparedTransaction() throws if read_only_mode == 1
This prevents preparing transaction, and committing/rolling-back prepared
transactions.
Those checks serve as a safety measure against holes in the already existing
read-only transaction logic. Note that read-onyl transactions won't generate
clog updates, because those are already skipped for transactions that neither
wrote xlog entries, temporary tables nor deleted files.

The following holes currently exist in the read-only transaction logic. Fixing
those is not critical - the lowlevel checks outlined above catch them all -
but would allow displaying better error messages.
.) nextval(), setval()
.) CLUSTER
.) NOTIFY
Disallowing those in all read-only transactions (Not only on PITR slaves) seems
sensible, but it might create compatibility problems.

Allowing read-only queries and WAL archive replay to run side-by-side
---------------------------------------------------------------------
Of all the interlocks postgres uses to ensure that data is not removed
from under a transaction's feet, three are relevant for PITR slaves.

*) Locks on relations. A select takes an AccessShare lock on every referenced
relation, thereby locking out concurrent DROP,CLUSTER,.. commands. This
is ineffective on PITR slaves, because there is no trace in the WAL that
a lock has been granted.

*) VACUUM and GetOldestXmin(). VACUUM makes sure not to remove tuples still
needed by some transactions by comparing their xmin and xmax to
GetOldestXmin(). But the value returned by GetOldestXmin() on the *server*
has no chance to take queries on the *slave* into account which may
eventually run when the WAL records generated by VACUUM are replayed.

*) The xmin, xmas and list of currently running xid's in SnapshotData ensure
that a single statement or a whole transaction see a constant view of the data,
even if other transactions commit while the exection of the statement or
transaction is still in progress. Creating such a snapshot on the slave
is tricky, because the wal contains no information about the transactions
that were running at a specific point. If a transactions runs for a long time
without doing updates or deleted, it's xid will not show up in the wal
during that time.

A read-only slave would still replay a part of the wal during startup (before
queries are executed) - but only enough wal to guarantee that the data is
consistent. The condition for consistency is exactly the same as the one
currently used to decide whether there was enough wal to make a
filesystem-level backup consistent or not.

There a three ways to overcome the problems stated above, presented here in
the order of increasing complexity

1) Don't run WAL replay and queries concurrently - stop WAL replaying before
starting a transaction.
This allows the transaction to just use a "empty" snapshot, meaning that
just the information from the clog is used to determine visiblity. A global
lock would be acquired in read mode by the WAL replaying process before
replaying a chunk of WAL records. The same lock would be acquired in read
mode by a backend before starting a transaction. Since there is no need for
a real snapshot, and since a read-only transaction's xid never hits the
disk, read-only transactions could just use a constant dummy xid. To be on
the safe side, the chunks after which the WAL replaying process releases
and reacquires the lock would be choosen such that at the end of each chuck
all *_safe_restartpoint functions return true.

2) Only serialize WAL replay and queries if data is actually removed.
This is a refined version of (1) where the global lock is only acquired
before actually removing data. Inserting tuples into the heap or index
should be safe. (Note: The HOT patch might make this more difficult, but
that will be judged when there is consus on that patch). The exception to
this rule are system catalogs, since those are accessed using SnapshotNow -
but since system catalogs have fixed oids, it seems possible to check for
that during wal replay.

3) Log information about granted locks and currently running transactions
into the WAL.
Upon grating a lock on a relation that would conflict with AccessShare, a
xlog record is written containing the oid of the relation. The checkpoint
record is extended to contain a list of currently running transactions on
the master at the time of the checkpoint. This allows the slave to immitate
the locking that was going on on the master, and also to maintain a list of
"concurrent" transactions (In the sense that they were current on the
master when the WAL records being replayed were written).

A backend on the slave can then use this list of transactions to construct
a snapshot, and it is guaranteed that the WAL replay pauses if the changes
it is about to do would conflict with a read-only query.

Since replaying the locking will open up the possibilites of deadlocks on
the slave, it will be necessary to guarantee that it's never the WAL
replayer that is aborted, but rather one of the other backends.

User-Interface
--------------
A new GUC "recovery_allow_readonly" will be introduced. If set to false, postgres
will behave exactly as it does now. If set to true, postgres will allow read-only
queries while replaying WAL records.

Another possibility would be to move this setting into the recovery.conf. The
problems
with this approach is that the recovery.conf file is deleted after the information
it contains is incorporated into pg_control. Thus, the readonly setting would
need to
be stored in pg_control too, making it impossible for the user to change it later
(e.g, after interrupting and restarting WAL replay which is possible with 8.2)

Steps taken during the implementation
-------------------------------------
I will start working on the read-only query support - although I'll only
handle the necessary grade of "read-onlyness" needed for PITR slaves, not for
postgres running on a read-only datadir. Then I'll implement solution (1) of
"Allowing read-only queries and WAL archive replay to run side-by-side", even
though this solution will show limited performance. If this is done, I'll get
my patch into a state where it is considered acceptable for inclusion into the
core. After those goals are archived, I'll try to improve the performance by
relaxing the locking requirements according to either (2), (3) or something
completly different, depending on input from the community.

Costs, Benefits, Open Issues
----------------------------
Costs:
*) Point (3) of "Allowing read-only queries and WAL archive replay to run
side-by-side" would slightly enlare the WAL. One would need to measure
the impact, but since a query that does locking will probably also change
data, it can be assumed that the increase in WAL traffic will hardly be
noticeable.
*) The changes necessary to support read-only queries touch quite a few functions.
But only a simpel "if readonly then throw error" has to be added. This could
even be wrappen inside a macro or function.
*) The WAL replaying could will have to be reorganized - but changes to this part
are unavoidable when implementing this feature.

Benefits:
*) Can be used for master-slave replication. The master database doesn't need
to be modified in any way (apart from defining an archive_command).
This makes this kind of master-slave replication easier to setup and
maintan then trigger-based solutions.
*) Automatically replicates every type of database object, with any special
code needed per object. This is another advantage over trigger-based
solutions.
*) Can be used to run long-running queries (like reporting, or pg_dump)
without preventing the vacuuming of other tables on the master.

Limitations:
*) Point (1) of "Allowing read-only queries and WAL archive replay to run
side-by-side" severly query load you may put on the slave before
it starts falling further and further behind the master. Point (2) and
(3) are meant to address this, but it isn't yet clear how to implement
those.
*) Postgres wouldn't automatically switch into read-write mode when the
replaying process finishes. Thus, failing over to the slave requires
a postgres restart.

Open Questions/Problems
*) How should the flat files be dealt with? Currently, they are updated
after wal replay finishes, which is not acceptable on the slave.
Will have to find out if the wal already contains enough information
to be more clever, or if this information can be added easily. If
both fail, they could be recreated periodically (say, at every
RestartPoint).


---------------------------(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, 07:31 AM
Simon Riggs
 
Posts: n/a
Default Re: Updated propsoal for read-only queries on PITRslaves (SoC 2007)

On Thu, 2007-03-01 at 15:45 +0100, Florian G. Pflug wrote:

> I'm looking forward to any kind of suggestions, ideas, or
> critism - I'd like my proposal to be as detailed as
> possible before I submit it to SoC, so that if
> I get a chance to work on it, I can be reasonable sure
> that people here are happy with the way I approach the problem.


I'm happy with your approach to the problem:

- your thinking is in detail, written and clear
- you cover various options, not just your favourite
- you're doing it on list

So I'll support you SoC submission.

--
Simon Riggs
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
  #3 (permalink)  
Old 04-12-2008, 07:36 AM
Jim Nasby
 
Posts: n/a
Default Re: Updated propsoal for read-only queries on PITR slaves (SoC 2007)

On Mar 1, 2007, at 8:45 AM, Florian G. Pflug wrote:
> Another possibility would be to move this setting into the
> recovery.conf. The problems
> with this approach is that the recovery.conf file is deleted after
> the information
> it contains is incorporated into pg_control. Thus, the readonly
> setting would need to
> be stored in pg_control too, making it impossible for the user to
> change it later
> (e.g, after interrupting and restarting WAL replay which is
> possible with 8.2)


I think it would be best to very clearly divide setting up a cluster
as a read-only slave from doing an actual recovery. One obvious way
to do this would be to require that all read-only GUCs have to live
in postgresql.conf and not recovery.conf. There's probably some other
more elegant solutions as well.
--
Jim Nasby jim@nasby.net
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)



---------------------------(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-12-2008, 07:36 AM
Florian G. Pflug
 
Posts: n/a
Default Re: Updated propsoal for read-only queries on PITR slaves(SoC 2007)

Jim Nasby wrote:
> On Mar 1, 2007, at 8:45 AM, Florian G. Pflug wrote:
>> Another possibility would be to move this setting into the
>> recovery.conf. The problems
>> with this approach is that the recovery.conf file is deleted after the
>> information
>> it contains is incorporated into pg_control. Thus, the readonly
>> setting would need to
>> be stored in pg_control too, making it impossible for the user to
>> change it later
>> (e.g, after interrupting and restarting WAL replay which is possible
>> with 8.2)

>
> I think it would be best to very clearly divide setting up a cluster as
> a read-only slave from doing an actual recovery. One obvious way to do
> this would be to require that all read-only GUCs have to live in
> postgresql.conf and not recovery.conf. There's probably some other more
> elegant solutions as well.


The main argument for putting this into recovery.conf ist that it changes
the behaviour only during recovery. Much like restore_command ist
part of the recovery.conf. But I agree that overall postgresql.conf
seems saner.

greetings, Florian Pflug


---------------------------(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 09:11 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 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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847