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 1rkpFJ-00FhyB-1A for pgsql-sql@arkaria.postgresql.org; Thu, 14 Mar 2024 17:52:01 +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 1rkpFH-001gaC-9S for pgsql-sql@arkaria.postgresql.org; Thu, 14 Mar 2024 17:51:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rkfWf-00Flim-Ep for pgsql-sql@lists.postgresql.org; Thu, 14 Mar 2024 07:29:18 +0000 Received: from mail-yw1-x112e.google.com ([2607:f8b0:4864:20::112e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rkfWc-004QGV-3k for pgsql-sql@lists.postgresql.org; Thu, 14 Mar 2024 07:29:16 +0000 Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-60cc4124a39so6320787b3.3 for ; Thu, 14 Mar 2024 00:29:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710401354; x=1711006154; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=k549iFiQ1B7Oqq2zktaIxibcVYayaufvnmXVyM3gB9k=; b=iwNR5mju410KtpVo1MvD6pfpTVX9wHK0qtpHVyrUNCVjVQsNv0FubZznuxHBBk3KeK SG7Bu3ttYipz3F1m4DaWtNo0yfZBAa3gBjGZnsdIC5Yh6EdNCbjAJ8zqn2iBZUtjTy4w c5ltB46pHmRhQyX4h8fw4xwb0unk5NpdiCuBhx9k/mvocwqyDmiv+e+80bGVxRbQYBjB c0brUVbTh+yQ5Gh9bNyWEwtfBZI9srD/+r9yUfRpchERx55se24/F9h4f6rCOyKA3b7E 9+JdYO7ULSRBTT6bFqac1Vc2FhaSjcDKJUumd0N2y2vCp/cfbslytMUQd/tOChwSx0H3 hSzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710401354; x=1711006154; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=k549iFiQ1B7Oqq2zktaIxibcVYayaufvnmXVyM3gB9k=; b=WZBI3ioIyECedBOdftSo2Zi9rOzfyHUseBAz6h6gcQBiZ/zMDNQ9LxCMcMIzdNzaU0 3VbdD9dlcNrusIBiAfoAVX8bIpb2xOHJbSwSDsz0hgNfFze24dhcvwU50G9tom3I+VEP fK61aPEzlxxomgKXZ2mKrnrzpYV/7eVec4ZxkntrtWSi5/sij9wRwP3qzr3PDw2ipmok OZPu3a+HyP+YKmPYkmDqQFuX2M7TWsyvTkpOsdL0l6GgGezfNRtE2ABNAaR8LhDirLc3 7RpU/EX3b9xaiNYV0Tzz8Ow2yjCrQlc/EIELdZ4/5Z9aoeWiq7GLCpRd2z+HKYW8MEBZ ZFTQ== X-Gm-Message-State: AOJu0YxOHUGCmvAJ7N4c2RTTBF1XMC3Uh3NaTLCGdV0sSj5/QLnIS5JC gMw0hFkP7j3iG5Ce2u6LIRKw41Urs5BO5Y9ae5ATD5OHonh20CiepC1V2NrdbW97unMKvY4QQU8 apx9yuOO9KTwyNlU5pOP8H30kuqsZ5h2ZG7Y= X-Google-Smtp-Source: AGHT+IHOuLAW0ergS4XHyRBQWwX2B74HISODiOOgo6/M+rkkgR2Ya+D5bY+XrTOum+vknrI69tsej63VzquMb2qVUps= X-Received: by 2002:a0d:d44e:0:b0:609:254f:766d with SMTP id w75-20020a0dd44e000000b00609254f766dmr938657ywd.26.1710401354546; Thu, 14 Mar 2024 00:29:14 -0700 (PDT) MIME-Version: 1.0 References: <2046097.1710340244@sss.pgh.pa.us> In-Reply-To: <2046097.1710340244@sss.pgh.pa.us> From: Sheryl Prabhu David Date: Thu, 14 Mar 2024 12:59:03 +0530 Message-ID: Subject: Re: Nested loop behaviour with pg_stat_activity To: Tom Lane Cc: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000294995061399d9ce" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000294995061399d9ce Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks a lot Tom, Postgres community truly is impressive, I never thought I will get a response this soon if at all. In Oracle with v$session being the inner table of a nested loop for each scan we get different copies of it even as the data changes under us, thats the hack someone figured out, and it gives us tons of samples - in the order of ~500 per second with a single SQL run. In Postgres the reason I mention the behaviour is different is that the count I get with the outer group by for each connected client is one record with a count of 2000 if I use 2000 records from generate_series. Oracles behaviour would be multiple records each with a different wait event adding up to 2000. As you describe this may not be possible with Postgres. I will try SELECT pg_stat_clear_snapshot() but not sure if that can be done is a single SQL statement. Will let you know if it works. Thanks a lot again. Regards Prabhu David On Wed, Mar 13, 2024 at 8:00=E2=80=AFPM Tom Lane wrote: > Sheryl Prabhu David writes: > > In an Oracle database using a SQL like the one below, we have the abili= ty > > to use, a series along the lines of Postgresqls generate_series() as se= en > > in the 'connect by' line as outer table of a forced nested loop, and > > v$session(Oracles equivalent of pg_stat_activity) as the inner table, t= o > > get many samples of v$session each second. > > That's ... impressively ill-defined. How do you know you are getting > consistent "samples" at all? > > > Trying something similar in Postgres does not produce an equivalent > result. > > Instead of 'Number of sample' times *DIFFERENT* copies of > pg_stat_activity, > > we are seeing 'Number of sample' times *SAME* copy of pg_stat_activity, > > unlike Oracle. MVCC and Isolation guarantees for regular tables is > expected > > to produce this kind of a result, but I was hoping pg_stat_activity > being a > > portal into internal data structures will act similar to Oracles > v$session > > bypassing MVCC+Isolation. I am hoping to find out if there is anyway to > > force Oracle type behaviour for pg_stat_activity, please help. > > A Postgres session captures a snapshot of pg_stat_activity on first > reference, and holds it until end of transaction or you call > pg_stat_clear_snapshot(). Without this behavior you would get total > garbage from queries as the data changes under you, especially so > from join queries which may require multiple scans of the input. > > I don't think there's any way to precisely duplicate what you describe > doing in Postgres, but you can easily get a similar result by > alternating "SELECT pg_stat_clear_snapshot()" with selects from > pg_stat_activity. > > regards, tom lane > --000000000000294995061399d9ce Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks a lot Tom, Postgres community truly is impressive, = I never thought I will get a response this soon if at all.

In Oracle with v$session being the inner table of a nested loop for each= scan we get different copies of it even as the data changes under us, that= s the hack someone figured out,=C2=A0and it gives us tons of samples - in t= he order of ~500 per second with a single SQL run.=C2=A0

In Postgres the reason I mention the behaviour is different is that = the count I get with the outer group by for each connected client is one re= cord with a count of 2000 if I use 2000 records from generate_series. Oracl= es behaviour would be multiple records each with a different wait event add= ing up to 2000.

As you describe this may not be po= ssible with Postgres. I will try SELECT pg_stat_clear_snapshot() but not su= re if that can be done is a single SQL statement. Will let you know if it w= orks.

Thanks a lot again.

Regards
Prabhu David

<= div dir=3D"ltr" class=3D"gmail_attr">On Wed, Mar 13, 2024 at 8:00=E2=80=AFP= M Tom Lane <tgl@sss.pgh.pa.us&g= t; wrote:
Sheryl= Prabhu David <nestor.ssn@gmail.com> writes:
> In an Oracle database using a SQL like the one below, we have the abil= ity
> to use, a series along the lines of Postgresqls generate_series() as s= een
> in the 'connect by' line as outer table of a forced nested loo= p, and
> v$session(Oracles equivalent of pg_stat_activity) as the inner table, = to
> get many samples of v$session each second.

That's ... impressively ill-defined.=C2=A0 How do you know you are gett= ing
consistent "samples" at all?

> Trying something similar in Postgres does not produce an equivalent re= sult.
> Instead of 'Number of sample' times *DIFFERENT* copies of pg_s= tat_activity,
> we are seeing 'Number of sample' times *SAME* copy of pg_stat_= activity,
> unlike Oracle. MVCC and Isolation guarantees for regular tables is exp= ected
> to produce this kind of a result, but I was hoping pg_stat_activity be= ing a
> portal into internal data structures will act similar to Oracles v$ses= sion
> bypassing MVCC+Isolation. I am hoping to find out if there is anyway t= o
> force Oracle type behaviour for pg_stat_activity, please help.

A Postgres session captures a snapshot of pg_stat_activity on first
reference, and holds it until end of transaction or you call
pg_stat_clear_snapshot().=C2=A0 Without this behavior you would get total garbage from queries as the data changes under you, especially so
from join queries which may require multiple scans of the input.

I don't think there's any way to precisely duplicate what you descr= ibe
doing in Postgres, but you can easily get a similar result by
alternating "SELECT pg_stat_clear_snapshot()" with selects from pg_stat_activity.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--000000000000294995061399d9ce--