Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCOzg-0007nS-1s for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 14:07:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCOze-0000sp-TM for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 14:07:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCOze-0000sV-EP for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 14:07:10 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iCOzc-0002Vq-6R for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 14:07:09 +0000 Received: by mail-pl1-x644.google.com with SMTP id d3so6579645plr.1 for ; Mon, 23 Sep 2019 07:07:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:organization:references:date:in-reply-to :message-id:user-agent:mime-version; bh=dQjbADIFN1/9aAou4387I4BzEpXN1/MhxSSxZcBgEsk=; b=oxe4fl83bknvmcjAP6OrBbVZweIf1vqzwnw3NoFV9pAXGcSRFfqYlU+u0QWmQIlclt GFha2tHxylOGucF3xcMqo16AwYTACe25WcvB35rBr0C6rQxyQdFrMYmXLt7aaYnhyIlT B9cZdp9233mHTppz2m3LeanfXy5WnAQsvlagz7w6LjVDoeoTEHxDdqnSWdOfqdfvQI1e X9wMJzicfsdZ/YycaD0lMzvQKqOgfBp4/bgx2bIeCeU3LvRG95n97d7pYvvmwjE4KcKu mA3mGvJSfzyu3aP6dNeeHDXetIWsS1v3p9V7m3ypmsdPTZqwh7xi+xaoPowe3y6sU/ul /T6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:organization:references:date :in-reply-to:message-id:user-agent:mime-version; bh=dQjbADIFN1/9aAou4387I4BzEpXN1/MhxSSxZcBgEsk=; b=FPL6ZJf8b/wDu3bqHwK8/46cuATe+M0kXDEAovyOBri7nQgBB3Rs7qqxta2dR+LLde c6Y002c2H4i5k2lqvPOF9H9v/NEf/86Cmq0QuGlTjT4Zw7D55eJgNwHcC1tit3L66fQl o7PiWsj4NW9lVUNS9079q/S4wQT76IVmwHX9NlHT6HuHxf/HM1buGYD+gpavIU6bhc0F asWJnMtIxI9zeyGiSYn6Ft1Tl/1e8O1GCKNQy7XUOxpOy0qc6ythKV5uFfN3gtsKky6l 2AuBpOt3kSA3BwOBXZJoxBJfNjH7zStNktqlAEo/11xBjtITBVX8qyF70marM3BP+DzT d9Xg== X-Gm-Message-State: APjAAAUImSla2V2ERYqnUUuF9HO6b04amHRxOgCE2FfEcBA3xp0jjYxG /r93qcH+5VFQDW4JoUPglA== X-Google-Smtp-Source: APXvYqzN1D9aY6jBBecJhMOiZB4MtMNymmuPz6kkCymUljLTOiaDVvBWb3NdpTlqPVc2FYJYAkmaaw== X-Received: by 2002:a17:902:59db:: with SMTP id d27mr30041213plj.253.1569247626845; Mon, 23 Sep 2019 07:07:06 -0700 (PDT) Received: from Pankajs-MacBook-Pro.local ([125.99.219.187]) by smtp.gmail.com with ESMTPSA id v15sm16744647pfn.27.2019.09.23.07.07.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Sep 2019 07:07:06 -0700 (PDT) Received: by Pankajs-MacBook-Pro.local (Postfix, from userid 501) id 3BBF620390EF9C; Mon, 23 Sep 2019 19:37:02 +0530 (IST) From: Pankaj Jangid To: Francisco Olarte Cc: Pankaj Jangid , "pgsql-generallists.postgresql.org" Subject: Re: How to represent a bi-directional list in db? Organization: Code is Great! References: Date: Mon, 23 Sep 2019 19:37:02 +0530 In-Reply-To: (Francisco Olarte's message of "Mon, 23 Sep 2019 10:54:48 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (darwin) MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Francisco Olarte writes: >> Could you please elaborate? Suppose I have this table, >> CREATE TABLE stages ( >> id SERIAL PRIMARY KEY, >> name VARCHAR(80) NOT NULL, >> next_id INTEGER REFERENCE stages NULL, >> ); >> What would be the backward query in that case? Forward is clear. This is >> forward query, >> SELECT name FROM stages WHERE next_id = 123; > > No. That is a BACKWARDS QUERY. You are in row 123, you go BACK to its > preceedeing one. > If you need a traversable list containing (ListID, id,name) = x,1,A; > x,2,b; x,3;c ( I've added the ListId column to make it more > interesting/reallistic, you normally do not have a single table) > In sql you can build a (ListId, id, prev_id, name ) table ( PREV is > easier, as when you insert a row, in a normal application, you know > the previous one, but not the next one ) with the data > (x,1,null,a),(x,2,1,b),(x,3,2,c) ( the last one is a synthetic > sentinel and assumes nullable id), you can do it in a lot of ways. > > To traverse it forward you just querying "select id where listid=x and > next_id is null" to locate the head (1), and then just go forward by > selecting with prev_id = last got id until you hit zero results. > > To traverse backwards there are several ways. In the real cases I've > used I always had a "list row" where I could store the node for the > 1st stage. In that cases i linked them circularly, (id=1, prev=3), so > bidirectional traversing was easy. Or you can use a special sentinel > node ( with a marker, like name=null). The thing is you locate the > last row, and then just query with id=last got prev_id. I do not > remember the details, but probably your "stages" are stages of > something which has a row, which can readily have a "first_stage_id" > or something similar. > > Lists in tables are not the same as in C, where you directly store > pointers which point outwards. In this case any unique data serves as > a pointer, slow ( table scan ) by default, faster if you index the > column. > > Anyway, unless you need the "linked list" functionality for something > ( really heavy manipulation of large stage lists, splicing things > around ), I've normally found it's easier, in sql, to model this kind > of thing with a master-detail + order column. > ( whatever = (id, ...., first_stage_id), stages=(id, order, .... ) ) > Thanks a lot Francisco. This is great help. My stages are stages of processes. So yes processes are also stored in a table. I got the idea. I'll add another column in the processes table which points to the first stage (first_stage_id). And quries Forward pass: 1. select name from stages where id = 2. select name from stages where prev_id = 3. repeat (2) Backward pass: 1. select name from stages where prev_id = 2. select name from stages where id = 3. repeat (2) This is assuming I also create a circular list. I can also store last_stage_id in the process table if we don't want to create circular list in db. Regards. -- Pankaj Jangid