Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNfAm-000332-Pp for pgsql-general@arkaria.postgresql.org; Sat, 20 Mar 2021 17:14:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNfAl-0004CG-OT for pgsql-general@arkaria.postgresql.org; Sat, 20 Mar 2021 17:13:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNfAk-0004A6-AX for pgsql-general@lists.postgresql.org; Sat, 20 Mar 2021 17:13:59 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNfAf-0004ic-D4 for pgsql-general@lists.postgresql.org; Sat, 20 Mar 2021 17:13:57 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 39F6C5C00F3; Sat, 20 Mar 2021 13:13:52 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 20 Mar 2021 13:13:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=K Y8If/KN0shP7/ziBpMcHWSeusalUBgp3NATXHM2avs=; b=KFeOsQs08uKghymJu 43vRFEyLN/RulefTAs32WtTU5QiO5WaTV7XQ1zAFyrew6rHDklQdjmHrL3Vz42hb d9h3qx/RWDzz3E5jjAvrM10pAKhuU0+EqoL3ZoQA8xFeb7cTcltXjpVuK9eUZKGf hzcQ/IsBBfpqxoF6o34ZRbE/6M3t3APUNbfYvBOF0+3g1+V3J6+apPXSJcNDEUAG +RGI0FTaeQ2fZQ5zkDswQmeY5m8VWWhcjv22fRSKDUHOxWdsBNwZTRd9vAh6DM+s Tu/uX5IlF4GilWG4GfEgEdZVXLIa+iNOfzhXeMM53AwFWV8eoYxZatsCmt/AEPFk C+fOg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=KY8If/KN0shP7/ziBpMcHWSeusalUBgp3NATXHM2a vs=; b=Udsshmla9GCBaONzD/pSW5J8C2W1OlJ3eXI0hJwGoGkNg2qtjyF3IB81Q jfy+TWTmwxamu5behRVqkr8AEv73o15qek04xVm6bqhSdLseCO3Q2MAsHlCxe8ri azyp5BhJ4eQdi9EJq+rlIx+vlbUm/pQ60r8SpjeTymV/LVCGecAl/0tz4a9zualK LLDwNOameXqJKdW0302FeAgfFAizn2p8QZ1RO0kD1pY6CbFfA+s55Uzdxpgg/3oU MIDUrlWphs0gcZTpFOPIIGV+GZJ+WxGpfTz7ZUw9FTTOYsuXn8SFEX473QAzx5Wh b9CCBvYeVJOrd+F2Msi7yqPrRnGcg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudegtddgledvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepveevfeevfe eugeffkeekhfegieeufffgteeujedvgffhueejjefhkeejleegtdegnecukfhppeeljedr uddufedrvddujedrvdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.4] (97-113-217-27.tukw.qwest.net [97.113.217.27]) by mail.messagingengine.com (Postfix) with ESMTPA id 2AD94240252; Sat, 20 Mar 2021 13:13:51 -0400 (EDT) Subject: Re: Programmatic Trigger Create To: Niko Ware , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <5566ea3a-e0ae-8595-a13f-ec525e7c9eeb@aklaver.com> Date: Sat, 20 Mar 2021 10:13:50 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/20/21 10:03 AM, Niko Ware wrote: > I would like to programmatically create audit trail functions which are > called by triggers for custom user tables. This will be used for audit > trail generation in our application. The user is able to define a custom > table. Therefore, I need a custom audit trail function. The audit trail > function outputs the row changes in human readable form (e.g., "process > name changed from "my process" to "your process"  by user xxxx on host xxx". > > The basic steps are as follows: > > 1. User defines the table > 2. Create table via  EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd > 3. Programatically constructs the audit trail function for > insert/update/delete. > 4. Create the function via EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd > 5. Create the trigger via EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd > > Here is an example in function source which is passed to "EXEC SQL > EXECUTE IMMEDIATE" via char*: In what program is: EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd being done? > > > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 > 11 > 12 > > CREATE OR REPLACE FUNCTION name_changes_log() > RETURNS TRIGGER AS > $BODY$ > BEGIN > IF NEW.name <> OLD.name THEN > INSERT INTO kids_audit(kids_id,kids_name,modified_on) > VALUES(OLD.id,OLD.name,now()); > END IF; > > RETURN NEW; > END; > $BODY$ LANGUAGE plpgsql; > > > The "execute immediate" succeeds, but the function is not created. The > application is connected to the database as a user which has permission > to create functions. > > I could output the function text to a file and then use "psql" to > process the "create trigger", but this seems a bit hacky. > > Thanks in advance, > Thomas > > > -- Adrian Klaver adrian.klaver@aklaver.com