Sgrùdadh air loidsig gnìomhachais a chuir an gnìomh aig ìre gnìomhan stòr PostgreSQL

B’ e an artaigil a bha na bhrosnachadh airson an sgeidse seo a sgrìobhadh “Rè cuarantine, mheudaich an eallach obrach 5 tursan, ach bha sinn deiseil.” Mar a ghluais Lingualeo gu PostgreSQL le 23 millean neach-cleachdaidh. Fhuair mi cuideachd an artaigil a chaidh fhoillseachadh o chionn 4 bliadhna inntinneach - Cur an gnìomh loidsig gnìomhachais ann am MySQL.

Bha e inntinneach gun robh an aon bheachd - "cuir an gnìomh loidsig gnìomhachais anns an stòr-dàta".

Sgrùdadh air loidsig gnìomhachais a chuir an gnìomh aig ìre gnìomhan stòr PostgreSQL

Cha b’ e mise a-mhàin a thàinig nam inntinn.

Cuideachd, airson an ama ri teachd, bha mi airson a bhith a 'gleidheadh, an toiseach, dhomh fhìn, leasachaidhean inntinneach a dh' èirich nuair a bha iad air an cur an gnìomh. Gu sònraichte leis gun deach co-dhùnadh ro-innleachdail a dhèanamh o chionn ghoirid an ailtireachd atharrachadh agus loidsig gnìomhachais a ghluasad chun ìre cùl-fhiosrachaidh. Gus nach bi a h-uile dad a chaidh a leasachadh gu feum sam bith do dhuine sam bith agus nach bi ùidh aig duine sam bith ann.

Chan eil na dòighean a chaidh a mhìneachadh mar sheòrsa de lorg no sònraichte. fios agad ciamar, tha a h-uile dad clasaigeach agus chaidh a chuir an gnìomh grunn thursan (mar eisimpleir, chleachd mi dòigh-obrach coltach ris 20 bliadhna air ais air Oracle) Cho-dhùin mi a h-uile càil a chruinneachadh ann an aon àite. Air eagal gu bheil e feumail dha cuideigin. Mar a tha cleachdadh air sealltainn, gu math tric bidh an aon bheachd a’ tighinn gu diofar dhaoine gu neo-eisimeileach. Agus tha e feumail a chumail dhut fhèin mar chuimhneachan.

Gu dearbh, chan eil dad san t-saoghal seo foirfe, gu mì-fhortanach tha mearachdan agus typos comasach. Thathas a' cur fàilte mhòr air càineadh agus beachdan, agus aon mhion-fhiosrachadh eile - tha mion-fhiosrachadh sònraichte air a chur an gnìomh air a fàgail a-mach. Ach, tha a h-uile dad fhathast air a chleachdadh ann am fìor phròiseact obrach. Mar sin, chan eil anns an artaigil ach sgeidse agus tuairisgeul air a’ bhun-bheachd coitcheann, gun dad a bharrachd. Tha mi an dòchas gu bheil mion-fhiosrachadh gu leòr ann airson an dealbh iomlan a thuigsinn.

Is e am beachd coitcheann “roinn agus ceannsachadh, falach agus sealbhachadh”

Tha am beachd clasaigeach - sgeama air leth airson bùird, sgeama air leth airson gnìomhan air an stòradh.
Chan eil cothrom aig an neach-dèiligidh air an dàta gu dìreach. Chan urrainn don neach-dèiligidh ach an gnìomh a tha air a stòradh a ghairm agus am freagairt a gheibhear a phròiseasadh.

Dreuchdan

CREATE ROLE store;

CREATE ROLE sys_functions;

CREATE ROLE loc_audit_functions;

CREATE ROLE service_functions;

CREATE ROLE business_functions;

Sgeamaichean

Sgeama stòraidh clàr

Clàran targaid a tha a 'cur an gnìomh aonadan cuspaireil.

CREATE SCHEMA store AUTHORIZATION store ;

Diagram gnìomh siostam

Gnìomhan siostam, gu sònraichte airson atharrachaidhean clàr logaidh.

CREATE SCHEMA sys_functions AUTHORIZATION sys_functions ;

Sgeama sgrùdaidh ionadail

Gnìomhan agus clàran gus sgrùdadh ionadail a chuir an gnìomh air coileanadh gnìomhan stòraichte agus atharrachaidhean air clàran targaid.

CREATE SCHEMA loc_audit_functions AUTHORIZATION loc_audit_functions;

Diagram gnìomh seirbheis

Gnìomhan airson seirbheis agus gnìomhan DML.

CREATE SCHEMA service_functions AUTHORIZATION service_functions;

Diagram gnìomh gnìomhachais

Gnìomhan airson gnìomhan gnìomhachais deireannach ris an canar an neach-dèiligidh.

CREATE SCHEMA business_functions AUTHORIZATION business_functions;

Còraichean ruigsinneachd

Ròl - DBA tha làn chothrom aige air a h-uile sgeama (sgaraichte bho dhreuchd sealbhadair DB).

CREATE ROLE dba_role;
GRANT store TO dba_role;
GRANT sys_functions TO dba_role;
GRANT loc_audit_functions TO dba_role;
GRANT service_functions TO dba_role;
GRANT business_functions TO dba_role;

Ròl - CLEACHDADH aig a bheil an t-sochair GNÌOMH anns an diagram gnìomhan_gnothachais.

CREATE ROLE user_role;

Sochairean eadar sgeamaichean

TABHARTAS
Leis gu bheil a h-uile gnìomh air a chruthachadh leis a ’ghnè DEARBH-BHEACHD SHLAINTEIL stiùireadh a dhìth AIRSON A H-UILE gnìomh… Bhon phoball;

REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA sys_functions FROM public ; 
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA  loc_audit_functions  FROM public ; 
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA  service_functions FROM public ; 
REVOKE EXECUTE ON ALL FUNCTION IN SCHEMA  business_functions FROM public ; 

GRANT USAGE ON SCHEMA sys_functions TO dba_role ; 
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA sys_functions TO dba_role ;
GRANT USAGE ON SCHEMA loc_audit_functions  TO dba_role ; 
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA loc_audit_functions  TO dba_role ;
GRANT USAGE ON SCHEMA service_functions TO dba_role ; 
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA service_functions TO dba_role ;
GRANT USAGE ON SCHEMA business_functions TO dba_role ; 
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA business_functions TO dba_role ;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA business_functions TO user_role ;

GRANT ALL PRIVILEGES ON SCHEMA store TO GROUP business_functions ;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA store TO business_functions ;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA store TO business_functions ;

Mar sin tha sgeama an stòr-dàta deiseil. Faodaidh tu tòiseachadh air an dàta a lìonadh.

Clàran targaid

Tha cruthachadh bùird glè bheag. Gun fheartan sònraichte, ach a-mhàin gun deach co-dhùnadh gun a chleachdadh SRAID agus cruthaich sreathan gu soilleir. A bharrachd air an sin, gu dearbh, an cleachdadh as motha de stiùiridhean

COMMENT ON ...

Beachdan airson всех nithean, gun eisgeachd.

Sgrùdadh ionadail

Gus coileanadh gnìomhan stòraichte agus atharrachaidhean air clàran targaid a chlàradh, thathas a’ cleachdadh clàr sgrùdaidh ionadail, a tha a ’toirt a-steach, am measg rudan eile, mion-fhiosrachadh mun cheangal teachdaiche, bileag a’ mhodal ris an canar, agus fìor luachan an cuir a-steach agus paramadairean toraidh ann an cruth JSON.

Feartan siostam

Dealbhaichte airson atharrachaidhean a chlàradh ann an clàran targaid. Tha iad nan gnìomhan brosnachaidh.

Teamplaid - gnìomh siostam

---------------------------------------------------------
-- INSERT
CREATE OR REPLACE FUNCTION sys_functions.table_insert_log ()
RETURNS TRIGGER AS $$
BEGIN
  PERFORM loc_audit_functions.make_log( ' '||'table' , 'insert' , json_build_object('id', NEW.id)  );
  RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE TRIGGER table_after_insert AFTER INSERT ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_insert_log();

---------------------------------------------------------
-- UPDATE
CREATE OR REPLACE FUNCTION sys_functions.table_update_log ()
RETURNS TRIGGER AS $$
BEGIN
  IF OLD.column != NEW.column
  THEN
    PERFORM loc_audit_functions.make_log( ' '||'table' , 'update' , json_build_object('OLD.column', OLD.column , 'NEW.column' , NEW.column )  );
  END IF ;
  RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE TRIGGER table_after_update AFTER UPDATE ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_update_log ();

---------------------------------------------------------
-- DELETE
CREATE OR REPLACE FUNCTION sys_functions.table_delete_log ()
RETURNS TRIGGER AS $$
BEGIN
  PERFORM loc_audit_functions.make_log( ' '||'table' , 'delete' , json_build_object('id', OLD.id )  );
  RETURN NULL ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE TRIGGER table_after_delete AFTER DELETE ON storage.table FOR EACH ROW EXECUTE PROCEDURE sys_functions.table_delete_log ();

Gnìomhan seirbheis

Air a dhealbhadh gus gnìomhachd seirbheis agus DML a chuir an gnìomh air bùird targaid.

Teamplaid - seirbheis seirbheis

--INSERT
--RETURN id OF NEW ROW
CREATE OR REPLACE FUNCTION service_functions.table_insert ( new_column store.table.column%TYPE )
RETURNS integer AS $$
DECLARE
  new_id integer ;
BEGIN
  -- Generate new id
  new_id = nextval('store.table.seq');

  -- Insert into table
  INSERT INTO store.table
  ( 
    id ,
    column
   )
  VALUES
  (
   new_id ,
   new_column
   );

RETURN new_id ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

--DELETE
--RETURN ROW NUMBERS DELETED
CREATE OR REPLACE FUNCTION service_functions.table_delete ( current_id integer ) 
RETURNS integer AS $$
DECLARE
  rows_count integer  ;    
BEGIN
  DELETE FROM store.table WHERE id = current_id; 

  GET DIAGNOSTICS rows_count = ROW_COUNT;                                                                           

  RETURN rows_count ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;
 
-- UPDATE DETAILS
-- RETURN ROW NUMBERS UPDATED
CREATE OR REPLACE FUNCTION service_functions.table_update_column 
(
  current_id integer 
  ,new_column store.table.column%TYPE
) 
RETURNS integer AS $$
DECLARE
  rows_count integer  ; 
BEGIN
  UPDATE  store.table
  SET
    column = new_column
  WHERE id = current_id;

  GET DIAGNOSTICS rows_count = ROW_COUNT;                                                                           

  RETURN rows_count ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

Gnìomhan gnìomhachais

Dealbhaichte airson gnìomhan gnìomhachais deireannach ris an canar an neach-dèiligidh. Bidh iad daonnan a 'tilleadh - JSON. Gus casg a chuir air agus logadh a-steach mearachdan gnìomh, cleachd am bloca FIOSRACHADH.

Teamplaid - gnìomh gnìomhachais

CREATE OR REPLACE FUNCTION business_functions.business_function_template(
--Input parameters        
 )
RETURNS JSON AS $$
DECLARE
  ------------------------
  --for exception catching
  error_message text ;
  error_json json ;
  result json ;
  ------------------------ 
BEGIN
--LOGGING
  PERFORM loc_audit_functions.make_log
  (
    'business_function_template',
    'STARTED',
    json_build_object
    (
	--IN Parameters
    ) 
   );

  PERFORM business_functions.notice('business_function_template');            

  --START BUSINESS PART
  --END BUSINESS PART

  -- SUCCESFULLY RESULT
  PERFORM business_functions.notice('result');
  PERFORM business_functions.notice(result);

  PERFORM loc_audit_functions.make_log
  (
    'business_function_template',
    'FINISHED', 
    json_build_object( 'result',result )
  );

  RETURN result ;
