⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maintenance.sgml

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 SGML
📖 第 1 页 / 共 3 页
字号:
    just specific columns of a table, so the flexibility exists to update some    statistics more frequently than others if your application requires it.    In practice, however, the usefulness of this feature is doubtful.    Beginning in <productname>PostgreSQL</productname> 7.2,    <command>ANALYZE</> is a fairly fast operation even on large tables,    because it uses a statistical random sampling of the rows of a table    rather than reading every single row.  So it's probably much simpler    to just run it over the whole database every so often.   </para>   <tip>    <para>     Although per-column tweaking of <command>ANALYZE</> frequency may not be     very productive, you may well find it worthwhile to do per-column     adjustment of the level of detail of the statistics collected by     <command>ANALYZE</>.  Columns that are heavily used in <literal>WHERE</> clauses     and have highly irregular data distributions may require a finer-grain     data histogram than other columns.  See <command>ALTER TABLE SET     STATISTICS</>.    </para>   </tip>   <para>    Recommended practice for most sites is to schedule a database-wide    <command>ANALYZE</> once a day at a low-usage time of day; this can    usefully be combined with a nightly <command>VACUUM</>.  However,    sites with relatively slowly changing table statistics may find that    this is overkill, and that less-frequent <command>ANALYZE</> runs    are sufficient.   </para>  </sect2>  <sect2 id="vacuum-for-wraparound">   <title>Preventing transaction ID wraparound failures</title>   <indexterm zone="vacuum-for-wraparound">    <primary>transaction ID</primary>    <secondary>wraparound</secondary>   </indexterm>   <para>    <productname>PostgreSQL</productname>'s MVCC transaction semantics    depend on being able to compare transaction ID (<acronym>XID</>)    numbers: a row version with an insertion XID greater than the current    transaction's XID is <quote>in the future</> and should not be visible    to the current transaction.  But since transaction IDs have limited size    (32 bits at this writing) a cluster that runs for a long time (more    than 4 billion transactions) would suffer <firstterm>transaction ID    wraparound</>: the XID counter wraps around to zero, and all of a sudden    transactions that were in the past appear to be in the future &mdash; which    means their outputs become invisible.  In short, catastrophic data loss.    (Actually the data is still there, but that's cold comfort if you can't    get at it.)   </para>   <para>    Prior to <productname>PostgreSQL</productname> 7.2, the only defense    against XID wraparound was to re-<command>initdb</> at least every 4    billion transactions. This of course was not very satisfactory for    high-traffic sites, so a better solution has been devised. The new    approach allows a server to remain up indefinitely, without    <command>initdb</> or any sort of restart. The price is this    maintenance requirement: <emphasis>every table in the database must    be vacuumed at least once every billion transactions</emphasis>.   </para>   <para>    In practice this isn't an onerous requirement, but since the    consequences of failing to meet it can be complete data loss (not    just wasted disk space or slow performance), some special provisions    have been made to help database administrators avoid disaster.    For each database in the cluster, <productname>PostgreSQL</productname>    keeps track of the time of the last database-wide <command>VACUUM</>.    When any database approaches the billion-transaction danger level,    the system begins to emit warning messages.  If nothing is done, it    will eventually shut down normal operations until appropriate    manual maintenance is done.  The remainder of this    section gives the details.   </para>   <para>    The new approach to XID comparison distinguishes two special XIDs,    numbers 1 and 2 (<literal>BootstrapXID</> and    <literal>FrozenXID</>). These two XIDs are always considered older    than every normal XID. Normal XIDs (those greater than 2) are    compared using modulo-2<superscript>31</> arithmetic. This means    that for every normal XID, there are two billion XIDs that are    <quote>older</> and two billion that are <quote>newer</>; another    way to say it is that the normal XID space is circular with no    endpoint. Therefore, once a row version has been created with a particular    normal XID, the row version will appear to be <quote>in the past</> for    the next two billion transactions, no matter which normal XID we are    talking about. If the row version still exists after more than two billion    transactions, it will suddenly appear to be in the future. To    prevent data loss, old row versions must be reassigned the XID    <literal>FrozenXID</> sometime before they reach the    two-billion-transactions-old mark. Once they are assigned this    special XID, they will appear to be <quote>in the past</> to all    normal transactions regardless of wraparound issues, and so such    row versions will be good until deleted, no matter how long that is. This    reassignment of XID is handled by <command>VACUUM</>.   </para>   <para>    <command>VACUUM</>'s normal policy is to reassign <literal>FrozenXID</>    to any row version with a normal XID more than one billion transactions in the    past.  This policy preserves the original insertion XID until it is not    likely to be of interest anymore.  (In fact, most row versions will probably    live and die without ever being <quote>frozen</>.)  With this policy,    the maximum safe interval between <command>VACUUM</> runs on any table    is exactly one billion transactions: if you wait longer, it's possible    that a row version that was not quite old enough to be reassigned last time    is now more than two billion transactions old and has wrapped around    into the future &mdash; i.e., is lost to you.  (Of course, it'll reappear    after another two billion transactions, but that's no help.)   </para>   <para>    Since periodic <command>VACUUM</> runs are needed anyway for the reasons    described earlier, it's unlikely that any table would not be vacuumed    for as long as a billion transactions.  But to help administrators ensure    this constraint is met, <command>VACUUM</> stores transaction ID    statistics in the system table <literal>pg_database</>.  In particular,    the <literal>datfrozenxid</> column of a database's    <literal>pg_database</> row is updated at the completion of any    database-wide <command>VACUUM</command> operation (i.e.,    <command>VACUUM</> that does not    name a specific table).  The value stored in this field is the freeze    cutoff XID that was used by that <command>VACUUM</> command.  All normal    XIDs older than this cutoff XID are guaranteed to have been replaced by    <literal>FrozenXID</> within that database.  A convenient way to    examine this information is to execute the query<programlisting>SELECT datname, age(datfrozenxid) FROM pg_database;</programlisting>    The <literal>age</> column measures the number of transactions from the    cutoff XID to the current transaction's XID.   </para>   <para>    With the standard freezing policy, the <literal>age</> column will start    at one billion for a freshly-vacuumed database.  When the <literal>age</>    approaches two billion, the database must be vacuumed again to avoid    risk of wraparound failures.  Recommended practice is to <command>VACUUM</command> each    database at least once every half-a-billion (500 million) transactions,    so as to provide plenty of safety margin.  To help meet this rule,    each database-wide <command>VACUUM</> automatically delivers a warning    if there are any <literal>pg_database</> entries showing an    <literal>age</> of more than 1.5 billion transactions, for example:<programlisting>play=# VACUUM;WARNING:  database "mydb" must be vacuumed within 177009986 transactionsHINT:  To avoid a database shutdown, execute a full-database VACUUM in "mydb".VACUUM</programlisting>   </para>   <para>    If the warnings emitted by <command>VACUUM</> go ignored, then    <productname>PostgreSQL</productname> will begin to emit a warning    like the above on every transaction start once there are fewer than 10    million transactions left until wraparound.  If those warnings also are    ignored, the system will shut down and refuse to execute any new    transactions once there are fewer than 1 million transactions left    until wraparound:<programlisting>play=# select 2+2;ERROR:  database is shut down to avoid wraparound data loss in database "mydb"HINT:  Stop the postmaster and use a standalone backend to VACUUM in "mydb".</programlisting>    The 1-million-transaction safety margin exists to let the    administrator recover without data loss, by manually executing the    required <command>VACUUM</> commands.  However, since the system will not    execute commands once it has gone into the safety shutdown mode,    the only way to do this is to stop the postmaster and use a standalone    backend to execute <command>VACUUM</>.  The shutdown mode is not enforced    by a standalone backend.  See the <xref linkend="app-postgres"> reference    page for details about using a standalone backend.   </para>   <para>    <command>VACUUM</> with the <command>FREEZE</> option uses a more    aggressive freezing policy: row versions are frozen if they are old enough    to be considered good by all open transactions. In particular, if a    <command>VACUUM FREEZE</> is performed in an otherwise-idle    database, it is guaranteed that <emphasis>all</> row versions in that    database will be frozen. Hence, as long as the database is not    modified in any way, it will not need subsequent vacuuming to avoid    transaction ID wraparound problems. This technique is used by    <command>initdb</> to prepare the <literal>template0</> database.    It should also be used to prepare any user-created databases that    are to be marked <literal>datallowconn</> = <literal>false</> in    <literal>pg_database</>, since there isn't any convenient way to    <command>VACUUM</command> a database that you can't connect to.   </para>   <warning>    <para>     A database that is marked <literal>datallowconn</> = <literal>false</>     in <literal>pg_database</> is assumed to be properly frozen; the     automatic warnings and wraparound protection shutdown do not take     such databases into account.  Therefore it's up to you to ensure     you've correctly frozen a database before you mark it with     <literal>datallowconn</> = <literal>false</>.    </para>   </warning>  </sect2>  <sect2 id="autovacuum">   <title id="autovacuum-title">The auto-vacuum daemon</title>   <indexterm>    <primary>autovacuum</primary>    <secondary>general information</secondary>   </indexterm>   <para>    Beginning in <productname>PostgreSQL </productname> 8.1, there is a    separate optional server process called the <firstterm>autovacuum    daemon</firstterm>, whose purpose is to automate the execution of    <command>VACUUM</command> and <command>ANALYZE </command> commands.    When enabled, the autovacuum daemon runs periodically and checks for    tables that have had a large number of inserted, updated or deleted    tuples.  These checks use the row-level statistics collection facility;    therefore, the autovacuum daemon cannot be used unless <xref    linkend="guc-stats-start-collector"> and <xref    linkend="guc-stats-row-level"> are set to <literal>true</literal>.  Also,    it's important to allow a slot for the autovacuum process when choosing    the value of <xref linkend="guc-superuser-reserved-connections">.   </para>   <para>    The autovacuum daemon, when enabled, runs every <xref    linkend="guc-autovacuum-naptime"> seconds and determines which database    to process.  Any database which is close to transaction ID wraparound    is immediately processed.  In this case, autovacuum issues a    database-wide <command>VACUUM</command> call, or <command>VACUUM    FREEZE</command> if it's a template database, and then terminates.  If    no database fulfills this criterion, the one that was least recently    processed by autovacuum is chosen.  In this case each table in    the selected database is checked, and individual <command>VACUUM</command>    or <command>ANALYZE</command> commands are issued as needed.   </para>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -