jackc / pgtype

MIT License
300 stars 111 forks source link

feature: support void type #171

Closed sasakiyori closed 2 years ago

sasakiyori commented 2 years ago

When executing select pg_sleep(1), one result row was returned. The oid type value of the result is 2278, which is a void type.

postgres=# select pg_sleep(1);
 pg_sleep
----------

(1 row)

When using pgx to exec the sql above, there is not a suited pgtype for scanning.

// no suited pgtype for output
var out Void
row := conn.QueryRow(context.Background(), "select pg_sleep(1);")
err := row.Scan(&out)

Maybe we no need to use QueryRow for this?

I don't know if there are more scenarios for using the void type. But if void can be added into pgtype enum list, that will be better.

jackc commented 2 years ago

Maybe we no need to use QueryRow for this?

Correct. In this particular example Exec would be better.

I don't know if there are more scenarios for using the void type.

Since void is a type that only has one value (or no values depending on how you look at it), there is no point scanning a void type. However, if you somehow had a query that returned a real value and a void (e.g. select now(), pg_sleep(1)) you handle the void by passing nil to Scan() for the void .

e.g.

var t time.Time
row := conn.QueryRow(context.Background(), "select now(), pg_sleep(1)")
err := row.Scan(&t, nil)
sasakiyori commented 2 years ago

Thanks! This way to process void is good enough.