Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
<!--
doc/src/sgml/ref/repack.sgml
PostgreSQL documentation
-->
 
<refentry id="sql-repack">
 <indexterm zone="sql-repack">
  <primary>REPACK</primary>
 </indexterm>
 
 <refmeta>
  <refentrytitle>REPACK</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 
 <refnamediv>
  <refname>REPACK</refname>
  <refpurpose>rewrite a table to reclaim disk space</refpurpose>
 </refnamediv>
 
 <refsynopsisdiv>
<synopsis>
REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] [ <replaceable class="parameter">table_and_columns</replaceable> [ USING INDEX [ <replaceable class="parameter">index_name</replaceable> ] ] ]
REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING INDEX
 
<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
 
    VERBOSE [ <replaceable class="parameter">boolean</replaceable> ]
    ANALYZE [ <replaceable class="parameter">boolean</replaceable> ]
    CONCURRENTLY [ <replaceable class="parameter">boolean</replaceable> ]
 
<phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase>
 
    <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ]
</synopsis>
 </refsynopsisdiv>
 
 <refsect1>
  <title>Description</title>
 
  <para>
   <command>REPACK</command> reclaims storage occupied by dead
   tuples. Unlike <command>VACUUM</command>, it does so by rewriting the
   entire contents of the table specified
   by <replaceable class="parameter">table_name</replaceable> into a new disk
   file with no extra space (except for the space guaranteed by
   the <literal>fillfactor</literal> storage parameter), allowing unused space
   to be returned to the operating system.
  </para>
 
  <para>
   Without
   a <replaceable class="parameter">table_name</replaceable>, <command>REPACK</command>
   processes every table and materialized view in the current database that
   the current user has the <literal>MAINTAIN</literal> privilege on. This
   form of <command>REPACK</command> cannot be executed inside a transaction
   block.  Also, this form is not allowed if
   the <literal>CONCURRENTLY</literal> option is used.
  </para>
 
  <para>
   If a <literal>USING INDEX</literal> clause is specified, the rows are
   physically reordered based on information from an index.  Please see
   <xref linkend="sql-repack-notes-on-clustering"/> below.
  </para>
 
  <para>
   When a table is being repacked, an <literal>ACCESS EXCLUSIVE</literal> lock
   is acquired on it, unless the <literal>CONCURRENTLY</literal> option is given.
   See <xref linkend="sql-repack-notes-on-concurrently"/> for a discussion
   on the effects of this option.
  </para>
 </refsect1>
 
 <refsect1>
  <title>Parameters</title>
 
  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">table_name</replaceable></term>
    <listitem>
     <para>
      The name (possibly schema-qualified) of a table.
     </para>
    </listitem>
   </varlistentry>
 
   <varlistentry>
    <term><replaceable class="parameter">column_name</replaceable></term>
    <listitem>
     <para>
      The name of a specific column to analyze. Defaults to all columns.
      If a column list is specific, <literal>ANALYZE</literal> must also
      be specified.
     </para>
    </listitem>
   </varlistentry>
 
   <varlistentry>
    <term><replaceable class="parameter">index_name</replaceable></term>
    <listitem>
     <para>
      The name of an index.
     </para>
    </listitem>
   </varlistentry>
 
   <varlistentry>
    <term><literal>CONCURRENTLY</literal></term>
    <listitem>
     <para>
      Allow other transactions to use the table while it is being repacked.
      See <xref linkend="sql-repack-notes-on-concurrently" /> for more details.
     </para>
 
    </listitem>
   </varlistentry>
 
   <varlistentry>
    <term><literal>VERBOSE</literal></term>
    <listitem>
     <para>
      Prints a progress report as each table is repacked
      at <literal>INFO</literal> level.
     </para>
    </listitem>
   </varlistentry>
 
   <varlistentry>
    <term><literal>ANALYZE</literal></term>
    <term><literal>ANALYSE</literal></term>
    <listitem>
     <para>
      Applies <xref linkend="sql-analyze"/> on the table after repacking.  This is
      currently only supported when a single (non-partitioned) table is specified.
      This option cannot be used inside a transaction block, or from a function,
      procedure, or <command>DO</command> block.
     </para>
    </listitem>
   </varlistentry>
 
   <varlistentry>
    <term><replaceable class="parameter">boolean</replaceable></term>
    <listitem>
     <para>
      Specifies whether the selected option should be turned on or off.
      You can write <literal>TRUE</literal>, <literal>ON</literal>, or
      <literal>1</literal> to enable the option, and <literal>FALSE</literal>,
      <literal>OFF</literal>, or <literal>0</literal> to disable it.  The
      <replaceable class="parameter">boolean</replaceable> value can also
      be omitted, in which case <literal>TRUE</literal> is assumed.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>
 
 <refsect1 id="sql-repack-notes">
  <title>Notes</title>
 
  <para>
   To repack a table, one must have the <literal>MAINTAIN</literal> privilege
   on the table.
  </para>
 
  <para>
   <command>REPACK</command> is primarily meant to remove bloat and, with
   <literal>USING INDEX</literal>, to cluster the table, in order to improve
   performance.  Although it also advances the table's
   <structfield>relfrozenxid</structfield> and <structfield>relminmxid</structfield>,
   it is not a good way to prevent transaction ID or multixact ID wraparound:
   it rewrites the whole table and all of its indexes, so it takes much longer
   than <command>VACUUM</command>, and it can fail after most of the work
   is done, especially with <literal>CONCURRENTLY</literal> on a busy table.
   <command>VACUUM</command> is recommended for that instead (see
   <xref linkend="vacuum-for-wraparound"/>).
   When a table gets close to wraparound, <command>VACUUM</command> skips index
   vacuuming on its own (see <xref linkend="guc-vacuum-failsafe-age"/>), and its
   <link linkend="sql-vacuum"><literal>INDEX_CLEANUP OFF</literal></link>
   option can do the same earlier.
  </para>
 
  <para>
   <command>REPACK</command> refuses to process a table on which an invalid
   index exists.  Such indexes must be dropped or reindexed by the user ahead
   of time.
  </para>
 
  <para>
   While <command>REPACK</command> is running, the <xref
   linkend="guc-search-path"/> is temporarily changed to <literal>pg_catalog,
   pg_temp</literal>.
  </para>
 
  <para>
   Each backend running <command>REPACK</command> will report its progress
   in the <structname>pg_stat_progress_repack</structname> view. See
   <xref linkend="repack-progress-reporting"/> for details.
  </para>
 
  <para>
   Repacking a partitioned table repacks each of its partitions. If an index
   is specified, each partition is repacked using the partition of that
   index. <command>REPACK</command> on a partitioned table cannot be executed
   inside a transaction block.
  </para>
 
  <refsect2 id="sql-repack-notes-on-clustering" xreflabel="Notes on Clustering">
   <title>Notes on Clustering</title>
 
   <para>
    If the <literal>USING INDEX</literal> clause is specified, the rows in
    the table are physically rearranged according to the ordering implied by
    the specified index; this is known as <firstterm>clustering</firstterm>.
    This can have performance implications:
    in cases where you are accessing single rows randomly within a table, the
    actual order of the data in the table is unimportant. However, if you tend
    to access some data more than others, and there is an index that groups
    them together, you will benefit from using clustering.  If
    you are requesting a range of indexed values from a table, or a single
    indexed value that has multiple matching rows,
    clustering will help because once the index identifies the
    table page for the first row that matches, all other rows that match are
    probably already on the same table page, and so you save disk accesses and
    speed up the query.
   </para>
 
   <para>
    For B-Tree indexes, the ordering used by clustering is the index's linear
    sort order.  Other clusterable index access methods may use a different
    ordering strategy, one which may not necessarily correspond to any SQL
    sort order.
    If an index name is specified in the command, that index is used and
    is recorded as the table's clustering index.
    (This also applies to an index given to the <command>CLUSTER</command>
    command.)
    If no index name is specified, then the index that has been configured as
    the clustering one is used; if none has been configured, an error is thrown.
    An index can be set manually using <command>ALTER TABLE ... CLUSTER ON</command>,
    and reset with <command>ALTER TABLE ... SET WITHOUT CLUSTER</command>.
   </para>
 
   <para>
    Clustering is a one-time operation: when the table is
    subsequently updated, the changes are not clustered.  That is, no attempt
    is made to store new or updated rows according to the clustering order.
    (If one wishes, one can periodically recluster by issuing the command again.
    Also, setting the table's <literal>fillfactor</literal> storage parameter
    to less than 100% can aid in preserving cluster ordering during updates,
    since updated rows are kept on the same page if enough space is available
    there.)
   </para>
 
   <para>
    When clustering on a B-Tree index, <command>REPACK</command> can rewrite
    the table using either an index scan on the specified index, or a
    sequential scan followed by sorting.  It will attempt to choose the method
    that will be faster, based on planner cost parameters and available
    statistical information.  When clustering on an index of an access method
    other than B-Tree, <command>REPACK</command> always uses an index scan.
   </para>
 
   <para>
    Because the planner records statistics about the ordering of tables, it is
    advisable to specify the <literal>ANALYZE</literal> option, or to
    run <link linkend="sql-analyze"><command>ANALYZE</command></link> on the
    newly repacked table.  Otherwise, the planner might make poor choices of
    query plans.
   </para>
 
   <para>
    If no table name is specified in <command>REPACK USING INDEX</command>,
    all tables which have a clustering index defined and which the calling
    user has privileges for are processed.
   </para>
  </refsect2>
 
  <refsect2 id="sql-repack-notes-on-resources" xreflabel="Notes on Resources">
   <title>Notes on Resources</title>
 
   <para>
    <command>REPACK</command> creates a temporary copy of the table and all
    of its indexes, so you need free disk space at least equal to the sum
    of the table size and the index sizes.
   </para>
 
   <para>
    If the <literal>USING INDEX</literal> clause is given and the planner
    chooses a sequential scan and sort to implement it, a temporary sort
    file of up to the size of the table is needed in addition to the above.
    This method is usually faster than an index scan, but if the disk space
    requirement is intolerable, you can disable this choice by temporarily
    setting <xref linkend="guc-enable-sort"/> to <literal>off</literal>; or
    omitting <literal>USING INDEX</literal> if the table doesn't need to be
    reordered.
   </para>
 
   <para>
    For the purposes of transaction ID wraparound
    (see <xref linkend="vacuum-for-wraparound"/>),
    <command>REPACK</command> is considered a single long-running
    transaction, which prevents <command>VACUUM</command> from cleaning
    up dead rows from other tables.
    It is advisable to monitor <structname>pg_stat_activity.backend_xid</structname>
    for the process running <command>REPACK</command> when repacking
    very large tables, to avoid causing excessive bloat in other tables.
   </para>
 
   <para>
    It is advisable to set <xref linkend="guc-maintenance-work-mem"/> to a
    reasonably large value (but not more than the amount of RAM you can
    dedicate to the <command>REPACK</command> operation) before repacking.
   </para>
  </refsect2>
 
  <refsect2 id="sql-repack-notes-on-concurrently" xreflabel="Notes on Concurrent Operation">
   <title>Notes on Concurrent Operation</title>
 
   <warning>
    <para>
     <command>REPACK</command> with the <literal>CONCURRENTLY</literal>
     option is not MVCC-safe.  After the repacking transaction commits,
     the table will appear empty to concurrent transactions, if they are
     using a snapshot taken before repacking started.
     See <xref linkend="mvcc-caveats"/> for more details.
    </para>
   </warning>
 
   <para>
    <command>REPACK</command> copies the contents of the table
    (ignoring dead tuples) into a new file, sorted by the specified index,
    and also creates a new file for each index. It then swaps the old and
    new files for the table and all the indexes, and deletes the old files.
    Without the <literal>CONCURRENTLY</literal> option, an
    <literal>ACCESS EXCLUSIVE</literal> lock is acquired at the beginning
    and remains held throughout the operation to make sure that the old
    files do not change during the processing; otherwise, any concurrent
    changes would get lost due to the swap.
   </para>
 
   <para>
    By contrast, when the <literal>CONCURRENTLY</literal> option is
    specified, the bulk of the operation is run with
    <literal>SHARE UPDATE EXCLUSIVE</literal> lock, and the
    <literal>ACCESS EXCLUSIVE</literal> lock is only acquired
    during the final phase to swap the table and index files.
    The data changes that took place during the creation of the new
    table and index files are captured using logical decoding
    (see <xref linkend="logicaldecoding"/>) and applied before
    the <literal>ACCESS EXCLUSIVE</literal> lock is requested.
    Once that lock is obtained, a final pass over any remaining captured
    concurrent data changes is done and the files are swapped.
    Thus the lock is typically held for a short time, depending
    on the amount of changes accumulated while the lock was being
    waited for.
   </para>
 
   <para>
    Processing of these concurrent changes requires a fixed small
    amount of memory for each tuple concurrently updated or deleted,
    not limited by <xref linkend="guc-maintenance-work-mem"/>;
    if more than about 104 million rows are concurrently updated or
    deleted during the execution of <command>REPACK</command>, the
    command fails.  In addition, the complete tuple data for all
    concurrent changes (except for TOAST columns that are not modified
    by <command>UPDATE</command>) are stored on disk separately.
   </para>
 
   <para>
    <command>REPACK (CONCURRENTLY)</command> might fail to complete if DDL
    commands are executed on the table by other transactions during the
    repacking.
   </para>
 
   <para>
    <command>REPACK (CONCURRENTLY) USING INDEX</command>
    does not try to order the rows inserted into the table after the
    repacking started.
   </para>
 
   <para>
    The <literal>CONCURRENTLY</literal> option cannot be used in the
    following cases:
 
    <itemizedlist>
     <listitem>
      <para>
       The relation is not a table (e.g., it is a materialized view).
      </para>
     </listitem>
 
     <listitem>
      <para>
       The table is <literal>UNLOGGED</literal>.
      </para>
     </listitem>
 
     <listitem>
      <para>
       The table is partitioned.
      </para>
     </listitem>
 
     <listitem>
      <para>
       The table lacks a primary key and index-based replica identity.
      </para>
     </listitem>
 
     <listitem>
      <para>
       The table is a system catalog or a <acronym>TOAST</acronym> table.
      </para>
     </listitem>
 
     <listitem>
      <para>
       The table's access method is not <literal>heap</literal>.
      </para>
     </listitem>
 
     <listitem>
      <para>
       The table is declared as a catalog table using the
       <link linkend="reloption-user-catalog-table"><literal>user_catalog_table</literal></link>
       storage parameter.
      </para>
     </listitem>
 
     <listitem>
      <para>
       <command>REPACK</command> is executed inside a transaction block.
      </para>
     </listitem>
 
     <listitem>
      <para>
       The <link linkend="guc-max-repack-replication-slots"><varname>max_repack_replication_slots</varname></link>
       configuration parameter does not allow for the creation of an
       additional replication slot.
      </para>
     </listitem>
    </itemizedlist>
   </para>
  </refsect2>
 </refsect1>
 
 <refsect1>
  <title>Examples</title>
 
  <para>
   Repack the table <literal>employees</literal>:
