Find Replica Identity for a Postgres table
Asked Answered
E

3

31

Is there a way to see what kind of replica identity a Postgres table has, whether using pgAdmin or through a query?

Equiponderate answered 19/3, 2019 at 20:25 Comment(0)
S
42

You can query the pg_class system catalog:

SELECT CASE relreplident
          WHEN 'd' THEN 'default'
          WHEN 'n' THEN 'nothing'
          WHEN 'f' THEN 'full'
          WHEN 'i' THEN 'index'
       END AS replica_identity
FROM pg_class
WHERE oid = 'mytablename'::regclass;
Submergible answered 19/3, 2019 at 20:38 Comment(2)
It's also useful to know all tables with no primary key if you use default logical replica identity - dba.stackexchange.com/questions/29932/…Felder
If you set the replica identity of a table to an index you could find the relevant index with SELECT indexrelid::regclass FROM pg_index WHERE indrelid='mytablename'::regclass AND indisreplident;Roxanneroxburgh
C
1

Find replica identify for multiple tables. The query will list the tablename as its replica identity status.

SELECT oid::regclass,CASE relreplident
          WHEN 'd' THEN 'default'
          WHEN 'n' THEN 'nothing'
          WHEN 'f' THEN 'full'
          WHEN 'i' THEN 'index'
       END AS replica_identity
FROM pg_class
WHERE oid in ('public.testtable1'::regclass,'public.testtable1'::regclass);
Crowded answered 10/7, 2023 at 9:25 Comment(0)
J
1

Improving the best answer by Laurenz Albe, this query list for all tables in a schema:

SELECT 
table_name, 
oid,
CASE relreplident
    WHEN 'd' THEN 'default'
    WHEN 'n' THEN 'nothing'
    WHEN 'f' THEN 'full'
    WHEN 'i' THEN 'index'
END AS replica_identity
FROM pg_class JOIN information_schema.tables t
ON oid = CONCAT('"',table_name,'"')::regclass
WHERE table_schema = '<your_schema>' 

Why am I using concat? Just due upper case table names.

Johnnajohnnie answered 7/5 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.