show executed query in phpPgAdmin - postgresql

Is there a way to show the SQL query executed by phpPgAdmin as the way phpMyAdmin does?
For example, if I modify a column, it should show the ALTER command being executed.
If this is not possible, what other interface could I use to get this feature?

It's not possible with any currently released version of phpPgAdmin, although the feature could probably be added. You'd need to intercept the SQL being sent to the back-end, and then display this back out to the user. SQL execution is pretty well centralized, and if you look at the "history" feature you will see a way to trap/show queries, so munging those bits together would probably get you what you want. HTH, if someone implements this, please send a pull request!

As a quick dirty hack you could alter sources a bit to enable sql logging:
In classes/database/ADODB_base.php in
function execute($sql) {
...
}
add these lines at the beginning:
global $misc;
$misc->saveScriptHistory($sql);
This worked in my 5.0.3 version.

Related

Best way to track the progress of a long-running function (from outside) - PostgreSQL 11?

What is the best way to track progress of a
long-running function in PostgreSQL 11?
Since every function executes in a single transaction, even if the function writes to some "log" table no other session/transaction can see this output unless the function completes with SUCCESS.
I read about some attempts here but they are from 2010.
https://www.endpointdev.com/blog/2010/04/viewing-postgres-function-progress-from/
Also, this approach looks terribly inconvenient.
As of today what is the best way to track progress?
One approach that I know... is to turn the func to a procedure and then do partial commits in the SP. But what if I want to return some result set from the func... In that case I cannot turn it into a SP, right? So... how to proceed in that case?
Many thanks in advance.
NOTE: The function is written in PL/pgSQL, the most common procedural SQL language available in PostgreSQL.
I don't know that there's a great way to do it built-in to postgres yet, but there are a couple ways to achieve logging that will be visible outside of a function.
You can use the pg_background extension to run an insert in the background that will be visible outside of the function. This requires compiling and installing this extension.
Use dblink to connect to the same database and insert data. This will most likely require setting up some permissions.
Neither option is ideal, but hopefully one can work for you. Converting your function to a procedure may also work, but you won't be able to call the procedure from within a transaction.

Can I debug a PostgreSQL query sent from an external source, that I can't edit?

I see how to debug queries stored as Functions in the database. But my problem is with an external QGIS plugin that connects to my Postgres 10.4 via network and does a complex query and calculations, and stores the results back into PostGIS tables:
FOR r IN c LOOP
SELECT
(1 - ST_LineLocatePoint(path.geom, ST_Intersection(r.geom, path.geom))) * ST_Length(path.geom)
INTO
station
(continues ...)
When it errors, it just returns that line number as the failing location, but no clue where it was in the loop through hundreds of features. (And any features it has processed are not stored to the output tables when it fails.) I totally don't know enough about the plugin and about SQL to hack the external query, and I suspect if it was a reasonable task the plugin author would have included more revealing debug messages.
So is there some way I could use pgAdmin4 (or anything) from the server side to watch the query process? Even being able to see if it fails the first time through the loop or later would help immensely. Knowing the loop count at failure would point me to the exact problem feature. Being able to see "station" or "r.geom" would make it even easier.
Perfectly fine if the process is miserably slow or interferes with other queries, I'm the only user on this server.
This is not actually a way to watch the RiverGIS query in action, but it is the best I have found. It extracts the failing ST_Intersects() call from the RiverGIS code and runs it under your control, where you can display any clues you want.
When you're totally mystified where the RiverGIS problem might be, run this SQL query:
SELECT
xs."XsecID" AS "XsecID",
xs."ReachID" AS "ReachID",
xs."Station" AS "Station",
xs."RiverCode" AS "RiverCode",
xs."ReachCode" AS "ReachCode",
ST_Intersection(xs.geom, riv.geom) AS "Fraction"
FROM
"<your project name>"."StreamCenterlines" AS riv,
"<your project name>"."XSCutLines" AS xs
WHERE
ST_Intersects(xs.geom, riv.geom)
ORDER BY xs."ReachID" ASC, xs."Station" DESC
Obviously replace <your project name> with the QGIS project name.
Also works for the BankLines step if you replace "StreamCenterlines" with "BankLines". Probably could be adapted to other situations where ST_Intersects() fails without a clue.
You'll get a listing with shorter geometry strings for good cross sections and double-length strings for bad ones. Probably need to widen your display column a lot to see this.
Works for me in pgAdmn4, or in QGIS3 -> Database -> DB Manager -> (click the wrench icon). You could select only bad lines, but I find the background info helpful.

How to use pgp.as.format for a transaction

Is it possible to use pgp.as.format to see raw postgres queries generated by a transaction db.tx(t => ...)? I can't figure out the syntax for this use-case.
If you can't figure out what query is generated, you have the following options:
Start using pg-monitor that will show you all the queries automatically
Manually connect to event query
Use function as.format
Is it possible to use pgp.as.format
Why wouldn't it be possible? The function is there to be used independently whenever needed. But in your case, for viewing generated queries, the other two options are better.

How to execute a query every 5 mins

I'm very new to OSQuery and i'd like to execute a query (e.g. SELECT * FROM last) every 5 minutes. Is there any chance, to define a script, which executes this routine in within a crontab or something else like this?
Probably it should be enough to execute the script with the query as parameter, but there is nothing in the documentation, so i guess, it won't be supported yet.
I checked their Community and also their FAQ but haven't found something relating to my problem.
OSQuery is currently on the latest version (1.7.3), self compiled, running on Ubuntu Server, 64 bit 15.10.
If you need more information to help me, just let me know.
The recommended method is using scheduled queries. You create a 'pack' like one of these GitHub link which includes the queries and frequencies. Then update the osqueryd config to include the pack.
After even more documentation and different sites, i have found a pretty cool snippet, which allows, to send the query as parameter, by calling the osqueryi process.
/path/to/osqueryi --json "YOUR QUERY"
And this returns the result in your terminal - JSON Format. So it's pretty easy to write a script (any language), execute the snippet above and parse the content. This script can be a cron as well.
Maybe you could write a script (or a C program) to perform your query.
And then use the cron to run your program every 5 minutes.

SQl Developer Single Query result tab please

In the free application SQL-Developer (provided by Oracle), I tried searching around for this but couldn't immediately find a solution. I find the opening of multiple SQL query result window/tabs mildly annoying. I'm sure there are very useful cases for this feature, but my question is:
Can we turn the multiple query result windows to just one (Toad style). If there's a shortcut key for this, that would be super awesome.
SQL Developer 4.0 (2013)
Close all the Query Result tabs
Tools > Preferences
Database > Worksheet > uncheck "Show query results in new tabs"
So, it turns out this is a bug. If you look closely, you'll notice that for each query run, your results tab is automatically being pinned, causing each new query run to need it's own new tab to display the results.
The automatic pinning is only supposed to be enabled when you explicitly go into Tools->Preferences->Database->Worksheet->"Automatically Freeze Result Tabs".
It is an identified bug, that Oracle fixed with SQL Developer 2.1.1.
Go to oracle.com and download the latest version and this should go away. It was definitely very annoying.
Instead of "Run Statement" click on "Run Script"(F5). This would give the output of two queries in the same Query Result window.
Directly to the database or from an application? Do you mean returning one result set from multiple tables together or multiple result sets in one pane? Are you using SQL 2000, 2005, 2008, or something else? The question is vaguely worded, but I'll try to help anyway.
For the purposes of this answer, I think you're trying to query the database directly. Open SQL Server Management Studio 2005 or newer (Not sure if this works in 2000), click New Query, and type the multiple queries into the pane. i.e.:
select * from table1
select * from table2
will return two result sets in the same window/pane.
Unpin helps keep current tab for next query.
(I am using Oracle SQL Developer 22.x)
I found that this was happening in SQL Dev v4.0.2.15 because I was running 2 SELECT statements. Running one SELECT at a time re-used the Results tab.