jeroenrinzema / psql-wire

PostgreSQL server wire protocol. Build your own server and start serving connections.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Supporting different forms of prepared statement query params

kishaningithub opened this issue · comments

As of now if i give a prepared statement of the below form and i pass in the $1 argument, it works as expected

    select * 
    from (
		select 1 id
		union all
		select 2 id
		union all
		select 3 id
    )
    where id > $1

but if i send the below query I get the error sql: expected 0 arguments, got 1 from the client side (notice i am using ? for params)

    select * 
    from (
		select 1 id
		union all
		select 2 id
		union all
		select 3 id
    )
    where id > ?

Logs from psql-wire

2022-12-12T11:16:45.662+0530	INFO	psql-wire@v0.4.0/wire.go:88	serving incoming connections	{"addr": "[::]:35432"}
2022-12-12T11:16:45.665+0530	DEBUG	psql-wire@v0.4.0/wire.go:126	serving a new client connection
2022-12-12T11:16:45.666+0530	DEBUG	psql-wire@v0.4.0/handshake.go:137	attempting to upgrade the client to a TLS connection
2022-12-12T11:16:45.666+0530	DEBUG	psql-wire@v0.4.0/handshake.go:140	no TLS certificates available continuing with a insecure connection
2022-12-12T11:16:45.666+0530	ERROR	psql-wire@v0.4.0/wire.go:116	an unexpected error got returned while serving a client connection	{"error": "EOF"}
github.com/jeroenrinzema/psql-wire.(*Server).Serve.func2
	/Users/kishanb/go/pkg/mod/github.com/jeroenrinzema/psql-wire@v0.4.0/wire.go:116
2022-12-12T11:16:45.666+0530	DEBUG	psql-wire@v0.4.0/wire.go:126	serving a new client connection
2022-12-12T11:16:45.666+0530	DEBUG	psql-wire@v0.4.0/handshake.go:137	attempting to upgrade the client to a TLS connection
2022-12-12T11:16:45.667+0530	DEBUG	psql-wire@v0.4.0/handshake.go:140	no TLS certificates available continuing with a insecure connection
2022-12-12T11:16:45.667+0530	ERROR	psql-wire@v0.4.0/wire.go:116	an unexpected error got returned while serving a client connection	{"error": "EOF"}
github.com/jeroenrinzema/psql-wire.(*Server).Serve.func2
	/Users/kishanb/go/pkg/mod/github.com/jeroenrinzema/psql-wire@v0.4.0/wire.go:116
2022-12-12T11:16:45.667+0530	DEBUG	psql-wire@v0.4.0/wire.go:126	serving a new client connection
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/wire.go:137	handshake successfull, validating authentication
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:68	reading client parameters
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:86	client parameter	{"key": "user", "value": "postgres"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:86	client parameter	{"key": "database", "value": "postgres"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/auth.go:33	authenticating client connection
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/wire.go:150	connection authenticated, writing server parameters
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:102	writing server parameters
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:113	server parameter	{"key": "is_superuser", "value": "off"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:113	server parameter	{"key": "session_authorization", "value": "postgres"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:113	server parameter	{"key": "server_encoding", "value": "UTF8"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/handshake.go:113	server parameter	{"key": "client_encoding", "value": "UTF8"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/command.go:39	ready for query... starting to consume commands
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/command.go:65	incoming command	{"length": 149, "type": "P"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/command.go:270	incoming extended query	{"query": "\n      select * \n      from (\n\t\t  select 1 id\n\t\t  union all\n\t\t  select 2 id\n\t\t  union all\n\t\t  select 3 id\n      )\n      where id > ?\n   ", "name": "pgx_0", "parameters": 0}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/command.go:65	incoming command	{"length": 11, "type": "D"}
2022-12-12T11:16:45.668+0530	DEBUG	psql-wire@v0.4.0/command.go:65	incoming command	{"length": 4, "type": "S"}
    rdapp_test.go:94: 
        	Error Trace:	/Users/kishanb/Programming/Personal/rdapp/componenttest/rdapp_test.go:94
        	Error:      	Received unexpected error:
        	            	sql: expected 0 arguments, got 1
        	Test:       	TestRedshiftDataAPIProxyTestSuite/TestPreparedStatementQueryExecution
--- FAIL: TestRedshiftDataAPIProxyTestSuite (0.01s)
    --- FAIL: TestRedshiftDataAPIProxyTestSuite/TestPreparedStatementQueryExecution (0.01s)


FAIL
2022-12-12T11:16:45.669+0530	DEBUG	psql-wire@v0.4.0/command.go:65	incoming command	{"length": 23, "type": "Q"}
2022-12-12T11:16:45.669+0530	DEBUG	psql-wire@v0.4.0/command.go:216	incoming simple query	{"query": "deallocate \"pgx_0\""}

If you notice the log line incoming extended query it says parameters as 0

This can be simulated by using the example code from the stdlib database/sql package

Link to code: https://pkg.go.dev/database/sql#DB.Prepare (screenshot below for easy access)

image

Thanks for reporting this! I think this is due to the PositionalParameter regex not including question marks. I will update the regex and check if this resolves your issue.

@kishaningithub I have just opened a PR would you be able to check it out and see if this resolves your issue?