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 1k2puh-00043Z-KI for pgsql-general@arkaria.postgresql.org; Tue, 04 Aug 2020 05:55:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k2puA-00083Q-Si for pgsql-general@arkaria.postgresql.org; Tue, 04 Aug 2020 05:54:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k2puA-00083H-Go for pgsql-general@lists.postgresql.org; Tue, 04 Aug 2020 05:54:30 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k2pu3-0003S1-39 for pgsql-general@lists.postgresql.org; Tue, 04 Aug 2020 05:54:29 +0000 Received: by mail-wr1-x442.google.com with SMTP id z18so32616239wrm.12 for ; Mon, 03 Aug 2020 22:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=/jQUvR2HIRn0an2F0b0Q0BRtjKU2GbQzNJ7QoJK/G5M=; b=UidAmFmmEc9w9Q6NpaK0oVBPHbiRzFV0incxHWLaL1OhIObpNWqGyO5UZhPqnOfARf ktubWEtSngedf6w5/Pin+3TOU4EGPjRWy/CFgAvrgM/Kk+FU4B3qtAY9kdxS5ISC3bFV Z5Ox3JpxmgK7WO8Pf9xSbDUqnHIyBgnkZVJblM3QlQvfU/94cTbIyFOdva7BoyccTNVP 5+wx6H3MRaKyfV/671BVgaf1sXqLDLpPQnuX6mtRGDw9acCs/aE0cPJjnrLPL2jLRRH1 ITwSnx7woOjr01AUiBLl1wdJzKa/GFOJcSr2bs6xtuL053yoJ18u/tbiNeA4haGwgeEK lmHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=/jQUvR2HIRn0an2F0b0Q0BRtjKU2GbQzNJ7QoJK/G5M=; b=dXKGCZDjnYwnEdej+RtwJexnc2sCSGIglKR0HCi33lIOQe4nXqMFe8YxDyUARRPGUV isy4gBL91+bJI3x8bMQyWCCYrEqIVbqb4bYyzd0rS8mLBJdaeJHC/bZPY4Y56RRCPwK7 umSjnAHeSaoj2TKHn5aQKEY3TVsgnrWMhUVifIamOl0/qHQRmjs+Wk9GStL1uytArW+D q6RA0DBN89dvF8dQB4DOlw3N+SUt6ijLDP0QazWxrI+FuJnFBftD9lOLKRFw7HP9WlhS qUHVykbjkCG3eF4ag7oBvWIPJIQNHDfx9OwfTyZsqIqcPKqtptBUykIa5MJ+KYzET4n6 sF0Q== X-Gm-Message-State: AOAM531vUMLU9HRj8q83sIPFfWSS80lHp1b8dBBqhn1GNUr+dAssCGBo HdXQgSsm7UjsdEcxAfHap98= X-Google-Smtp-Source: ABdhPJyCnFvEJVnhvVmcwolD2rp2G9zT2m1VTeR3vBoOYprktHklFNfamrGwVTulVFo1KGFeVnbiNw== X-Received: by 2002:adf:9ec5:: with SMTP id b5mr16851077wrf.190.1596520461414; Mon, 03 Aug 2020 22:54:21 -0700 (PDT) Received: from ?IPv6:2a02:587:8001:2800:1c6:dbce:b8ed:a7d9? ([2a02:587:8001:2800:1c6:dbce:b8ed:a7d9]) by smtp.gmail.com with ESMTPSA id k15sm18438659wrp.43.2020.08.03.22.54.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 22:54:20 -0700 (PDT) From: Stelios Sfakianakis Message-Id: <483E8B68-D9DB-4533-BA82-206F41E53626@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_347672B5-03A4-4053-B5C3-3E9D2C5EB446" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Keeping state in a foreign data wrapper Date: Tue, 4 Aug 2020 08:54:14 +0300 In-Reply-To: Cc: pgsql-general@lists.postgresql.org To: Ian Lawrence Barwick References: X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_347672B5-03A4-4053-B5C3-3E9D2C5EB446 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Thank you Ian for the prompt reply! I will certainly have a look at = pg_stat_statements I also came across mysql_fdw (https://github.com/EnterpriseDB/mysql_fdw = ) that features a connection = pool shared across queries. It uses a hash table with the serverid and = userid as lookup key : = https://github.com/EnterpriseDB/mysql_fdw/blob/REL-2_5_4/connection.c#L55 = The hash table is allocated in the cache memory context but it worries = me that 1) no locks are used, 2) the "ConnectionHash" variable is = declared static so in the multi-process architecture of Postgres could = have been the case that multiple copies of this exist when the shared = library of mysql_fdw is loaded? Best regards Stelios > On 4 Aug 2020, at 06:25, Ian Lawrence Barwick = wrote: >=20 > 2020=E5=B9=B48=E6=9C=884=E6=97=A5(=E7=81=AB) 1:24 Stelios Sfakianakis = : >>=20 >> Hi, >>=20 >> I am trying to implement a FDW in Postgres for accessing a web api = and I would like to keep information like for example the total number = of requests submiited. Ideally these data should be kept =E2=80=9Cper-user= =E2=80=9D and of course with the proper locking to eliminate race = conditions. So the question I have is how to design such a global (but = per user and database) state, using the C FDW API of Postgres. I have = gone through the API and for example I see various fdw_private fields in = the query planning structures and callbacks but these do not seem to be = relevant to my requirements. Another possiblity seems to be to use = shared memory = (https://www.postgresql.org/docs/12/xfunc-c.html#id-1.8.3.13.14) but it = is even less clear how to do it. >=20 > Shared memory would probably work; you'd need to load the FDW via > "shared_preload_libraries" and have the FDW handler function(s) update > shared memory with whatever statistics you want to track. You could > then define SQL functions to retrieve the stored values, and possibly > persist them over server restarts by storing/retrieving them from a > file. >=20 > Look at "pg_stat_statements" for a good example of how to do that kind = of thing. >=20 > Regards >=20 > Ian Barwick >=20 > --=20 > Ian Barwick https://www.2ndQuadrant.com/ > PostgreSQL Development, 24x7 Support, Training & Services --Apple-Mail=_347672B5-03A4-4053-B5C3-3E9D2C5EB446 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Thank= you Ian for the prompt reply! I will certainly have a look at = pg_stat_statements

