diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml
index dd50c4475d..aaf758d495 100644
--- a/doc/src/sgml/plpython.sgml
+++ b/doc/src/sgml/plpython.sgml
@@ -956,6 +956,17 @@ foo = rv[i]["my_column"]
+
+
+ __str__()
+
+
+ The standard __str__ method is defined so that it
+ is possible for example to debug query execution results
+ using plpy.debug(rv).
+
+
+
diff --git a/src/pl/plpython/expected/plpython_spi.out b/src/pl/plpython/expected/plpython_spi.out
index 3cda9589b3..b07a429439 100644
--- a/src/pl/plpython/expected/plpython_spi.out
+++ b/src/pl/plpython/expected/plpython_spi.out
@@ -263,6 +263,24 @@ CONTEXT: PL/Python function "result_empty_test"
(1 row)
+CREATE FUNCTION result_str_test(cmd text) RETURNS text
+AS $$
+plan = plpy.prepare(cmd)
+result = plpy.execute(plan)
+return str(result)
+$$ LANGUAGE plpythonu;
+SELECT result_str_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
+ result_str_test
+--------------------------------------------------------------------------------------
+
+(1 row)
+
+SELECT result_str_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
+ result_str_test
+--------------------------------------
+
+(1 row)
+
-- cursor objects
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
res = plpy.cursor("select fname, lname from users")
diff --git a/src/pl/plpython/plpy_resultobject.c b/src/pl/plpython/plpy_resultobject.c
index ea93ad7d29..077bde6dc3 100644
--- a/src/pl/plpython/plpy_resultobject.c
+++ b/src/pl/plpython/plpy_resultobject.c
@@ -22,6 +22,7 @@ static Py_ssize_t PLy_result_length(PyObject *arg);
static PyObject *PLy_result_item(PyObject *arg, Py_ssize_t idx);
static PyObject *PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx);
static int PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice);
+static PyObject *PLy_result_str(PyObject *arg);
static PyObject *PLy_result_subscript(PyObject *arg, PyObject *item);
static int PLy_result_ass_subscript(PyObject *self, PyObject *item, PyObject *value);
@@ -74,7 +75,7 @@ static PyTypeObject PLy_ResultType = {
&PLy_result_as_mapping, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
- 0, /* tp_str */
+ &PLy_result_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
@@ -248,6 +249,26 @@ PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *
return rv;
}
+static PyObject *
+PLy_result_str(PyObject *arg)
+{
+ PLyResultObject *ob = (PLyResultObject *) arg;
+
+#if PY_MAJOR_VERSION >= 3
+ return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
+ Py_TYPE(ob)->tp_name,
+ ob->status,
+ ob->nrows,
+ ob->rows);
+#else
+ return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
+ ob->ob_type->tp_name,
+ PyInt_AsLong(ob->status),
+ PyInt_AsLong(ob->nrows),
+ PyString_AsString(PyObject_Str(ob->rows)));
+#endif
+}
+
static PyObject *
PLy_result_subscript(PyObject *arg, PyObject *item)
{
diff --git a/src/pl/plpython/sql/plpython_spi.sql b/src/pl/plpython/sql/plpython_spi.sql
index 6250e90d19..7a84473803 100644
--- a/src/pl/plpython/sql/plpython_spi.sql
+++ b/src/pl/plpython/sql/plpython_spi.sql
@@ -169,6 +169,16 @@ $$ LANGUAGE plpythonu;
SELECT result_empty_test();
+CREATE FUNCTION result_str_test(cmd text) RETURNS text
+AS $$
+plan = plpy.prepare(cmd)
+result = plpy.execute(plan)
+return str(result)
+$$ LANGUAGE plpythonu;
+
+SELECT result_str_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
+SELECT result_str_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
+
-- cursor objects
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$