<programlisting>
REPACK employees;
</programlisting>
  </para>
 
  <para>
   Repack the table <literal>employees</literal> on the basis of its
   index <literal>employees_ind</literal> (since an index is specified, this is
   effectively clustering):
<programlisting>
REPACK employees USING INDEX employees_ind;
</programlisting>
  </para>
 
  <para>
   Repack the <literal>employees</literal> table following the same index
   as was used before, in concurrent mode:
<programlisting>
REPACK (CONCURRENTLY) employees USING INDEX;
</programlisting>
  </para>
 
  <para>
   Repack the table <literal>cases</literal> on physical ordering,
   running an <command>ANALYZE</command> on the given columns once
   repacking is done, showing informational messages:
<programlisting>
REPACK (ANALYZE, VERBOSE) cases (district, case_nr);
</programlisting>
  </para>
 
  <para>
   Repack all tables in the database on which you have
   the <literal>MAINTAIN</literal> privilege:
<programlisting>
REPACK;
</programlisting>
  </para>
 
  <para>
   Repack all tables for which a clustering index has previously been
   configured on which you have the <literal>MAINTAIN</literal> privilege,
   showing informational messages:
<programlisting>
REPACK (VERBOSE) USING INDEX;
</programlisting>
  </para>
 
 </refsect1>
 
 <refsect1>
  <title>Compatibility</title>
 
  <para>
   There is no <command>REPACK</command> statement in the SQL standard.
  </para>
 </refsect1>
 
 <refsect1>
  <title>See Also</title>
 
  <simplelist type="inline">
   <member><xref linkend="repack-progress-reporting"/></member>
  </simplelist>
 </refsect1>
 
</refentry>