|
| 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/parallel.h" |
| 17 | +#include"access/xact.h" |
| 18 | +#include"commands/extension.h" |
| 19 | +#include"executor/executor.h" |
| 20 | +#include"fmgr.h" |
| 21 | +#include"foreign/foreign.h" |
| 22 | +#include"libpq/libpq.h" |
| 23 | +#include"libpq-fe.h" |
| 24 | +#include"miscadmin.h" |
| 25 | +#include"optimizer/planner.h" |
| 26 | +#include"pgstat.h" |
| 27 | +#include"postgres_fdw.h" |
| 28 | +#include"storage/latch.h" |
| 29 | +#include"tcop/utility.h" |
| 30 | +#include"utils/builtins.h" |
| 31 | +#include"utils/guc.h" |
| 32 | +#include"utils/memutils.h" |
| 33 | + |
| 34 | + |
| 35 | +PG_MODULE_MAGIC; |
| 36 | + |
| 37 | +void_PG_init(void); |
| 38 | + |
| 39 | +staticProcessUtility_hook_typenext_ProcessUtility_hook=NULL; |
| 40 | +staticExecutorStart_hook_typeprev_ExecutorStart=NULL; |
| 41 | +staticExecutorEnd_hook_typeprev_ExecutorEnd=NULL; |
| 42 | + |
| 43 | +staticvoidHOOK_Utility_injection(PlannedStmt*pstmt,constchar*queryString, |
| 44 | +ProcessUtilityContextcontext,ParamListInfoparams, |
| 45 | +DestReceiver*dest,char*completionTag); |
| 46 | +staticvoidHOOK_ExecStart_injection(QueryDesc*queryDesc,inteflags); |
| 47 | +staticvoidHOOK_ExecEnd_injection(QueryDesc*queryDesc); |
| 48 | + |
| 49 | +/* Remote instance parameters. */ |
| 50 | +char*remote_server_fdwname; |
| 51 | + |
| 52 | +staticboolExtensionIsActivated= false; |
| 53 | +staticPGconn*conn=NULL; |
| 54 | + |
| 55 | +staticOidserverid=InvalidOid; |
| 56 | +staticUserMapping*user=NULL; |
| 57 | + |
| 58 | + |
| 59 | +/* |
| 60 | + * Module load/unload callback |
| 61 | + */ |
| 62 | +void |
| 63 | +_PG_init(void) |
| 64 | +{ |
| 65 | +DefineCustomStringVariable("repeater.fdwname", |
| 66 | +"Remote host fdw name", |
| 67 | +NULL, |
| 68 | +&remote_server_fdwname, |
| 69 | +"remoteserv", |
| 70 | +PGC_SIGHUP, |
| 71 | +GUC_NOT_IN_SAMPLE, |
| 72 | +NULL, |
| 73 | +NULL, |
| 74 | +NULL); |
| 75 | + |
| 76 | +/* ProcessUtility hook */ |
| 77 | +next_ProcessUtility_hook=ProcessUtility_hook; |
| 78 | +ProcessUtility_hook=HOOK_Utility_injection; |
| 79 | + |
| 80 | +prev_ExecutorStart=ExecutorStart_hook; |
| 81 | +ExecutorStart_hook=HOOK_ExecStart_injection; |
| 82 | + |
| 83 | +prev_ExecutorEnd=ExecutorEnd_hook; |
| 84 | +ExecutorEnd_hook=HOOK_ExecEnd_injection; |
| 85 | +} |
| 86 | + |
| 87 | +staticbool |
| 88 | +ExtensionIsActive(void) |
| 89 | +{ |
| 90 | +if (ExtensionIsActivated) |
| 91 | +return true; |
| 92 | + |
| 93 | +if ( |
| 94 | +!IsTransactionState()|| |
| 95 | +!OidIsValid(get_extension_oid("pg_repeater", true)) |
| 96 | +) |
| 97 | +return false; |
| 98 | + |
| 99 | +ExtensionIsActivated= true; |
| 100 | +returnExtensionIsActivated; |
| 101 | +} |
| 102 | + |
| 103 | +/* |
| 104 | + * We need to send some DML queries for sync database schema to a plan execution |
| 105 | + * at a remote instance. |
| 106 | + */ |
| 107 | +staticvoid |
| 108 | +HOOK_Utility_injection(PlannedStmt*pstmt, |
| 109 | +constchar*queryString, |
| 110 | +ProcessUtilityContextcontext, |
| 111 | +ParamListInfoparams, |
| 112 | +DestReceiver*dest, |
| 113 | +char*completionTag) |
| 114 | +{ |
| 115 | +Node*parsetree=pstmt->utilityStmt; |
| 116 | + |
| 117 | +if (ExtensionIsActive()&& |
| 118 | +pstmt->canSetTag&& |
| 119 | +(context!=PROCESS_UTILITY_SUBCOMMAND) |
| 120 | +) |
| 121 | +{ |
| 122 | +if (!user) |
| 123 | +{ |
| 124 | +MemoryContextoldCxt=MemoryContextSwitchTo(TopMemoryContext); |
| 125 | + |
| 126 | +serverid=get_foreign_server_oid(remote_server_fdwname, true); |
| 127 | +Assert(OidIsValid(serverid)); |
| 128 | + |
| 129 | +user=GetUserMapping(GetUserId(),serverid); |
| 130 | +MemoryContextSwitchTo(oldCxt); |
| 131 | +} |
| 132 | +switch (nodeTag(parsetree)) |
| 133 | +{ |
| 134 | +caseT_CopyStmt: |
| 135 | +caseT_CreateExtensionStmt: |
| 136 | +caseT_ExplainStmt: |
| 137 | +caseT_FetchStmt: |
| 138 | +caseT_VacuumStmt: |
| 139 | +break; |
| 140 | +default: |
| 141 | +{ |
| 142 | +PGresult*res; |
| 143 | + |
| 144 | +if (nodeTag(parsetree)==T_TransactionStmt) |
| 145 | +{ |
| 146 | +TransactionStmt*stmt= (TransactionStmt*)parsetree; |
| 147 | + |
| 148 | +if ( |
| 149 | +/*(stmt->kind != TRANS_STMT_ROLLBACK_TO) && */ |
| 150 | +(stmt->kind!=TRANS_STMT_SAVEPOINT) |
| 151 | +) |
| 152 | +break; |
| 153 | +} |
| 154 | +conn=GetConnection(user, true); |
| 155 | +Assert(conn!=NULL); |
| 156 | + |
| 157 | +res=PQexec(conn,queryString); |
| 158 | +PQclear(res); |
| 159 | +} |
| 160 | +break; |
| 161 | +} |
| 162 | +} |
| 163 | + |
| 164 | +if (next_ProcessUtility_hook) |
| 165 | +(*next_ProcessUtility_hook) (pstmt,queryString,context,params, |
| 166 | +dest,completionTag); |
| 167 | +else |
| 168 | +standard_ProcessUtility(pstmt,queryString, |
| 169 | +context,params, |
| 170 | +dest,completionTag); |
| 171 | +} |
| 172 | + |
| 173 | +staticvoid |
| 174 | +HOOK_ExecStart_injection(QueryDesc*queryDesc,inteflags) |
| 175 | +{ |
| 176 | +Node*parsetree=queryDesc->plannedstmt->utilityStmt; |
| 177 | + |
| 178 | +if (prev_ExecutorStart) |
| 179 | +prev_ExecutorStart(queryDesc,eflags); |
| 180 | +else |
| 181 | +standard_ExecutorStart(queryDesc,eflags); |
| 182 | + |
| 183 | +/* |
| 184 | + * This not fully correct sign for prevent passing each subquery to the |
| 185 | + * remote instance. Only for demo. |
| 186 | + */ |
| 187 | +if (ExtensionIsActive()&& |
| 188 | +queryDesc->plannedstmt->canSetTag&& |
| 189 | +!IsParallelWorker()&& |
| 190 | +((parsetree==NULL)|| (nodeTag(parsetree)!=T_CreatedbStmt))&& |
| 191 | +!(eflags&EXEC_FLAG_EXPLAIN_ONLY)) |
| 192 | +{ |
| 193 | +Oidserverid; |
| 194 | +UserMapping*user; |
| 195 | +char*query, |
| 196 | +*query_container, |
| 197 | +*plan, |
| 198 | +*plan_container; |
| 199 | +intqlen, |
| 200 | +qlen1, |
| 201 | +plen, |
| 202 | +plen1; |
| 203 | +PGresult*res; |
| 204 | + |
| 205 | +serverid=get_foreign_server_oid(remote_server_fdwname, true); |
| 206 | +Assert(OidIsValid(serverid)); |
| 207 | + |
| 208 | +user=GetUserMapping(GetUserId(),serverid); |
| 209 | +conn=GetConnection(user, true); |
| 210 | + |
| 211 | +set_portable_output(true); |
| 212 | +plan=nodeToString(queryDesc->plannedstmt); |
| 213 | +set_portable_output(false); |
| 214 | +plen=b64_enc_len(plan,strlen(plan)+1); |
| 215 | +plan_container= (char*)palloc0(plen+1); |
| 216 | +plen1=b64_encode(plan,strlen(plan),plan_container); |
| 217 | +Assert(plen>plen1); |
| 218 | + |
| 219 | +qlen=b64_enc_len(queryDesc->sourceText,strlen(queryDesc->sourceText)+1); |
| 220 | +query_container= (char*)palloc0(qlen+1); |
| 221 | +qlen1=b64_encode(queryDesc->sourceText,strlen(queryDesc->sourceText),query_container); |
| 222 | +Assert(qlen>qlen1); |
| 223 | + |
| 224 | +query=palloc0(qlen+plen+100); |
| 225 | +sprintf(query,"SELECT public.pg_exec_plan('%s', '%s');",query_container,plan_container); |
| 226 | + |
| 227 | +res=PQexec(conn,query); |
| 228 | +PQclear(res); |
| 229 | +pfree(query); |
| 230 | +pfree(query_container); |
| 231 | +pfree(plan_container); |
| 232 | +} |
| 233 | +} |
| 234 | + |
| 235 | +staticvoid |
| 236 | +HOOK_ExecEnd_injection(QueryDesc*queryDesc) |
| 237 | +{ |
| 238 | +if (prev_ExecutorEnd) |
| 239 | +prev_ExecutorEnd(queryDesc); |
| 240 | +else |
| 241 | +standard_ExecutorEnd(queryDesc); |
| 242 | +} |