Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10)); - Mailing list pgsql-hackers

From Yulin PEI
Subject Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10));
Date
Msg-id HK0PR01MB22744393C474D503E16C8509F4709@HK0PR01MB2274.apcprd01.prod.exchangelabs.com
Whole thread Raw
Responses Re: Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10));
List pgsql-hackers
HI hackers,
    I found it could cause a crash when executing sql statement: `CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10)); ` in postgres 13.2 release.

    The crash happens at view.c:89 and I did some analysis:

``` 
ColumnDef  *def = makeColumnDef(tle->resname,
                        exprType((Node *) tle->expr),
                        exprTypmod((Node *) tle->expr),
                        exprCollation((Node *) tle->expr));



/*
 * It's possible that the column is of a collatable type but the
 * collation could not be resolved, so double-check.
 */
// Here is the analysis:
//example :  ('4' COLLATE "C")::INT

//exprCollation((Node *) tle->expr) is the oid of collate "COLLATE 'C'" so def->collOid is valid
//exprType((Node *) tle->expr)) is 23 which is the oid of type int4.
//We know that int4 is not collatable by calling type_is_collatable()
if (type_is_collatable(exprType((Node *) tle->expr)))
{
   if (!OidIsValid(def->collOid))
      ereport(ERROR,
            (errcode(ERRCODE_INDETERMINATE_COLLATION),
             errmsg("could not determine which collation to use for view column \"%s\"",
                  def->colname),
             errhint("Use the COLLATE clause to set the collation explicitly.")));
}
else
   // So we are here! int is not collatable and def->collOid is valid.
   Assert(!OidIsValid(def->collOid));
```

I am not sure whether to fix this bug in function DefineVirtualRelation or to fix this bug in parse tree and analyze procedure, so maybe we can discuss.




Best Regard!
Yulin PEI


pgsql-hackers by date:

Previous
From: Erik Rijkers
Date:
Subject: Re: SQL/JSON: JSON_TABLE
Next
From: vignesh C
Date:
Subject: Re: Replication slot stats misgivings