Re: Alter varchar column size recrea índice?

2022-02-08 Thread Hellmuth Vargas
Hola Jaime, muchas  gracias, tenía la duda porque en varias ocasiones  lo
he hecho (en las condiciones antes descritas) y no había evidenciado un
REINDEX como efecto colateral.



El mar, 8 feb 2022 a la(s) 11:09, Jaime Casanova (
jcasa...@systemguards.com.ec) escribió:

> On Tue, Feb 08, 2022 at 05:06:24AM -0500, Hellmuth Vargas wrote:
> > Hola lista
> >
> > Leyendo por ahí en el siguiente post
> >
> >
> >
> https://www.linkedin.com/posts/somdyuti-paul-042aa27_rds-postgresql-aws-activity-6896664452180131840-QZGN
> >
> >
> > Expone que cuando se altera el tamaño de una columna varchar  INDEXADA
> > internamente se recrear el índice asociado Ojo solo estamos cambiando
> > el tamaño... Que opinan?
> >
>
> Mejor que una opinión... una prueba (en postgres 14)
>
> """
> postgres=# create table prueba (v varchar(10));
> CREATE TABLE
> postgres=# create index on prueba (v);
> CREATE INDEX
> postgres=# select oid, relname, relfilenode from pg_class where relname
> like 'prueba%';
>   oid   |   relname| relfilenode
> +--+-
>  102710 | prueba   |  102710
>  102713 | prueba_v_idx |  102713
> (2 filas)
>
> postgres=# alter table prueba alter v type varchar(15);
> ALTER TABLE
> postgres=# select oid, relname, relfilenode from pg_class where relname
> like 'prueba%';
>   oid   |   relname| relfilenode
> +--+-
>  102710 | prueba   |  102710
>  102714 | prueba_v_idx |  102713
> (2 filas)
> """
>
> ¿Cómo se que no se reindexó el índice?
> Porque al reindexar (igual que al reescribir la tabla) el relfilenode
> cambia.
>
> """
> postgres=# reindex index prueba_v_idx ;
> REINDEX
> postgres=# select oid, relname, relfilenode from pg_class where relname
> like 'prueba%';
>   oid   |   relname| relfilenode
> +--+-
>  102710 | prueba   |  102710
>  102714 | prueba_v_idx |  102715
> (2 filas)
> """
>
> Ahora, este no siempre fue el caso. Hace eones atras cualquier ALTER
> TABLE causaba que se reescribiera la tabla. Aunque nunca he sabido, y
> con eso quiero decir que no creo que alguna vez fuera así, este caso
> en particular tuviera el efecto de no reescribir la tabla pero si
> reindexar el índice.
>
> --
> Jaime Casanova
> Director de Servicios Profesionales
> SystemGuards - Consultores de PostgreSQL
>


-- 
Cordialmente,

Ing. Hellmuth I. Vargas S.
Esp. Telemática y Negocios por Internet
Oracle Database 10g Administrator Certified Associate
EnterpriseDB Certified PostgreSQL 9.3 Associate


Re: Alter varchar column size recrea índice?

2022-02-08 Thread Jaime Casanova
On Tue, Feb 08, 2022 at 05:06:24AM -0500, Hellmuth Vargas wrote:
> Hola lista
> 
> Leyendo por ahí en el siguiente post
> 
> 
> https://www.linkedin.com/posts/somdyuti-paul-042aa27_rds-postgresql-aws-activity-6896664452180131840-QZGN
> 
> 
> Expone que cuando se altera el tamaño de una columna varchar  INDEXADA
> internamente se recrear el índice asociado Ojo solo estamos cambiando
> el tamaño... Que opinan?
> 

Mejor que una opinión... una prueba (en postgres 14)

"""
postgres=# create table prueba (v varchar(10));
CREATE TABLE
postgres=# create index on prueba (v);
CREATE INDEX
postgres=# select oid, relname, relfilenode from pg_class where relname like 
'prueba%';
  oid   |   relname| relfilenode 
+--+-
 102710 | prueba   |  102710
 102713 | prueba_v_idx |  102713
(2 filas)

postgres=# alter table prueba alter v type varchar(15);
ALTER TABLE
postgres=# select oid, relname, relfilenode from pg_class where relname like 
'prueba%';
  oid   |   relname| relfilenode 
+--+-
 102710 | prueba   |  102710
 102714 | prueba_v_idx |  102713
(2 filas)
"""

¿Cómo se que no se reindexó el índice?
Porque al reindexar (igual que al reescribir la tabla) el relfilenode
cambia.

"""
postgres=# reindex index prueba_v_idx ;
REINDEX
postgres=# select oid, relname, relfilenode from pg_class where relname like 
'prueba%';
  oid   |   relname| relfilenode
+--+-
 102710 | prueba   |  102710
 102714 | prueba_v_idx |  102715
(2 filas)
"""

Ahora, este no siempre fue el caso. Hace eones atras cualquier ALTER
TABLE causaba que se reescribiera la tabla. Aunque nunca he sabido, y
con eso quiero decir que no creo que alguna vez fuera así, este caso 
en particular tuviera el efecto de no reescribir la tabla pero si 
reindexar el índice.

-- 
Jaime Casanova
Director de Servicios Profesionales
SystemGuards - Consultores de PostgreSQL




Re: Alter varchar column size recrea índice?

2022-02-08 Thread Hellmuth Vargas
Hola lista

Solo estoy preguntando por el caso particular donde vamos a alterar el
tamaño de un campo varchar:

varchar(n) -> varchar(m) donde m>n

Osea, en principio, no se altera la información almacenada




El mar., 8 de febrero de 2022 5:06 a. m., Hellmuth Vargas 
escribió:

>
> Hola lista
>
> Leyendo por ahí en el siguiente post
>
>
>
> https://www.linkedin.com/posts/somdyuti-paul-042aa27_rds-postgresql-aws-activity-6896664452180131840-QZGN
>
>
> Expone que cuando se altera el tamaño de una columna varchar  INDEXADA
> internamente se recrear el índice asociado Ojo solo estamos cambiando
> el tamaño... Que opinan?
>
> Adjunto el comentario
>
> Postgres Diaries #5 Does Postgres acquire Access Exclusive Lock when
> changing the length of a VARCHAR Column? No. It does not take ACCESS
> EXCLUSIVE lock on the table as it does not rewrite the entire table (unlike
> changing the datatype of a column or adding new column, etc), but it does
> try to rebuild/rewrite any index on the column which we are changing (and
> this may increase the time it takes to run the alter). Example- Say there
> is a table TEST (id1 integer, id2 varchar(64)). We want to increase id2
> from varchar(64) to varchar(128) and there is an index "idx_test_id2" on
> id2. The steps to increase VARCHAR length in the fastest possible way with
> no production database impact are:- 1. set lock_timeout='5s'; 2. drop index
> idx_test_id2; 3. set lock_timeout='5s'; 4. ALTER TABLE test ALTER COLUMN
> id2 TYPE varchar(128); 5. set maintenance_work_mem=''; 6.
> create index concurrently idx_test_id2 on test(id2);
>


Alter varchar column size recrea índice?

2022-02-08 Thread Hellmuth Vargas
Hola lista

Leyendo por ahí en el siguiente post


https://www.linkedin.com/posts/somdyuti-paul-042aa27_rds-postgresql-aws-activity-6896664452180131840-QZGN


Expone que cuando se altera el tamaño de una columna varchar  INDEXADA
internamente se recrear el índice asociado Ojo solo estamos cambiando
el tamaño... Que opinan?

Adjunto el comentario

Postgres Diaries #5 Does Postgres acquire Access Exclusive Lock when
changing the length of a VARCHAR Column? No. It does not take ACCESS
EXCLUSIVE lock on the table as it does not rewrite the entire table (unlike
changing the datatype of a column or adding new column, etc), but it does
try to rebuild/rewrite any index on the column which we are changing (and
this may increase the time it takes to run the alter). Example- Say there
is a table TEST (id1 integer, id2 varchar(64)). We want to increase id2
from varchar(64) to varchar(128) and there is an index "idx_test_id2" on
id2. The steps to increase VARCHAR length in the fastest possible way with
no production database impact are:- 1. set lock_timeout='5s'; 2. drop index
idx_test_id2; 3. set lock_timeout='5s'; 4. ALTER TABLE test ALTER COLUMN
id2 TYPE varchar(128); 5. set maintenance_work_mem=''; 6.
create index concurrently idx_test_id2 on test(id2);