Skip to content

Commit 329b5da

Browse files
committed
Optimize query cache invalidation for ALTER ROLE.
Commit 6b7d585 invalidates query cache if any ALTER ROLE/USER statement is used. Actually this is an overkill. Because following queries do not affect the privilege of the role. - ALTER ROLE user WITH [ENCRYPTED] PASSWORD - ALTER ROLE user WITH CONNECTION LIMIT So do not invalidate query cache if those commands are used. Backpatch-through: v4.1 Discussion: https://www.pgpool.net/pipermail/pgpool-hackers/2024-October/004532.html
1 parent c5c1ca6 commit 329b5da

File tree

7 files changed

+138
-3
lines changed

7 files changed

+138
-3
lines changed

doc.ja/src/sgml/memcache.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
以下のコマンドはクエリキャッシュとデータベース内容の整合性を失わせる可能性があるので、実行されるとクエリキャッシュをすべて削除します。
123123
<programlisting>
124124
ALTER DATABASE
125-
ALTER ROLE
125+
ALTER ROLE or USER (WITH CONNECTION LIMITとWITH [ENCRYPTED] PASSWORDを除く)
126126
ALTER TABLE
127127
REVOKE
128128
</programlisting>

doc/src/sgml/memcache.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
they are executed:
8080
<programlisting>
8181
ALTER DATABASE
82-
ALTER ROLE
82+
ALTER ROLE or USER (except WITH CONNECTION LIMIT and WITH [ENCRYPTED] PASSWORD)
8383
ALTER TABLE
8484
REVOKE
8585
</programlisting>

src/protocol/CommandComplete.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static int forward_command_complete(POOL_CONNECTION * frontend, char *packet, in
4545
static int forward_empty_query(POOL_CONNECTION * frontend, char *packet, int packetlen);
4646
static int forward_packet_to_frontend(POOL_CONNECTION * frontend, char kind, char *packet, int packetlen);
4747
static void process_clear_cache(POOL_CONNECTION_POOL * backend);
48+
static bool check_alter_role_statement(AlterRoleStmt *stmt);
4849

4950
POOL_STATUS
5051
CommandComplete(POOL_CONNECTION * frontend, POOL_CONNECTION_POOL * backend, bool command_complete)
@@ -475,11 +476,44 @@ handle_query_context(POOL_CONNECTION_POOL * backend)
475476
process_clear_cache(backend);
476477
}
477478
else if (IsA(node, AlterTableStmt) || IsA(node, AlterDatabaseStmt) ||
478-
IsA(node, AlterDatabaseSetStmt) || IsA(node, AlterRoleStmt))
479+
IsA(node, AlterDatabaseSetStmt))
479480
{
480481
/* Clear query cache */
481482
process_clear_cache(backend);
482483
}
484+
else if (IsA(node, AlterRoleStmt))
485+
{
486+
if (check_alter_role_statement(castNode(AlterRoleStmt, node)))
487+
{
488+
/* Clear query cache */
489+
process_clear_cache(backend);
490+
}
491+
}
492+
}
493+
494+
/*
495+
* Check whether the ALTER ROLE statement needs query cache invalidation.
496+
* stmt must be AlterRoleStmt.
497+
*/
498+
static bool
499+
check_alter_role_statement(AlterRoleStmt *stmt)
500+
{
501+
ListCell *l;
502+
503+
foreach(l, stmt->options)
504+
{
505+
DefElem *elm = (DefElem *) lfirst(l);
506+
507+
/*
508+
* We want to detect other than ALTER ROLE foo WITH PASSWORD or
509+
* WITH CONNECTION LIMIT case. It does not change any privilege of the
510+
* role.
511+
*/
512+
if (strcmp(elm->defname, "password") &&
513+
strcmp(elm->defname, "connectionlimit"))
514+
return true;
515+
}
516+
return false;
483517
}
484518

