Skip to content

Commit ab82340

Browse files
committed
Avoid permission failure in pg_sequences.last_value
Before, reading pg_sequences.last_value would fail unless the user had appropriate sequence permissions, which would make the pg_sequences view cumbersome to use. Instead, return null instead of the real value when there are no permissions. From: Michael Paquier <michael.paquier@gmail.com> Reported-by: Shinoda, Noriyoshi <noriyoshi.shinoda@hpe.com>
1 parent ad6af3f commit ab82340

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9882,7 +9882,10 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
98829882
<entry></entry>
98839883
<entry>The last sequence value written to disk. If caching is used,
98849884
this value can be greater than the last value handed out from the
9885-
sequence. Null if the sequence has not been read from yet.</entry>
9885+
sequence. Null if the sequence has not been read from yet. Also, if
9886+
the current user does not have <literal>USAGE</literal>
9887+
or <literal>SELECT</literal> privilege on the sequence, the value is
9888+
null.</entry>
98869889
</row>
98879890
</tbody>
98889891
</tgroup>

src/backend/catalog/system_views.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ CREATE OR REPLACE VIEW pg_sequences AS
175175
S.seqincrement AS increment_by,
176176
S.seqcycle AS cycle,
177177
S.seqcache AS cache_size,
178-
pg_sequence_last_value(C.oid) AS last_value
178+
CASE
179+
WHEN has_sequence_privilege(C.oid, 'SELECT,USAGE'::text)
180+
THEN pg_sequence_last_value(C.oid)
181+
ELSE NULL
182+
END AS last_value
179183
FROM pg_sequence S JOIN pg_class C ON (C.oid = S.seqrelid)
180184
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
181185
WHERE NOT pg_is_other_temp_schema(N.oid)

src/test/regress/expected/rules.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,10 @@ pg_sequences| SELECT n.nspname AS schemaname,
16471647
s.seqincrement AS increment_by,
16481648
s.seqcycle AS cycle,
16491649
s.seqcache AS cache_size,
1650-
pg_sequence_last_value((c.oid)::regclass) AS last_value
1650+
CASE
1651+
WHEN has_sequence_privilege(c.oid, 'SELECT,USAGE'::text) THEN pg_sequence_last_value((c.oid)::regclass)
1652+
ELSE NULL::bigint
1653+
END AS last_value
16511654
FROM ((pg_sequence s
16521655
JOIN pg_class c ON ((c.oid = s.seqrelid)))
16531656
LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace)))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy