|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * repeater.c |
| 4 | + * Simple demo for remote plan execution patch. |
| 5 | + * |
| 6 | + * Transfer query plan to a remote instance and wait for result. |
| 7 | + * Remote instance parameters (host, port) defines by GUCs. |
| 8 | + * |
| 9 | + * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group |
| 10 | + * Portions Copyright (c) 2018-2019, Postgres Professional |
| 11 | + *------------------------------------------------------------------------- |
| 12 | + */ |
| 13 | + |
| 14 | +#include"postgres.h" |
| 15 | + |
| 16 | +#include"access/xact.h" |
| 17 | +#include"commands/extension.h" |
| 18 | +#include"executor/executor.h" |
| 19 | +#include"fmgr.h" |
| 20 | +#include"libpq/libpq.h" |
| 21 | +#include"libpq-fe.h" |
| 22 | +#include"optimizer/planner.h" |
| 23 | +#include"tcop/utility.h" |
| 24 | +#include"utils/guc.h" |
| 25 | + |
| 26 | +PG_MODULE_MAGIC; |
| 27 | + |
| 28 | +void_PG_init(void); |
| 29 | + |
| 30 | +staticProcessUtility_hook_typenext_ProcessUtility_hook=NULL; |
| 31 | +staticExecutorStart_hook_typeprev_ExecutorStart=NULL; |
| 32 | +staticExecutorEnd_hook_typeprev_ExecutorEnd=NULL; |
| 33 | + |
| 34 | +staticvoidHOOK_Utility_injection(PlannedStmt*pstmt,constchar*queryString, |
| 35 | +ProcessUtilityContextcontext,ParamListInfoparams, |
| 36 | +QueryEnvironment*queryEnv,DestReceiver*dest, |
| 37 | +char*completionTag); |
| 38 | +staticvoidHOOK_ExecStart_injection(QueryDesc*queryDesc,inteflags); |
| 39 | +staticvoidHOOK_ExecEnd_injection(QueryDesc*queryDesc); |
| 40 | + |
| 41 | +/* Remote instance parameters. */ |
| 42 | +char*remote_server_fdwname; |
| 43 | + |
| 44 | +staticboolExtensionIsActivated= false; |
| 45 | +staticPGconn*conn=NULL; |
| 46 | + |
| 47 | +/* |
| 48 | + * Module load/unload callback |
| 49 | + */ |
| 50 | +void |
| 51 | +_PG_init(void) |
| 52 | +{ |
| 53 | +DefineCustomStringVariable("repeater.fdwname", |
| 54 | +"Remote host fdw name", |
| 55 | +NULL, |
| 56 | +&remote_server_fdwname, |
| 57 | +"remoteserv", |
| 58 | +PGC_SIGHUP, |
| 59 | +GUC_NOT_IN_SAMPLE, |
| 60 | +NULL, |
| 61 | +NULL, |
| 62 | +NULL); |
| 63 | + |
| 64 | +/* ProcessUtility hook */ |
| 65 | +next_ProcessUtility_hook=ProcessUtility_hook; |
| 66 | +ProcessUtility_hook=HOOK_Utility_injection; |
| 67 | + |
| 68 | +prev_ExecutorStart=ExecutorStart_hook; |
| 69 | +ExecutorStart_hook=HOOK_ExecStart_injection; |
| 70 | + |
| 71 | +prev_ExecutorEnd=ExecutorEnd_hook; |
| 72 | +ExecutorEnd_hook=HOOK_ExecEnd_injection; |
| 73 | +} |
| 74 | + |
| 75 | +staticbool |
| 76 | +ExtensionIsActive(void) |
| 77 | +{ |
| 78 | +if (ExtensionIsActivated) |
| 79 | +return true; |
| 80 | + |
| 81 | +if ( |
| 82 | +!IsTransactionState()|| |
| 83 | +!OidIsValid(get_extension_oid("repeater", true)) |
| 84 | +) |
| 85 | +return false; |
| 86 | + |
| 87 | +ExtensionIsActivated= true; |
| 88 | +returnExtensionIsActivated; |
| 89 | +} |
| 90 | + |
| 91 | +#include"miscadmin.h" |
| 92 | +#include"pgstat.h" |
| 93 | +#include"storage/latch.h" |
| 94 | + |
| 95 | +#include"foreign/foreign.h" |
| 96 | +#include"postgres_fdw.h" |
| 97 | + |
| 98 | +staticOidserverid=InvalidOid; |
| 99 | +staticUserMapping*user=NULL; |
| 100 | + |
| 101 | +staticbool |
| 102 | +pgfdw_cancel_query(PGconn*conn) |
| 103 | +{ |
| 104 | +PGcancel*cancel; |
| 105 | +charerrbuf[256]; |
| 106 | +PGresult*result=NULL; |
| 107 | + |
| 108 | +if ((cancel=PQgetCancel(conn))) |
| 109 | +{ |
| 110 | +if (!PQcancel(cancel,errbuf,sizeof(errbuf))) |
| 111 | +{ |
| 112 | +ereport(WARNING, |
| 113 | +(errcode(ERRCODE_CONNECTION_FAILURE), |
| 114 | +errmsg("could not send cancel request: %s", |
| 115 | +errbuf))); |
| 116 | +PQfreeCancel(cancel); |
| 117 | +return false; |
| 118 | +} |
| 119 | + |
| 120 | +PQfreeCancel(cancel); |
| 121 | +} |
| 122 | +else |
| 123 | +elog(FATAL,"Can't get connection cancel descriptor"); |
| 124 | + |
| 125 | +PQconsumeInput(conn); |
| 126 | +PQclear(result); |
| 127 | + |
| 128 | +return true; |
| 129 | +} |
| 130 | + |
| 131 | +staticvoid |
| 132 | +cancelQueryIfNeeded(PGconn*conn,constchar*query) |
| 133 | +{ |
| 134 | +Assert(conn!=NULL); |
| 135 | +Assert(query!=NULL); |
| 136 | + |
| 137 | +if (PQtransactionStatus(conn)!=PQTRANS_IDLE) |
| 138 | +{ |
| 139 | +PGresult*res; |
| 140 | + |
| 141 | +printf("CONN status BEFORE EXEC: %d, txs: %d errmsg: %s\n", |
| 142 | +PQstatus(conn), |
| 143 | +PQtransactionStatus(conn), |
| 144 | +PQerrorMessage(conn)); |
| 145 | + |
| 146 | +res=PQgetResult(conn); |
| 147 | + |
| 148 | +if (PQresultStatus(res)==PGRES_FATAL_ERROR) |
| 149 | +Assert(pgfdw_cancel_query(conn)); |
| 150 | +else |
| 151 | +pgfdw_get_result(conn,query); |
| 152 | +} |
| 153 | + |
| 154 | +} |
| 155 | + |
| 156 | +/* |
| 157 | + * We need to send some DML queries for sync database schema to a plan execution |
| 158 | + * at a remote instance. |
| 159 | + */ |
| 160 | +staticvoid |
| 161 | +HOOK_Utility_injection(PlannedStmt*pstmt, |
| 162 | +constchar*queryString, |
| 163 | +ProcessUtilityContextcontext, |
| 164 | +ParamListInfoparams, |
| 165 | +QueryEnvironment*queryEnv, |
| 166 | +DestReceiver*dest, |
| 167 | +char*completionTag) |
| 168 | +{ |
| 169 | +Node*parsetree=pstmt->utilityStmt; |
| 170 | + |
| 171 | +if (ExtensionIsActive()&& |
| 172 | +pstmt->canSetTag&& |
| 173 | +(context!=PROCESS_UTILITY_SUBCOMMAND) |
| 174 | + ) |
| 175 | +{ |
| 176 | +if (!user) |
| 177 | +{ |
| 178 | +MemoryContextoldCxt=MemoryContextSwitchTo(TopMemoryContext); |
| 179 | + |
| 180 | +serverid=get_foreign_server_oid(remote_server_fdwname, true); |
| 181 | +Assert(OidIsValid(serverid)); |
| 182 | + |
| 183 | +user=GetUserMapping(GetUserId(),serverid); |
| 184 | +MemoryContextSwitchTo(oldCxt); |
| 185 | +} |
| 186 | +switch (nodeTag(parsetree)) |
| 187 | +{ |
| 188 | +caseT_CopyStmt: |
| 189 | +caseT_CreateExtensionStmt: |
| 190 | +caseT_ExplainStmt: |
| 191 | +caseT_FetchStmt: |
| 192 | +caseT_VacuumStmt: |
| 193 | +break; |
| 194 | +default: |
| 195 | +if (nodeTag(parsetree)==T_TransactionStmt) |
| 196 | +{ |
| 197 | +TransactionStmt*stmt= (TransactionStmt*)parsetree; |
| 198 | + |
| 199 | +if ( |
| 200 | +//(stmt->kind != TRANS_STMT_ROLLBACK_TO) && |
| 201 | +(stmt->kind!=TRANS_STMT_SAVEPOINT) |
| 202 | +) |
| 203 | +break; |
| 204 | +} |
| 205 | +if (conn) |
| 206 | +cancelQueryIfNeeded(conn,queryString); |
| 207 | +conn=GetConnection(user, true); |
| 208 | +cancelQueryIfNeeded(conn,queryString); |
| 209 | +Assert(conn!=NULL); |
| 210 | + |
| 211 | +Assert(PQsendQuery(conn,queryString)); |
| 212 | +break; |
| 213 | +}; |
| 214 | +} |
| 215 | + |
| 216 | +if (next_ProcessUtility_hook) |
| 217 | +(*next_ProcessUtility_hook) (pstmt,queryString,context,params, |
| 218 | +queryEnv,dest,completionTag); |
| 219 | +else |
| 220 | +standard_ProcessUtility(pstmt,queryString, |
| 221 | +context,params,queryEnv, |
| 222 | +dest,completionTag); |
| 223 | +if (conn) |
| 224 | +cancelQueryIfNeeded(conn,queryString); |
| 225 | +} |
| 226 | + |
| 227 | +staticvoid |
| 228 | +HOOK_ExecStart_injection(QueryDesc*queryDesc,inteflags) |
| 229 | +{ |
| 230 | +Node*parsetree=queryDesc->plannedstmt->utilityStmt; |
| 231 | + |
| 232 | +if (prev_ExecutorStart) |
| 233 | +prev_ExecutorStart(queryDesc,eflags); |
| 234 | +else |
| 235 | +standard_ExecutorStart(queryDesc,eflags); |
| 236 | + |
| 237 | +/* |
| 238 | + * This not fully correct sign for prevent passing each subquery to |
| 239 | + * the remote instance. Only for demo. |
| 240 | + */ |
| 241 | +if (ExtensionIsActive()&& |
| 242 | +queryDesc->plannedstmt->canSetTag&& |
| 243 | +((parsetree==NULL)|| (nodeTag(parsetree)!=T_CreatedbStmt))&& |
| 244 | +!(eflags&EXEC_FLAG_EXPLAIN_ONLY)) |
| 245 | +{ |
| 246 | +Oidserverid; |
| 247 | +UserMapping*user; |
| 248 | + |
| 249 | +serverid=get_foreign_server_oid(remote_server_fdwname, true); |
| 250 | +Assert(OidIsValid(serverid)); |
| 251 | + |
| 252 | +user=GetUserMapping(GetUserId(),serverid); |
| 253 | +conn=GetConnection(user, true); |
| 254 | +cancelQueryIfNeeded(conn,queryDesc->sourceText); |
| 255 | + |
| 256 | +if (PQsendPlan(conn,serialize_plan(queryDesc,eflags))==0) |
| 257 | +pgfdw_report_error(ERROR,NULL,conn, false,queryDesc->sourceText); |
| 258 | +} |
| 259 | +} |
| 260 | + |
| 261 | +staticvoid |
| 262 | +HOOK_ExecEnd_injection(QueryDesc*queryDesc) |
| 263 | +{ |
| 264 | +if (conn) |
| 265 | +cancelQueryIfNeeded(conn,queryDesc->sourceText); |
| 266 | + |
| 267 | +if (prev_ExecutorEnd) |
| 268 | +prev_ExecutorEnd(queryDesc); |
| 269 | +else |
| 270 | +standard_ExecutorEnd(queryDesc); |
| 271 | +} |