In PostgreSQL on how can one know whether a specific view was created by an extension?
What SQL query must be executed to find out? No manual solutions.
Check if the view shows up in \dx+ output in psql -E.
This will also show the queries that psql uses to get the result, which will help you construct a query.
Axel Fontaine pays attention to what Laurenz Albe says
with \ dx + output in psql -E. I got the following query (replace pg_stat_statements by your extension ):
SELECT c.relname FROM pg_catalog.pg_depend join pg_class c on (c.oid=pg_depend.objid) WHERE refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND deptype = 'e' AND refobjid = ( SELECT e.oid FROM pg_catalog.pg_extension e WHERE e.extname='pg_stat_statements') and c.relkind='v' ORDER BY 1;
;-)
Related
I need to see the queries that are being sent to Oracle to execute them. Can someone give me specific detailed instructions on how to do this ?
If you want to see the queries from a specific user, you can use this (assuming you have privileges to query v$session and v$sqlarea (usually through SELECT_CATALOG_ROLE)
SELECT sess.sid,
sess.username,
sqla.optimizer_mode,
sqla.hash_value,
sqla.address,
sqla.cpu_time,
sqla.elapsed_time,
sqla.sql_text
FROM v$sqlarea sqla, v$session sess
WHERE sess.sql_hash_value = sqla.hash_value
AND sess.sql_address = sqla.address
AND sess.username = 'SCOTT'
Replace SCOTT with the appropriate username in your system
Output:
544 SCOTT ALL_ROWS 2004330732 07000001064088E8 89391 131836 SELECT sess.sid, sess.username,
sqla.optimizer_mode, sqla.h
ash_value, sqla.address, s
qla.cpu_time, sqla.elapsed_time,
sqla.sql_text FROM v$sqlarea sq
la, v$session sess WHERE sess.sql_hash_
value = sqla.hash_value AND sess.sql_
address = sqla.address AND sess.usern
ame = 'SCOTT'
This query will show queries that are currently running:
select sql_text from v$sqlarea where users_executing > 0;
See documentation of V$SQLAREA
You can check and get the data if you have access to these two oracle tables/views (v$sqlarea & v$sqltext), Also accoridng to your need you can also modify the query and add A.cpu_time, A.elapsed_time if required.
Query -
SELECT A.SQL_ID,
A.FIRST_LOAD_TIME,
A.SQL_TEXT,
A.SQL_FULLTEXT
FROM v$sqlarea A, v$sqltext B
WHERE A.PARSING_SCHEMA_NAME = 'TESTUSER' --YOUR USERNAME
AND A.SQL_ID = B.SQL_ID
AND A.HASH_VALUE = B.HASH_VALUE
ORDER BY A.FIRST_LOAD_TIME DESC
Output -
I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.
I'm trying to create new view from 2 different table of same schema. This is my query, let me know if I'm missing anything. When I check the syntax it is fine and test it, throws 00933 error.
CREATE OR REPLACE FORCE VIEW "APDA"."countview"
(
"dealidint", "companyidint", "nametxt", "county", "street",
"state", "city", "zip", "geocodelatdec", "geocodelongdec",
"volidint", "reportdate", "vehicletotalint", "salvagetotalint"
)
AS
SELECT a."dealidint",
a."companyidint",
a."nametxt",
a."county",
a."street",
a."state",
a."city",
a."zip",
a."geocodelatdec",
a."geocodelongdec",
c."dealervolumeidint",
c."reportdate",
c."vehicletotalint",
c."salvagetotalint"
FROM "APDA"."company" a
JOIN
"APDA"."volume" c
ON c."dealidint" = a."dealidint";
I don't see the reason. The only suspect thing I notice is the two blank lines. In SQLPlus I don't think these would cause this error, but they would cause the command to be misinterpreted.
My suggestions are:
- try a different tool. If you are getting the error in SQL Developer, try it in SQLPlus. It won't necessarily work, but you might get different feedback.
- reduce it to a minimal statement that works, then add elements back in one at a time until the error occurs
your column name "volidint" not in selected column, i see "dealervolumeidint" select vs "volidint".
Column name "volidint" is not available in your below select query. Please correct that by using "As".
CREATE OR REPLACE FORCE VIEW "APDA"."countview"
(
"dealidint", "companyidint", "nametxt", "county", "street",
"state", "city", "zip", "geocodelatdec", "geocodelongdec",
"volidint", "reportdate", "vehicletotalint", "salvagetotalint"
)
AS
SELECT a."dealidint",
a."companyidint",
a."nametxt",
a."county",
a."street",
a."state",
a."city",
a."zip",
a."geocodelatdec",
a."geocodelongdec",
c."dealervolumeidint" as "volidint",
c."reportdate",
c."vehicletotalint",
c."salvagetotalint"
FROM "APDA"."company" a
JOIN
"APDA"."volume" c
ON c."dealidint" = a."dealidint";
Thanks to all you pitched to answer my query. The one I have shared is a stripped down query with the same syntax. The query was fine but my actual query has an extra character on the join. Which is like - "APDA"."vvolume".
After that I was able to create the view.
Thanks again.
I want to get the list of arguments of a stored procedure in postgresql, is there any query to get the arguments ?
I got one system procedure to do the same, which is :
SELECT pg_get_function_identity_arguments('funcname'::regproc);
But here when there are more than one functions with same name then it will return an error.
Any solutions??
Thanks in advance..
It's too late for OP but anyway: I searched answer today myself and did not found needed answer.
I came up with this solution:
SELECT
pronamespace::regnamespace,
proname,
pg_get_function_arguments(oid) AS args_def, -- full definition of arguments with defaults
UNNEST(string_to_array(pg_get_function_identity_arguments(oid), ',' )) AS arg -- each argument with type
FROM
pg_proc
Works in postgresql 11.x, I suppose that must work in other versions too but not tested.
Hope this helps somebody in future.
For my tool that reports PostgreSQL schema I use something like:
SELECT CASE
WHEN pg_proc.proretset
THEN 'setof ' || pg_catalog.format_type(pg_proc.prorettype, NULL)
ELSE pg_catalog.format_type(pg_proc.prorettype, NULL) END,
pg_proc.proargtypes,
pg_proc.proargnames,
pg_proc.prosrc,
pg_proc.proallargtypes,
pg_proc.proargmodes,
pg_language.lanname
FROM pg_catalog.pg_proc
JOIN pg_catalog.pg_namespace ON (pg_proc.pronamespace = pg_namespace.oid)
JOIN pg_catalog.pg_language ON (pg_proc.prolang = pg_language.oid)
WHERE
pg_proc.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
AND (pg_proc.proargtypes[0] IS NULL
OR pg_proc.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)
AND NOT pg_proc.proisagg
AND pg_proc.proname ilike 'MY_FUNCTION_NAME'
AND pg_namespace.nspname = 'public'
AND pg_catalog.pg_function_is_visible(pg_proc.oid);
See 2nd and 3rd column of its result.
Here is a clean version:
SELECT
proname,
unnest(proargnames) as arguments,
unnest(string_to_array((oidvectortypes(proargtypes)), ',')) as
arguments_type
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p ON pronamespace = n.oid
JOIN pg_type t ON p.prorettype = t.oid
WHERE nspname = 'public'
group by proname, proargtypes, proargnames;
I am using BIDS to connect to a Progress DB through an ODBC connection:
This query works fine
SELECT
PUB."master"."app-number",
...
PUB."property"."prop-id",
FROM
PUB."master" master JOIN PUB."property" property ON
master."lt-acnt" = property."lt-acnt"
...
LEFT OUTER JOIN PUB."arm" arm ON
master."lt-acnt" = arm."lt-acnt"
WHERE
...
However, I need to add some additional fields from another table. The problem is that I only need the information from the last time these new fields were updated.
I have tried:
SELECT
yt."app-number"
...
yt."disc-adj-tot",
yt."rt-adj-nbr",
yt."base-disc-per"
FROM (
SELECT PUB."master"."app-number",
...
PUB."lt-rt-adj-hdr"."disc-adj-tot",
PUB."lt-rt-adj-hdr"."rt-adj-nbr",
PUB."lt-rt-adj-hdr"."base-disc-per"
FROM PUB."master" master JOIN PUB."property" property ON
master."lt-acnt" = property."lt-acnt"
...
JOIN PUB."lt-rt-adj-hdr" lt_rt_adj_hdr ON
lt_master."lt-acnt" = lt_rt_adj_hdr."lt-acnt") yt
INNER JOIN(
SELECT "app-number",
MAX("rt-adj-nbr") "rt-adj-nbr"
FROM ( PUB."lt-master" lt_master JOIN
PUB."lt-rt-adj-hdr" lt_rt_adj_hdr ON
lt_master."lt-acnt" = lt_rt_adj_hdr."lt-acnt")
GROUP BY "app-number") ss on yt."app-number" = ss."app-number" and
yt."rt-adj-nbr" = ss."rt-adj-nbr"
WHERE ...
This query just hangs and will not return results unless a very simple WHERE clause like "WHERE yt."app-number" = 123456" is used. I am completely stuck.
Has the owner of the Progress DB ever run "update statistics"? The Progress SQL query optimizer needs to have good statistics in order to execute efficiently. Progress applications usually use the 4GL engine rather than SQL so, in many cases, the administrator is not keeping the SQL statistics updated. Which often leads to very poor SQL query performance.
From the 4GL side the admin can use this script to generate a program that will do the job:
/* genUpdateSQL.p
*
* mpro dbName -p util/genUpdateSQL.p -param "tmp/updSQLstats.sql"
*
* sqlexp -user userName -password passWord -db dnName -S servicePort -infile tmp/updSQLstats.sql -outfile tmp/updSQLtats.log
*
*/
output to value( ( if session:parameter <> "" then session:parameter else "updSQLstats.sql" )).
for each _file no-lock where _hidden = no:
put unformatted
"UPDATE TABLE STATISTICS AND INDEX STATISTICS AND ALL COLUMN STATISTICS FOR PUB."
'"' _file._file-name '"' ";"
skip
.
put unformatted "commit work;" skip.
end.
output close.
return.
Or, you could do it if you have sufficient privileges (just plug in your table name for _file._file-name).