Skip to content

Custom types - getting "ERROR: type 'x' already exists" in multiple cases #1076

@nguiard

Description

@nguiard

Hi,
Thanks for this awesome library.

I'm fooling around with custom types.

  • This works:
use pgx::prelude::*;
use serde::{Serialize, Deserialize};

pgx::pg_module_magic!();


#[derive(Debug, Serialize, Deserialize, PostgresType)]
pub struct A;

#[pg_extern]
fn myfunc() -> A {
    A
}
  • However, this does not anymore, when you wrap the return in a Result
use pgx::prelude::*;
use pgx::spi;
use serde::{Serialize, Deserialize};

pgx::pg_module_magic!();


#[derive(Debug, Serialize, Deserialize, PostgresType)]
pub struct A;

#[pg_extern]
fn myfunc() -> Result<A, spi::Error> {
    Ok(A)
}

It compiles fine of course, but when you try to create extension, you get ERROR: type "a" already exists. (also, it's not dependent on spi, changing spi::Error to std::error::Error produces the same result).

It seems that this is tied to ordering problems in the generated sql file. Because this also fails with the same error:

use pgx::prelude::*;
use pgx::spi;
use serde::{Serialize, Deserialize};

pgx::pg_module_magic!();


#[derive(Debug, Serialize, Deserialize, PostgresType)]
pub struct B;

#[derive(Debug, Serialize, Deserialize, PostgresType)]
pub struct A;

#[pg_extern]
fn myfunc(_x: B) -> Result<A, spi::Error> {
    Ok(A)
}

But, rename struct A to struct Z, and it now works!

use pgx::prelude::*;
use pgx::spi;
use serde::{Serialize, Deserialize};

pgx::pg_module_magic!();


#[derive(Debug, Serialize, Deserialize, PostgresType)]
pub struct B;

#[derive(Debug, Serialize, Deserialize, PostgresType)]
pub struct Z;

#[pg_extern]
fn myfunc(_x: B) -> Result<Z, spi::Error> {
    Ok(Z)
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions