pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tim Cross <theophilusx@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Storing state machine
Date: Mon, 19 Apr 2021 16:27:15 +1000
Message-ID: <87y2de7ooi.fsf@gmail.com> (raw)
In-Reply-To: <CAOoXFP_=OWmOoyzb8iUN3wHdhTgL-qxFJiHki9veTO-NFRQaqw@mail.gmail.com>
References: <CAOoXFP_=OWmOoyzb8iUN3wHdhTgL-qxFJiHki9veTO-NFRQaqw@mail.gmail.com>


Mohan Radhakrishnan <radhakrishnan.mohan@gmail.com> writes:

> Hello,
>            We have a workflow when we receive events into the service. But we don't have a way to choreograph or orchestrate the workflow. The
> services are all independent and receive and respond to events.
>
> Since there is no order imposed by the event queues I was thinking of storing a simple state machine in the table.
>
> 1. Order PENDING
> 2. Order line 1 PENDING 
> 3. Order line 2 PENDING 
> 4. Order line 1 PROCESSED
> 5. Order line 2 PROCESSED 
> 6. Order PROCESSED
>
> Order and Order lines can be PROCESSED in any order. But at the end all lines should be  PROCESSED and then the order is also  PROCESSED.
> I won't be able to use any PostgreSql functions because we don't depend on those features.
>
> Are there any PostgreSql features that could support this pattern ? Is it just like any other eventually consistent pattern ?
>   

What you appear to have here is two entities - orders and order items.
An order entity has a 'state' (pending/processed) and is linked to 
one or more order items which in turn have a state.

The information about order state could be derived rather than actually
stored i.e. an order is pending if any of its order items are pending
and is processed if all of its order items are processed. At a minimum,
storing the order item state would be sufficient and a basic sql
statement would be able to tell you what the state of an order is.

In general, you don't want to store duplicate or redundant information
as this can be a source of anomalies. (e.g. order state is not updated
to 'processed' when all items are processed or is updated to processed,
but then another item is added and for some reason, the state is not
switched back to pending etc).

in general, it is usually a mistake or poor design to use one table to
represent different 'entities'. That is a 'bit bucket' approach which
really degrades the ability of the database to do what it is good at -
managing entities and their relationships.



view thread (4+ messages)  latest in thread

Message-ID: <87y2de7ooi.fsf@gmail.com>
Permalink:  ../87y2de7ooi.fsf@gmail.com/
Also on:    postgresql.org/message-id/87y2de7ooi.fsf@gmail.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: theophilusx@gmail.com
  Subject: Re: Storing state machine
  In-Reply-To: <87y2de7ooi.fsf@gmail.com>
  References: <CAOoXFP_=OWmOoyzb8iUN3wHdhTgL-qxFJiHki9veTO-NFRQaqw@mail.gmail.com> <87y2de7ooi.fsf@gmail.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