age icon indicating copy to clipboard operation
age copied to clipboard

Passing agtype argument to prepared statement in Golang

Open P34K1N opened this issue 5 months ago • 0 comments

Hello, I'm trying to use AGE's prepared statement to bulk insert data into my graph. To work with my Postgres database I use standard golang sql package. My code looks like this:

data := "<some data as serialized JSON>"

query := `SELECT *
FROM cypher('some_graph', $$
	<some code>
$$, $1)
AS (v agtype);`

stmt, err := conn.PrepareContext(ctx, query)
if err != nil {
	return fmt.Errorf("error on preparing statement: %w", err)
}

_, err = stmt.ExecContext(ctx, data)
if err != nil {
	return fmt.Errorf("error on executing statement: %w", err)
}

err = stmt.Close()
if err != nil {
	return fmt.Errorf("error on closing statement: %w", err)
}

If I execute this code I get the following error: ERROR: invalid input syntax for type agtype (SQLSTATE 22P02)

If I try to cast $1 to agtype in any way (for example, $1::varchar::agtype) I get this: ERROR: third argument of cypher function must be a parameter (SQLSTATE 22023)

So I need to somehow pass my argument in a way that makes PG recognize it as agtype without any typecasting. How do I do this?

My PG version is 17 if that matters.

P34K1N avatar Sep 24 '25 07:09 P34K1N