----------------------------------------------------------------------------------------------------------
-- EXCEPTION CATCHING
EXCEPTION                        
  WHEN OTHERS THEN    
    PERFORM loc_audit_functions.make_log
    (
      'business_function_template',
      'STARTED',
      json_build_object
      (
	--IN Parameters	
      ) , TRUE );

     PERFORM loc_audit_functions.make_log
     (
       'business_function_template',
       ' ERROR',
       json_build_object('SQLSTATE',SQLSTATE ), TRUE 
     );

     PERFORM loc_audit_functions.make_log
     (
       'business_function_template',
       ' ERROR',
       json_build_object('SQLERRM',SQLERRM  ), TRUE 
      );

     GET STACKED DIAGNOSTICS error_message = RETURNED_SQLSTATE ;
     PERFORM loc_audit_functions.make_log
     (
      'business_function_template',
      ' ERROR-RETURNED_SQLSTATE',json_build_object('RETURNED_SQLSTATE',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = COLUMN_NAME ;
     PERFORM loc_audit_functions.make_log
     (
       'business_function_template',
       ' ERROR-COLUMN_NAME',
       json_build_object('COLUMN_NAME',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = CONSTRAINT_NAME ;
     PERFORM loc_audit_functions.make_log
     (
      'business_function_template',
      ' ERROR-CONSTRAINT_NAME',
      json_build_object('CONSTRAINT_NAME',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = PG_DATATYPE_NAME ;
     PERFORM loc_audit_functions.make_log
     (
       'business_function_template',
       ' ERROR-PG_DATATYPE_NAME',
       json_build_object('PG_DATATYPE_NAME',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = MESSAGE_TEXT ;
     PERFORM loc_audit_functions.make_log
     (
       'business_function_template',
       ' ERROR-MESSAGE_TEXT',json_build_object('MESSAGE_TEXT',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = SCHEMA_NAME ;
     PERFORM loc_audit_functions.make_log
     (s
       'business_function_template',
       ' ERROR-SCHEMA_NAME',json_build_object('SCHEMA_NAME',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_DETAIL ;
     PERFORM loc_audit_functions.make_log
     (
      'business_function_template',
      ' ERROR-PG_EXCEPTION_DETAIL',
      json_build_object('PG_EXCEPTION_DETAIL',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_HINT ;
     PERFORM loc_audit_functions.make_log
     (
       'business_function_template',
       ' ERROR-PG_EXCEPTION_HINT',json_build_object('PG_EXCEPTION_HINT',error_message  ), TRUE );

     GET STACKED DIAGNOSTICS error_message = PG_EXCEPTION_CONTEXT ;
     PERFORM loc_audit_functions.make_log
     (
      'business_function_template',
      ' ERROR-PG_EXCEPTION_CONTEXT',json_build_object('PG_EXCEPTION_CONTEXT',error_message  ), TRUE );                                      

    RAISE WARNING 'ALARM: %' , SQLERRM ;

    SELECT json_build_object
    (
      'isError' , TRUE ,
      'errorMsg' , SQLERRM
     ) INTO error_json ;

  RETURN  error_json ;
END
$$ LANGUAGE plpgsql SECURITY DEFINER;

An toradh

Gus cunntas a thoirt air an dealbh choitcheann, tha mi a’ smaoineachadh gu bheil e ceart gu leòr. Ma tha ùidh aig duine sam bith anns na mion-fhiosrachadh agus na toraidhean, sgrìobh beachdan, bidh mi toilichte suathadh a bharrachd a chuir ris an dealbh.

PS

Logadh mearachd sìmplidh - seòrsa paramadair cuir a-steach

-[ RECORD 1 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1072
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          | STARTED
jsonb_pretty    | {
                |     "dko": {
                |         "id": 4,
                |         "type": "Type1",                                                                                                                                                                                            
                |         "title": "CREATED BY addKD",
                |         "Weight": 10,
                |         "Tr": "300",
                |         "reduction": 10,
                |         "isTrud": "TRUE",
                |         "description": "decription",
                |         "lowerTr": "100",
                |         "measurement": "measurement1",
                |         "methodology": "m1",                                                                                                                                                                                           
                |         "passportUrl": "files",
                |         "upperTr": "200",
                |         "weightingFactor": 100.123,
                |         "actualTrValue": null,
                |         "upperTrCalcNumber": "120"
                |     },
                |     "CardId": 3
                | }
-[ RECORD 2 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1073
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR
jsonb_pretty    | {
                |     "SQLSTATE": "22P02"
                | }
-[ RECORD 3 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1074
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR
jsonb_pretty    | {
                |     "SQLERRM": "invalid input syntax for type numeric: "null""
                | }
-[ RECORD 4 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1075
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-RETURNED_SQLSTATE
jsonb_pretty    | {
                |     "RETURNED_SQLSTATE": "22P02"
                | }
-[ RECORD 5 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1076
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-COLUMN_NAME
jsonb_pretty    | {
                |     "COLUMN_NAME": ""
                | }

-[ RECORD 6 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1077
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-CONSTRAINT_NAME
jsonb_pretty    | {
                |     "CONSTRAINT_NAME": ""
                | }
-[ RECORD 7 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1078
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-PG_DATATYPE_NAME
jsonb_pretty    | {
                |     "PG_DATATYPE_NAME": ""
                | }
-[ RECORD 8 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1079
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-MESSAGE_TEXT
jsonb_pretty    | {
                |     "MESSAGE_TEXT": "invalid input syntax for type numeric: "null""
                | }
-[ RECORD 9 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1080
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-SCHEMA_NAME
jsonb_pretty    | {
                |     "SCHEMA_NAME": ""
                | }
-[ RECORD 10 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1081
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-PG_EXCEPTION_DETAIL
jsonb_pretty    | {
                |     "PG_EXCEPTION_DETAIL": ""
                | }
-[ RECORD 11 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1082
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-PG_EXCEPTION_HINT
jsonb_pretty    | {
                |     "PG_EXCEPTION_HINT": ""
                | }
-[ RECORD 12 ]-
date_trunc      | 2020-08-19 13:15:46
id              | 1083
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-PG_EXCEPTION_CONTEXT
jsonb_pretty    | {
usename         | emp1
log_module      | addKD
log_module_hash | 0b4c1529a89af3ddf6af3821dc790e8a
status          |  ERROR-MESSAGE_TEXT
jsonb_pretty    | {
                |     "MESSAGE_TEXT": "invalid input syntax for type numeric: "null""
                | }

Source: www.habr.com

Cuir beachd ann