Created
January 24, 2026 12:46
-
-
Save igrishaev/96833936f78598664b0b909d1c110eae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| create table goods ( | |
| id serial primary key, | |
| title text not null, | |
| created_at timestamptz not null default current_timestamp | |
| ); | |
| insert into goods(title) | |
| select | |
| format('good_%s', x) | |
| from | |
| generate_series(1, 10000) as seq(x); | |
| create table goods_deleted (like goods); | |
| select * from goods | |
| where id between 555 and 666; | |
| with deleted as ( | |
| delete from goods | |
| where id between 555 and 666 | |
| returning * | |
| ) | |
| insert into goods_deleted | |
| select * from deleted; | |
| ┌─────┬──────────┬───────────────────────────────┐ | |
| │ id │ title │ created_at │ | |
| ├─────┼──────────┼───────────────────────────────┤ | |
| │ 555 │ good_555 │ 2026-01-24 14:48:36.533086+03 │ | |
| │ 556 │ good_556 │ 2026-01-24 14:48:36.533086+03 │ | |
| │ 557 │ good_557 │ 2026-01-24 14:48:36.533086+03 │ | |
| │ 558 │ good_558 │ 2026-01-24 14:48:36.533086+03 │ | |
| │ 559 │ good_559 │ 2026-01-24 14:48:36.533086+03 │ | |
| │ 560 │ good_560 │ 2026-01-24 14:48:36.533086+03 │ | |
| with restored as ( | |
| delete from goods_deleted | |
| where id between 555 and 666 | |
| returning * | |
| ) | |
| insert into goods | |
| select * from restored; | |
| create table any_deleted ( | |
| entity text not null, | |
| pk integer not null, | |
| data jsonb not null, | |
| created_at timestamptz not null default current_timestamp | |
| ); | |
| with deleted as ( | |
| delete from goods | |
| where id between 555 and 666 | |
| returning * | |
| ) | |
| insert into any_deleted | |
| select | |
| 'goods', | |
| deleted.id, | |
| to_jsonb(deleted) | |
| from deleted; | |
| ┌─[ RECORD 1 ]────────────────────────────────────────────────────────────────────────────────────┐ | |
| │ entity │ goods │ | |
| │ pk │ 555 │ | |
| │ data │ {"id": 555, "title": "good_555", "created_at": "2026-01-24T14:48:36.533086+03:00"} │ | |
| │ created_at │ 2026-01-24 14:57:32.595317+03 │ | |
| ├─[ RECORD 2 ]────────────────────────────────────────────────────────────────────────────────────┤ | |
| │ entity │ goods │ | |
| │ pk │ 556 │ | |
| │ data │ {"id": 556, "title": "good_556", "created_at": "2026-01-24T14:48:36.533086+03:00"} │ | |
| │ created_at │ 2026-01-24 14:57:32.595317+03 │ | |
| ├─[ RECORD 3 ]────────────────────────────────────────────────────────────────────────────────────┤ | |
| │ entity │ goods │ | |
| │ pk │ 557 │ | |
| │ data │ {"id": 557, "title": "good_557", "created_at": "2026-01-24T14:48:36.533086+03:00"} │ | |
| │ created_at │ 2026-01-24 14:57:32.595317+03 │ | |
| with deleted as ( | |
| delete from any_deleted | |
| where | |
| entity = 'goods' | |
| and pk between 555 and 666 | |
| returning * | |
| ) | |
| insert into goods | |
| select | |
| rec.id, | |
| rec.title, | |
| rec.created_at | |
| from | |
| deleted, | |
| jsonb_to_record(deleted.data) as rec( | |
| id integer, | |
| title text, | |
| created_at timestamptz | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment