我正在使用pgbouncer-rr在redshift集群中进行查询重写(pgbouncer调用rewrite_query.py进行重写,这是该项目更多信息的链接-https://github.com/awslabs/pgbouncer-rr-patch)。 Pgbouncer-rr基于pgbouncer,其代码被合并到pgbouncer中并启动。我已经成功地使用它来重写查询,但是当我尝试将插入语句转换为卸载到外部表时遇到了一个问题。
执行流程为pgbouncer-> rewrite.c-> pycall.c-> rewrite_query.py
在python模块中,我编写了orig和转换后的sql,这是它在日志中的外观-因此python模块能够进行转换而没有任何问题。
06:03:35AM on January 31, 2019 ---- INSERT INTO nm1(c1) select c1 from t
06:03:35AM on January 31, 2019 ---- CONVERTED TO:
06:03:35AM on January 31, 2019 ---- unload (' SELECT c1 FROM t' ) to 's3://mybucket/nm1/' iam_role 'arn:aws:iam::99999:role/RedshiftDefaultRole,arn:aws:iam::99999:role/RedshiftWriteAccess' ALLOWOVERWRITE ; insert into schema1.nm1_decoy select (1) from x.nm1;
但是当您看到pgbouncer日志时,查询返回的值不变。
2019-01-31 06:28:20.980 989 NOISE C-0x22ebe60: dev/dbuser@10.10.10.10:57222 pkt='Q' len=42
2019-01-31 06:28:20.980 989 DEBUG C-0x22ebe60: dev/dbuser@10.10.10.10:57222 rewrite_query: Username => dbuser
2019-01-31 06:28:20.980 989 DEBUG C-0x22ebe60: dev/dbuser@10.10.10.10:57222 rewrite_query: Orig Query=> INSERT INTO nm1(c1) select c1 from t
2019-01-31 06:28:21.011 989 WARNING C-0x22ebe60: dev/dbuser@10.10.10.10:57222 pValue right after the call PyString_AsString(pValue): unload (' SELECT c1 FROM t' ) to 's3:
//mybucket/nm1/' iam_role 'arn:aws:iam::99999:role/RedshiftDefaultRole,arn:aws:iam::99999:role/RedshiftWriteAccess' ALLOWOVERWRITE ; insert into schema1.nm1_decoy select (1) from crm_unload.nm1;
2019-01-31 06:28:21.011 989 WARNING C-0x22ebe60: dev/dbuser@10.10.10.10:57222 Result after PyString_AsString(pValue) and in else NULL condition: (null)
2019-01-31 06:28:21.011 989 DEBUG C-0x22ebe60: dev/dbuser@10.10.10.10:57222 query unchanged
这是代码pgbouncer / src / pycall.c,它调用rewrite_query.py模块进行转换。我不了解C数据结构及其与python的交互方式,只是将日志slog_error语句用于调试目的。看起来由于某种原因,它进入了PyString_Check(pValue)检查的else条件。当pValue是字符串时,为什么检查失败?因此,基本上是通过if PyString_Check(pValue)检查失败后返回的insert语句,而不是返回卸载查询。
pValue = PyObject_CallObject(pFunc, pArgs);
slog_warning(client,"pValue right after the call PyString_AsString(pValue): %s", PyString_AsString(pValue));
if (pValue == NULL) {
slog_error(client, "Python Function <%s> failed to return a value",
py_function);
goto finish;
}
if (PyString_Check(pValue)) {
slog_warning(client,"PyStringCheck succeeded on rewrite query return value pValue.");
res = strdup(PyString_AsString(pValue));
slog_warning(client,"Result after PyString_AsString(pValue) and strdup() call: %s",res);
} else {
res = NULL;
slog_warning(client,"Result after PyString_AsString(pValue) and in else NULL condition: %s",res);
}
答案 0 :(得分:0)
这是由于python对象类型已从str更改为Unicode。我正在使用sqlparse做解析,并且此模块正在将变量转换为unicode,并且未通过PyString_Check检查。为了解决这个问题,在调用sqlparse之后,我将变量的编码转换为python中的ascii(版本为2.7.15)-varname.encode(“ ascii”)