Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYNkP-0001da-I1 for pgsql-general@arkaria.postgresql.org; Mon, 19 Apr 2021 06:51:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lYNkO-0003MN-3M for pgsql-general@arkaria.postgresql.org; Mon, 19 Apr 2021 06:51:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYNkN-0003MG-PU for pgsql-general@lists.postgresql.org; Mon, 19 Apr 2021 06:51:03 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lYNkK-0001hH-It for pgsql-general@lists.postgresql.org; Mon, 19 Apr 2021 06:51:02 +0000 Received: by mail-pl1-x635.google.com with SMTP id n10so6011269plc.0 for ; Sun, 18 Apr 2021 23:51:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:date:in-reply-to:message-id :mime-version; bh=pTKHFWET4sprCRTk/mPkEpjX/3/fMJY1pHTUUriP+6Q=; b=VWgmqcoso1iGXpMfZPVz1XX8SUZWwK8LRzjvrMEFhBJlAy8xheXBig9GhxuS7HAJ21 Q6bieD2+A6OZGgUUcCEgBPGXTrwvgAqQQkkIH6Gp/ad9GUBvqR1R/1dFwAyG2n9caC9F BVdVedtLkzyOVJLEuXqIhhek31uZRoMzj1D0gmH/CXbukiZ0N0+Db/wlseItEd5HXfwV yM9WDH3lYo5XNkYgPp9EqGWOxSW9IHhRyAOiQTdx0Q0P1sOlrMVbNSaOIrBt+wkuLD14 8ek1pCCLovl+6nW9qNS/lJxKvpxOCwTajy5d6Z75H24/TZWTvgQO2sZHPXgOK6Wk9Zc6 41Ow== 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:subject:date :in-reply-to:message-id:mime-version; bh=pTKHFWET4sprCRTk/mPkEpjX/3/fMJY1pHTUUriP+6Q=; b=jzc2/PfPyoJIike9QX7n6YSDTbHblFoQ2c/0YqHiqhxpZDQTg7Bxth10SAtsB0jdFM u9Lgi9pIBzCL7Al67cGafiVA0kNx98Bq+tuaHgoI8MnHa6F/Vd+eEjtbVqKc+m1ErGGj da8zGlOGS7kYdr1Sk+vfe5qvNHvoODFLpMya0X5GUVSJBs75PQnhzISVKSI5/6P1ZC72 XUXSL+k/iN4QcQV/WcPjupaXGo2TFfLESp4woTZbJvE4FheHNaClK9ZgfKPYwJUtly7Z 2XqvZNBgHkQgXzgjeEODwyYScxwNMbdx6OW9wjSDANgl6GzPAgNC59ZOyJ3wu/nNgmZu xbMA== X-Gm-Message-State: AOAM531riVKbwCPINZ+ypS6eXE8uqarzQKLxlV0oxE9Cplts8agqa6a5 5umdGIlwZzf1q6CW+7VCtEKUsn+vLCg= X-Google-Smtp-Source: ABdhPJz6MT0j8/CDiOA941cJzss2VlEEoykGzZS6XKXJpKevN/w1Du6I5EYhDT9lAGS4NuaKNGEyYg== X-Received: by 2002:a17:90a:684b:: with SMTP id e11mr9214564pjm.87.1618815058281; Sun, 18 Apr 2021 23:50:58 -0700 (PDT) Received: from tim-desktop (220-235-1-83.dyn.iinet.net.au. [220.235.1.83]) by smtp.gmail.com with ESMTPSA id 20sm11527326pfw.40.2021.04.18.23.50.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Apr 2021 23:50:57 -0700 (PDT) References: User-agent: mu4e 1.5.11; emacs 28.0.50 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Storing state machine Date: Mon, 19 Apr 2021 16:27:15 +1000 In-reply-to: Message-ID: <87y2de7ooi.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Mohan Radhakrishnan 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.