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 1fE050-0003an-Oo for pgsql-general@arkaria.postgresql.org; Wed, 02 May 2018 22:18:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fE04z-0006eN-RT for pgsql-general@arkaria.postgresql.org; Wed, 02 May 2018 22:18:29 +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 1fE01n-0000KG-6C for pgsql-general@lists.postgresql.org; Wed, 02 May 2018 22:15:11 +0000 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fE01e-00020J-Tq for pgsql-general@lists.postgresql.org; Wed, 02 May 2018 22:15:10 +0000 Received: by mail-pf0-x244.google.com with SMTP id q22so12954938pff.11 for ; Wed, 02 May 2018 15:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=s0zvG9SLlXGO05NR01kx6XNH0mNNvRnWCi6H/F4WeW0=; b=AL8wpTCS7LyRSVSrPKzUQwSjhBIcN1EY9F7tTktVYb3HY/+/jGZsbBXLo7gCK0ZZpn VcqONFym2SNupMiy0wue/SNTA8WtfhBJT2gTBYYed8R1rmhtOVuo4kHSuv0LzSDD0Qs/ nbsNy1KqRwQoaUJAulkL9bWAKVg5UoqOJfEM6Fmf25gThKxSI6DF/OWx1jxIzCqDe0wx VvRObrvztUaWGb2lbpuY008sT9fIygIJmSWBmkzDOrOjQ9rkL1+5VVSa5U3Qdj5ODsYJ IGg1MtcEjh43JWVqDA7v4A+csSUfJuGms3JmSNHuqZj+elNjwY7SL0xAbYRIYsBTrdr8 95Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=s0zvG9SLlXGO05NR01kx6XNH0mNNvRnWCi6H/F4WeW0=; b=kv8SuchBwJ7N+MfsAycPavMnl7NUIG44MH5EHgHjRx1fBHoeMK/0NNzEXfefLxOT3q TwIQrsVNO0UmG1nau4cxGBGi9AJ7c1r9eiTCJ70eZGMixlMoRX4R1Gm2j1+IJQ3Lyi1K lvwhg/qB1t4XqhLJHye0VPLsigENbQCNR+zJbQ/uOdcclhHJwXOo7DKhelwAbsG0DzTj qXjfqbYWlq9kitAPW29pDQ0kHJcXMZOwRI+pibCAzZiEFzKcOohPiIs3183dv3gn2cuP 255S3SB1RpIW8O4WGVqxOE/lcAalcztdZTGPbrhiEQbPF01W4fLQtna3etu/HPtYNRNJ ZDDw== X-Gm-Message-State: ALQs6tATpfooM8Br8zaUawde/8R/j/o87v+ZHgH97TKpcZ1eG/W1uhdg XMnthWX+SySGvUDzif6+jw0/kyXi X-Google-Smtp-Source: AB8JxZrJ46r5py7WcpUprv6BTV3SctcfejpwWGbRo84fSI434yCJfgitY8w7hYMtFzWgx45uMN84OQ== X-Received: by 10.98.10.72 with SMTP id s69mr20890527pfi.134.1525299299995; Wed, 02 May 2018 15:14:59 -0700 (PDT) Received: from tim-desktop (2001-44b8-31f2-bb00-bd03-2b98-e0d5-0fa5.static.ipv6.internode.on.net. [2001:44b8:31f2:bb00:bd03:2b98:e0d5:fa5]) by smtp.gmail.com with ESMTPSA id k13sm29169192pfj.186.2018.05.02.15.14.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 May 2018 15:14:59 -0700 (PDT) References: User-agent: mu4e 0.9.18; emacs 26.1 From: Tim Cross To: Alastair McKinley Cc: "pgsql-general\@lists.postgresql.org" Subject: Re: Index/trigger implementation for accessing latest records In-reply-to: Date: Thu, 03 May 2018 08:14:55 +1000 Message-ID: <876045yaao.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alastair McKinley writes: > Hi, > > > I have a table that stores a location identifier per person which will be appended to many times. > > However, for many queries in this system we only need to know the most recent location per person, which is limited to about 1000 records. > > > Is the following trigger/index strategy a reasonable and safe approach to fast access to the latest location records per person? > > > 1. A boolean column (latest_record default true) to identify the latest record per person > 2. A before insert trigger that updates all other records for that person to latest_record = false > 3. A partial index on the latest_record column where latest_record is true > > > Aside from performance, is it safe to update other records in the table from the insert trigger in this way? > > > Minimal example is shown below: > > > create table location_records > > ( > > id bigserial, > > person_id bigint, > > location_id bigint, > > latest_record boolean not null default true > > ); > > > 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(); > > > create index latest_record_index on location_records(latest_record) where latest_record is true; > > > insert into location_records(person_id,location_id) values (1,1); > > insert into location_records(person_id,location_id) values (1,2); > > insert into location_records(person_id,location_id) values (1,3); > > > insert into location_records(person_id,location_id) values (2,3); > > insert into location_records(person_id,location_id) values (2,4); > > > select * from location_records; > My personal bias will come out here .... I don't think using a trigger is a good solution here. Although very powerful, the problem with triggers is that they are a 'hidden' side effect which is easily overlooked and often adds an additional maintenance burden which could be avoided using alternative approaches. Consider a few months down the road and your on holidays. One of your colleagues is asked to add a new feature which involves inserting records into this table. During testing, they observe an odd result - a field changing which according to the SQL they wrote should not. The simple new feature now takes twice as long to develop as your colleague works out there is a trigger on the table. Worse yet, they don't notice and put there changes into production and then issue start getting raised about communications going to the wrong location for customers etc. Triggers often become a lot more complicated than they will initially appear. In your example, what happens for updates as opposed to inserts? What happens if the 'new' location is actually the same as a previously recorded location etc. In your case, I would try to make what your doing more explicit and avoid the trigger. There are a number of ways to do this such as - A function to insert the record. The function could check to see if that customer has any previous records and if so, set the boolean flag to false for all existing records and true for the new one. You might even want to break it up into two functions so that you have one which just sets the flag based on a unique key parameter - this would provide a way of resetting the current location without having to do an insert. - Use a timestamp instead of a boolean and change your logic to select the current location by selecting the record with the latest timestamp. - Keep the two actions as separate SQL - one to insert a record and one to set the current location. This has the advantage of making actions clear and easier to maintain and can be useful in domains where people move between locations (for example, I've done this for a University where the data represented the students current address, which would change in and out of semester periods, but often cycle between two addresses, their college and their parental home). The downside of this approach is that applications which insert this information must remember to execute both SQL statements. If you have multiple interfaces, this might become a maintenance burden (one of the advantages of using a DB function). Tim -- Tim Cross