Postgres - update CTE with incrementing values

NoobException

I have a table with organization_id and name columns. I need to add a composite unique index to those columns, but we have some duplicate names that need to be updated. I can find the offending records with this query:

with cte as (select l.id, t.*
from locations l
join (
  select name, organization_id, count(*) as qty
  from locations
  group by name, organization_id
  having count(*) > 1
) t on l.name = t.name and l.organization_id = t.organization_id )

I would like to update the records by appending some some incrementing values to the names (this isn't production data, don't judge me :D).

something like

update locations
set name = locations.name || (select i from generate_series(1..some_count))::text
from cte
Adam

Try with sequence:

CREATE SEQUENCE public.blahblah;

Then

UPDATE
    locations
SET
    name = locations.name || nextval('public.blahblah')
FROM
    cte
WHERE
    locations.id = cte.id;

It's safe because

even if multiple sessions execute nextval concurrently, each will safely receive a distinct sequence value

and

To avoid blocking concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used and will not be returned again. This is true even if the surrounding transaction later aborts, or if the calling query ends up not using the value.

See documentation.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

UPDATE + WITH(ROWLOCK)+ CTE

分類Dev

Postgresの再帰CTE

分類Dev

WITH cte UPDATE SET with WHERE breaks

分類Dev

WITH cte UPDATE SET with WHERE breaks

分類Dev

Using .map() to add incrementing values to JS objects

分類Dev

postgres insert using CTE and use the inersted id in next sub query

分類Dev

Insert or update data using cte_results in SQL Server

分類Dev

UPDATEのWHERE条件でCTEを参照する

分類Dev

Python SQLAlchemy Update Postgres Record

分類Dev

Update certain values in update request

分類Dev

Postgres order column with NaN values

分類Dev

Extract JSON values recursivey in postgres

分類Dev

Update values sqlite database

分類Dev

How to Update Values in Meteor?

分類Dev

CTEクエリを防止するPostgresルール

分類Dev

CTEをPostgresからMSSQLに変換する

分類Dev

CTEクエリを置き換えるPostgresクエリ

分類Dev

postgres jsonb_set multiple keys update

分類Dev

postgres jsonb_set multiple keys update

分類Dev

postgres jsonb_set multiple keys update

分類Dev

Postgres - No actual UPDATE, has it side-effects?

分類Dev

Postgres - UPDATE's becomes slower over time

分類Dev

nodejs & postgres : best way to do select for update

分類Dev

Postgres INSERT ON CONFLICT DO UPDATE vsINSERTまたはUPDATE

分類Dev

Detect a series of declining values in a postgres table

分類Dev

Postgres: getting the maximum and minimum values, and timestamps they occur

分類Dev

Why does postgres group null values?

分類Dev

Postgres - Remove NULL values from a json array

分類Dev

Check if Postgres jsonb contains one of the values