Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fDyQm-00041T-PV for pgsql-general@arkaria.postgresql.org; Wed, 02 May 2018 20:32:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fDyQh-00020w-JL for pgsql-general@arkaria.postgresql.org; Wed, 02 May 2018 20:32:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fDyQh-00020n-BR for pgsql-general@lists.postgresql.org; Wed, 02 May 2018 20:32:47 +0000 Received: from mail-qk0-x234.google.com ([2607:f8b0:400d:c09::234]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fDyQb-00086o-No for pgsql-general@lists.postgresql.org; Wed, 02 May 2018 20:32:46 +0000 Received: by mail-qk0-x234.google.com with SMTP id b131so12318830qkg.2 for ; Wed, 02 May 2018 13:32:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=/Mk5Nw3lSbs1RLXJVhIwDI/EDIjXTm+3erCWha3lsOc=; b=C6cx9td+Y3ppUDqXNOXZicxteRyZ85por6yhvdi6exlrAdtEiuOsxVqNi+bcjgjXDC wLLVzsA94ZwFj1LsV6jHFax4YH7iiGdNSPt0g/LE5z3NnziVOq3aT+/nkWrie4aAmozc AyqPiaFd7L2RYGBD5Q18HQosyRhNuyfvSVUgN8FoMnA5yNUi6Bx/1Ca5cVxGYYR07zud 0W2totcUeYRLOZvuRTJJwgkvTi0NmERkwrD1b0UTI1hqLwxZ7Hdg8eA6ZxqWhQ9qAtWY lFKqbzV2ff4KsjkoAih5BamRxrlu5KpG37zi7Oq0UGxee7UROqHhMARVY9zKdM0jwEWk wu4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=/Mk5Nw3lSbs1RLXJVhIwDI/EDIjXTm+3erCWha3lsOc=; b=Nd3K0nhsoN3AiKZSRvu0OgdUalnc6oAX8atzQVaDmPZ27EQogCnQ9DY72eHL1xsJWc 18lAvD0QzxKWk/EfXXb0RgRXECSnWPjqiCgqKxcMqn6c2n69+WerhJfyb6miZAjLiK2B 7eWeZUqLNh24DnTwuY8b3pb5veQ7nHwRpCOvsQmzPrEqEyas2SoRbEZMW63IVJswlE2f 50L7ZDFDxLUMb/7SvYQ4Xt/sN+9/pWe1Pj8LRKAOMYqYmLCN/z4lZ3VcCvdpQua5EpB5 Gwz6R2gIaY4xOWVyZuvXb+JeU40nFBTfZtUmDFtUyUIh6n+oqVvnrkIgTcOoLGP1LNKz Fkkw== X-Gm-Message-State: ALQs6tA5ikn3tIotSlaxH/lClg2G1Exl3MjiTA17A8hJFMl/iBBy12AB ESnJAiFGCM48QBVve+HABVwGjdPI X-Google-Smtp-Source: AB8JxZrngdqdS8Mq+9/UH/INPAcjpwFoQg7rGphzJ7tnD/SYtJgh5O2pvXxbFDqTvbelBa7KrBNerA== X-Received: by 10.55.200.155 with SMTP id t27mr16184404qkl.214.1525293159533; Wed, 02 May 2018 13:32:39 -0700 (PDT) Received: from alvin.alvh.no-ip.org ([179.56.29.253]) by smtp.gmail.com with ESMTPSA id k188sm9592251qkf.57.2018.05.02.13.32.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 May 2018 13:32:39 -0700 (PDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 3A89F800; Wed, 2 May 2018 17:32:36 -0300 (-03) Date: Wed, 2 May 2018 17:32:36 -0300 From: Alvaro Herrera To: Alastair McKinley Cc: "pgsql-general@lists.postgresql.org" Subject: Re: Index/trigger implementation for accessing latest records Message-ID: <20180502203236.lbua3y77zx2k7qg4@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alastair McKinley wrote: > create function latest_record_update() returns trigger as > $$ > BEGIN > update location_records set latest_record = false where person_id = new.person_id and latest_record is true and id != new.id; > return new; > END; > $$ language plpgsql; > > create trigger latest_record_trigger before insert on location_records > for each row execute procedure latest_record_update(); For maximum safety, you should use BEFORE trigger only to modify the row being inserted/updated (or to abort the operation); any change to other rows should occur in an AFTER trigger. One risk associated with failing to do that is that some other BEFORE trigger further modifies the new row, making your trigger-invoked UPDATE wrong. Also, be sure to think about possible infinite recursion. Another angle to keep in mind is what happens with insertions of historical records, i.e. those that are not latest (today you think "ah, but that never happens" and three months from now this is requested as a feature. Time to rethink the whole development ...) You'd clobber the latest_record flag without a replacement for it, which is probably undesirable. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services