Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s0hiJ-006eoW-SZ for pgsql-sql@arkaria.postgresql.org; Sat, 27 Apr 2024 13:03:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s0hiF-006WJS-GE for pgsql-sql@arkaria.postgresql.org; Sat, 27 Apr 2024 13:03:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s0hiF-006WJJ-6e for pgsql-sql@lists.postgresql.org; Sat, 27 Apr 2024 13:03:32 +0000 Received: from mail-oo1-xc2d.google.com ([2607:f8b0:4864:20::c2d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s0hiE-000Lfj-3q for pgsql-sql@lists.postgresql.org; Sat, 27 Apr 2024 13:03:31 +0000 Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-5acf5c1a2f5so1982632eaf.0 for ; Sat, 27 Apr 2024 06:03:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714223007; x=1714827807; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=rw4KIRazTJwqQD48wK9ObycXRcaV8K4EVjDI0zoShb4=; b=Ov903l4A7tH1CcxmdgL2bDDQkBBLXfDjOJ7HWBb84wkqF/UEMDHZfltTxP6s7Mabjk l2/u96sNfFsiRvG7VX3jUrtLEQ6jYlE1AjRj18Et0YPfuTOXZiq/YJSYXXp1T+J0xhFM hJv3lXmHoJt8o8awIB8DZdmFyTFi81aU63CB1L61+GyiouGtSbKxhB8cxrcD5FdXsnoE hh4uL7yuy2h8jeQJ+UOihLVDy8V6dKVCh2Pd42jVpqAiyB33FbE5yGqw1Il5ddNT/lgS DILTgcsHWiHAJaPSnvMOIpsiLjo7xLKQ2lelPtwFVAioZMI4qzpmCYWNxel6rVOK3mvg ZwUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714223007; x=1714827807; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=rw4KIRazTJwqQD48wK9ObycXRcaV8K4EVjDI0zoShb4=; b=HXiJQpgwVPnU1ihP0+8WJ2BNamcvPVm4dfGBekxKDNX2ZcIbt4bCu6V58WZDVbQHqw HcEbe1hiMqGE08A42ps65pxTqpbPj/ox/g6/ER7uP8v3Z544cbskwAo5aoXHZJM+gs4Y m3BXdCuYwJ5KgcuVD3jlvrcwteQrK0Fer7OBc6KnWUM2B8E01Js/PEz+nWcGE6rpePZX yiL3qcOPpnkQR2kAaipqmtdLkoPj/mYYtjWN1k46TDSyglNKM8WGtMC9J2u5e1WG+RE8 pHKB1cqQh/nz/+Kxf2uIMM/pyMl8klmgbk6zXOU8gtTrSHrDJ4a2114hYVIULR+5UO14 4lNg== X-Gm-Message-State: AOJu0YzO7JsWz3VD+ZP+hpUgeRLXW97ZVn/ZjmuyNCxAFhadBfQs4sS1 jecW2UXCLxSsOi1SRqaeF2aZTLNeWgF+Twc8vvQGKsyT/CyovTyudj0b2qCzjq2R+5+4KmZtk82 Zyh1Nq9S9q9xvMqMekRVvbVc00WnHO+suNuE= X-Google-Smtp-Source: AGHT+IGrkDT66P337/Cstv1UqPz+KewLP0A32b7oUFnFl/LiA/4M/pP9Bw7wJiqLYpgIFoZbKMmAjSD6gPcsMFCbtpc= X-Received: by 2002:a05:6358:441f:b0:186:e49:2f21 with SMTP id z31-20020a056358441f00b001860e492f21mr2410066rwc.20.1714223007281; Sat, 27 Apr 2024 06:03:27 -0700 (PDT) MIME-Version: 1.0 From: kunwar singh Date: Sat, 27 Apr 2024 09:03:16 -0400 Message-ID: Subject: How to pass arguments in postgres to sql scripts. To: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000006a3d3d061713a595" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006a3d3d061713a595 Content-Type: text/plain; charset="UTF-8" Hi friends, Question: How to pass arguments in postgres to sql scripts. Context. When I am monitoring my production Oracle databases I have a lot of simple sql scripts (which require one or more arguments) which make my monitoring/troubleshooting life simpler. How can I achieve the same in postgres? We are migrating our Oracle databases to Postgres and I am modifying my scripts to do the same in Postgres. Oracle ====== cat appjobcheck.sql SELECT SID, SYS_CONTEXT.GET_CURRENT_SCHEMA() AS "SCHEMA", -- Similar to datname USERNAME, PROGRAM AS "APPLICATION_NAME", STATUS, SQL_ID, -- Use V$SQL to get full query text based on SQL_ID LOGON_TIME AS "BACKEND_START", SQL_EXEC_START AS "QUERY_START", FROM V$SESSION WHERE STATUS = 'ACTIVE' -- Filter to active sessions AND TYPE != 'BACKGROUND'; -- Exclude background processes AND program='&1'; The way to invoke from sqlplus is . SQL> @appjobcheck batchprocessapp1 ---- batchprocessapp1 is what I want to monitor Postgres ========== cat appjobcheck.sql SELECT pid, datname, usename, application_name, state, query, backend_start, query_start FROM pg_stat_activity where application_name='&1'; The way I am trying invoke from postgres is . postgres=> \i appjobcheck.sql batchprocessapp1 ---- batchprocessapp1 is what I want to monitor pid | datname | usename | application_name | state | query | backend_start | query_start -----+---------+---------+------------------+-------+-------+---------------+------------- (0 rows) \i: extra argument "batchprocessapp1" ignored -- Cheers, Kunwar --0000000000006a3d3d061713a595 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi friends, =C2=A0Question: How to pass arguments in = postgres to sql scripts. Context. When I am monitoring my production Oracle= databases I have a lot of simple sql scripts (which require one or more ar= guments) =C2=A0which make my monitoring/troubleshooting life simpler. How c= an I achieve the same in postgres? We are migrating our Oracle databases to= Postgres =C2=A0and I am modifying my scripts to do the same in Postgres.

Oracle
=3D=3D=3D=3D=3D=3D
cat appjobchec= k.sql

SELECT
=C2=A0 =C2=A0 SID,
=C2=A0 =C2=A0 SYS_CONTEXT.GET= _CURRENT_SCHEMA() AS "SCHEMA", -- Similar to datname
=C2=A0 = =C2=A0 USERNAME,
=C2=A0 =C2=A0 PROGRAM AS "APPLICATION_NAME",<= br>=C2=A0 =C2=A0 STATUS,
=C2=A0 =C2=A0 SQL_ID, -- Use V$SQL to get full = query text based on SQL_ID
=C2=A0 =C2=A0 LOGON_TIME AS "BACKEND_STA= RT",
=C2=A0 =C2=A0 SQL_EXEC_START AS "QUERY_START",
FR= OM
=C2=A0 =C2=A0 V$SESSION
WHERE
=C2=A0 =C2=A0 STATUS =3D 'ACT= IVE' -- Filter to active sessions
=C2=A0 =C2=A0 AND TYPE !=3D 'B= ACKGROUND'; -- Exclude background processes
=C2=A0 =C2=A0 AND progra= m=3D'&1';

The way to invoke from sqlplus is .

SQ= L> @appjobcheck batchprocessapp1 =C2=A0---- batchprocessapp1 is what I w= ant to monitor


Postgres
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ca= t appjobcheck.sql

SELECT
=C2=A0 =C2=A0 pid,
=C2=A0 =C2=A0 d= atname,
=C2=A0 =C2=A0 usename,
=C2=A0 =C2=A0 application_name,
= =C2=A0 =C2=A0 state,
=C2=A0 =C2=A0 query,
=C2=A0 =C2=A0 backend_star= t,
=C2=A0 =C2=A0 query_start
FROM pg_stat_activity where application_= name=3D'&1';

The way I am trying invoke from postgres is= .


postgres=3D> \i appjobcheck.sql batchprocessapp1 =C2=A0--= -- batchprocessapp1 is what I want to monitor
=C2=A0pid | datname | usen= ame | application_name | state | query | backend_start | query_start
---= --+---------+---------+------------------+-------+-------+---------------+-= ------------
(0 rows)

\i: extra argument "batchprocessapp1&q= uot; ignored

--
Cheers,
Kunwar
--0000000000006a3d3d061713a595--