Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added option to allow pg_jobmon to run on a cluster not using the sta…

…ndard port of 5432
  • Loading branch information...
commit f3a2d8e34d26a721ac30b34baa25d384c2c6535d 1 parent f5de66f
Keith Fiske keithf4 authored

Showing 1 changed file with 164 additions and 0 deletions. Show diff stats Hide diff stats

  1. +164 0 updates/pg_jobmon--0.4.2--0.4.3.sql
164 updates/pg_jobmon--0.4.2--0.4.3.sql
... ... @@ -0,0 +1,164 @@
  1 +-- Added port column to dblink_mapping table to allow changing the default port.
  2 +
  3 +ALTER TABLE @extschema@.dblink_mapping ADD COLUMN port text;
  4 +ALTER TABLE @extschema@.dblink_mapping ALTER COLUMN username DROP NOT NULL;
  5 +
  6 +/*
  7 + * dblink Authentication mapping
  8 + */
  9 +CREATE OR REPLACE FUNCTION auth() RETURNS text
  10 + LANGUAGE plpgsql
  11 + AS $$
  12 +DECLARE
  13 +
  14 + v_auth text = '';
  15 + v_port text;
  16 + v_password text;
  17 + v_username text;
  18 +
  19 +BEGIN
  20 + SELECT username, port, pwd INTO v_username, v_port, v_password FROM @extschema@.dblink_mapping;
  21 +
  22 + IF v_port IS NULL THEN
  23 + v_auth = 'dbname=' || current_database();
  24 + ELSE
  25 + v_auth := 'port='||v_port||' dbname=' || current_database();
  26 + END IF;
  27 +
  28 + IF v_username IS NOT NULL THEN
  29 + v_auth := v_auth || ' user='||v_username;
  30 + END IF;
  31 +
  32 + IF v_password IS NOT NULL THEN
  33 + v_auth := v_auth || ' password='||v_password;
  34 + END IF;
  35 + RETURN v_auth;
  36 +END
  37 +$$;
  38 +
  39 +
  40 +/*
  41 + * Add Job
  42 + */
  43 +CREATE OR REPLACE FUNCTION add_job(p_job_name text) RETURNS bigint
  44 + LANGUAGE plpgsql
  45 + AS $$
  46 +DECLARE
  47 + v_job_id bigint;
  48 + v_remote_query text;
  49 + v_dblink_schema text;
  50 +BEGIN
  51 + SELECT nspname INTO v_dblink_schema FROM pg_namespace n, pg_extension e WHERE e.extname = 'dblink' AND e.extnamespace = n.oid;
  52 +
  53 + v_remote_query := 'SELECT @extschema@._autonomous_add_job (' ||
  54 + quote_literal(current_user) || ',' ||
  55 + quote_literal(p_job_name) || ',' ||
  56 + pg_backend_pid() || ')';
  57 +
  58 + EXECUTE 'SELECT job_id FROM ' || v_dblink_schema || '.dblink('||quote_literal(@extschema@.auth())||
  59 + ','|| quote_literal(v_remote_query) || ',TRUE) t (job_id int)' INTO v_job_id;
  60 +
  61 + IF v_job_id IS NULL THEN
  62 + RAISE EXCEPTION 'Job creation failed';
  63 + END IF;
  64 +
  65 + RETURN v_job_id;
  66 +END
  67 +$$;
  68 +
  69 +
  70 +/*
  71 + * Add Step
  72 + */
  73 +CREATE OR REPLACE FUNCTION add_step(p_job_id bigint, p_action text) RETURNS bigint
  74 + LANGUAGE plpgsql
  75 + AS $$
  76 +DECLARE
  77 + v_step_id bigint;
  78 + v_remote_query text;
  79 + v_dblink_schema text;
  80 +
  81 +BEGIN
  82 +
  83 + SELECT nspname INTO v_dblink_schema FROM pg_namespace n, pg_extension e WHERE e.extname = 'dblink' AND e.extnamespace = n.oid;
  84 +
  85 + v_remote_query := 'SELECT @extschema@._autonomous_add_step (' ||
  86 + p_job_id || ',' ||
  87 + quote_literal(p_action) || ')';
  88 +
  89 + EXECUTE 'SELECT step_id FROM ' || v_dblink_schema || '.dblink('||quote_literal(@extschema@.auth())||
  90 + ','|| quote_literal(v_remote_query) || ',TRUE) t (step_id int)' INTO v_step_id;
  91 +
  92 + IF v_step_id IS NULL THEN
  93 + RAISE EXCEPTION 'Job creation failed';
  94 + END IF;
  95 +
  96 + RETURN v_step_id;
  97 +END
  98 +$$;
  99 +
  100 +
  101 +/*
  102 + * Close Job
  103 + */
  104 +CREATE OR REPLACE FUNCTION close_job(p_job_id bigint) RETURNS void
  105 + LANGUAGE plpgsql
  106 + AS $$
  107 +DECLARE
  108 + v_remote_query text;
  109 + v_dblink_schema text;
  110 +BEGIN
  111 +
  112 + SELECT nspname INTO v_dblink_schema FROM pg_namespace n, pg_extension e WHERE e.extname = 'dblink' AND e.extnamespace = n.oid;
  113 +
  114 + v_remote_query := 'SELECT @extschema@._autonomous_close_job('||p_job_id||')';
  115 +
  116 + EXECUTE 'SELECT devnull FROM ' || v_dblink_schema || '.dblink('||quote_literal(@extschema@.auth())||
  117 + ',' || quote_literal(v_remote_query) || ',TRUE) t (devnull int)';
  118 +END
  119 +$$;
  120 +
  121 +
  122 +/*
  123 + * Fail Job
  124 + */
  125 +CREATE OR REPLACE FUNCTION fail_job(p_job_id bigint) RETURNS void
  126 + LANGUAGE plpgsql
  127 + AS $$
  128 +DECLARE
  129 + v_remote_query text;
  130 + v_dblink_schema text;
  131 +BEGIN
  132 +
  133 + SELECT nspname INTO v_dblink_schema FROM pg_namespace n, pg_extension e WHERE e.extname = 'dblink' AND e.extnamespace = n.oid;
  134 +
  135 + v_remote_query := 'SELECT @extschema@._autonomous_fail_job('||p_job_id||')';
  136 +
  137 + EXECUTE 'SELECT devnull FROM ' || v_dblink_schema || '.dblink('||quote_literal(@extschema@.auth())||
  138 + ',' || quote_literal(v_remote_query) || ',TRUE) t (devnull int)';
  139 +
  140 +END
  141 +$$;
  142 +
  143 +
  144 +/*
  145 + * Update Step
  146 + */
  147 +CREATE OR REPLACE FUNCTION update_step(p_step_id bigint, p_status text, p_message text) RETURNS void
  148 + LANGUAGE plpgsql
  149 + AS $$
  150 +DECLARE
  151 + v_remote_query text;
  152 + v_dblink_schema text;
  153 +BEGIN
  154 + SELECT nspname INTO v_dblink_schema FROM pg_namespace n, pg_extension e WHERE e.extname = 'dblink' AND e.extnamespace = n.oid;
  155 +
  156 + v_remote_query := 'SELECT @extschema@._autonomous_update_step ('||
  157 + p_step_id || ',' ||
  158 + quote_literal(p_status) || ',' ||
  159 + quote_literal(p_message) || ')';
  160 +
  161 + EXECUTE 'SELECT devnull FROM ' || v_dblink_schema || '.dblink('||quote_literal(@extschema@.auth())||
  162 + ','|| quote_literal(v_remote_query) || ',TRUE) t (devnull int)';
  163 +END
  164 +$$;

0 comments on commit f3a2d8e

Please sign in to comment.
Something went wrong with that request. Please try again.