agora inbox for pgsql-admin@postgresql.org  
help / color / mirror / Atom feed
How to definitively determine whether a statement is a SELECT statement?
2+ messages / 2 participants
[nested] [flat]

* How to definitively determine whether a statement is a SELECT statement?
@ 2026-08-31 16:06  Ron Johnson <ronljohnsonjr@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Ron Johnson @ 2026-08-31 16:06 UTC (permalink / raw)
  To: Pgsql-admin <pgsql-admin@lists.postgresql.org>

Sometimes, the developers add comments to the top of statements, and so we
see that in pg_stat_activity.query as seen in this example:

select query
from pg_stat_activity
where pid = 1054079;
 query
----------------------------------------
-- Some comment written by the developer
SELECT blah blah FROM .blah

I could case-insensitively search pg_stat_activity.query for "SELECT " but
that will fail if there is a SELECT in the CTE or subquery of a DELETE or
UPDATE statement, and writing a parser to strip out all comments is a bit
too much effort for a simple query.

-- 
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: How to definitively determine whether a statement is a SELECT statement?
@ 2026-09-01 05:07  bertrand HARTWIG <hartwig.bertrand@gmail.com>
  parent: Ron Johnson <ronljohnsonjr@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: bertrand HARTWIG @ 2026-09-01 05:07 UTC (permalink / raw)
  To: Ron Johnson <ronljohnsonjr@gmail.com>; +Cc: Pgsql-admin <pgsql-admin@lists.postgresql.org>

Hello,

You can use this python lib : pglast

from pglast import parse_sql
from pglast.ast import SelectStmt


def is_select_query(sql: str) -> bool:
    try:
        statements = parse_sql(sql)
    except Exception:
        return False  # SQL invalide

    if len(statements) != 1:
        return False  # Refuse plusieurs instructions SQL

    return isinstance(statements[0].stmt, SelectStmt)


print(is_select_query("SELECT * FROM users"))          # True
print(is_select_query("WITH x AS (SELECT 1) SELECT * FROM x"))  # True
print(is_select_query("INSERT INTO users(name) VALUES ('Alice')"))  # False
print(is_select_query("SELECT 1; DELETE FROM users"))  # False

Bertrand

> Le 31 août 2026 à 18:06, Ron Johnson <ronljohnsonjr@gmail.com> a écrit :
> 
> Sometimes, the developers add comments to the top of statements, and so we see that in pg_stat_activity.query as seen in this example:
> 
> select query
> from pg_stat_activity
> where pid = 1054079;
>  query  
> ----------------------------------------
> -- Some comment written by the developer
> SELECT blah blah FROM .blah
> 
> I could case-insensitively search pg_stat_activity.query for "SELECT " but that will fail if there is a SELECT in the CTE or subquery of a DELETE or UPDATE statement, and writing a parser to strip out all comments is a bit too much effort for a simple query.
> 
> --
> Death to <Redacted>, and butter sauce.
> Don't boil me, I'm still alive.
> <Redacted> lobster!

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-09-01 05:07 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-31 16:06 How to definitively determine whether a statement is a SELECT statement? Ron Johnson <ronljohnsonjr@gmail.com>
2026-09-01 05:07 ` bertrand HARTWIG <hartwig.bertrand@gmail.com>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox