Re: does update of column with no relation imply a relation check of other column?

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Greg Jaskiewicz <gryzman(at)gmail(dot)com>
Cc: Anibal David Acosta <aa(at)devshock(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: does update of column with no relation imply a relation check of other column?
Date: 2011-10-31 18:19:18
Message-ID: CA+TgmoaVauvUbTfmBR-U7FFu9KH5QF5wC+T=hDvw6k3L6tktLg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Wed, Oct 19, 2011 at 12:42 PM, Greg Jaskiewicz <gryzman(at)gmail(dot)com> wrote:
> For example:
> Table A
> -id (PK)
> -name
>
> Table B
> -table_a_id (PK, FK)
> -address
>
> When I do an insert on table B, the database check if value for column
> “table_a_id” exists in table A
> But, if I do an update of column “address” of table B, does the database
> check again?
>
> My question is due to the nature of and update in postgres, that basically
> is a new version “insert”.
>
> In short - I believe it does. No reason for it not to.

I just tested this, and it seems not.

rhaas=# create table a (id serial primary key);
NOTICE: CREATE TABLE will create implicit sequence "a_id_seq" for
serial column "a.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"a_pkey" for table "a"
CREATE TABLE
rhaas=# create table b (table_a_id integer primary key references a
(id), address text);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"b_pkey" for table "b"
CREATE TABLE
rhaas=# insert into a DEFAULT VALUES ;
INSERT 0 1
rhaas=# insert into b values (1);
INSERT 0 1

Then, in another session:

rhaas=# begin;
BEGIN
rhaas=# lock a;
LOCK TABLE

Back to the first session:

rhaas=# update b set address = 'cow';
UPDATE 1
rhaas=# select * from b;
table_a_id | address
------------+---------
1 | cow
(1 row)

rhaas=# update b set table_a_id = table_a_id + 1;
<blocks>

So it seems that, when the fk field was unchanged, nothing was done
that required accessing table a; otherwise, the access exclusive lock
held by the other session would have blocked it.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Robert Haas 2011-10-31 18:24:46 Re: Composite keys
Previous Message Claudio Freire 2011-10-31 17:52:14 Re: Composite keys