The tuple is updated at the same time when creating functions in postgresql / PL / pgSQL

When initializing my process, it runs the PL / pgSQL statement below, creating two functions. However, every time I create several processes at the same time as part of an end-to-end test, parallel execution of this statement leads to an error tuple concurrently updatedthat I cannot do without. Any help would be greatly appreciated.

CREATE OR REPLACE FUNCTION
  count_rows(schema text, tablename text) returns integer
  AS
  $body$
  DECLARE
    result integer;
    query varchar;
  BEGIN
    query := 'SELECT count(1) FROM "' || schema || '"."' || tablename || '"';
    execute query into result;
    return result;
  END;
  $body$
  LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION
  delete_if_empty(schema text, tablename text) RETURNS INTEGER
  AS
  $$
  DECLARE
    result integer;
    query varchar;
  BEGIN
    query := 'SELECT COUNT(*) FROM "' || schema || '"."' || tablename || '"';
    execute query into result;
    IF result = 0 THEN
      EXECUTE 'DROP TABLE "' || schema || '"."' || tablename || '" CASCADE;';
      EXECUTE 'NOTIFY "' || schema || '", ''DESTROY_TABLE:' || tablename || ''';';
      RETURN 1;
    END IF;
    RETURN 0;
  END;
  $$
  LANGUAGE plpgsql;

  SELECT version()
+4
source share
1 answer

As described here , postgres currently does not allow use CREATE FUNCTIONat the same time:

- , "" ". : , " ".

DDL /, , , , .

, CREATE FUNCTION .

posgres .

: https://vladmihalcea.com/how-do-postgresql-advisory-locks-work/

, :

BEGIN; -- start of transaction

SELECT pg_advisory_xact_lock(2142616474639426746); -- random 64-bit signed ('bigint') lock number

CREATE OR REPLACE FUNCTION myfunction ...

COMMIT;

, . .

+2

Source: https://habr.com/ru/post/1660382/


All Articles