TRUNCATE - PostgreSQL

Anuncio
WikiPrint - from Polar Technologies
TRUNCATE
Nombre
TRUNCATE -- vacía una tabla o un conjunto de ellas
Sinopsis
TRUNCATE [ TABLE ] [ ONLY ] nombre [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
Descripción
TRUNCATE elimina rápidamente todas las filas de un conjunto de tablas. Tiene el mismo efecto que un DELETE sin filtrar sobre cada tabla, pero es más
rápido dado que actualmente no recorre la tabla. Además, reclama el espacio de disco inmediatamente, en vez de requerir una operación VACUUM
subsecuente. Es más útil en tablas grandes.
Parámetros
nombre
El nombre (calificado opcionalmente por el esquema) de la tabla a ser truncada. Si se especifica ONLY, solo dicha tabla es truncada. Si no se
especifica ONLY, la tabla y todas sus descendientes (si hay) serán truncadas.
RESTART IDENTITY
Automáticamente reinicia las secuencias propias de la columna de la tabla truncada.
CONTINUE IDENTITY
No cambia los valores de las secuencias. Esto es lo predeterminado.
CASCADE
Automáticamente trunca todas las tablas que tienen referencias por clave foránea de cualquier tabla mencionada, o de cualquier tabla agregada al
grupo debido al CASCADE.
RESTRICT
Rechaza truncar si alguna de las tablas tiene referencias de clave foránea y no estan listadas en el comando. Esto es lo predeterminado.
Notes
You must have the TRUNCATE privilege on a table to truncate it.
TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which blocks all other concurrent operations on the table. If concurrent
access to a table is required, then the DELETE command should be used instead.
TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command.
Checking validity in such cases would require table scans, and the whole point is not to do one. The CASCADE option can be used to automatically
include all dependent tables - but be very careful when using this option, or else you might lose data you did not intend to!
TRUNCATE will not fire any ON DELETE triggers that might exist for the tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers are
defined for any of the tables, then all BEFORE TRUNCATE triggers are fired before any truncation happens, and all AFTER TRUNCATEtriggers are fired
after the last truncation is performed. The triggers will fire in the order that the tables are to be processed (first those listed in the command, and then any
that were added due to cascading).
Warning
TRUNCATE is not MVCC-safe (see Chapter 13 for general information about MVCC). After truncation, the table will appear empty to all concurrent
transactions, even if they are using a snapshot taken before the truncation occurred. This will only be an issue for a transaction that did not access the
truncated table before the truncation happened - any transaction that has done so would hold at least anACCESS SHARE lock, which would block
TRUNCATE until that transaction completes. So truncation will not cause any apparent inconsistency in the table contents for successive queries on the
same table, but it could cause visible inconsistency between the contents of the truncated table and other tables in the database.
TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not
commit.
1
WikiPrint - from Polar Technologies
Warning
Any ALTER SEQUENCE RESTART operations performed as a consequence of using the RESTART IDENTITY option are nontransactional and will not
be rolled back on failure. To minimize the risk, these operations are performed only after all the rest of TRUNCATE's work is done. However, there is still
a risk if TRUNCATE is performed inside a transaction block that is aborted afterwards. For example, consider
BEGIN;TRUNCATE TABLE foo RESTART IDENTITY;COPY foo FROM ...;COMMIT;
If the COPY fails partway through, the table data rolls back correctly, but the sequences will be left with values that are probably smaller than they had
before, possibly leading to duplicate-key failures or other problems in later transactions. If this is likely to be a problem, it's best to avoid using RESTART
IDENTITY, and accept that the new contents of the table will have higher serial numbers than the old.
Examples
Truncate the tables bigtable and fattable:
TRUNCATE bigtable, fattable;
The same, and also reset any associated sequence generators:
TRUNCATE bigtable, fattable RESTART IDENTITY;
Truncate the table othertable, and cascade to any tables that reference othertable via foreign-key constraints:
TRUNCATE othertable CASCADE;
Compatibility
The SQL:2008 standard includes a TRUNCATE command with the syntax TRUNCATE TABLE tablename. The clauses CONTINUE
IDENTITY/RESTART IDENTITY also appear in that standard but have slightly different but related meanings. Some of the concurrency behavior of this
command is left implementation-defined by the standard, so the above notes should be considered and compared with other implementations if
necessary.
2
Descargar