|
| 1 | +''' |
| 2 | +common.py |
| 3 | +Copyright (c) 2016-2019, Postgres Professional |
| 4 | +''' |
| 5 | + |
| 6 | +importpsycopg2 |
| 7 | +importpsycopg2.extensions |
| 8 | +importselect |
| 9 | + |
| 10 | +# Some queries from TPC-DS may freeze or be even broken, |
| 11 | +# so we allow some sort of failure, since we do not test |
| 12 | +# Postgres, but rather that pg_query_state do not crash |
| 13 | +# anything under stress load. |
| 14 | +MAX_PG_QS_RETRIES=50 |
| 15 | + |
| 16 | + |
| 17 | +defwait(conn): |
| 18 | +"""wait for some event on connection to postgres""" |
| 19 | +while1: |
| 20 | +state=conn.poll() |
| 21 | +ifstate==psycopg2.extensions.POLL_OK: |
| 22 | +break |
| 23 | +elifstate==psycopg2.extensions.POLL_WRITE: |
| 24 | +select.select([], [conn.fileno()], []) |
| 25 | +elifstate==psycopg2.extensions.POLL_READ: |
| 26 | +select.select([conn.fileno()], [], []) |
| 27 | +else: |
| 28 | +raisepsycopg2.OperationalError("poll() returned %s"%state) |
| 29 | + |
| 30 | +defn_async_connect(config,n=1): |
| 31 | +"""establish n asynchronious connections to the postgres with specified config""" |
| 32 | + |
| 33 | +aconfig=config.copy() |
| 34 | +aconfig['async']=True |
| 35 | + |
| 36 | +result= [] |
| 37 | +for_inrange(n): |
| 38 | +conn=psycopg2.connect(**aconfig) |
| 39 | +wait(conn) |
| 40 | +result.append(conn) |
| 41 | +returnresult |
| 42 | + |
| 43 | +defn_close(conns): |
| 44 | +"""close connections to postgres""" |
| 45 | + |
| 46 | +forconninconns: |
| 47 | +conn.close() |
| 48 | + |
| 49 | +defpg_query_state(config,pid,verbose=False,costs=False,timing=False, \ |
| 50 | +buffers=False,triggers=False,format='text', \ |
| 51 | +stress_in_progress=False): |
| 52 | +""" |
| 53 | +Get query state from backend with specified pid and optional parameters. |
| 54 | +Save any warning, info, notice and log data in global variable 'notices' |
| 55 | +""" |
| 56 | + |
| 57 | +conn=psycopg2.connect(**config) |
| 58 | +curs=conn.cursor() |
| 59 | + |
| 60 | +ifstress_in_progress: |
| 61 | +set_guc(conn,'statement_timeout',TPC_DS_STATEMENT_TIMEOUT) |
| 62 | +n_retries=0 |
| 63 | + |
| 64 | +result= [] |
| 65 | +whilenotresult: |
| 66 | +curs.callproc('pg_query_state', (pid,verbose,costs,timing,buffers,triggers,format)) |
| 67 | +result=curs.fetchall() |
| 68 | + |
| 69 | +ifstress_in_progress: |
| 70 | +n_retries+=1 |
| 71 | +ifn_retries>=MAX_PG_QS_RETRIES: |
| 72 | +print('\npg_query_state tried %s times with no effect, giving up'%MAX_PG_QS_RETRIES) |
| 73 | +break |
| 74 | + |
| 75 | +notices=conn.notices[:] |
| 76 | +conn.close() |
| 77 | +returnresult,notices |
| 78 | + |
| 79 | +defquery_state(config,async_conn,query,args={},num_workers=0,stress_in_progress=False): |
| 80 | +""" |
| 81 | +Get intermediate state of 'query' on connection 'async_conn' after number of 'steps' |
| 82 | +of node executions from start of query |
| 83 | +""" |
| 84 | + |
| 85 | +acurs=async_conn.cursor() |
| 86 | +conn=psycopg2.connect(**config) |
| 87 | +curs=conn.cursor() |
| 88 | + |
| 89 | +set_guc(async_conn,'enable_mergejoin','off') |
| 90 | +set_guc(async_conn,'max_parallel_workers_per_gather',num_workers) |
| 91 | +acurs.execute(query) |
| 92 | + |
| 93 | +# extract current state of query progress |
| 94 | +pg_qs_args= { |
| 95 | +'config':config, |
| 96 | +'pid':async_conn.get_backend_pid() |
| 97 | +} |
| 98 | +fork,vinargs.items(): |
| 99 | +pg_qs_args[k]=v |
| 100 | +result,notices=pg_query_state(**pg_qs_args) |
| 101 | +wait(async_conn) |
| 102 | + |
| 103 | +set_guc(async_conn,'pg_query_state.executor_trace','off') |
| 104 | +set_guc(async_conn,'enable_mergejoin','on') |
| 105 | + |
| 106 | +conn.close() |
| 107 | +returnresult,notices |
| 108 | + |
| 109 | +defset_guc(async_conn,param,value): |
| 110 | +acurs=async_conn.cursor() |
| 111 | +acurs.execute('set %s to %s'% (param,value)) |
| 112 | +wait(async_conn) |