485519
/*

src/test/regression/tests/006.memqcache/expected.n

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,32 @@ NOTICE: DB node id: 0 statement: SELECT * FROM t1;
567567
2 |
568568
(1 row)
569569

570+
--
571+
-- ALTER ROLE WITH ENCRYPTED PASSWORD and
572+
-- ALTER ROLE WITH CONNECTION LIMIT 10
573+
-- do not invalidate query cache
574+
SELECT 10;
575+
NOTICE: DB node id: 0 statement: SELECT 10;
576+
?column?
577+
----------
578+
10
579+
(1 row)
580+
581+
SELECT 10;
582+
?column?
583+
----------
584+
10
585+
(1 row)
586+
587+
ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
588+
NOTICE: DB node id: 0 statement: ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
589+
ALTER ROLE
590+
ALTER ROLE foo WITH CONNECTION LIMIT 10;
591+
NOTICE: DB node id: 0 statement: ALTER ROLE foo WITH CONNECTION LIMIT 10;
592+
ALTER ROLE
593+
SELECT 10;
594+
?column?
595+
----------
596+
10
597+
(1 row)
598+

src/test/regression/tests/006.memqcache/expected.r

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,34 @@ NOTICE: DB node id: 0 statement: SELECT * FROM t1;
625625
2 |
626626
(1 row)
627627

628+
--
629+
-- ALTER ROLE WITH ENCRYPTED PASSWORD and
630+
-- ALTER ROLE WITH CONNECTION LIMIT 10
631+
-- do not invalidate query cache
632+
SELECT 10;
633+
NOTICE: DB node id: 0 statement: SELECT 10;
634+
?column?
635+
----------
636+
10
637+
(1 row)
638+
639+
SELECT 10;
640+
?column?
641+
----------
642+
10
643+
(1 row)
644+
645+
ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
646+
NOTICE: DB node id: 0 statement: ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
647+
NOTICE: DB node id: 1 statement: ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
648+
ALTER ROLE
649+
ALTER ROLE foo WITH CONNECTION LIMIT 10;
650+
NOTICE: DB node id: 0 statement: ALTER ROLE foo WITH CONNECTION LIMIT 10;
651+
NOTICE: DB node id: 1 statement: ALTER ROLE foo WITH CONNECTION LIMIT 10;
652+
ALTER ROLE
653+
SELECT 10;
654+
?column?
655+
----------
656+
10
657+
(1 row)
658+

src/test/regression/tests/006.memqcache/expected.s

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,32 @@ NOTICE: DB node id: 0 statement: SELECT * FROM t1;
567567
2 |
568568
(1 row)
569569

570+
--
571+
-- ALTER ROLE WITH ENCRYPTED PASSWORD and
572+
-- ALTER ROLE WITH CONNECTION LIMIT 10
573+
-- do not invalidate query cache
574+
SELECT 10;
575+
NOTICE: DB node id: 0 statement: SELECT 10;
576+
?column?
577+
----------
578+
10
579+
(1 row)
580+
581+
SELECT 10;
582+
?column?
583+
----------
584+
10
585+
(1 row)
586+
587+
ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
588+
NOTICE: DB node id: 0 statement: ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
589+
ALTER ROLE
590+
ALTER ROLE foo WITH CONNECTION LIMIT 10;
591+
NOTICE: DB node id: 0 statement: ALTER ROLE foo WITH CONNECTION LIMIT 10;
592+
ALTER ROLE
593+
SELECT 10;
594+
?column?
595+
----------
596+
10
597+
(1 row)
598+

src/test/regression/tests/006.memqcache/test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,18 @@ EOF
446446
# $PGPROTO -d test -f ../alter_database2.data |& del_details_from_error >> result
447447
# $PGPROTO -d test -f ../alter_database3.data |& del_details_from_error >> result
448448

449+
$PSQL -a test >> result 2>&1 <<EOF
450+
--
451+
-- ALTER ROLE WITH ENCRYPTED PASSWORD and
452+
-- ALTER ROLE WITH CONNECTION LIMIT 10
453+
-- do not invalidate query cache
454+
SELECT 10;
455+
SELECT 10;
456+
ALTER ROLE foo WITH ENCRYPTED PASSWORD 'foo';
457+
ALTER ROLE foo WITH CONNECTION LIMIT 10;
458+
SELECT 10;
459+
EOF
460+
449461
./shutdownall
450462

451463
cd ..

0 commit comments

Comments
 (0)