plpgsql

Preventing 'invalid input syntax for type json' in Postgres

狂风中的少年 提交于 2019-12-10 02:27:46
问题 I have a text column that contains JSON and also plan text. I want to convert it to JSON, and then select a particular property. For example: user_data _________ {"user": {"name": "jim"}} {"user": {"name": "sally"}} some random data string I've tried: select user_data::json#>'{user,name}' from users I get: ERROR: invalid input syntax for type json DETAIL: Token "some" is invalid. CONTEXT: JSON user_data, line 1: some... Is it possible to prevent this? 回答1: If you want to skip the rows with

Compare software version in postgres

ⅰ亾dé卋堺 提交于 2019-12-10 01:21:25
问题 Is there a way to compare software version (e.g. X.Y.Z > A.B.C) in postgres ? I'm searching for a function on string/varchar or a "version" type. I found out that http://pgxn.org/dist/semver/doc/semver.html, but i'm looking for alternatives (not so easy to deploy..) Thanks a lot. 回答1: You can split the version to array and then do array comparison. select regexp_split_to_array(v1, '\.')::int[] v1, regexp_split_to_array(v2, '\.')::int[] v2, regexp_split_to_array(v1, '\.')::int[] > regexp_split

How to pass a record to a PL/pgSQL function?

我只是一个虾纸丫 提交于 2019-12-09 17:48:11
问题 I have 8 similar PL/pgSQL functions; they are used as INSTEAD OF INSERT/UPDATE/DELETE triggers on views to make them writable. The views each combine columns of one generic table (called "things" in the example below) and one special table ("shaped_things" and "flavored_things" below). PostgreSQL's inheritance feature can't be used in our case, by the way. The triggers have to insert/update rows in the generic table; these parts are identical across all 8 functions. Since the generic table

track revisions in postgresql

こ雲淡風輕ζ 提交于 2019-12-09 13:15:35
问题 I have to keep track of revisions of records in a table. What I've done is create a second table that inherits from the first and adds a revision counter. CREATE TABLE A ( id SERIAL, foo TEXT, PRIMARY KEY (id)); CREATE TABLE B ( revision INTEGER NOT NULL) INHERITS (A); Then I created a trigger that would update table B every time A is inserted/updated. What I can't figure out is how to make B.revision keep an individual "sequence" for each id. Example: table A has 2 rows, i & j. i has been

PostgreSQL IF-THEN-ELSE control structure

只愿长相守 提交于 2019-12-09 09:12:35
问题 Why do I always get the following error from Postgres? syntax error at or near "IF" I read PostgreSQL: Documentation: 8.3: Control Structures. First I tried to execute a difficult query (with subquery), but then I tried to execute a simple one like this: IF 2 <> 0 THEN select * from users; END IF; The error is still the same. What am I doing wrong? 回答1: IF 2 <> 0 THEN select * from users; END IF; You cannot use PL/pgSQL statements outside plpgsql functions. And if this fragment is from

Robust approach for building SQL queries programmatically

帅比萌擦擦* 提交于 2019-12-09 06:24:34
问题 I have to resort to raw SQL where the ORM is falling short (using Django 1.7). The problem is that most of the queries end up being 80-90% similar. I cannot figure out a robust & secure way to build queries without violating re-usability. Is string concatenation the only way out, i.e. build parameter-less query strings using if-else conditions, then safely include the parameters using prepared statements (to avoid SQL injection). I want to follow a simple approach for templating SQL for my

Debug PostgreSQL function using pgAdmin

隐身守侯 提交于 2019-12-09 04:05:30
问题 I refer this to enable the debugger in the PostgreSQL server in order to debugging the plpgsql function by stepping through the codes using the pgadmin. I have already set shared_preload_libraries = '$libdir/plugins/plugin_debugger.dll' in the postgresql.conf , run pldbgapi.sql , and restarted the server. These steps should have been run successfully and plugin_debugger.dll should be loaded successfully as can be verified by using the command show shared_preload_libraries , and I can see the

PLPGSQL Function to Calculate Bearing

。_饼干妹妹 提交于 2019-12-09 03:48:08
问题 Basically I can't get my head around the syntax of plpgsql and would appreciate some help with the following efforts. I have a table containing 1000's of wgs84 points. The following SQL will retrieve a set of points within a bounding box on this table: SELECT id, ST_X(wgs_geom), ST_Y(wgs_geom), ST_Z(wgs_geom) FROM points_table INNER JOIN (SELECT ST_Transform(ST_GeomFromText('POLYGON((-1.73576102027 1.5059743629, -1.73591122397 51.5061067655,-1.73548743495 51.5062838333,-1.73533186682 1

UPDATE a whole row in PL/pgSQL

大憨熊 提交于 2019-12-09 03:38:54
问题 I have plpgsql function: CREATE OR REPLACE FUNCTION test() RETURNS VOID AS $$ DECLARE my_row my_table%ROWTYPE; BEGIN SELECT * INTO my_row FROM my_table WHERE id='1'; my_row.date := now(); END; $$ LANGUAGE plpgsql; I would like to know if it's possible to directly UPDATE my_row record. The only way I've found to do it now is: UPDATE my_table SET date=now() WHERE id='1'; Note this is only an example function, the real one is far more complex than this. I'm using PostgreSQL 9.2. UPDATE: Sorry

Writing a function in SQL to loop through a date range in a UDF

巧了我就是萌 提交于 2019-12-08 23:56:35
问题 I am trying to automate the process of running a PLPGSQL function for a range of dates. Typically I have to run the following code that generates a single table per day per function call: SELECT dhcp.singleday('2012-11-24'::date, '2012-11-25'::date); SELECT dhcp.singleday('2012-11-25'::date, '2012-11-26'::date); SELECT dhcp.singleday('2012-11-26'::date, '2012-11-27'::date); SELECT dhcp.singleday('2012-11-27'::date, '2012-11-28'::date); SELECT dhcp.singleday('2012-11-28'::date, '2012-11-29':