Update sql question - Mailing list pgsql-general

From Don Isgitt
Subject Update sql question
Date
Msg-id 42A5B229.1090007@soundenergy.com
Whole thread Raw
Responses Re: Update sql question  (Richard Huxton <dev@archonet.com>)
Re: Update sql question  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hello,

First, software info:

gds2=# select version();

version

--------------------------------------------------------------------------------------------------------
 PostgreSQL 7.4 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.2.3
20030502 (Red Hat Linux 3.2.3-20)
(1 row)

Now, description of what I did and the results:


gds2=# begin;
BEGIN
gds2=# select count(*) from master where state='NM' and operator is null;
count
-------
     0
(1 row)

gds2=# update master set operator=(select
coalesce(newopr,master.operator) from opr_match where state=master.state
and oldopr=master.operator limit 1) where state='NM' and operator is not
null;
UPDATE 88486
gds2=# select count(*) from master where state='NM' and operator is null;
count
-------
   261
(1 row)

gds2=# rollback;
ROLLBACK

Now, what I was trying to do:

master table has ~1000000 records, opr_match~120000 records and each has
a state field. master has an operator field (among many others),
opr_match has an oldopr and newopr field. I want to update master
operator field iff opr_match.oldopr=master.operator; if no match, just
update to self. Fine.

What am I doing wrong that gives me the 261 null operator fields after
the update?

Thank you very much for your assistance.

Don

p.s. The "operator is not null" where clause is needed for other states
that do have null operator values.

pgsql-general by date:

Previous
From: peter royal
Date:
Subject: locking question - why is this not a deadlock?
Next
From: Bruce Momjian
Date:
Subject: Re: Logging query plan for queries