Skip to content

Instantly share code, notes, and snippets.

@yahonda
Created March 26, 2024 00:14
Show Gist options
  • Save yahonda/c830379b33d66a743aef159aa03d7e49 to your computer and use it in GitHub Desktop.
Save yahonda/c830379b33d66a743aef159aa03d7e49 to your computer and use it in GitHub Desktop.
  • Run non prepared statement using psql
$ bin/psql -d test
psql (17devel)
Type "help" for help.

test=# \d
                 List of relations
 Schema |         Name         |   Type   |  Owner
--------+----------------------+----------+---------
 public | ar_internal_metadata | table    | yahonda
 public | posts                | table    | yahonda
 public | posts_id_seq         | sequence | yahonda
 public | schema_migrations    | table    | yahonda
(4 rows)

test=# DROP TABLE IF EXISTS posts;
DROP TABLE
test=# CREATE TABLE posts (id bigserial primary key);
CREATE TABLE
test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  1
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1);
 count
-------
     1
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  2
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2);
 count
-------
     2
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  3
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3);
 count
-------
     3
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  4
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4);
 count
-------
     4
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  5
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5);
 count
-------
     5
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  6
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5, 6);
 count
-------
     6
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  7
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5, 6, 7);
 count
-------
     7
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  8
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5, 6, 7, 8);
 count
-------
     8
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
  9
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9);
 count
-------
     9
(1 row)

test=# BEGIN;
BEGIN
test=*# INSERT INTO posts DEFAULT VALUES RETURNING id;
 id
----
 10
(1 row)

INSERT 0 1
test=*# COMMIT;
COMMIT
test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 count
-------
    10
(1 row)

test=# SELECT COUNT(*) FROM posts WHERE posts.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
 count
-------
    10
(1 row)

test=#
  • pg_stat_statements shows whose in clause valure are normalized
postgres=# select query,calls from pg_stat_statements where query like 'SELECT COUNT%' order by 1;
                                            query                                            | calls
---------------------------------------------------------------------------------------------+-------
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1)                                           |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2)                                       |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3)                                   |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4)                               |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5)                           |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5, $6)                       |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5, $6, $7)                   |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5, $6, $7, $8)               |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5, $6, $7, $8, $9)           |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)      |     1
 SELECT COUNT(*) FROM posts WHERE posts.id IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) |     1
(11 rows)

postgres=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment