Re: Use ctid in where clause in update from statement

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Use ctid in where clause in update from statement
Date: 2019-07-01 09:35:49
Message-ID: d7a5335e-947b-be84-d5c7-11203cb086b8@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I come from the Oracle world and am trying to convert some queries to
> PostgreSQL syntax. One of these queries is a MERGE statement, which I
> converted into an UPDATE SET FROM WHERE construct. In the original
> query I use the pseudo column ROWID to match a source row with a
> target row.
>
> In the PostgreSQL version I use the column ctid for this. The above query becomes:
>
> *UPDATE*test_large d
>    *SET* grp = s.grp
>   *FROM* (*SELECT* ctid, test_large.*
>           *FROM* test_large
>          *WHERE* grp = 1) s
>  *WHERE* d.ctid = s.ctid;

Why don't you join on the primary key column?
The ctid comparison is typically quite slow.

Thomas

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daulat Ram 2019-07-01 09:42:28 RE: Memory settings
Previous Message Achilleas Mantzios 2019-07-01 09:27:16 Re: Use ctid in where clause in update from statement