Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gTs2C-0002sH-Mi for pgsql-general@arkaria.postgresql.org; Mon, 03 Dec 2018 17:29:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gTs2B-0007nk-Cs for pgsql-general@arkaria.postgresql.org; Mon, 03 Dec 2018 17:29:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gTs29-0007nd-MH for pgsql-general@lists.postgresql.org; Mon, 03 Dec 2018 17:29:27 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gTs25-0008Tj-3z for pgsql-general@postgresql.org; Mon, 03 Dec 2018 17:29:25 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 38BE022D52; Mon, 3 Dec 2018 12:29:19 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 03 Dec 2018 12:29:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=h DDntkoUH04zk5Hc3pK9J+1PIDuwSS7iJaCbOdSdbzc=; b=UAxn8EbZfvrysjt4u 9F4T/u20fhFtYgakrImE5QXLsjDUQGpF86+VlPMcXGh6DsoQMGugMtqSDB4+l0al ydq7JCVKVASic+Q+T6pqcB0TMkHwKt6/buPCfU+siCsBje/VILh+yC2GlHQ+56Pa EP0Th1hRfAyR/cYHPVwngVa/uAFMr5Kj2InlY6kwl8Ah0JhlvuFWTq5iRwhEG7Ef qwjXecZEJU9KYi1GeEFLvS8X+pCvPu+aO+CtN8wbGLKAb9M+PAIugEo8d5h2wbnA VLclgozPmeM1fpH3nNgGLAXIQMAWQSEojr2toJ+lflMCuCkEwxYPQqdeuv+hcjTR un7Ww== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=fm1; bh=hDDntkoUH04zk5Hc3pK9J+1PIDuwSS7iJaCbOdSdb zc=; b=ug4RxZztHPlIhncvfPBdZIrJjOGlZwu3XoAFcT43sMN9c71o57xuGlIm2 0EEHheK5WVeMcLd+kkbhSRYQOkPtzqWxpOfk803y2fOUWcI1bR/oSVFsNrUdgowQ U0PJ7BfcLIsMsP450G6EsLergLFL+I3CoJ/rjq/ao1JlDWE9YKbgxs4Gc/STEJJJ FkGbl8XP9EzZXg6wiTOD5BUFG6fCGoJ6DIxbTiQLA27LvIvAyFazSWdAJkaGBzIc 2eNtkfjFIiezR5wcUvPVrZKG73skrtFfjCP8D6GElIzOjYv5uS9Wf+qTkhB0u2Vh GEHpIojQrRx54/kKtvJZ+anmqhbzg== X-ME-Sender: X-ME-Proxy: Received: from ranger.suse (unknown [74.94.73.222]) by mail.messagingengine.com (Postfix) with ESMTPA id 14977102E8; Mon, 3 Dec 2018 12:29:17 -0500 (EST) Subject: Re: How to watch for schema changes To: Igor Korot Cc: pgsql-general References: <53538bab-5f26-247d-15bf-94785fdfb252@aklaver.com> From: Adrian Klaver Message-ID: <761d4f03-08f4-e354-9cc0-d3cc5d0319c4@aklaver.com> Date: Mon, 3 Dec 2018 09:29:17 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/3/18 8:16 AM, Igor Korot wrote: > Hi, Adrian, > > On Mon, Dec 3, 2018 at 9:17 AM Adrian Klaver wrote: >> >> On 12/2/18 5:24 AM, Igor Korot wrote: >>> Hi, Adrian, >>> Sorry for the delay to come back to this. I was busy doing other things. >>> >>> On Tue, Jul 3, 2018 at 12:32 PM Adrian Klaver wrote: >>>> >>>> On 07/03/2018 10:21 AM, Igor Korot wrote: >>>>> Hi, ALL, >>>>> Is there any trigger or some other means I can do on the server >>>>> which will watch for CREATE/ALTER/DROP TABLE command and after successful >>>>> execution of those will issue a NOTIFY statement? >>>> >>>> https://www.postgresql.org/docs/10/static/event-triggers.html >>> >>> So if I understand correctly, I should write the trigger for the event >>> I am interested in. >>> And in this trigger I write a little SQL that will write the DDL >>> command in some temporary table. >>> >>> I'm just looking for a way to execute this trigger and a function from >>> my C++ code >>> on the connection (either ODBC or thru the libpq). >> >> Event triggers are fired by database events not by external prompting, >> so you do not need to have your code execute the trigger. You do have >> the option of disabling/enabling then though: >> >> https://www.postgresql.org/docs/10/sql-altereventtrigger.html > . > Yes, but the code to the event triogger needs to be written and then executed on > connection, right? > > So, this is what I'm thingking: > > [code] > ret = SQLExecute( m_hstmt, L"CREATE FUNCTION schema_watch_check() > RETURNS event_trigger AS schema_watch LANGUAGE C", SQL_NTS ); > if( ret == SQL_SUCCESS || ret== SQL_SUCCESS_WITH_INFO ) > { > ret = SQLExecute( m_hstmt, L"CREATE EVENT TRIGGER schema_watch ON > ddl_command_end EXECUTE PROCEDURE schema_watch_check()", SQL_NTS ); > } > [/code] > > and something to that extent on the libpq connection. > > Am I missing something here? Yes this: https://www.postgresql.org/docs/9.6/sql-createeventtrigger.html "CREATE EVENT TRIGGER creates a new event trigger. Whenever the designated event occurs and the WHEN condition associated with the trigger, if any, is satisfied, the trigger function will be executed. For a general introduction to event triggers, see Chapter 38. The user who creates an event trigger becomes its owner." So event triggers are associated with events(www.postgresql.org/docs/9.6/event-trigger-matrix.html) in a particular database. A rough description is that they are triggers on changes to the system catalogs. You could, I guess, create and drop them for each connection. To me it would seem more efficient to create them once. You then have the choice of leaving them running or using the ALTER command I posted previously to ENABLE/DISABLE them. > > Now the other question is - how to make it work? > I can write the function code, compile it and place in the folder > where my executable is (or it should be in some postgreSQL dir?) and > then executing above code > will be enough. Is this correct? > >> >>> >>> And then in my C++ code I will continuously query this temporary table. >> >> Why a temporary table? They are session specific and if the session >> aborts prematurely you will lose the information. > > Is there a better alternative? > > Thank you. > >> >>> >>> Or there is a better alternative on the algorithm? >>> >>> Thank you. >>> >>>>>> >>>>> Thank you. >>>>> >>>> >>>> >>>> -- >>>> Adrian Klaver >>>> adrian.klaver@aklaver.com >>> >> >> >> -- >> Adrian Klaver >> adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com