<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Vertica Documentation – Triggers</title>
    <link>/en/extending/stored-procedures/executing-stored-procedures/triggers/</link>
    <description>Recent content in Triggers on Vertica Documentation</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/extending/stored-procedures/executing-stored-procedures/triggers/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Extending: Scheduled execution</title>
      <link>/en/extending/stored-procedures/executing-stored-procedures/triggers/scheduled-execution/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/extending/stored-procedures/executing-stored-procedures/triggers/scheduled-execution/</guid>
      <description>
        
        
        &lt;p&gt;Stored procedures can be scheduled to execute automatically with the privileges of the trigger definer. You can use this to automate various tasks, like logging database activity, revoking privileges, or creating roles.&lt;/p&gt;
&lt;h2 id=&#34;enabling-and-disabling-scheduling&#34;&gt;Enabling and disabling scheduling&lt;/h2&gt;
&lt;p&gt;Scheduling can be toggled at the database level with the &lt;a href=&#34;../../../../../en/sql-reference/config-parameters/stored-procedure-parameters/&#34;&gt;EnableStoredProcedureScheduler&lt;/a&gt; configuration parameter:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;-- Enable scheduler
=&amp;gt; SELECT SET_CONFIG_PARAMETER(&amp;#39;EnableStoredProcedureScheduler&amp;#39;, 1);

-- Disable scheduler
=&amp;gt; SELECT SET_CONFIG_PARAMETER(&amp;#39;EnableStoredProcedureScheduler&amp;#39;, 0);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can toggle an individual schedule &lt;a href=&#34;../../../../../en/sql-reference/functions/stored-procedure-functions/enable-trigger/&#34;&gt;ENABLE_TRIGGER&lt;/a&gt;, or disable it by &lt;a href=&#34;../../../../../en/sql-reference/statements/drop-statements/drop-trigger/&#34;&gt;dropping&lt;/a&gt; the schedule&#39;s associated trigger.&lt;/p&gt;
&lt;h2 id=&#34;scheduling-a-stored-procedure&#34;&gt;Scheduling a stored procedure&lt;/h2&gt;
&lt;p&gt;The general workflow for implementing scheduled execution for a single stored procedure is as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../../en/sql-reference/statements/create-statements/create-procedure-stored/&#34;&gt;Create&lt;/a&gt; a stored procedure.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../../en/sql-reference/statements/create-statements/create-schedule/&#34;&gt;Create&lt;/a&gt; a schedule. A schedule can either use a list of timestamps for one-off triggers or a &lt;code&gt;cron&lt;/code&gt; expression for recurring events.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../../en/sql-reference/statements/create-statements/create-trigger/&#34;&gt;Create&lt;/a&gt; a trigger, associating it with the stored procedure and trigger.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;(Optional) &lt;a href=&#34;../../../../../en/sql-reference/functions/stored-procedure-functions/execute-trigger/&#34;&gt;Manually execute the trigger&lt;/a&gt; to test it.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;one-off-triggers&#34;&gt;One-off triggers&lt;/h3&gt;
&lt;p&gt;One-off triggers run a finite number of times.&lt;/p&gt;
&lt;p&gt;The following example creates a trigger that revokes privileges on the customer_dimension table from the user Bob after 24 hours:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a stored procedure to &lt;a href=&#34;../../../../../en/sql-reference/statements/revoke-statements/revoke-table/&#34;&gt;revoke&lt;/a&gt; privileges from Bob:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE PROCEDURE revoke_all_on_table(table_name VARCHAR, user_name VARCHAR)
LANGUAGE PLvSQL
AS $$
BEGIN
    EXECUTE &amp;#39;REVOKE ALL ON &amp;#39; || QUOTE_IDENT(table_name) || &amp;#39; FROM &amp;#39; || QUOTE_IDENT(user_name);
END;
$$;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a schedule with a timestamp for 24 hours later:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE SCHEDULE 24_hours_later USING DATETIMES(&amp;#39;2022-12-16 12:00:00&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a trigger with the stored procedure and schedule:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TRIGGER revoke_trigger ON SCHEDULE 24_hours_later EXECUTE PROCEDURE revoke_all_on_table(&amp;#39;customer_dimension&amp;#39;, &amp;#39;Bob&amp;#39;) AS DEFINER;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;recurring-triggers&#34;&gt;Recurring triggers&lt;/h3&gt;
&lt;p&gt;Recurring triggers run at a recurring date or time.&lt;/p&gt;
&lt;p&gt;The following example creates a weekly trigger that logs to the USER_COUNT table the number of users in the database:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM USER_COUNT;
  total  |           timestamp
---------+-------------------------------
     293 | 2022-12-04 00:00:00.346664-00
     302 | 2022-12-11 00:00:00.782242-00
     301 | 2022-12-18 00:00:00.144633-00
     301 | 2022-12-25 00:00:00.548832-00
(4 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create the table to log the user counts:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE USER_COUNT(total INT, timestamp TIMESTAMPTZ)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create the stored procedure to log to the table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE PROCEDURE log_user_count()
LANGUAGE PLvSQL
AS $$
DECLARE
    num_users int := SELECT count (user_id) FROM users;
    timestamp datetime := SELECT NOW();
BEGIN
    PERFORM INSERT INTO USER_COUNT VALUES(num_users, timestamp);
END;
$$;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create the schedule for 12:00 AM on Sunday:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE SCHEDULE weekly_sunday USING CRON &amp;#39;0 0 * * 0&amp;#39;;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create the trigger with the stored procedure and schedule:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TRIGGER user_log_trigger ON SCHEDULE weekly_sunday EXECUTE PROCEDURE log_user_count() AS DEFINER;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;viewing-upcoming-schedules&#34;&gt;Viewing upcoming schedules&lt;/h2&gt;
&lt;p&gt;Schedules are managed and coordinated by the Active Scheduler Node (ASN). If the ASN goes down, a different node is automatically designated as the new ASN. To view scheduled tasks, query &lt;a href=&#34;../../../../../en/sql-reference/system-tables/v-catalog-schema/scheduler-time-table/&#34;&gt;SCHEDULER_TIME_TABLE&lt;/a&gt; on the ASN.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Determine the ASN with &lt;a href=&#34;../../../../../en/sql-reference/functions/stored-procedure-functions/active-scheduler-node/&#34;&gt;ACTIVE_SCHEDULER_NODE&lt;/a&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT active_scheduler_node();
active_scheduler_node
-----------------------
 initiator
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On the ASN, query &lt;a href=&#34;../../../../../en/sql-reference/system-tables/v-catalog-schema/scheduler-time-table/&#34;&gt;SCHEDULER_TIME_TABLE&lt;/a&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM scheduler_time_table;

  schedule_name  | attached_trigger | scheduled_execution_time
-----------------+------------------+--------------------------
 daily_1am_gmt   | log_user_actions | 2022-12-15 01:00:00-00
 24_hours_later  | revoke_trigger   | 2022-12-16 12:00:00-00
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

      </description>
    </item>
    
  </channel>
</rss>
