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 1nIFW9-0002kn-Iy for pgsql-general@arkaria.postgresql.org; Thu, 10 Feb 2022 19:54:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nIFW7-0002PM-Ux for pgsql-general@arkaria.postgresql.org; Thu, 10 Feb 2022 19:54:11 +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 1nIFW7-0002KM-J9 for pgsql-general@lists.postgresql.org; Thu, 10 Feb 2022 19:54:11 +0000 Received: from mail-qk1-x733.google.com ([2607:f8b0:4864:20::733]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nIFW4-0005pI-LA for pgsql-general@postgresql.org; Thu, 10 Feb 2022 19:54:10 +0000 Received: by mail-qk1-x733.google.com with SMTP id g145so6026399qke.3 for ; Thu, 10 Feb 2022 11:54:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:to:content-language:from :subject; bh=lTeKLEFwRBktaZXhldqLPTuvDY3egM4Zc1HwVvcvdoU=; b=cYzoRZr4H7rEDWnvyvJkoh8SPne2kYLz6aA7i/NFAv4MH8wlRl8tKtlEj3+2NHI7FU W3EA2RM/yQk2YFHa8K+1aV62W6Uuc70yMMBN+bpSf2q6QOYYRvTkLcAQ2rtjYr4XCBk8 p2SgJUfd4YY7gGpt2FNSGLEVNflj51X5avpt7RSenECF6wyWJe7Qt8fD5WhJ4mF81dfD r1Luh9Da+rsHxsPyK9VO2L2BFw2GWM4OzHM9ffb4PX8+c3fcERPomD0btSp7xd++vkdg vf0Jp4CHFVmu56hrOQi0Lb+CTVaB5X2V0wZ7sprMhbJscpnKM0IqFu7etyZgWUKSqxpy q+tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:to :content-language:from:subject; bh=lTeKLEFwRBktaZXhldqLPTuvDY3egM4Zc1HwVvcvdoU=; b=Z7+2UgpEinOVsE3Y+2BXgL79J+oQQ7p/sSf4d/cSHl/h3o1ZXGMRi3qTTw3z2nKta/ YEGtfCn+cbjvQkT1Wmxk5qv9plDjpZXJx8ilXnTbivViV5s0tiA6GSmdxxpgIoqCIFBY sU8Ezp9w0Di4xI5/xu6dApVONqns7lBH26fXxU9doLyOjRew/bh8vHjBMgQGXgLKf3mq iHK+UiDWwx/bBpqP6YrU/cM+UwJQhrW1eTQ+BSImpG3CnAur7pc4Vqv+UrCTSFr8jY1e QlMSCkhg251P2O3NlX8Cyy9hXeTUgyGLPwstY+jheW4dgiMwuWH2BkMP4cRvTUbNc8fL XFdA== X-Gm-Message-State: AOAM53328UxNwunHYIELVCOhk8LKvOoAsjwiBFSg0pMBQfvf/p0O5j/m Hd+ftliAK4Bj6DbaFW2ozoIhKJ/TLYjIzw== X-Google-Smtp-Source: ABdhPJwgpIeZfGJJAuB1bWg4Bf83rm4Qs8QOAsgH2ak/LuIJJPxMjT7oD/UpJYBKKhtBnOfWa7cEqg== X-Received: by 2002:ae9:c10e:: with SMTP id z14mr4713117qki.538.1644522846823; Thu, 10 Feb 2022 11:54:06 -0800 (PST) Received: from [192.168.2.100] (pool-72-79-19-93.nwrknj.east.verizon.net. [72.79.19.93]) by smtp.gmail.com with ESMTPSA id v17sm11084220qtx.97.2022.02.10.11.54.06 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Feb 2022 11:54:06 -0800 (PST) Content-Type: multipart/alternative; boundary="------------P3FW7ByjMIrKcaHUjbXvKulN" Message-ID: <45425911-b002-4c2d-256c-49877eca7457@gmail.com> Date: Thu, 10 Feb 2022 14:54:05 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 To: PG-General Mailing List Content-Language: en-US From: Mladen Gogala Subject: How to determine whether I'm running on a standby? List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------P3FW7ByjMIrKcaHUjbXvKulN Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi! I am running PostgreSQL 13.5, one primary cluster and two standby clusters, managed by PgBouncer which will automatically promote one of the  standbys to the primary in case of failure. I have a job that is cleaning "old data",  with "old data" being whatever business side of the company deems as "old data". The cleanup is a PgPL/SQL procedure which uses a bunch of DELETE commands. The procedure works fine on the primary but reports a bunch of errors on the read-only standbys. In another database system, I have the table called V$DATABASE and the column named DATABASE_ROLE which can be either primary or some kind of standby (physical or logical). Is there anything like that in the world of Postgres? I would like to do something like this: DECLARE DB_ROLE VARCHAR2(64); BEGIN SELECT DATABASE_ROLE INTO DB_ROLE FROM V$DATABASE; IF DB_ROLE = 'PRIMARY' THEN    ELSE     exit; END IF; END; What would a similar idiom in the PostgreSQL world look like? I have the job in crontab on the primary and I don't really know which of the 3 clusters will be my primary at any given moment. So far, I am using manual labor to do that, which is silly. Knowing Postgres idioms, there will probably be a function for this but I was unable to figure out which one. Regards -- Mladen Gogala Database Consultant Tel: (347) 321-1217 https://dbwhisperer.wordpress.com --------------P3FW7ByjMIrKcaHUjbXvKulN Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi!

I am running PostgreSQL 13.5, one primary cluster and two standby clusters, managed by PgBouncer which will automatically promote one of the  standbys to the primary in case of failure. I have a job that is cleaning "old data",  with "old data" being whatever business side of the company deems as "old data". The cleanup is a PgPL/SQL procedure which uses a bunch of DELETE commands. The procedure works fine on the primary but reports a bunch of errors on the read-only standbys. In another database system, I have the table called V$DATABASE and the column named DATABASE_ROLE which can be either primary or some kind of standby (physical or logical). Is there anything like that in the world of Postgres? I would like to do something like this:

DECLARE 
DB_ROLE VARCHAR2(64);
BEGIN
SELECT DATABASE_ROLE INTO DB_ROLE FROM V$DATABASE;
IF DB_ROLE = 'PRIMARY' THEN
   <do a bunch of deletions>
ELSE
    exit;
END IF;
END;

What would a similar idiom in the PostgreSQL world look like? I have the job in crontab on the primary and I don't really know which of the 3 clusters will be my primary at any given moment. So far, I am using manual labor to do that, which is silly. Knowing Postgres idioms, there will probably be a function for this but I was unable to figure out which one.

Regards

-- 
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
--------------P3FW7ByjMIrKcaHUjbXvKulN--