I = also came across mysql_fdw (https://github.com/EnterpriseDB/mysql_fdw) that features = a connection pool shared across queries. It uses a hash table with the = serverid and userid as lookup key : https://github.com/EnterpriseDB/mysql_fdw/blob/REL-2_5_4/connec= tion.c#L55 The hash table is allocated in the cache memory = context but it worries me that 1) no locks are used, 2) the = "ConnectionHash" variable is declared static so in the multi-process = architecture of Postgres could have been the case that multiple copies = of this exist when the shared library of mysql_fdw is loaded?

Best regards
Stelios

On 4 Aug 2020, at 06:25, Ian = Lawrence Barwick <barwick@gmail.com> wrote:

2020=E5=B9=B48=E6=9C=884=E6=97=A5(=E7=81=AB) 1:24 Stelios = Sfakianakis <sgsfak@gmail.com>:

Hi,

I= am trying to  implement a FDW in Postgres for accessing a web api = and I would like to keep information like for example the total number = of requests submiited. Ideally these data should be kept =E2=80=9Cper-user= =E2=80=9D and of course with the proper locking to eliminate race = conditions. So the question I have is how to design such a global (but = per user and database) state, using the C FDW API of Postgres. I have = gone through the API and for example I see various fdw_private fields in = the query planning structures and callbacks but these do not seem to be = relevant to my requirements. Another possiblity seems to be to use = shared memory (https://www.postgresql.org/docs/12/xfunc-c.html#id-1.8.3.13.14<= /a>) but it is even less clear how to do it.

Shared memory would probably = work; you'd need to load the FDW via
"shared_preload_libraries" and have the FDW handler = function(s) update
shared memory with whatever statistics = you want to track. You could
then define SQL functions to = retrieve the stored values, and possibly
persist them over = server restarts by storing/retrieving them from a
file.

Look at "pg_stat_statements" for a good = example of how to do that kind of thing.

Regards

Ian Barwick

--
Ian Barwick =             &n= bsp;     
https://www.2ndQuadrant.com/
PostgreSQL = Development, 24x7 Support, Training & Services

= --Apple-Mail=_347672B5-03A4-4053-B5C3-3E9D2C5EB446--