PostgreSQL Locking Module Provider

The PostgreSQL Locking Module Provider uses PostgreSQL's advisory locks to control and manage locks across multiple instances of Medusa. Advisory locks are lightweight locks that do not interfere with other database transactions. By using PostgreSQL's advisory locks, Medusa can create distributed locks directly through the database.

The provider uses the existing PostgreSQL database in your application to manage locks, so you don't need to set up a separate database or service to manage locks.

NoteWhile this provider is suitable for production environments, it's recommended to use the Redis Locking Module Provider if possible.

Register the PostgreSQL Locking Module Provider#

To register the PostgreSQL Locking Module Provider, add it to the list of providers of the Locking Module in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "@medusajs/medusa/locking",6      options: {7        providers: [8          {9            resolve: "@medusajs/medusa/locking-postgres",10            id: "postgres-lock",11            // set this if you want this provider to be used by default12            // and you have other Locking Module Providers registered.13            is_default: true,14          },15        ]16      }17    }18  ]19})

Run Migrations#

The PostgreSQL Locking Module Provider requires a new locking table in the database to store the locks. So, you must run the migrations after registering the provider:

Terminal
npx medusa db:migrate

This will run the migration in the PostgreSQL Locking Module Provider and create the necessary table in the database.


Use Provider with Locking Module#

The PostgreSQL Locking Module Provider will be the default provider if you don't register any other providers, or if you set the is_default flag to true:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "@medusajs/medusa/locking",6      options: {7        providers: [8          {9            resolve: "@medusajs/medusa/locking-postgres",10            id: "postgres-lock",11            is_default: true,12          },13        ]14      }15    }16  ]17})

If you use the Locking Module in your customizations, the PostgreSQL Locking Module Provider will be used by default in this case. You can also explicitly use this provider by passing its identifier lp_locking-postgres to the Locking Module's service methods.

For example, when using the acquire method in a workflow step:

Code
1import { Modules } from "@medusajs/framework/utils"2import { createStep } from "@medusajs/framework/workflows-sdk"3
4const step1 = createStep(5  "step-1",6  async ({}, { container }) => {7    const lockingModuleService = container.resolve(8      Modules.LOCKING9    )10
11    await lockingModuleService.acquire("prod_123", {12      provider: "lp_locking-postgres",13    })14  } 15)
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break