pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Slick way to update multiple tables.
Date: Thu, 1 Apr 2021 12:37:06 -0700
Message-ID: <2662957f-36c0-4e02-6be9-f7bf52729309@illuminatedcomputing.com> (raw)
In-Reply-To: <CAHOFxGq_3HGE1B+tJ5keBQu0FUTEu_5zN_m0Znv75+J8uT8STg@mail.gmail.com>
References: <CAPDmd-AQ9M_DGEuttxB25TGCkQxT=WqU=p4_0AVKVTiJNBfbLA@mail.gmail.com>
	<CAHOFxGo8=uHhC9w8+oY3L1SK41ZstAZ5yUgT0OyWXYiBqQsntw@mail.gmail.com>
	<CAPDmd-DnZSu6o7uAPyqU-AM5gmni+yH9x7Ln1ANCxu0MBDACOg@mail.gmail.com>
	<CAHOFxGrkjkd-tHccQEVMdXbBe+7HN_osjX45C8K98mn1kbebBA@mail.gmail.com>
	<CAHOFxGq_3HGE1B+tJ5keBQu0FUTEu_5zN_m0Znv75+J8uT8STg@mail.gmail.com>

On 4/1/21 11:54 AM, Michael Lewis wrote:
> postgresql.org/docs/current/sql-createview.html 
> <http://postgresql.org/docs/current/sql-createview.html;
> 
> My apologies. It seems INSTEAD OF triggers are required to implement 
> updates across multiple tables. I thought not if all were simple joins. 
> My mistake.

Even with INSTEAD OF triggers, if you use a view then I suppose you 
would be forced to update some of the records more often that necessary? 
(Unless your tables are 1-to-1-to-1 of course.) Or if there is some 
trick to avoid that I'd be curious to know about it.

Here is something I've done in the past:

WITH
update1(ok) AS (
   UPDATE foo SET ... WHERE ...
   RETURNING 'ok'
),
update2(ok) AS (
   UPDATE bar SET ... WHERE ...
   RETURNING 'ok'
),
update3(ok) AS (
   UPDATE baz SET ... WHERE ...
   RETURNING 'ok'
)
SELECT ok FROM update1
UNION ALL
SELECT ok FROM update2
UNION ALL
SELECT ok FROM update3
;

You could even select different messages from each update if you want to 
know how many rows you touched in each table.

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com





view thread (7+ messages)  latest in thread

Message-ID: <2662957f-36c0-4e02-6be9-f7bf52729309@illuminatedcomputing.com>
Permalink:  ../2662957f-36c0-4e02-6be9-f7bf52729309@illuminatedcomputing.com/
Also on:    postgresql.org/message-id/2662957f-36c0-4e02-6be9-f7bf52729309@illuminatedcomputing.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pj@illuminatedcomputing.com
  Subject: Re: Slick way to update multiple tables.
  In-Reply-To: <2662957f-36c0-4e02-6be9-f7bf52729309@illuminatedcomputing.com>
  References: <CAPDmd-AQ9M_DGEuttxB25TGCkQxT=WqU=p4_0AVKVTiJNBfbLA@mail.gmail.com> <CAHOFxGo8=uHhC9w8+oY3L1SK41ZstAZ5yUgT0OyWXYiBqQsntw@mail.gmail.com> <CAPDmd-DnZSu6o7uAPyqU-AM5gmni+yH9x7Ln1ANCxu0MBDACOg@mail.gmail.com> <CAHOFxGrkjkd-tHccQEVMdXbBe+7HN_osjX45C8K98mn1kbebBA@mail.gmail.com> <CAHOFxGq_3HGE1B+tJ5keBQu0FUTEu_5zN_m0Znv75+J8uT8STg@mail.gmail.com> <2662957f-36c0-4e02-6be9-f7bf52729309@illuminatedcomputing.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox