Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Move truncated records to another table in Postgresql 9.5

Problem is following: remove all records from one table, and insert them to another.

I have a table that is partitioned by date criteria. To avoid partitioning each record one by one, I'm collecting the data in one table, and periodically move them to another table. Copied records have to be removed from first table. I'm using DELETE query with RETURNING, but the side effect is that autovacuum is having a lot of work to do to clean up the mess from original table.

I'm trying to achieve the same effect (copy and remove records), but without creating additional work for vacuum mechanism.

As I'm removing all rows (by delete without where conditions), I was thinking about TRUNCATE, but it does not support RETURNING clause. Another idea was to somehow configure the table, to automatically remove tuple from page on delete operation, without waiting for vacuum, but I did not found if it is possible.

Can you suggest something, that I could use to solve my problem?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to use something like:

--Open your transaction

BEGIN;

--Prevent concurrent writes, but allow concurrent data access

LOCK TABLE table_a IN SHARE MODE;

--Copy the data from table_a to table_b, you can also use CREATE TABLE AS to do this

INSERT INTO table_b AS SELECT * FROM table_a;

--Zeroying table_a

TRUNCATE TABLE table_a;

--Commits and release the lock

COMMIT;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda