Restore replication settings when modifying a field type

Quan Zongliang <quanzongliang@gmail.com>

From: Quan Zongliang <quanzongliang@gmail.com>
To: Pgsql Hackers <pgsql-hackers@postgresql.org>
Date: 2019-10-26T08:50:48Z
Lists: pgsql-hackers

Attachments

When the user modifies the REPLICA IDENTIFY field type, the logical 
replication settings are lost.

For example:

postgres=# \d+ t1
                                     Table "public.t1"
  Column |  Type   | Collation | Nullable | Default | Storage | Stats 
target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
  col1   | integer |           |          |         | plain   | 
     |
  col2   | integer |           | not null |         | plain   | 
     |
Indexes:
     "t1_col2_key" UNIQUE CONSTRAINT, btree (col2) REPLICA IDENTITY


postgres=# alter table t1 alter col2 type smallint;
ALTER TABLE
postgres=# \d+ t1
                                      Table "public.t1"
  Column |   Type   | Collation | Nullable | Default | Storage | Stats 
target | Description
--------+----------+-----------+----------+---------+---------+--------------+-------------
  col1   | integer  |           |          |         | plain   | 
      |
  col2   | smallint |           | not null |         | plain   | 
      |
Indexes:
     "t1_col2_key" UNIQUE CONSTRAINT, btree (col2)

In fact, the replication property of the table has not been modified, 
and it is still 'i'(REPLICA_IDENTITY_INDEX). But the previously 
specified index property 'indisreplident' is set to false because of the 
rebuild.

So I developed a patch. If the user modifies the field type. The 
associated index is REPLICA IDENTITY. Rebuild and restore replication 
settings.

Regards,
Quan Zongliang

Commits

  1. Preserve replica identity index across ALTER TABLE rewrite

  2. Prevent reindex of invalid indexes on TOAST tables